Skip to content

fix(prettier): print labeled statements like Prettier (#278) - #322

Merged
leonidaz merged 3 commits into
mainfrom
fix/prettier-labeled-statement-278
Sep 24, 2026
Merged

leonidaz merged 3 commits into
mainfrom
fix/prettier-labeled-statement-278

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #278

Summary

The formatter had no printer for LabeledStatement, so a label and the loop or block it labels printed as /* Unknown: LabeledStatement */. The output failed to compile wherever a break or continue named the label, and otherwise ran without the loop. Labeled statements now print the way Prettier prints them:

Input Before Now
outer: for (…) { … continue outer; … } /* Unknown: LabeledStatement */ unchanged
block: { + break block; + } /* Unknown: LabeledStatement */ unchanged
label: ; /* Unknown: LabeledStatement */ label:;
empty: {} /* Unknown: LabeledStatement */ empty: { + }
a: // note + for (;;) {…} /* Unknown: LabeledStatement */ // note + a: for (;;) {…}

Changes

  • Printer. printLabeledStatement prints label: body. The body goes through printClause, so an empty body prints as label:; like Prettier. An empty labeled block expands to { + } because Prettier's printBlock does that for any parent outside its function and loop list. LabeledStatement joins the expanded-block parents. [Bug]: Formatter expands empty loop and catch blocks that Prettier prints as {} #281 will replace that list with Prettier's.
  • Comments between the label and the body. The parser attaches them to the body. Prettier's handleLabeledStatementComments moves a comment that starts or ends its line, counting the comments beside it on that line, onto the labeled statement. commentStartsOrEndsLine makes the same call, and those comments print above the label. A block comment that shares its line with both the label and the body stays on its side of the colon: a /* x */: for, b: /* y */ run();, c: /* z */ ;.
  • Idempotence. The moved comments print through printLeadingComments, the same helper printTsrxNode uses for every node's leading comments (fix(prettier): keep one pair of parentheses per stacked JSDoc cast (#291) #308 pulled it out on main). On the second pass those comments are the statement's own leading comments, so both passes print them the same way.
  • prettier-ignore. A moved // prettier-ignore is the last comment before the label, so the whole labeled statement keeps its source, as in Prettier.

Output differences from Prettier that remain

These are existing issues, not label-specific:

Verification

Related findings

Filed separately because they have different causes:

case -1 /* LessThan */: moving its comment under the case is #304.

🤖 Generated with Claude Code


Note

Low Risk
Scoped to formatter output for a previously unsupported AST node; no runtime, auth, or data-path changes.

Overview
Labeled statements (outer: for …, block: { break block; }, etc.) no longer emit /* Unknown: LabeledStatement */ and drop the labeled body. The plugin now routes LabeledStatement through a new printLabeledStatement printer so output stays valid when break/continue reference the label.

Formatting follows Prettier: empty bodies become label:;, empty labeled blocks expand to label: { / }, and comments between the label and body move above the label when they start or end a line (inline block comments stay on their side of :). A // prettier-ignore immediately after the label preserves the whole statement’s source. LabeledStatement is also treated as a control-flow parent so empty labeled blocks get the same brace layout as other statement parents.

Tests cover loops, blocks, nested labels, comment placement, semi: false, and component bodies.

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

leonidaz and others added 3 commits September 24, 2026 17:11
The printer had no LabeledStatement case, so a label and the loop or block
it labels printed as `/* Unknown: LabeledStatement */`. The output failed to
compile wherever a `break` or `continue` named the label, and otherwise ran
without the loop.

printLabeledStatement prints `label: body`, or `label:;` for an empty body
through printClause. An empty labeled block expands to `{` + `}` like
Prettier's printBlock does for a parent outside its function and loop list.

Comments between the label and the body are attached to the body. Like
Prettier's handleLabeledStatementComments, one that starts or ends its line
(counting the comments beside it on that line) moves above the label, and
the others stay on their side of the colon. The moved comments print through
printLeadingComments, the loop printTsrxNode already used for leading
comments, so the second pass prints them the same way. A moved
`prettier-ignore` keeps the whole statement's source, as in Prettier.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
#308 took the same leading-comment loop out of printTsrxNode as
printLeadingComments(node, comments, …), so this branch's copy is dropped
and printLabeledStatement calls main's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 48f971f into main Sep 24, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/prettier-labeled-statement-278 branch September 24, 2026 21:48
@github-actions github-actions Bot mentioned this pull request Sep 24, 2026
trueadm added a commit to octanejs/octane that referenced this pull request Sep 30, 2026
…ng .tsrx (#1452)

@tsrx/prettier-plugin 0.4.10 had no LabeledStatement printer. Its fallback
logged "Unknown node type" and wrote `/* Unknown: LabeledStatement */` in
place of the statement, and `pnpm format:files` still wrote the file. It also
printed an empty statement body as nothing, so `while (x);` followed by a
statement made that statement the loop body.

Patch the plugin (patches/@[email protected]) with a
backport of tsrx-org/tsrx#322 (labeled statements, including comments around
the colon and prettier-ignore) and the empty-body part of tsrx-org/tsrx#283
(`printClause` for if/else/for/for-in/for-of/while/do bodies). A node type
with no printer now throws instead of emitting a placeholder. Upstream fixed
all three in 0.4.12 and 0.5.x, but 0.5.2 would reformat 1,564 of 2,217
.tsrx files, so the upgrade stays a separate change.

The same fallback had already deleted `export * from './server'` from puck's
SlotRender module, so every `<Render>` of a slot field threw "Element type is
invalid". Restore it and cover slot rendering in the puck render contract.

scripts/tsrx-prettier-plugin.test.mjs (in ci:workflow:test) pins the output,
checks idempotence, compares the AST before and after formatting, and checks
the unknown-node throw; scripts/fixtures/tsrx-prettier-statements.tsrx makes
`pnpm format:check` fail if the patch is lost.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

[Bug]: Formatter replaces labeled statements with an Unknown comment

1 participant