Repository navigation
fix(prettier): print labeled statements like Prettier (#278) - #322
Merged
Merged
Conversation
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]>
Merged
Merged
3 of 5 tasks
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 abreakorcontinuenamed the label, and otherwise ran without the loop. Labeled statements now print the way Prettier prints them:outer: for (…) { … continue outer; … }/* Unknown: LabeledStatement */block: {+break block;+}/* Unknown: LabeledStatement */label: ;/* Unknown: LabeledStatement */label:;empty: {}/* Unknown: LabeledStatement */empty: {+}a: // note+for (;;) {…}/* Unknown: LabeledStatement */// note+a: for (;;) {…}Changes
printLabeledStatementprintslabel: body. The body goes throughprintClause, so an empty body prints aslabel:;like Prettier. An empty labeled block expands to{+}because Prettier'sprintBlockdoes that for any parent outside its function and loop list.LabeledStatementjoins the expanded-block parents. [Bug]: Formatter expands empty loop and catch blocks that Prettier prints as {} #281 will replace that list with Prettier's.handleLabeledStatementCommentsmoves a comment that starts or ends its line, counting the comments beside it on that line, onto the labeled statement.commentStartsOrEndsLinemakes 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 */ ;.printLeadingComments, the same helperprintTsrxNodeuses for every node's leading comments (fix(prettier): keep one pair of parentheses per stacked JSDoc cast (#291) #308 pulled it out onmain). 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-ignoreis 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:
/* x */+/* y */+b: for …). Prettier keeps them on one line. This is [Bug]: Formatter breaks the line between block comments that share a line #279.{+}on two lines where Prettier prints{}. This is [Bug]: Formatter expands empty loop and catch blocks that Prettier prints as {} #281.h: // x+i: // y+for …) change on the second pass. Prettier's own output does the same (// x+h: // y+i: forbecomes// x+// y+h: i: for). Our first pass matches Prettier's.Verification
pnpm test --project prettier-pluginafter mergingmainat73a5cd82(fix(prettier): lay out arrays like Prettier (#275, #295, #300) #292, fix(prettier): keep one pair of parentheses per stacked JSDoc cast (#291) #308, fix(core): attach comments around empty statements like Prettier (#288, #303) #309): 822 pass, including 14 new cases. All 14 fail onmain. The suite'sformathelper also checks that a second pass changes nothing.tsgoforprettier-pluginand a Prettier check of the plugin sources and changesets pass. Before the merge,pnpm format:check,pnpm changeset:check, andpnpm test --project tsrx-mcppassed (83 tests; it formats through the plugin)..tsrxand JS/TS source files formatted byte-for-byte the same withmain's plugin and this one. None of them uses a label.node_modulesand formatted each one inside a function. For each output it checked four things: formatting is idempotent, Babel's AST matches the input's, every comment survives, and noUnknownnode is left. Prettier's own output passed the same AST check as a baseline. 878 pass. Two fail because of [Bug]: A block comment before ) in a function body moves into the parameter list or is deleted #320 (found here), which the probe's zero-parameter wrapper triggers. Two areyieldloops that a non-generator wrapper can't hold, and 11 more don't parse inside the wrapper, so they were skipped.semi: falsegives the same four failures. The 100 snippets where Prettier prints a leading;guard (fix(prettier): guard statements without semicolons, keep directives and tagged-template type arguments (#263, #266, #268) #290) all pass once both ASTs ignore empty statements.Related findings
Filed separately because they have different causes:
)anywhere in a function body attaches to the last parameter and prints in the parameter list. In a named function with no parameters it attaches to the name and is deleted. The cause is in@tsrx/core's comment attachment.forheader that doesn't fit breaks inside a clause instead of one clause per line, andfor (x; ;)loses a space. A label in front of a loop makes this more likely.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 routesLabeledStatementthrough a newprintLabeledStatementprinter so output stays valid whenbreak/continuereference the label.Formatting follows Prettier: empty bodies become
label:;, empty labeled blocks expand tolabel: {/}, 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-ignoreimmediately after the label preserves the whole statement’s source.LabeledStatementis 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.