Skip to content

Read an @case arm's body from the BlockStatement the next @tsrx/core gives #1355

Closed

Description

@leonidaz

Warning

Don't start until the @tsrx/core release that includes tsrx-org/tsrx#879 is published (0.5.0), and the @tsrx/oxc release that follows it. Upgrade both together, with #1338, #1353 and #1354.

Summary

The next @tsrx/core gives an @case or @default arm its { … } body as a node (tsrx-org/tsrx#879, tsrx-org/tsrx#842):

@switch (x) {
  @case 1: {
    const y = 1;
    <b>{y}</b>
  }
  @default: {
    // only a comment
  }
}
@case 1's consequent @default's consequent
@tsrx/core 0.3 / 0.4 [VariableDeclaration, JSXElement] []
@tsrx/core 0.5.0 [BlockStatement], whose body is [VariableDeclaration, JSXElement] [BlockStatement] with an empty body, the comment its inner comment

It is the shape every parser gives a JavaScript case 1: { … }, and the shape of the @if, @for and @try bodies. The block spans the arm's { to }, gets its own scope, and carries the same metadata as an @if body (native_tsrx_template_block: true, templateMode: 'script'). Plain JavaScript switch statements are unchanged.

What Octane needs to do

Read at b7bade7c. @switch lowers through b.switch(n.discriminant, n.cases) (compile.js:21206, 23783), so every SwitchStatement reader below sees the new arms. About 5 places, 10–20 lines plus a helper (e.g. armStatements(c) returning c.consequent[0].body, or statementsOf(c.consequent[0])):

  1. compile-universal.js:3577 breaks: compileBlockValueAst([Block]) reads the block as setup, so the arm renders nothing.
  2. makeSwitchCall breaks: it passes [Block] to unionEnv and hoistBodyHelper. makeIfCall (30719) already unwraps .body.
  3. ssrEmitSwitch breaks: it passes [Block] to ssrCompileSub, and canBorrowSsrHostBranchRange now always returns false. ssrEmitIf (14509) already unwraps.
  4. collectSingleRootIfDeps (compile.js:7747) and anchorlessRootShape (7813) silently lose their optimization (they find no output nodes). Pass clause.consequent[0] instead of the array.

Already fine: renderer-boundaries.js:458, hydrate-boundaries.js:1019, style-scopes.js, slot-hooks.js, inline-hook-memo.js, hook-deps.js, strong-mode.js (they recurse into BlockStatement), and volar.js:759 (reads only keyword).

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions