Skip to content

feat(core): give an @case or @default arm its { … } body as a BlockStatement - #879

Merged
leonidaz merged 1 commit into
mainfrom
fix/core-case-body-842
Sep 27, 2026
Merged

leonidaz merged 1 commit into
mainfrom
fix/core-case-body-842

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

What this PR changes

An @case or @default arm's { … } body is now a node, as in every parser's tree for a JavaScript case 1: { … }:

@switch (x) {
  @case 1: /* c */ {
    const y = 1;
    <b>{y}</b>
  }
  @default: {
    // only a comment
  }
}
@case 1's consequent @default's consequent
before [VariableDeclaration, JSXElement] [] (the comment has no node to be in)
now [BlockStatement { … }] from the { to the }, holding both [BlockStatement { }], the comment its inner comment

Babel, typescript-estree, oxc, SWC and acorn all give case 1: { … } a consequent of one BlockStatement, and TypeScript one Block; only a case without braces has a flat list. An @case always has braces. The @if, @for and @try bodies were already blocks, so @case/@default was the only directive body that wasn't a node. The block carries the same template-block metadata as an @if body (native_tsrx_template_block, templateMode: 'script'); return stays invalid in an arm.

What compiles and renders is unchanged: core's transform already handles a block in a case, and all five compilers and runtimes pass without a change.

Changes

Verification

Downstream

Surveyed at Ripple 97d090b9 and Octane b7bade7c:

Closes #842.
Closes #774.

🤖 Generated with Claude Code

…Statement

An `@case`/`@default` arm always has a `{ … }` body, but core kept only
its statements, so the braces weren't a node (#842). Its `consequent` is
now one BlockStatement from the `{` to the `}`, as a JavaScript
`case 1: { … }` has in every parser and as the `@if`, `@for` and `@try`
bodies are, with the same template-block metadata. A comment in an empty
arm is the block's inner comment.

- tsrx-prettier-plugin: the adapter no longer rebuilds the block by
  searching the source for the `{`.
- @tsrx/prettier-plugin: prints the arm's block like an `@if` body, and
  keeps the comments of an empty `@switch` inside it (core's attachment
  now treats an empty `@switch` like an empty `switch`), which fixes
  #774.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit 00d47ed into main Sep 27, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/core-case-body-842 branch September 27, 2026 20:33
thejackshelton added a commit to tsrx-org/yuku that referenced this pull request Sep 27, 2026
…n @tsrx/core 0.5.0

Each arm's consequent is now the one BlockStatement from its '{' to its '}',
the shape of a JavaScript 'case 1: { ... }' and of the @if, @for and @Try
bodies (tsrx-org/tsrx#879). The block is in the tree, so the scope analysis
no longer builds one per case after parsing; that code goes, and with it
the extra nodes that broke the attached-comment table (yuku#12).

Type parameter names are already Identifiers and enum members already sit
in a TSEnumBody; tests pin both.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant