Skip to content

Add a JavaScript formatter, written as s-expressions - #13

Merged
Demonstrandum merged 6 commits into
masterfrom
js-formatter
Nov 6, 2026
Merged

Demonstrandum merged 6 commits into
masterfrom
js-formatter

Conversation

@Demonstrandum

@Demonstrandum Demonstrandum commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Builds on #12 (JSON formatter). Until that is merged, this diff includes it.

Adds JavaScript output, with --js and (%formatter/js ...). JavaScript is written as s-expressions, so scripts can use seam's macros, and seam's ; comments and strings no longer get in the way.

  • A list is a call: (f a b) → f(a, b) and ((. a b) 1) → a.b(1). Symbols are written as they are, so (console.log x) works too.
  • Operators start a list: (+ a b c), (- a), comparisons, &&/||/??, !, typeof, await, and assignments (=, +=, ...). A nested operation is always put in parentheses, so precedence never matters.
  • (. a b "c-d" 0) → a.b["c-d"][0], (at a i), (new Map), (? c a b) and (array ...). A list of attributes is an object, the same as for JSON.
  • Statements:
    • const/let/var
    • (if c then else), using (do ...) for blocks
    • (for x of xs ...) and (for k in o ...)
    • while, return, throw, break, continue
    • import, which is written word for word
    • (export (const ...)) and (export default x)
  • Functions: (=> (x (= y 1)) body...) and (function name (params) body...). A function returns its last expression.
  • A statement used as an expression is an error, pointing at it. For if, the error suggests (? ...) instead.
  • A document is indented, one statement per line. %formatter/js is compact unless given :document true.

The output is raw text, so (script (%formatter/js ...)) is written as it is. It isn't applied to <script> automatically, so existing pages are unchanged.

samples/js-concept.sex (the 2020 mockup) now compiles. Its tagged templates (Color`black`) became calls with strings, since seam has no template syntax.

I checked with node --check that the indented and compact output of the sample both parse, and ran a small generated program in node to check its results. Tests cover each form and the errors.


Note

Medium Risk
Large formatter/parser changes affect HTML emission and macro expansion, so regressions could break builds or script output despite strong tests.

Overview
Introduces JSON and JavaScript markup formatters exposed via --json / --js, %formatter/json, and %formatter/js, including docs/javascript.md, USAGE.md, and README updates plus a JSON Feed sample (feed.json.sex, build wiring, samples/js-concept.sex refresh).

HTML <script> emission now prefers raw string bodies, otherwise walks children through a new generate_script helper that quotes strings with shared write_string, while macro recognition relies on Node::macro_name, YAML scalars become typed nodes, string escaping handles \\, and extensive tests cover JSON, JS, script safety, and escaped symbols.

Reviewed by Cursor Bugbot for commit 78b1611. Bugbot is set up for automated code reviews on this repo. Configure here.

--json and %formatter/json write a list of attributes as an object, any
other list as an array, and true, false and null as themselves. %json
and %yaml now read unquoted numbers, booleans and null as numbers and
symbols instead of strings, so JSON read in is written back the same.
Raw text inside a <script> is written as is, so JSON can be given to a
script, and the blog sample gains a JSON Feed.
Writing JavaScript as seam source is fragile, since ; starts a comment,
and strings were written back with Rust's escapes. A script that is a
single string is now written as it is, as with style, and otherwise
strings in a script are written as JavaScript strings. JSON is best
given to scripts as a data block, which is what the docs now show.
--js and %formatter/js write a list as a call unless it starts with an
operator or a keyword: (+ a b), (. a b), (const x 1), (=> (x) body),
(if c then else), (for x of xs ...), and so on. A list of attributes is
an object, and strings are JavaScript strings, so scripts can be written
with seam's syntax and macros without its comments and strings getting
in the way. The js-concept sample now compiles.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread crates/seam/src/assemble/js.rs Resolved
Comment thread crates/seam/src/assemble/js.rs Resolved
Comment thread crates/seam/src/assemble/js.rs Resolved
The JavaScript formatter now writes every form in docs/javascript.md:
arrays, objects and destructuring, ([] a i) access, optional chaining,
templates, regexes, every operator, declarations, functions, generators,
async, classes, every statement, labels, comments, imports and exports.
An escaped head, (\array x), is always a call.

A symbol written \% is no longer a macro, and \\ is a backslash.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread crates/seam/src/assemble/js.rs Resolved
Comment thread crates/seam/src/assemble/js.rs Resolved
… text.

Only a declaration, or a named function or class, can be exported, so
(export (async (=> ...))) is an error instead of invalid JavaScript.
(. 1 toString) is written (1).toString, and template text, regexes and
comments write </ as <\/, so they cannot end an HTML script.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit e7b75fa. Configure here.

Comment thread crates/seam/src/assemble/js.rs Resolved
@Demonstrandum
Demonstrandum merged commit 63cc634 into master Nov 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant