[Bug]: Read an @case arm's body from the BlockStatement the next @tsrx/core gives
[!WARNING] Don't start until the
@tsrx/corerelease that includes tsrx-org/tsrx#879 is published (0.5.0). Upgrade for it together with #1525, #1526 and #1527.
Issue Category
Other (preparing for the next @tsrx/core)
Bug Description
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 Ripple needs to do
Read at 97d090b9. About 6–8 places, 20–40 lines.
is_template_child_positionbreaks. A node in an arm now has the parentsBlockStatement→SwitchCase→JSXSwitchExpression, so the grandparent check fails, and a@{ … }in an@caseis no longer analyzed as template output (caller: the analyzer'sJSXCodeBlockvisitor,analyze/index.js:2413). Accept a grandparentSwitchCasewhose parent is a directive, as it does forCatchClause.- An arm's scope:
scopes.get(switch_case) || context.state.scopeat clientindex.js:1884,6129,6745, and serverindex.js:1346, and6433throughbranch_returning_body. The arm's bindings are now in the block's scope, whichflatten_switch_consequentskips. Preferscopes.get(switch_case.consequent[0]). - Empty arms:
analyze/index.js:873, client1882and server1344testconsequent.length === 0, which an empty@case x: {}no longer passes: it would get an empty branch instead ofreturn;(a snapshot change, not a behavior change). Test the block'sbody.length. - Optional: client
6754'sconsequent[0].type === 'BlockStatement'is now always true for@switch(it still works), and the comment at6433–6435("an arm has no block node") is stale.
Already fine: flatten_switch_consequent (utils.js:2683), is_directive_render_position (utils.js:228), the SwitchStatement walks in utils.js and the client and server transforms (they recurse into BlockStatement), hoist.js (compiled output), and style-scopes.js.
Reproduction Steps
After upgrading, an @case body with a @{ … } or setup bindings, and an empty @case.
Environment
- Ripple
mainat97d090b9,@tsrx/core0.3.x
Additional Context
- tsrx-org/tsrx#879: the change. tsrx-org/tsrx#842: the issue.
- Octane: octanejs/octane#1355.
@tsrx/oxc: tsrx-org/oxc#128.
Checklist
- I've searched for existing issues
Source: Ripple-TS/ripple