Skip to content

Commit 68a9a65

Browse files
committed
fix(core): capture interleaved JSX spread children in source order
A spread child counts as render output, so a later statement marks the body as interleaved, but capture still skipped it and the spread read the mutated value. Bind the spread expression where it appears and re-emit the spread.
1 parent 5d2a390 commit 68a9a65

4 files changed

Lines changed: 76 additions & 10 deletions

File tree

‎.changeset/jsx-spread-children.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ plain TSX, instead of failing with `Unexpected token`:
2020
its expression.
2121
- The formatter prints a spread child like an expression child, with its
2222
expression's comments inside the braces as Prettier prints them.
23+
- A spread child interleaved with a later statement is captured at its source
24+
position, so that statement does not change what the spread renders.
2325

2426
A spread as an attribute value (`<a b={...c} />`) or as a dynamic tag name
2527
(`<{...c} />`) is still an error, now with a message that says so.

‎packages/tsrx/src/transform/jsx-interleave.js‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export function is_interleaved_body(body_nodes, is_jsx_child) {
4444
* (`{/* … *\/}`) are inert and don't need capturing — their position relative
4545
* to mutations doesn't change output, and neither has an expression to bind.
4646
*
47+
* A spread child (`{...items}`) is capturable: the spread expression is bound
48+
* and re-emitted as a spread, so a later statement cannot change what it
49+
* spreads.
50+
*
4751
* @param {AST.Node | null | undefined} jsx
4852
* @returns {jsx is ESTreeJSX.JSXCapturableChild}
4953
*/
@@ -55,33 +59,56 @@ export function is_capturable_jsx_child(jsx) {
5559
if (jsx.metadata?.tsrx_reactive_block === true) return false;
5660
const t = jsx.type;
5761
if (t === 'JSXExpressionContainer') return jsx.expression.type !== 'JSXEmptyExpression';
58-
return t === 'JSXElement' || t === 'JSXFragment';
62+
return t === 'JSXElement' || t === 'JSXFragment' || t === 'JSXSpreadChild';
5963
}
6064

6165
/**
6266
* Build a `VariableDeclaration` that captures a JSX child into a const at
63-
* its source position, along with a JSXExpressionContainer referencing the
64-
* capture. The caller inserts the declaration into the enclosing block's
65-
* statements in source order and uses the reference in place of the JSX
66-
* child inside the returned fragment.
67+
* its source position, along with a reference to that const. An element or
68+
* expression child is referenced from a JSXExpressionContainer; a spread
69+
* child is referenced from a JSXSpreadChild so the `...` is preserved. The
70+
* caller inserts the declaration into the enclosing block's statements in
71+
* source order and uses the reference in place of the JSX child inside the
72+
* returned fragment.
6773
*
6874
* @param {ESTreeJSX.JSXCapturableChild} jsx
6975
* @param {number} capture_index
7076
* @param {(id: AST.Identifier, init: AST.Expression) => AST.Identifier} [anchor_id] gives the
7177
* capture's NAME an authored origin — the only anchorable token when the captured
7278
* expression itself starts with punctuation
73-
* @returns {{ declaration: AST.VariableDeclaration, reference: ESTreeJSX.JSXExpressionContainer }}
79+
* @returns {{
80+
* declaration: AST.VariableDeclaration,
81+
* reference: ESTreeJSX.JSXExpressionContainer | ESTreeJSX.JSXSpreadChild,
82+
* }}
7483
*/
7584
export function capture_jsx_child(jsx, capture_index, anchor_id) {
7685
const name = `_tsrx_child_${capture_index}`;
77-
const init = jsx.type === 'JSXExpressionContainer' ? jsx.expression : jsx;
86+
const init =
87+
jsx.type === 'JSXExpressionContainer' || jsx.type === 'JSXSpreadChild' ? jsx.expression : jsx;
7888

7989
const declaration = b.const(anchor_id ? anchor_id(b.id(name), init) : b.id(name), init);
8090

8191
// NOTE: JSXExpressionContainer nodes are intentionally created without
8292
// loc — they're synthetic wrappers whose source positions don't
8393
// correspond to source-map entries and adding loc causes Volar mapping
84-
// failures.
94+
// failures. A captured spread is the same kind of wrapper: it keeps the
95+
// source child's start/end so a diagnostic still points at `{...items}`,
96+
// and omits loc so the generated binding is not a source-map entry.
97+
// Platforms that lower spreads to expression children rewrite this node
98+
// on the following walk.
99+
if (jsx.type === 'JSXSpreadChild') {
100+
const reference = /** @type {ESTreeJSX.JSXSpreadChild} */ ({
101+
type: 'JSXSpreadChild',
102+
expression: b.id(name),
103+
metadata: { path: [] },
104+
});
105+
if (typeof jsx.start === 'number' && typeof jsx.end === 'number') {
106+
reference.start = jsx.start;
107+
reference.end = jsx.end;
108+
}
109+
return { declaration, reference };
110+
}
111+
85112
const reference = b.jsx_expression_container(b.id(name));
86113

87114
return { declaration, reference };

‎packages/tsrx/tests/shared/compile.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,41 @@ export function App() @{
22132213
expect(virtual_parse_diagnostics(code), code).toEqual([]);
22142214
});
22152215
}
2216+
2217+
it('captures a spread child before a later statement mutates its value', () => {
2218+
// A `{items}` sibling in this position is bound to `_tsrx_child_N`
2219+
// where it appears. `{...items}` has to do the same, then be
2220+
// re-emitted as a spread (or Solid's expression child), so the
2221+
// statement does not change what the spread renders.
2222+
const source = `export function App() @{
2223+
let items = ['a'];
2224+
<div>
2225+
{...items}
2226+
@{ items.push('b'); }
2227+
<span />
2228+
</div>
2229+
}`;
2230+
2231+
if (name === 'vue') {
2232+
const start = source.indexOf('{...items}');
2233+
const { errors } = compile(source, 'App.tsrx', { collect: true });
2234+
expect(errors.map((error) => [error.pos, error.end])).toEqual([
2235+
[start, start + '{...items}'.length],
2236+
]);
2237+
return;
2238+
}
2239+
2240+
const { code } = compile(source, 'App.tsrx');
2241+
const capture = code.indexOf('const _tsrx_child_0 = items;');
2242+
const mutation = code.indexOf("items.push('b')");
2243+
const rendered = name === 'solid' ? '{_tsrx_child_0}' : '{..._tsrx_child_0}';
2244+
2245+
expect(capture).toBeGreaterThan(-1);
2246+
expect(mutation).toBeGreaterThan(capture);
2247+
expect(code.indexOf(rendered)).toBeGreaterThan(mutation);
2248+
expect(code).not.toContain('{...items}');
2249+
expect(virtual_parse_diagnostics(code), code).toEqual([]);
2250+
});
22162251
});
22172252

22182253
describe(`[${name}] component export shapes`, () => {

‎packages/tsrx/types/index.d.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,12 +999,14 @@ declare module 'estree-jsx' {
999999

10001000
/**
10011001
* A JSX child that evaluates to a single expression, so it can be captured
1002-
* into a `const` at its source position.
1002+
* into a `const` at its source position. A spread child captures its
1003+
* expression and is re-emitted as a spread of that binding.
10031004
*/
10041005
type JSXCapturableChild =
10051006
| AST.TSRXJSXElement
10061007
| AST.TSRXJSXFragment
1007-
| (JSXExpressionContainer & { expression: AST.Expression });
1008+
| (JSXExpressionContainer & { expression: AST.Expression })
1009+
| JSXSpreadChild;
10081010

10091011
/** An attribute accepted by and emitted from the shared JSX transformer. */
10101012
type JSXAttributeNode = JSXAttribute | JSXSpreadAttribute;

0 commit comments

Comments
 (0)