@sigil-dev/compiler 0.7.6 → 0.8.0

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,430 +1,480 @@
1
- import { type PluginObject, types as t, template } from "@babel/core";
2
- import { handleDerived } from "./handlers/dom/derived.ts";
3
- import { handleEffect } from "./handlers/dom/effect.ts";
4
- import { handleState } from "./handlers/dom/state.ts";
5
- import { handleDerivedSSR } from "./handlers/ssr/derived.ts";
6
- import { handleStateSSR } from "./handlers/ssr/state.ts";
7
- import { processElement, processFragment } from "./jsx/index.ts";
8
- import { processElementSSR, processFragmentSSR } from "./jsx/ssr.ts";
9
- import { getMacro } from "./util/helpers.ts";
10
- import { MAGIC } from "./util/magic.ts";
11
- import { warnDeadReactivity } from "./util/dead-code.ts";
12
-
13
- const SSR_HELPERS = template.statements.ast(`
14
- const __SAFE = Symbol.for('sigil.safe');
15
- class __H {
16
- constructor(v) { this.v = v; this[__SAFE] = true; }
17
- toString() { return this.v; }
18
- }
19
- const __h = (v) => {
20
- if (v?.[__SAFE]) return v;
21
- if (Array.isArray(v)) return new __H(v.map(x => x?.[__SAFE] ? x.v : __e(x)).join(''));
22
- return new __H(String(v ?? ''));
23
- };
24
- const __e = (v) => {
25
- if (v?.[__SAFE]) return v.v;
26
- if (Array.isArray(v)) return v.map(x => x?.[__SAFE] ? x.v : __e(x)).join('');
27
- if (v === false || v === null || v === undefined) return '';
28
- return String(v).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
29
- };
30
- `);
31
- /**
32
- * Recursively convert key={expr} to data-key={expr} in JSX tree.
33
- * Needed for SSR output so hydration can map items by key.
34
- */
35
- function convertKeyToDataKey(node: t.JSXElement): void {
36
- for (let i = 0; i < node.openingElement.attributes.length; i++) {
37
- const attr = node.openingElement.attributes[i];
38
- if (
39
- t.isJSXAttribute(attr) &&
40
- t.isJSXIdentifier(attr.name) &&
41
- attr.name.name === "key"
42
- ) {
43
- node.openingElement.attributes[i] = t.jsxAttribute(
44
- t.jsxIdentifier("data-key"),
45
- attr.value,
46
- );
47
- }
48
- }
49
- for (const child of node.children) {
50
- if (t.isJSXElement(child)) {
51
- convertKeyToDataKey(child);
52
- } else if (
53
- t.isJSXExpressionContainer(child) &&
54
- t.isJSXElement(child.expression)
55
- ) {
56
- convertKeyToDataKey(child.expression);
57
- }
58
- }
59
- }
60
-
61
- interface SigilOptions {
62
- hash?: string;
63
- mode?: "dom" | "ssr" | "hydrate";
64
- }
65
-
66
- export default function sigilPlugin(): PluginObject {
67
- const signals = new Set<string>();
68
- const storeSignals = new Set<string>();
69
- let scopedHash: string | undefined;
70
- let isSSR = false;
71
- let isHydrate = false;
72
- let didTransform = false;
73
-
74
- return {
75
- name: "sigil",
76
- visitor: {
77
- Program: {
78
- enter(path, state) {
79
- signals.clear();
80
- storeSignals.clear();
81
- const opts = (state.opts || {}) as SigilOptions;
82
- scopedHash = opts.hash;
83
- didTransform = false;
84
- isSSR = opts.mode === "ssr";
85
- isHydrate = opts.mode === "hydrate";
86
-
87
- if (isSSR) {
88
- path.unshiftContainer("body", SSR_HELPERS);
89
- return;
90
- }
91
-
92
-
93
-
94
- },
95
- exit(path, state) {
96
- warnDeadReactivity(path, signals, storeSignals, state.filename);
97
-
98
- // Only inject runtime import if this file actually used sigil transforms
99
- if (!didTransform || isSSR) return;
100
-
101
- const requiredSpecifiers = [
102
- "createSignal", "createRawSignal", "createEffect", "createMemo", "reconcile",
103
- "claim", "claimText", "claimComment", "hydrateKeyedList",
104
- "insert", "getHydrationNodes", "pushHydrationNodes", "popHydrationNodes",
105
- "snapshot", "tracking", "createEffectPre", "createInspectEffect",
106
- ];
107
-
108
- const runtimeImport = path.node.body.find(
109
- (n): n is t.ImportDeclaration => t.isImportDeclaration(n) && n.source.value === "@sigil-dev/runtime"
110
- );
111
-
112
- if (runtimeImport) {
113
- const existingNames = new Set(
114
- runtimeImport.specifiers
115
- .filter(t.isImportSpecifier)
116
- .map(s => t.isIdentifier(s.imported) ? s.imported.name : (s.imported as t.StringLiteral).value)
117
- );
118
- for (const name of requiredSpecifiers) {
119
- if (!existingNames.has(name)) {
120
- runtimeImport.specifiers.push(
121
- t.importSpecifier(t.identifier(name), t.identifier(name))
122
- );
123
- }
124
- }
125
- } else {
126
- path.unshiftContainer("body", t.importDeclaration(
127
- requiredSpecifiers.map(name =>
128
- t.importSpecifier(t.identifier(name), t.identifier(name))
129
- ),
130
- t.stringLiteral("@sigil-dev/runtime"),
131
- ));
132
- }
133
- }
134
- },
135
- VariableDeclaration(path) {
136
- for (const declarator of path.get("declarations")) {
137
- const macro = getMacro(declarator);
138
- if (!macro) continue;
139
- didTransform = true;
140
- const { name, init } = macro;
141
- const varName = t.isIdentifier(declarator.node.id)
142
- ? declarator.node.id.name
143
- : null;
144
-
145
- if (name === "$state" || name === "$store" || name === "$state.raw") {
146
- if (name === "$state.raw") {
147
- if (!isSSR) {
148
- const args = init.node.arguments;
149
- init.replaceWith(
150
- t.callExpression(t.identifier(MAGIC.createRawSignal), args),
151
- );
152
- }
153
- continue;
154
- }
155
- isSSR
156
- ? handleStateSSR(path, declarator, init, signals)
157
- : handleState(path, declarator, init, signals);
158
- if (name === "$store" && varName) {
159
- storeSignals.add(varName);
160
- }
161
- } else if (name === "$derived") {
162
- isSSR
163
- ? handleDerivedSSR(path, declarator, init, signals)
164
- : handleDerived(path, declarator, init, signals);
165
- }
166
- }
167
- },
168
- ExpressionStatement(path) {
169
- if (isSSR) {
170
- // drop $effect, $inspect, and $debug entirely in SSR
171
- const expr = path.get("expression");
172
- if (expr.isCallExpression()) {
173
- const callee = expr.get("callee");
174
- if (callee.isIdentifier() && (callee.node.name === "$effect" || callee.node.name === "$inspect" || callee.node.name === "$debug")) {
175
- didTransform = true;
176
- path.remove();
177
- }
178
- }
179
- return;
180
- }
181
- handleEffect(path);
182
- // Handle $debug(expr) expression statements
183
- const expr = path.get("expression");
184
- if (expr.isCallExpression()) {
185
- const callee = expr.get("callee");
186
- if (callee.isIdentifier({ name: "$debug" })) {
187
- didTransform = true;
188
- path.replaceWith(
189
- t.ifStatement(
190
- t.memberExpression(
191
- t.memberExpression(
192
- t.memberExpression(
193
- t.identifier("import"),
194
- t.identifier("meta"),
195
- ),
196
- t.identifier("env"),
197
- ),
198
- t.identifier("DEV"),
199
- ),
200
- t.blockStatement([
201
- t.debuggerStatement(),
202
- t.expressionStatement(
203
- t.callExpression(
204
- t.memberExpression(
205
- t.identifier("console"),
206
- t.identifier("log"),
207
- ),
208
- expr.node.arguments,
209
- ),
210
- ),
211
- ]),
212
- ),
213
- );
214
- }
215
- }
216
- },
217
- CallExpression(path) {
218
- if (isSSR) return;
219
- const callee = path.get("callee");
220
-
221
- // Helper: wrap signal identifiers in call expressions
222
- const wrapSignal = (expr: t.Expression): t.Expression => {
223
- if (t.isIdentifier(expr) && signals.has(expr.name)) {
224
- return t.callExpression(expr, []);
225
- }
226
- return expr;
227
- };
228
-
229
- // Handle member expression callees: $state.snapshot(x), $effect.tracking(), $effect.pre(fn)
230
- if (callee.isMemberExpression()) {
231
- const object = callee.get("object");
232
- const property = callee.get("property");
233
- if (object.isIdentifier() && property.isIdentifier()) {
234
- const obj = object.node.name;
235
- const prop = property.node.name;
236
-
237
- if (obj === "$state" && prop === "snapshot") {
238
- didTransform = true;
239
- path.get("callee").replaceWith(t.identifier(MAGIC.snapshot));
240
- return;
241
- }
242
-
243
- if (obj === "$effect" && prop === "tracking") {
244
- didTransform = true;
245
- path.get("callee").replaceWith(t.identifier(MAGIC.tracking));
246
- return;
247
- }
248
-
249
- if (obj === "$effect" && prop === "pre") {
250
- didTransform = true;
251
- path.get("callee").replaceWith(t.identifier(MAGIC.createEffectPre));
252
- return;
253
- }
254
- }
255
- }
256
-
257
- // Handle $inspect(count).with(cb) chain
258
- if (callee.isMemberExpression()) {
259
- const property = callee.get("property");
260
- if (property.isIdentifier({ name: "with" })) {
261
- const object = callee.get("object");
262
- if (object.isCallExpression()) {
263
- const innerCallee = object.get("callee");
264
- if (innerCallee.isIdentifier({ name: "$inspect" })) {
265
- didTransform = true;
266
- const args = object.node.arguments.map(wrapSignal);
267
- const cb = path.node.arguments[0];
268
- path.replaceWith(
269
- t.callExpression(t.identifier(MAGIC.createInspectEffect), [
270
- t.arrowFunctionExpression([], t.arrayExpression(args)),
271
- cb,
272
- ]),
273
- );
274
- return;
275
- }
276
- }
277
- }
278
- }
279
-
280
- // Handle standalone $debug(...) — not as expression statement
281
- if (callee.isIdentifier({ name: "$debug" })) {
282
- didTransform = true;
283
- path.replaceWith(
284
- t.ifStatement(
285
- t.memberExpression(
286
- t.memberExpression(
287
- t.memberExpression(
288
- t.identifier("import"),
289
- t.identifier("meta"),
290
- ),
291
- t.identifier("env"),
292
- ),
293
- t.identifier("DEV"),
294
- ),
295
- t.blockStatement([
296
- t.debuggerStatement(),
297
- t.expressionStatement(
298
- t.callExpression(
299
- t.memberExpression(
300
- t.identifier("console"),
301
- t.identifier("log"),
302
- ),
303
- path.node.arguments,
304
- ),
305
- ),
306
- ]),
307
- ),
308
- );
309
- return;
310
- }
311
-
312
- // Handle standalone $inspect(...)
313
- if (callee.isIdentifier({ name: "$inspect" })) {
314
- didTransform = true;
315
- const args = path.node.arguments.map(wrapSignal);
316
- path.replaceWith(
317
- t.callExpression(t.identifier(MAGIC.createInspectEffect), [
318
- t.arrowFunctionExpression([], t.arrayExpression(args)),
319
- ]),
320
- );
321
- return;
322
- }
323
- },
324
- JSXElement(path) {
325
- didTransform = true;
326
- // console.log(
327
- // "[sigil] JSXElement visitor fired, parent:",
328
- // path.parentPath?.type,
329
- // "isSSR:", isSSR
330
- // );
331
- if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
332
- return;
333
- if (isSSR) {
334
- try {
335
- convertKeyToDataKey(path.node);
336
- path.replaceWith(
337
- t.callExpression(t.identifier("__h"), [
338
- processElementSSR(path.node, signals),
339
- ]),
340
- );
341
- } catch (e) {
342
- console.error("[sigil] processElementSSR threw:", path.toString());
343
- console.error(e);
344
- throw e;
345
- }
346
- return;
347
- }
348
-
349
- const statements: t.Statement[] = [];
350
- const genId = (() => { let i = 0; return () => `_el${i++}`; })();
351
-
352
- const root = processElement(
353
- path.node, statements, genId, signals, scopedHash,
354
- isHydrate,
355
- isHydrate ? "__nodes" : undefined,
356
- );
357
-
358
- const body: t.Statement[] = [];
359
-
360
- if (isHydrate) {
361
- // DECLARE __nodes locally — every IIFE owns its own context
362
- body.push(
363
- t.variableDeclaration("const", [
364
- t.variableDeclarator(
365
- t.identifier("__nodes"),
366
- t.callExpression(t.identifier("getHydrationNodes"), [])
367
- )
368
- ])
369
- );
370
- }
371
-
372
- body.push(...statements);
373
- body.push(t.returnStatement(t.identifier(root)));
374
-
375
- path.replaceWith(
376
- t.callExpression(
377
- t.arrowFunctionExpression([], t.blockStatement(body)),
378
- []
379
- )
380
- );
381
- },
382
- JSXFragment(path) {
383
- didTransform = true;
384
- if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
385
- return;
386
- if (isSSR) {
387
- path.replaceWith(
388
- t.callExpression(t.identifier("__h"), [
389
- processFragmentSSR(path.node, signals),
390
- ]),
391
- );
392
- return;
393
- }
394
-
395
- const statements: t.Statement[] = [];
396
- const genId = (() => { let i = 0; return () => `_el${i++}`; })();
397
-
398
- const root = processFragment(
399
- path.node, statements, genId, signals, scopedHash,
400
- isHydrate,
401
- isHydrate ? "__nodes" : undefined,
402
- );
403
-
404
- const body: t.Statement[] = [];
405
-
406
- if (isHydrate) {
407
- // DECLARE __nodes locally — every IIFE owns its own context
408
- body.push(
409
- t.variableDeclaration("const", [
410
- t.variableDeclarator(
411
- t.identifier("__nodes"),
412
- t.callExpression(t.identifier("getHydrationNodes"), [])
413
- )
414
- ])
415
- );
416
- }
417
-
418
- body.push(...statements);
419
- body.push(t.returnStatement(t.identifier(root)));
420
-
421
- path.replaceWith(
422
- t.callExpression(
423
- t.arrowFunctionExpression([], t.blockStatement(body)),
424
- []
425
- )
426
- );
427
- },
428
- },
429
- };
430
- }
1
+ import { type PluginObject, types as t, template } from "@babel/core";
2
+ import { handleDerived } from "./handlers/dom/derived.ts";
3
+ import { handleEffect } from "./handlers/dom/effect.ts";
4
+ import { handleState } from "./handlers/dom/state.ts";
5
+ import { handleDerivedSSR } from "./handlers/ssr/derived.ts";
6
+ import { handleStateSSR } from "./handlers/ssr/state.ts";
7
+ import { processElement, processFragment } from "./jsx/index.ts";
8
+ import { processElementSSR, processFragmentSSR } from "./jsx/ssr.ts";
9
+ import { warnDeadReactivity } from "./util/dead-code.ts";
10
+ import { getMacro } from "./util/helpers.ts";
11
+ import { MAGIC } from "./util/magic.ts";
12
+
13
+ const SSR_HELPERS = template.statements.ast(`
14
+ const __SAFE = Symbol.for('sigil.safe');
15
+ class __H {
16
+ constructor(v) { this.v = v; this[__SAFE] = true; }
17
+ toString() { return this.v; }
18
+ }
19
+ const __h = (v) => {
20
+ if (v?.[__SAFE]) return v;
21
+ if (Array.isArray(v)) return new __H(v.map(x => x?.[__SAFE] ? x.v : __e(x)).join(''));
22
+ return new __H(String(v ?? ''));
23
+ };
24
+ const __e = (v) => {
25
+ if (v?.[__SAFE]) return v.v;
26
+ if (Array.isArray(v)) return v.map(x => x?.[__SAFE] ? x.v : __e(x)).join('');
27
+ if (v === false || v === null || v === undefined) return '';
28
+ return String(v).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
29
+ };
30
+ `);
31
+ /**
32
+ * Recursively convert key={expr} to data-key={expr} in JSX tree.
33
+ * Needed for SSR output so hydration can map items by key.
34
+ */
35
+ function convertKeyToDataKey(node: t.JSXElement): void {
36
+ for (let i = 0; i < node.openingElement.attributes.length; i++) {
37
+ const attr = node.openingElement.attributes[i];
38
+ if (
39
+ t.isJSXAttribute(attr) &&
40
+ t.isJSXIdentifier(attr.name) &&
41
+ attr.name.name === "key"
42
+ ) {
43
+ node.openingElement.attributes[i] = t.jsxAttribute(
44
+ t.jsxIdentifier("data-key"),
45
+ attr.value,
46
+ );
47
+ }
48
+ }
49
+ for (const child of node.children) {
50
+ if (t.isJSXElement(child)) {
51
+ convertKeyToDataKey(child);
52
+ } else if (
53
+ t.isJSXExpressionContainer(child) &&
54
+ t.isJSXElement(child.expression)
55
+ ) {
56
+ convertKeyToDataKey(child.expression);
57
+ }
58
+ }
59
+ }
60
+
61
+ interface SigilOptions {
62
+ hash?: string;
63
+ mode?: "dom" | "ssr" | "hydrate";
64
+ }
65
+
66
+ export default function sigilPlugin(): PluginObject {
67
+ const signals = new Set<string>();
68
+ const storeSignals = new Set<string>();
69
+ let scopedHash: string | undefined;
70
+ let isSSR = false;
71
+ let isHydrate = false;
72
+ let didTransform = false;
73
+
74
+ return {
75
+ name: "sigil",
76
+ visitor: {
77
+ Program: {
78
+ enter(path, state) {
79
+ signals.clear();
80
+ storeSignals.clear();
81
+ const opts = (state.opts || {}) as SigilOptions;
82
+ scopedHash = opts.hash;
83
+ didTransform = false;
84
+ isSSR = opts.mode === "ssr";
85
+ isHydrate = opts.mode === "hydrate";
86
+
87
+ if (isSSR) {
88
+ path.unshiftContainer("body", SSR_HELPERS);
89
+ return;
90
+ }
91
+ },
92
+ exit(path, state) {
93
+ warnDeadReactivity(path, signals, storeSignals, state.filename);
94
+
95
+ // Only inject runtime import if this file actually used sigil transforms
96
+ if (!didTransform || isSSR) return;
97
+
98
+ const requiredSpecifiers = [
99
+ "createSignal",
100
+ "createRawSignal",
101
+ "createEffect",
102
+ "createMemo",
103
+ "reconcile",
104
+ "claim",
105
+ "claimText",
106
+ "claimComment",
107
+ "hydrateKeyedList",
108
+ "insert",
109
+ "getHydrationNodes",
110
+ "pushHydrationNodes",
111
+ "popHydrationNodes",
112
+ "snapshot",
113
+ "tracking",
114
+ "createEffectPre",
115
+ "createInspectEffect",
116
+ "withEffectScope",
117
+ ];
118
+ console.log(
119
+ "[sigil exit] didTransform:",
120
+ didTransform,
121
+ "withEffectScope in required:",
122
+ requiredSpecifiers.includes("withEffectScope"),
123
+ );
124
+ const runtimeImport = path.node.body.find(
125
+ (n): n is t.ImportDeclaration =>
126
+ t.isImportDeclaration(n) &&
127
+ n.source.value === "@sigil-dev/runtime",
128
+ );
129
+
130
+ if (runtimeImport) {
131
+ console.log(
132
+ "[sigil exit] found existing import, specifier count:",
133
+ runtimeImport.specifiers.length,
134
+ );
135
+ const existingNames = new Set(
136
+ runtimeImport.specifiers
137
+ .filter(t.isImportSpecifier)
138
+ .map((s) =>
139
+ t.isIdentifier(s.imported)
140
+ ? s.imported.name
141
+ : (s.imported as t.StringLiteral).value,
142
+ ),
143
+ );
144
+ for (const name of requiredSpecifiers) {
145
+ if (!existingNames.has(name)) {
146
+ runtimeImport.specifiers.push(
147
+ t.importSpecifier(t.identifier(name), t.identifier(name)),
148
+ );
149
+ }
150
+ }
151
+ } else {
152
+ path.unshiftContainer(
153
+ "body",
154
+ t.importDeclaration(
155
+ requiredSpecifiers.map((name) =>
156
+ t.importSpecifier(t.identifier(name), t.identifier(name)),
157
+ ),
158
+ t.stringLiteral("@sigil-dev/runtime"),
159
+ ),
160
+ );
161
+ }
162
+ },
163
+ },
164
+ VariableDeclaration(path) {
165
+ for (const declarator of path.get("declarations")) {
166
+ const macro = getMacro(declarator);
167
+ if (!macro) continue;
168
+ didTransform = true;
169
+ const { name, init } = macro;
170
+ const varName = t.isIdentifier(declarator.node.id)
171
+ ? declarator.node.id.name
172
+ : null;
173
+
174
+ if (name === "$state" || name === "$store" || name === "$state.raw") {
175
+ if (name === "$state.raw") {
176
+ if (!isSSR) {
177
+ const args = init.node.arguments;
178
+ init.replaceWith(
179
+ t.callExpression(t.identifier(MAGIC.createRawSignal), args),
180
+ );
181
+ }
182
+ continue;
183
+ }
184
+ isSSR
185
+ ? handleStateSSR(path, declarator, init, signals)
186
+ : handleState(path, declarator, init, signals);
187
+ if (name === "$store" && varName) {
188
+ storeSignals.add(varName);
189
+ }
190
+ } else if (name === "$derived") {
191
+ isSSR
192
+ ? handleDerivedSSR(path, declarator, init, signals)
193
+ : handleDerived(path, declarator, init, signals);
194
+ }
195
+ }
196
+ },
197
+ ExpressionStatement(path) {
198
+ if (isSSR) {
199
+ // drop $effect, $inspect, and $debug entirely in SSR
200
+ const expr = path.get("expression");
201
+ if (expr.isCallExpression()) {
202
+ const callee = expr.get("callee");
203
+ if (
204
+ callee.isIdentifier() &&
205
+ (callee.node.name === "$effect" ||
206
+ callee.node.name === "$inspect" ||
207
+ callee.node.name === "$debug")
208
+ ) {
209
+ didTransform = true;
210
+ path.remove();
211
+ }
212
+ }
213
+ return;
214
+ }
215
+ handleEffect(path);
216
+ // Handle $debug(expr) expression statements
217
+ const expr = path.get("expression");
218
+ if (expr.isCallExpression()) {
219
+ const callee = expr.get("callee");
220
+ if (callee.isIdentifier({ name: "$debug" })) {
221
+ didTransform = true;
222
+ path.replaceWith(
223
+ t.ifStatement(
224
+ t.memberExpression(
225
+ t.memberExpression(
226
+ t.memberExpression(
227
+ t.identifier("import"),
228
+ t.identifier("meta"),
229
+ ),
230
+ t.identifier("env"),
231
+ ),
232
+ t.identifier("DEV"),
233
+ ),
234
+ t.blockStatement([
235
+ t.debuggerStatement(),
236
+ t.expressionStatement(
237
+ t.callExpression(
238
+ t.memberExpression(
239
+ t.identifier("console"),
240
+ t.identifier("log"),
241
+ ),
242
+ expr.node.arguments,
243
+ ),
244
+ ),
245
+ ]),
246
+ ),
247
+ );
248
+ }
249
+ }
250
+ },
251
+ CallExpression(path) {
252
+ if (isSSR) return;
253
+ const callee = path.get("callee");
254
+
255
+ // Helper: wrap signal identifiers in call expressions
256
+ const wrapSignal = (expr: t.Expression): t.Expression => {
257
+ if (t.isIdentifier(expr) && signals.has(expr.name)) {
258
+ return t.callExpression(expr, []);
259
+ }
260
+ return expr;
261
+ };
262
+
263
+ // Handle member expression callees: $state.snapshot(x), $effect.tracking(), $effect.pre(fn)
264
+ if (callee.isMemberExpression()) {
265
+ const object = callee.get("object");
266
+ const property = callee.get("property");
267
+ if (object.isIdentifier() && property.isIdentifier()) {
268
+ const obj = object.node.name;
269
+ const prop = property.node.name;
270
+
271
+ if (obj === "$state" && prop === "snapshot") {
272
+ didTransform = true;
273
+ path.get("callee").replaceWith(t.identifier(MAGIC.snapshot));
274
+ return;
275
+ }
276
+
277
+ if (obj === "$effect" && prop === "tracking") {
278
+ didTransform = true;
279
+ path.get("callee").replaceWith(t.identifier(MAGIC.tracking));
280
+ return;
281
+ }
282
+
283
+ if (obj === "$effect" && prop === "pre") {
284
+ didTransform = true;
285
+ path
286
+ .get("callee")
287
+ .replaceWith(t.identifier(MAGIC.createEffectPre));
288
+ return;
289
+ }
290
+ }
291
+ }
292
+
293
+ // Handle $inspect(count).with(cb) chain
294
+ if (callee.isMemberExpression()) {
295
+ const property = callee.get("property");
296
+ if (property.isIdentifier({ name: "with" })) {
297
+ const object = callee.get("object");
298
+ if (object.isCallExpression()) {
299
+ const innerCallee = object.get("callee");
300
+ if (innerCallee.isIdentifier({ name: "$inspect" })) {
301
+ didTransform = true;
302
+ const args = object.node.arguments.map(wrapSignal);
303
+ const cb = path.node.arguments[0];
304
+ path.replaceWith(
305
+ t.callExpression(t.identifier(MAGIC.createInspectEffect), [
306
+ t.arrowFunctionExpression([], t.arrayExpression(args)),
307
+ cb,
308
+ ]),
309
+ );
310
+ return;
311
+ }
312
+ }
313
+ }
314
+ }
315
+
316
+ // Handle standalone $debug(...) — not as expression statement
317
+ if (callee.isIdentifier({ name: "$debug" })) {
318
+ didTransform = true;
319
+ path.replaceWith(
320
+ t.ifStatement(
321
+ t.memberExpression(
322
+ t.memberExpression(
323
+ t.memberExpression(
324
+ t.identifier("import"),
325
+ t.identifier("meta"),
326
+ ),
327
+ t.identifier("env"),
328
+ ),
329
+ t.identifier("DEV"),
330
+ ),
331
+ t.blockStatement([
332
+ t.debuggerStatement(),
333
+ t.expressionStatement(
334
+ t.callExpression(
335
+ t.memberExpression(
336
+ t.identifier("console"),
337
+ t.identifier("log"),
338
+ ),
339
+ path.node.arguments,
340
+ ),
341
+ ),
342
+ ]),
343
+ ),
344
+ );
345
+ return;
346
+ }
347
+
348
+ // Handle standalone $inspect(...)
349
+ if (callee.isIdentifier({ name: "$inspect" })) {
350
+ didTransform = true;
351
+ const args = path.node.arguments.map(wrapSignal);
352
+ path.replaceWith(
353
+ t.callExpression(t.identifier(MAGIC.createInspectEffect), [
354
+ t.arrowFunctionExpression([], t.arrayExpression(args)),
355
+ ]),
356
+ );
357
+ return;
358
+ }
359
+ },
360
+ JSXElement(path) {
361
+ didTransform = true;
362
+ // console.log(
363
+ // "[sigil] JSXElement visitor fired, parent:",
364
+ // path.parentPath?.type,
365
+ // "isSSR:", isSSR
366
+ // );
367
+ if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
368
+ return;
369
+ if (isSSR) {
370
+ try {
371
+ convertKeyToDataKey(path.node);
372
+ path.replaceWith(
373
+ t.callExpression(t.identifier("__h"), [
374
+ processElementSSR(path.node, signals),
375
+ ]),
376
+ );
377
+ } catch (e) {
378
+ console.error("[sigil] processElementSSR threw:", path.toString());
379
+ console.error(e);
380
+ throw e;
381
+ }
382
+ return;
383
+ }
384
+
385
+ const statements: t.Statement[] = [];
386
+ const genId = (() => {
387
+ let i = 0;
388
+ return () => `_el${i++}`;
389
+ })();
390
+
391
+ const root = processElement(
392
+ path.node,
393
+ statements,
394
+ genId,
395
+ signals,
396
+ scopedHash,
397
+ isHydrate,
398
+ isHydrate ? "__nodes" : undefined,
399
+ );
400
+
401
+ const body: t.Statement[] = [];
402
+
403
+ if (isHydrate) {
404
+ // DECLARE __nodes locally — every IIFE owns its own context
405
+ body.push(
406
+ t.variableDeclaration("const", [
407
+ t.variableDeclarator(
408
+ t.identifier("__nodes"),
409
+ t.callExpression(t.identifier("getHydrationNodes"), []),
410
+ ),
411
+ ]),
412
+ );
413
+ }
414
+
415
+ body.push(...statements);
416
+ body.push(t.returnStatement(t.identifier(root)));
417
+
418
+ path.replaceWith(
419
+ t.callExpression(
420
+ t.arrowFunctionExpression([], t.blockStatement(body)),
421
+ [],
422
+ ),
423
+ );
424
+ },
425
+ JSXFragment(path) {
426
+ didTransform = true;
427
+ if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
428
+ return;
429
+ if (isSSR) {
430
+ path.replaceWith(
431
+ t.callExpression(t.identifier("__h"), [
432
+ processFragmentSSR(path.node, signals),
433
+ ]),
434
+ );
435
+ return;
436
+ }
437
+
438
+ const statements: t.Statement[] = [];
439
+ const genId = (() => {
440
+ let i = 0;
441
+ return () => `_el${i++}`;
442
+ })();
443
+
444
+ const root = processFragment(
445
+ path.node,
446
+ statements,
447
+ genId,
448
+ signals,
449
+ scopedHash,
450
+ isHydrate,
451
+ isHydrate ? "__nodes" : undefined,
452
+ );
453
+
454
+ const body: t.Statement[] = [];
455
+
456
+ if (isHydrate) {
457
+ // DECLARE __nodes locally — every IIFE owns its own context
458
+ body.push(
459
+ t.variableDeclaration("const", [
460
+ t.variableDeclarator(
461
+ t.identifier("__nodes"),
462
+ t.callExpression(t.identifier("getHydrationNodes"), []),
463
+ ),
464
+ ]),
465
+ );
466
+ }
467
+
468
+ body.push(...statements);
469
+ body.push(t.returnStatement(t.identifier(root)));
470
+
471
+ path.replaceWith(
472
+ t.callExpression(
473
+ t.arrowFunctionExpression([], t.blockStatement(body)),
474
+ [],
475
+ ),
476
+ );
477
+ },
478
+ },
479
+ };
480
+ }