@sigil-dev/compiler 0.9.25 → 0.9.27

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.
@@ -56,6 +56,8 @@ export function handleDerived(
56
56
 
57
57
  for (const ref of refs) {
58
58
  if (!ref.isIdentifier()) continue;
59
+ // `export { count }` exports the signal itself, like `export let`
60
+ if (ref.parentPath?.isExportSpecifier()) continue;
59
61
  if (ref.parentPath?.isUpdateExpression()) continue;
60
62
  ref.replaceWith(t.callExpression(ref.node, []));
61
63
  }
@@ -1,106 +1,108 @@
1
- import type { NodePath } from "@babel/core";
2
- import { types as t } from "@babel/core";
3
- import { MAGIC } from "../../util/magic.ts";
4
-
5
- export function handleState(
6
- declaration: NodePath<t.VariableDeclaration>,
7
- declarator: NodePath<t.VariableDeclarator>,
8
- init: NodePath<t.CallExpression>,
9
- signals: Set<string>,
10
- dev?: boolean,
11
- fileKey?: string, // e.g. "src/routes/counter.tsx"
12
- raw = false, // $state.raw: same rewrites, no deep proxy
13
- ): void {
14
- const factory = raw ? MAGIC.createRawSignal : MAGIC.createSignal;
15
- const { id } = declarator.node;
16
- if (!t.isIdentifier(id)) throw new Error("Destructured $state not supported");
17
- const name = id.name;
18
-
19
- // check for // @sigil:reset escape comment
20
- const hasReset = declaration.node.leadingComments?.some(
21
- (c) => c.value.trim() === "@sigil:reset",
22
- );
23
-
24
- const setCall = (value: t.Expression) =>
25
- t.callExpression(
26
- t.memberExpression(t.identifier(name), t.identifier("set")),
27
- [value],
28
- );
29
- const binding = declaration.scope.getBinding(name);
30
- if (!binding) return;
31
- const refs = [...binding.referencePaths];
32
- const violations = [...binding.constantViolations];
33
-
34
- const originalArgs = init.node.arguments;
35
-
36
- if (dev && fileKey && !hasReset) {
37
- const key = `${fileKey}#${name}`;
38
- // createSignal(__hmrAdopt__("file#count", original))
39
- init.replaceWith(
40
- t.callExpression(t.identifier(factory), [
41
- t.callExpression(t.identifier("__hmrAdopt__"), [
42
- t.stringLiteral(key),
43
- ...(originalArgs as t.Expression[]),
44
- ]),
45
- ]),
46
- );
47
- // __hmrRegister__("file#count", count) — inserted after declaration
48
- declaration.insertAfter(
49
- t.expressionStatement(
50
- t.callExpression(t.identifier("__hmrRegister__"), [
51
- t.stringLiteral(key),
52
- t.identifier(name),
53
- ]),
54
- ),
55
- );
56
- } else {
57
- init.replaceWith(
58
- t.callExpression(t.identifier(factory), originalArgs as t.Expression[]),
59
- );
60
- }
61
-
62
- declaration.node.kind = "const";
63
- // ... rest of rewrites unchanged
64
- // rewrite writes
65
- // violations go before refs becase UpdateExpression
66
- // is both a read and a write and this prevents count()++
67
- for (const violation of violations) {
68
- if (violation.isUpdateExpression()) {
69
- const op = violation.node.operator === "++" ? "+" : "-";
70
- violation.replaceWith(
71
- setCall(
72
- t.binaryExpression(
73
- op,
74
- t.callExpression(t.identifier(name), []),
75
- t.numericLiteral(1),
76
- ),
77
- ),
78
- );
79
- } else if (violation.isAssignmentExpression()) {
80
- const { operator, right } = violation.node;
81
- if (operator === "=") {
82
- // count = 5 -> count.set(5)
83
- violation.replaceWith(setCall(right));
84
- } else {
85
- // count += 2 -> count.set(count() + 2)
86
- const baseOp = operator.slice(0, -1);
87
- violation.replaceWith(
88
- setCall(
89
- t.binaryExpression(
90
- baseOp as t.BinaryExpression["operator"],
91
- t.callExpression(t.identifier(name), []),
92
- right,
93
- ),
94
- ),
95
- );
96
- }
97
- }
98
- }
99
-
100
- for (const ref of refs) {
101
- if (!ref.isIdentifier()) continue;
102
- // weird syntax, but just swaps the read with a call.
103
- ref.replaceWith(t.callExpression(ref.node, []));
104
- }
105
- signals.add(name);
106
- }
1
+ import type { NodePath } from "@babel/core";
2
+ import { types as t } from "@babel/core";
3
+ import { MAGIC } from "../../util/magic.ts";
4
+
5
+ export function handleState(
6
+ declaration: NodePath<t.VariableDeclaration>,
7
+ declarator: NodePath<t.VariableDeclarator>,
8
+ init: NodePath<t.CallExpression>,
9
+ signals: Set<string>,
10
+ dev?: boolean,
11
+ fileKey?: string, // e.g. "src/routes/counter.tsx"
12
+ raw = false, // $state.raw: same rewrites, no deep proxy
13
+ ): void {
14
+ const factory = raw ? MAGIC.createRawSignal : MAGIC.createSignal;
15
+ const { id } = declarator.node;
16
+ if (!t.isIdentifier(id)) throw new Error("Destructured $state not supported");
17
+ const name = id.name;
18
+
19
+ // check for // @sigil:reset escape comment
20
+ const hasReset = declaration.node.leadingComments?.some(
21
+ (c) => c.value.trim() === "@sigil:reset",
22
+ );
23
+
24
+ const setCall = (value: t.Expression) =>
25
+ t.callExpression(
26
+ t.memberExpression(t.identifier(name), t.identifier("set")),
27
+ [value],
28
+ );
29
+ const binding = declaration.scope.getBinding(name);
30
+ if (!binding) return;
31
+ const refs = [...binding.referencePaths];
32
+ const violations = [...binding.constantViolations];
33
+
34
+ const originalArgs = init.node.arguments;
35
+
36
+ if (dev && fileKey && !hasReset) {
37
+ const key = `${fileKey}#${name}`;
38
+ // createSignal(__hmrAdopt__("file#count", original))
39
+ init.replaceWith(
40
+ t.callExpression(t.identifier(factory), [
41
+ t.callExpression(t.identifier("__hmrAdopt__"), [
42
+ t.stringLiteral(key),
43
+ ...(originalArgs as t.Expression[]),
44
+ ]),
45
+ ]),
46
+ );
47
+ // __hmrRegister__("file#count", count) — inserted after declaration
48
+ declaration.insertAfter(
49
+ t.expressionStatement(
50
+ t.callExpression(t.identifier("__hmrRegister__"), [
51
+ t.stringLiteral(key),
52
+ t.identifier(name),
53
+ ]),
54
+ ),
55
+ );
56
+ } else {
57
+ init.replaceWith(
58
+ t.callExpression(t.identifier(factory), originalArgs as t.Expression[]),
59
+ );
60
+ }
61
+
62
+ declaration.node.kind = "const";
63
+ // ... rest of rewrites unchanged
64
+ // rewrite writes
65
+ // violations go before refs becase UpdateExpression
66
+ // is both a read and a write and this prevents count()++
67
+ for (const violation of violations) {
68
+ if (violation.isUpdateExpression()) {
69
+ const op = violation.node.operator === "++" ? "+" : "-";
70
+ violation.replaceWith(
71
+ setCall(
72
+ t.binaryExpression(
73
+ op,
74
+ t.callExpression(t.identifier(name), []),
75
+ t.numericLiteral(1),
76
+ ),
77
+ ),
78
+ );
79
+ } else if (violation.isAssignmentExpression()) {
80
+ const { operator, right } = violation.node;
81
+ if (operator === "=") {
82
+ // count = 5 -> count.set(5)
83
+ violation.replaceWith(setCall(right));
84
+ } else {
85
+ // count += 2 -> count.set(count() + 2)
86
+ const baseOp = operator.slice(0, -1);
87
+ violation.replaceWith(
88
+ setCall(
89
+ t.binaryExpression(
90
+ baseOp as t.BinaryExpression["operator"],
91
+ t.callExpression(t.identifier(name), []),
92
+ right,
93
+ ),
94
+ ),
95
+ );
96
+ }
97
+ }
98
+ }
99
+
100
+ for (const ref of refs) {
101
+ if (!ref.isIdentifier()) continue;
102
+ // `export { count }` exports the signal itself, like `export let`
103
+ if (ref.parentPath?.isExportSpecifier()) continue;
104
+ // weird syntax, but just swaps the read with a call.
105
+ ref.replaceWith(t.callExpression(ref.node, []));
106
+ }
107
+ signals.add(name);
108
+ }
@@ -21,6 +21,8 @@ export function handleDerivedSSR(
21
21
  if (!binding) return;
22
22
  for (const ref of [...binding.referencePaths]) {
23
23
  if (!ref.isIdentifier()) continue;
24
+ // `export { count }` exports the signal itself, like `export let`
25
+ if (ref.parentPath?.isExportSpecifier()) continue;
24
26
  if (ref.parentPath?.isUpdateExpression()) continue;
25
27
  ref.replaceWith(t.callExpression(ref.node, []));
26
28
  }