@sigil-dev/compiler 0.9.21 → 0.9.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,317 +1,317 @@
1
- import { types as t } from "@babel/core";
2
- import { containsSignal } from "./utils";
3
-
4
- export type ProcessElementFn = (
5
- node: t.JSXElement,
6
- statements: t.Statement[],
7
- genId: () => string,
8
- signals: Set<string>,
9
- hash?: string,
10
- hydrate?: boolean,
11
- nodesVar?: string,
12
- ) => string;
13
-
14
- /**
15
- * Detect pattern: signal.map(item => <Tag key={expr} />) — or, since unkeyed
16
- * maps are also reconciled (index-keyed), signal.map(item => <Tag />).
17
- * Returns info needed to generate keyed list code, or null.
18
- * `hasKey` is false for the unkeyed form; buildKeyedList then keys by index.
19
- */
20
- export function findKeyedMapExpr(
21
- expr: t.Expression,
22
- signals: Set<string>,
23
- ): {
24
- callExpr: t.CallExpression;
25
- keyExpr: t.Expression | null;
26
- itemParam: t.Identifier;
27
- jsxBody: t.JSXElement;
28
- signalExpr: t.Expression;
29
- hasKey: boolean;
30
- } | null {
31
- if (!t.isCallExpression(expr)) return null;
32
- const callee = expr.callee;
33
- if (!t.isMemberExpression(callee)) return null;
34
- const prop = callee.property;
35
- if (!t.isIdentifier(prop) || prop.name !== "map") return null;
36
-
37
- const obj = callee.object;
38
- if (!t.isExpression(obj)) return null;
39
- if (!containsSignal(obj, signals)) return null;
40
-
41
- if (expr.arguments.length !== 1) return null;
42
- const arrowFn = expr.arguments[0];
43
- if (!t.isArrowFunctionExpression(arrowFn)) return null;
44
- if (arrowFn.params.length !== 1) return null;
45
- const itemParam = arrowFn.params[0];
46
- if (!t.isIdentifier(itemParam)) return null;
47
-
48
- const body = arrowFn.body;
49
- if (!t.isJSXElement(body)) return null;
50
-
51
- const keyAttr = body.openingElement.attributes.find(
52
- (a) =>
53
- t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && a.name.name === "key",
54
- );
55
- if (
56
- keyAttr &&
57
- t.isJSXAttribute(keyAttr) &&
58
- t.isJSXExpressionContainer(keyAttr.value)
59
- ) {
60
- return {
61
- callExpr: expr,
62
- keyExpr: keyAttr.value.expression as t.Expression,
63
- itemParam,
64
- jsxBody: body,
65
- signalExpr: obj,
66
- hasKey: true,
67
- };
68
- }
69
-
70
- // Unkeyed map — still reconciled, keyed by position.
71
- return {
72
- callExpr: expr,
73
- keyExpr: null,
74
- itemParam,
75
- jsxBody: body,
76
- signalExpr: obj,
77
- hasKey: false,
78
- };
79
- }
80
-
81
- /**
82
- * Generate keyed list code using reconcile().
83
- * In hydrate mode, uses hydrateKeyedList() to claim server-rendered nodes.
84
- */
85
- export function buildKeyedList(
86
- varName: string,
87
- keyed: NonNullable<ReturnType<typeof findKeyedMapExpr>>,
88
- statements: t.Statement[],
89
- genId: () => string,
90
- signals: Set<string>,
91
- processElement: ProcessElementFn,
92
- hash?: string,
93
- hydrate?: boolean,
94
- nodesVar?: string,
95
- parentVar?: string,
96
- ): void {
97
- const anchorVar = genId();
98
- const keyMapVar = genId();
99
-
100
- // Key extractor: the user's key={} expression wrapped in an arrow, or
101
- // index-keying for unkeyed maps. reconcile passes (item, index) so
102
- // (_item, _i) => _i works.
103
- const keyFn = t.arrowFunctionExpression(
104
- keyed.hasKey
105
- ? [keyed.itemParam]
106
- : [t.identifier("_item"), t.identifier("_i")],
107
- keyed.keyExpr ?? t.identifier("_i"),
108
- );
109
-
110
- // Inside a row, the item is an element of a $state array, which makes it a
111
- // reactive proxy: `item.done` must update the row when it changes, and
112
- // bind:checked={item.done} must write back to it. Treating the item param
113
- // as a signal gives its reads effects; a primitive item costs one effect
114
- // that never re-runs.
115
- const rowSignals = new Set(signals);
116
- rowSignals.add(keyed.itemParam.name);
117
-
118
- // Strip key prop from JSX before processing.
119
- // In SSR mode, the Babel plugin already converted key → data-key.
120
- // In DOM/hydrate mode, we don't need either.
121
- keyed.jsxBody.openingElement.attributes =
122
- keyed.jsxBody.openingElement.attributes.filter(
123
- (a) =>
124
- !(
125
- t.isJSXAttribute(a) &&
126
- t.isJSXIdentifier(a.name) &&
127
- (a.name.name === "key" || a.name.name === "data-key")
128
- ),
129
- );
130
-
131
- if (hydrate) {
132
- // ─── Hydration path: use hydrateKeyedList() ───
133
- // Strip key prop from JSX before processing
134
- keyed.jsxBody.openingElement.attributes =
135
- keyed.jsxBody.openingElement.attributes.filter(
136
- (a) =>
137
- !(
138
- t.isJSXAttribute(a) &&
139
- t.isJSXIdentifier(a.name) &&
140
- a.name.name === "key"
141
- ),
142
- );
143
-
144
- // Process the JSX element to get createNode body
145
- const createNodeStmts: t.Statement[] = [];
146
- const jsxVar = processElement(
147
- keyed.jsxBody,
148
- createNodeStmts,
149
- genId,
150
- rowSignals,
151
- hash,
152
- hydrate,
153
- nodesVar,
154
- );
155
-
156
- // const { anchor, keyMap } = hydrateKeyedList(__nodes, signal, keyFn, createNode, parent)
157
- const resultVar = genId();
158
- const hydrateArgs: t.Expression[] = [
159
- t.identifier(nodesVar ?? "__nodes"),
160
- keyed.signalExpr,
161
- keyFn,
162
- t.arrowFunctionExpression(
163
- [keyed.itemParam],
164
- t.blockStatement([
165
- ...createNodeStmts,
166
- t.returnStatement(t.identifier(jsxVar)),
167
- ]),
168
- ),
169
- ];
170
- if (hydrate && parentVar) hydrateArgs.push(t.identifier(parentVar));
171
- statements.push(
172
- t.variableDeclaration("const", [
173
- t.variableDeclarator(
174
- t.identifier(resultVar),
175
- t.callExpression(t.identifier("hydrateKeyedList"), hydrateArgs),
176
- ),
177
- ]),
178
- );
179
-
180
- // const anchor = result.anchor
181
- statements.push(
182
- t.variableDeclaration("const", [
183
- t.variableDeclarator(
184
- t.identifier(anchorVar),
185
- t.memberExpression(t.identifier(resultVar), t.identifier("anchor")),
186
- ),
187
- ]),
188
- );
189
-
190
- // const keyMap = result.keyMap
191
- statements.push(
192
- t.variableDeclaration("const", [
193
- t.variableDeclarator(
194
- t.identifier(keyMapVar),
195
- t.memberExpression(t.identifier(resultVar), t.identifier("keyMap")),
196
- ),
197
- ]),
198
- );
199
-
200
- // createEffect(() => reconcile(anchor, keyMap, signal, keyFn, createNode))
201
- statements.push(
202
- t.expressionStatement(
203
- t.callExpression(t.identifier("createEffect"), [
204
- t.arrowFunctionExpression(
205
- [],
206
- t.blockStatement([
207
- t.expressionStatement(
208
- t.callExpression(t.identifier("reconcile"), [
209
- t.identifier(anchorVar),
210
- t.identifier(keyMapVar),
211
- keyed.signalExpr,
212
- keyFn,
213
- t.arrowFunctionExpression(
214
- [keyed.itemParam],
215
- t.blockStatement([
216
- ...createNodeStmts,
217
- t.returnStatement(t.identifier(jsxVar)),
218
- ]),
219
- ),
220
- ]),
221
- ),
222
- ]),
223
- ),
224
- ]),
225
- ),
226
- );
227
- return;
228
- }
229
-
230
- // ─── DOM path (existing code) ───
231
-
232
- // const _anchor = document.createComment('')
233
- statements.push(
234
- t.variableDeclaration("const", [
235
- t.variableDeclarator(
236
- t.identifier(anchorVar),
237
- t.callExpression(
238
- t.memberExpression(
239
- t.identifier("document"),
240
- t.identifier("createComment"),
241
- ),
242
- [t.stringLiteral("")],
243
- ),
244
- ),
245
- ]),
246
- );
247
-
248
- // parent.append(_anchor)
249
- statements.push(
250
- t.expressionStatement(
251
- t.callExpression(
252
- t.memberExpression(t.identifier(varName), t.identifier("append")),
253
- [t.identifier(anchorVar)],
254
- ),
255
- ),
256
- );
257
-
258
- // const _keyMap = new Map()
259
- statements.push(
260
- t.variableDeclaration("const", [
261
- t.variableDeclarator(
262
- t.identifier(keyMapVar),
263
- t.newExpression(t.identifier("Map"), []),
264
- ),
265
- ]),
266
- );
267
-
268
- // Strip key prop from JSX before processing
269
- keyed.jsxBody.openingElement.attributes =
270
- keyed.jsxBody.openingElement.attributes.filter(
271
- (a) =>
272
- !(
273
- t.isJSXAttribute(a) &&
274
- t.isJSXIdentifier(a.name) &&
275
- a.name.name === "key"
276
- ),
277
- );
278
-
279
- // Process the JSX element to get createNode body
280
- const createNodeStmts: t.Statement[] = [];
281
- const jsxVar = processElement(
282
- keyed.jsxBody,
283
- createNodeStmts,
284
- genId,
285
- rowSignals,
286
- hash,
287
- );
288
-
289
- // createEffect(() => reconcile(...))
290
- // In DOM mode, use raw key expression. Keys are compared by value.
291
- statements.push(
292
- t.expressionStatement(
293
- t.callExpression(t.identifier("createEffect"), [
294
- t.arrowFunctionExpression(
295
- [],
296
- t.blockStatement([
297
- t.expressionStatement(
298
- t.callExpression(t.identifier("reconcile"), [
299
- t.identifier(anchorVar),
300
- t.identifier(keyMapVar),
301
- keyed.signalExpr,
302
- keyFn,
303
- t.arrowFunctionExpression(
304
- [keyed.itemParam],
305
- t.blockStatement([
306
- ...createNodeStmts,
307
- t.returnStatement(t.identifier(jsxVar)),
308
- ]),
309
- ),
310
- ]),
311
- ),
312
- ]),
313
- ),
314
- ]),
315
- ),
316
- );
317
- }
1
+ import { types as t } from "@babel/core";
2
+ import { containsSignal } from "./utils";
3
+
4
+ export type ProcessElementFn = (
5
+ node: t.JSXElement,
6
+ statements: t.Statement[],
7
+ genId: () => string,
8
+ signals: Set<string>,
9
+ hash?: string,
10
+ hydrate?: boolean,
11
+ nodesVar?: string,
12
+ ) => string;
13
+
14
+ /**
15
+ * Detect pattern: signal.map(item => <Tag key={expr} />) — or, since unkeyed
16
+ * maps are also reconciled (index-keyed), signal.map(item => <Tag />).
17
+ * Returns info needed to generate keyed list code, or null.
18
+ * `hasKey` is false for the unkeyed form; buildKeyedList then keys by index.
19
+ */
20
+ export function findKeyedMapExpr(
21
+ expr: t.Expression,
22
+ signals: Set<string>,
23
+ ): {
24
+ callExpr: t.CallExpression;
25
+ keyExpr: t.Expression | null;
26
+ itemParam: t.Identifier;
27
+ jsxBody: t.JSXElement;
28
+ signalExpr: t.Expression;
29
+ hasKey: boolean;
30
+ } | null {
31
+ if (!t.isCallExpression(expr)) return null;
32
+ const callee = expr.callee;
33
+ if (!t.isMemberExpression(callee)) return null;
34
+ const prop = callee.property;
35
+ if (!t.isIdentifier(prop) || prop.name !== "map") return null;
36
+
37
+ const obj = callee.object;
38
+ if (!t.isExpression(obj)) return null;
39
+ if (!containsSignal(obj, signals)) return null;
40
+
41
+ if (expr.arguments.length !== 1) return null;
42
+ const arrowFn = expr.arguments[0];
43
+ if (!t.isArrowFunctionExpression(arrowFn)) return null;
44
+ if (arrowFn.params.length !== 1) return null;
45
+ const itemParam = arrowFn.params[0];
46
+ if (!t.isIdentifier(itemParam)) return null;
47
+
48
+ const body = arrowFn.body;
49
+ if (!t.isJSXElement(body)) return null;
50
+
51
+ const keyAttr = body.openingElement.attributes.find(
52
+ (a) =>
53
+ t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && a.name.name === "key",
54
+ );
55
+ if (
56
+ keyAttr &&
57
+ t.isJSXAttribute(keyAttr) &&
58
+ t.isJSXExpressionContainer(keyAttr.value)
59
+ ) {
60
+ return {
61
+ callExpr: expr,
62
+ keyExpr: keyAttr.value.expression as t.Expression,
63
+ itemParam,
64
+ jsxBody: body,
65
+ signalExpr: obj,
66
+ hasKey: true,
67
+ };
68
+ }
69
+
70
+ // Unkeyed map — still reconciled, keyed by position.
71
+ return {
72
+ callExpr: expr,
73
+ keyExpr: null,
74
+ itemParam,
75
+ jsxBody: body,
76
+ signalExpr: obj,
77
+ hasKey: false,
78
+ };
79
+ }
80
+
81
+ /**
82
+ * Generate keyed list code using reconcile().
83
+ * In hydrate mode, uses hydrateKeyedList() to claim server-rendered nodes.
84
+ */
85
+ export function buildKeyedList(
86
+ varName: string,
87
+ keyed: NonNullable<ReturnType<typeof findKeyedMapExpr>>,
88
+ statements: t.Statement[],
89
+ genId: () => string,
90
+ signals: Set<string>,
91
+ processElement: ProcessElementFn,
92
+ hash?: string,
93
+ hydrate?: boolean,
94
+ nodesVar?: string,
95
+ parentVar?: string,
96
+ ): void {
97
+ const anchorVar = genId();
98
+ const keyMapVar = genId();
99
+
100
+ // Key extractor: the user's key={} expression wrapped in an arrow, or
101
+ // index-keying for unkeyed maps. reconcile passes (item, index) so
102
+ // (_item, _i) => _i works.
103
+ const keyFn = t.arrowFunctionExpression(
104
+ keyed.hasKey
105
+ ? [keyed.itemParam]
106
+ : [t.identifier("_item"), t.identifier("_i")],
107
+ keyed.keyExpr ?? t.identifier("_i"),
108
+ );
109
+
110
+ // Inside a row, the item is an element of a $state array, which makes it a
111
+ // reactive proxy: `item.done` must update the row when it changes, and
112
+ // bind:checked={item.done} must write back to it. Treating the item param
113
+ // as a signal gives its reads effects; a primitive item costs one effect
114
+ // that never re-runs.
115
+ const rowSignals = new Set(signals);
116
+ rowSignals.add(keyed.itemParam.name);
117
+
118
+ // Strip key prop from JSX before processing.
119
+ // In SSR mode, the Babel plugin already converted key → data-key.
120
+ // In DOM/hydrate mode, we don't need either.
121
+ keyed.jsxBody.openingElement.attributes =
122
+ keyed.jsxBody.openingElement.attributes.filter(
123
+ (a) =>
124
+ !(
125
+ t.isJSXAttribute(a) &&
126
+ t.isJSXIdentifier(a.name) &&
127
+ (a.name.name === "key" || a.name.name === "data-key")
128
+ ),
129
+ );
130
+
131
+ if (hydrate) {
132
+ // ─── Hydration path: use hydrateKeyedList() ───
133
+ // Strip key prop from JSX before processing
134
+ keyed.jsxBody.openingElement.attributes =
135
+ keyed.jsxBody.openingElement.attributes.filter(
136
+ (a) =>
137
+ !(
138
+ t.isJSXAttribute(a) &&
139
+ t.isJSXIdentifier(a.name) &&
140
+ a.name.name === "key"
141
+ ),
142
+ );
143
+
144
+ // Process the JSX element to get createNode body
145
+ const createNodeStmts: t.Statement[] = [];
146
+ const jsxVar = processElement(
147
+ keyed.jsxBody,
148
+ createNodeStmts,
149
+ genId,
150
+ rowSignals,
151
+ hash,
152
+ hydrate,
153
+ nodesVar,
154
+ );
155
+
156
+ // const { anchor, keyMap } = hydrateKeyedList(__nodes, signal, keyFn, createNode, parent)
157
+ const resultVar = genId();
158
+ const hydrateArgs: t.Expression[] = [
159
+ t.identifier(nodesVar ?? "__nodes"),
160
+ keyed.signalExpr,
161
+ keyFn,
162
+ t.arrowFunctionExpression(
163
+ [keyed.itemParam],
164
+ t.blockStatement([
165
+ ...createNodeStmts,
166
+ t.returnStatement(t.identifier(jsxVar)),
167
+ ]),
168
+ ),
169
+ ];
170
+ if (hydrate && parentVar) hydrateArgs.push(t.identifier(parentVar));
171
+ statements.push(
172
+ t.variableDeclaration("const", [
173
+ t.variableDeclarator(
174
+ t.identifier(resultVar),
175
+ t.callExpression(t.identifier("hydrateKeyedList"), hydrateArgs),
176
+ ),
177
+ ]),
178
+ );
179
+
180
+ // const anchor = result.anchor
181
+ statements.push(
182
+ t.variableDeclaration("const", [
183
+ t.variableDeclarator(
184
+ t.identifier(anchorVar),
185
+ t.memberExpression(t.identifier(resultVar), t.identifier("anchor")),
186
+ ),
187
+ ]),
188
+ );
189
+
190
+ // const keyMap = result.keyMap
191
+ statements.push(
192
+ t.variableDeclaration("const", [
193
+ t.variableDeclarator(
194
+ t.identifier(keyMapVar),
195
+ t.memberExpression(t.identifier(resultVar), t.identifier("keyMap")),
196
+ ),
197
+ ]),
198
+ );
199
+
200
+ // createEffect(() => reconcile(anchor, keyMap, signal, keyFn, createNode))
201
+ statements.push(
202
+ t.expressionStatement(
203
+ t.callExpression(t.identifier("createEffect"), [
204
+ t.arrowFunctionExpression(
205
+ [],
206
+ t.blockStatement([
207
+ t.expressionStatement(
208
+ t.callExpression(t.identifier("reconcile"), [
209
+ t.identifier(anchorVar),
210
+ t.identifier(keyMapVar),
211
+ keyed.signalExpr,
212
+ keyFn,
213
+ t.arrowFunctionExpression(
214
+ [keyed.itemParam],
215
+ t.blockStatement([
216
+ ...createNodeStmts,
217
+ t.returnStatement(t.identifier(jsxVar)),
218
+ ]),
219
+ ),
220
+ ]),
221
+ ),
222
+ ]),
223
+ ),
224
+ ]),
225
+ ),
226
+ );
227
+ return;
228
+ }
229
+
230
+ // ─── DOM path (existing code) ───
231
+
232
+ // const _anchor = document.createComment('')
233
+ statements.push(
234
+ t.variableDeclaration("const", [
235
+ t.variableDeclarator(
236
+ t.identifier(anchorVar),
237
+ t.callExpression(
238
+ t.memberExpression(
239
+ t.identifier("document"),
240
+ t.identifier("createComment"),
241
+ ),
242
+ [t.stringLiteral("")],
243
+ ),
244
+ ),
245
+ ]),
246
+ );
247
+
248
+ // parent.append(_anchor)
249
+ statements.push(
250
+ t.expressionStatement(
251
+ t.callExpression(
252
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
253
+ [t.identifier(anchorVar)],
254
+ ),
255
+ ),
256
+ );
257
+
258
+ // const _keyMap = new Map()
259
+ statements.push(
260
+ t.variableDeclaration("const", [
261
+ t.variableDeclarator(
262
+ t.identifier(keyMapVar),
263
+ t.newExpression(t.identifier("Map"), []),
264
+ ),
265
+ ]),
266
+ );
267
+
268
+ // Strip key prop from JSX before processing
269
+ keyed.jsxBody.openingElement.attributes =
270
+ keyed.jsxBody.openingElement.attributes.filter(
271
+ (a) =>
272
+ !(
273
+ t.isJSXAttribute(a) &&
274
+ t.isJSXIdentifier(a.name) &&
275
+ a.name.name === "key"
276
+ ),
277
+ );
278
+
279
+ // Process the JSX element to get createNode body
280
+ const createNodeStmts: t.Statement[] = [];
281
+ const jsxVar = processElement(
282
+ keyed.jsxBody,
283
+ createNodeStmts,
284
+ genId,
285
+ rowSignals,
286
+ hash,
287
+ );
288
+
289
+ // createEffect(() => reconcile(...))
290
+ // In DOM mode, use raw key expression. Keys are compared by value.
291
+ statements.push(
292
+ t.expressionStatement(
293
+ t.callExpression(t.identifier("createEffect"), [
294
+ t.arrowFunctionExpression(
295
+ [],
296
+ t.blockStatement([
297
+ t.expressionStatement(
298
+ t.callExpression(t.identifier("reconcile"), [
299
+ t.identifier(anchorVar),
300
+ t.identifier(keyMapVar),
301
+ keyed.signalExpr,
302
+ keyFn,
303
+ t.arrowFunctionExpression(
304
+ [keyed.itemParam],
305
+ t.blockStatement([
306
+ ...createNodeStmts,
307
+ t.returnStatement(t.identifier(jsxVar)),
308
+ ]),
309
+ ),
310
+ ]),
311
+ ),
312
+ ]),
313
+ ),
314
+ ]),
315
+ ),
316
+ );
317
+ }