@@ -43,6 +43,8 @@ export function is_interleaved_body(body_nodes, is_jsx_child) {
4343 * `const`. Static text children (`JSXText`) and comment-only containers
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.
46+ * A spread child (`{...items}`) is not inert: it reads its expression when the
47+ * returned fragment is built, so it is snapshotted the same way as `{items}`.
4648 *
4749 * @param {AST.Node | null | undefined } jsx
4850 * @returns {jsx is ESTreeJSX.JSXCapturableChild }
@@ -55,34 +57,59 @@ export function is_capturable_jsx_child(jsx) {
5557 if ( jsx . metadata ?. tsrx_reactive_block === true ) return false ;
5658 const t = jsx . type ;
5759 if ( t === 'JSXExpressionContainer' ) return jsx . expression . type !== 'JSXEmptyExpression' ;
58- return t === 'JSXElement' || t === 'JSXFragment' ;
60+ return t === 'JSXElement' || t === 'JSXFragment' || t === 'JSXSpreadChild' ;
5961}
6062
6163/**
6264 * 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.
65+ * its source position, along with a reference used in its place inside the
66+ * returned fragment. An element, fragment, or expression child is referenced
67+ * as `{_tsrx_child_N}`; a spread child is referenced as `{..._tsrx_child_N}`.
68+ * The caller inserts the declaration into the enclosing block's statements in
69+ * source order.
6770 *
6871 * @param {ESTreeJSX.JSXCapturableChild } jsx
6972 * @param {number } capture_index
7073 * @param {(id: AST.Identifier, init: AST.Expression) => AST.Identifier } [anchor_id] gives the
7174 * capture's NAME an authored origin — the only anchorable token when the captured
7275 * expression itself starts with punctuation
73- * @returns {{ declaration: AST.VariableDeclaration, reference: ESTreeJSX.JSXExpressionContainer } }
76+ * @returns {{
77+ * declaration: AST.VariableDeclaration,
78+ * reference: ESTreeJSX.JSXExpressionContainer | ESTreeJSX.JSXSpreadChild
79+ * }}
7480 */
7581export function capture_jsx_child ( jsx , capture_index , anchor_id ) {
7682 const name = `_tsrx_child_${ capture_index } ` ;
77- const init = jsx . type === 'JSXExpressionContainer' ? jsx . expression : jsx ;
83+ const init =
84+ jsx . type === 'JSXExpressionContainer' || jsx . type === 'JSXSpreadChild' ? jsx . expression : jsx ;
7885
7986 const declaration = b . const ( anchor_id ? anchor_id ( b . id ( name ) , init ) : b . id ( name ) , init ) ;
8087
81- // NOTE: JSXExpressionContainer nodes are intentionally created without
82- // loc — they're synthetic wrappers whose source positions don't
83- // correspond to source-map entries and adding loc causes Volar mapping
84- // failures.
85- const reference = b . jsx_expression_container ( b . id ( name ) ) ;
88+ // NOTE: JSXExpressionContainer and JSXSpreadChild nodes are intentionally
89+ // created without loc — they're synthetic wrappers whose source positions
90+ // don't correspond to source-map entries and adding loc causes Volar
91+ // mapping failures. A spread keeps a pointer at the authored node so a
92+ // target that rejects spread children can still report that source range.
93+ const reference =
94+ jsx . type === 'JSXSpreadChild'
95+ ? spread_child_reference ( b . id ( name ) , jsx )
96+ : b . jsx_expression_container ( b . id ( name ) ) ;
8697
8798 return { declaration, reference } ;
8899}
100+
101+ /**
102+ * `{..._tsrx_child_N}` standing in for an authored spread. No location of its
103+ * own — see {@link capture_jsx_child}.
104+ *
105+ * @param {AST.Expression } expression
106+ * @param {ESTreeJSX.JSXSpreadChild } authored
107+ * @returns {ESTreeJSX.JSXSpreadChild }
108+ */
109+ function spread_child_reference ( expression , authored ) {
110+ return {
111+ type : 'JSXSpreadChild' ,
112+ expression,
113+ metadata : { path : [ ] , tsrx_spread_diagnostic : authored } ,
114+ } ;
115+ }
0 commit comments