@@ -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 */
7584export 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 } ;
0 commit comments