@sigil-dev/compiler 0.6.12 → 0.7.1
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.
- package/package.json +1 -1
- package/src/babel/index.ts +210 -195
- package/src/babel/jsx/element.ts +24 -1
- package/src/babel/util/bind.ts +121 -61
- package/src/babel/util/dead-code.ts +28 -0
- package/test/jsx.test.ts +56 -0
- package/test/tree-shaking.test.ts +79 -0
package/package.json
CHANGED
package/src/babel/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { handleStateSSR } from "./handlers/ssr/state.ts";
|
|
|
7
7
|
import { processElement, processFragment } from "./jsx/index.ts";
|
|
8
8
|
import { processElementSSR, processFragmentSSR } from "./jsx/ssr.ts";
|
|
9
9
|
import { getMacro } from "./util/helpers.ts";
|
|
10
|
+
import { warnDeadReactivity } from "./util/dead-code.ts";
|
|
10
11
|
|
|
11
12
|
const SSR_HELPERS = template.statements.ast(`
|
|
12
13
|
const __SAFE = Symbol.for('sigil.safe');
|
|
@@ -32,208 +33,222 @@ const SSR_HELPERS = template.statements.ast(`
|
|
|
32
33
|
* Needed for SSR output so hydration can map items by key.
|
|
33
34
|
*/
|
|
34
35
|
function convertKeyToDataKey(node: t.JSXElement): void {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
+
}
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
interface SigilOptions {
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
hash?: string;
|
|
63
|
+
mode?: "dom" | "ssr" | "hydrate";
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
export default function sigilPlugin(): PluginObject {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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;
|
|
70
72
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
73
|
+
return {
|
|
74
|
+
name: "sigil",
|
|
75
|
+
visitor: {
|
|
76
|
+
Program: {
|
|
77
|
+
enter(path, state) {
|
|
78
|
+
signals.clear();
|
|
79
|
+
storeSignals.clear();
|
|
80
|
+
const opts = (state.opts || {}) as SigilOptions;
|
|
81
|
+
scopedHash = opts.hash;
|
|
82
|
+
isSSR = opts.mode === "ssr";
|
|
83
|
+
isHydrate = opts.mode === "hydrate";
|
|
80
84
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
// SSR doesn't need any runtime imports except the `escape` utility.
|
|
86
|
+
if (isSSR) {
|
|
87
|
+
path.unshiftContainer("body", SSR_HELPERS);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
86
90
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
91
|
+
path.unshiftContainer(
|
|
92
|
+
"body",
|
|
93
|
+
t.importDeclaration(
|
|
94
|
+
[
|
|
95
|
+
t.importSpecifier(
|
|
96
|
+
t.identifier("createSignal"),
|
|
97
|
+
t.identifier("createSignal"),
|
|
98
|
+
),
|
|
99
|
+
t.importSpecifier(
|
|
100
|
+
t.identifier("createEffect"),
|
|
101
|
+
t.identifier("createEffect"),
|
|
102
|
+
),
|
|
103
|
+
t.importSpecifier(
|
|
104
|
+
t.identifier("createMemo"),
|
|
105
|
+
t.identifier("createMemo"),
|
|
106
|
+
),
|
|
107
|
+
t.importSpecifier(
|
|
108
|
+
t.identifier("reconcile"),
|
|
109
|
+
t.identifier("reconcile"),
|
|
110
|
+
),
|
|
111
|
+
t.importSpecifier(t.identifier("claim"), t.identifier("claim")),
|
|
112
|
+
t.importSpecifier(
|
|
113
|
+
t.identifier("claimText"),
|
|
114
|
+
t.identifier("claimText"),
|
|
115
|
+
),
|
|
116
|
+
t.importSpecifier(
|
|
117
|
+
t.identifier("claimComment"),
|
|
118
|
+
t.identifier("claimComment"),
|
|
119
|
+
),
|
|
120
|
+
t.importSpecifier(
|
|
121
|
+
t.identifier("hydrateKeyedList"),
|
|
122
|
+
t.identifier("hydrateKeyedList"),
|
|
123
|
+
),
|
|
124
|
+
t.importSpecifier(
|
|
125
|
+
t.identifier("insert"),
|
|
126
|
+
t.identifier("insert"),
|
|
127
|
+
),
|
|
128
|
+
],
|
|
129
|
+
t.stringLiteral("@sigil-dev/runtime"),
|
|
130
|
+
),
|
|
131
|
+
);
|
|
132
|
+
},
|
|
133
|
+
exit(path, state) {
|
|
134
|
+
console.log("signals at exit:", [...signals]);
|
|
135
|
+
warnDeadReactivity(path, signals, storeSignals, state.filename)
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
VariableDeclaration(path) {
|
|
139
|
+
for (const declarator of path.get("declarations")) {
|
|
140
|
+
const macro = getMacro(declarator);
|
|
141
|
+
if (!macro) continue;
|
|
142
|
+
const { name, init } = macro;
|
|
143
|
+
const varName = t.isIdentifier(declarator.node.id)
|
|
144
|
+
? declarator.node.id.name
|
|
145
|
+
: null;
|
|
146
|
+
|
|
147
|
+
if (name === "$state" || name === "$store") {
|
|
148
|
+
isSSR
|
|
149
|
+
? handleStateSSR(path, declarator, init)
|
|
150
|
+
: handleState(path, declarator, init, signals);
|
|
151
|
+
if (name === "$store" && varName) {
|
|
152
|
+
storeSignals.add(varName);
|
|
153
|
+
}
|
|
154
|
+
} else if (name === "$derived") {
|
|
155
|
+
isSSR
|
|
156
|
+
? handleDerivedSSR(path, declarator, init)
|
|
157
|
+
: handleDerived(path, declarator, init, signals);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
ExpressionStatement(path) {
|
|
162
|
+
if (isSSR) {
|
|
163
|
+
// drop $effect entirely in SSR
|
|
164
|
+
const expr = path.get("expression");
|
|
165
|
+
if (expr.isCallExpression()) {
|
|
166
|
+
const callee = expr.get("callee");
|
|
167
|
+
if (callee.isIdentifier() && callee.node.name === "$effect") {
|
|
168
|
+
path.remove();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
handleEffect(path);
|
|
174
|
+
},
|
|
175
|
+
JSXElement(path) {
|
|
176
|
+
if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
|
|
177
|
+
return;
|
|
178
|
+
if (isSSR) {
|
|
179
|
+
convertKeyToDataKey(path.node);
|
|
180
|
+
path.replaceWith(
|
|
181
|
+
t.callExpression(t.identifier("__h"), [
|
|
182
|
+
processElementSSR(path.node, signals),
|
|
183
|
+
]),
|
|
184
|
+
);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const statements: t.Statement[] = [];
|
|
188
|
+
const genId = (() => {
|
|
189
|
+
let i = 0;
|
|
190
|
+
return () => `_el${i++}`;
|
|
191
|
+
})();
|
|
192
|
+
const root = processElement(
|
|
193
|
+
path.node,
|
|
194
|
+
statements,
|
|
195
|
+
genId,
|
|
196
|
+
signals,
|
|
197
|
+
scopedHash,
|
|
198
|
+
isHydrate,
|
|
199
|
+
isHydrate ? "__nodes" : undefined,
|
|
200
|
+
);
|
|
201
|
+
path.replaceWith(
|
|
202
|
+
t.callExpression(
|
|
203
|
+
t.arrowFunctionExpression(
|
|
204
|
+
[],
|
|
205
|
+
t.blockStatement([
|
|
206
|
+
...statements,
|
|
207
|
+
t.returnStatement(t.identifier(root)),
|
|
208
|
+
]),
|
|
209
|
+
),
|
|
210
|
+
[],
|
|
211
|
+
),
|
|
212
|
+
);
|
|
213
|
+
},
|
|
214
|
+
JSXFragment(path) {
|
|
215
|
+
if (path.parentPath?.isJSXElement() || path.parentPath?.isJSXFragment())
|
|
216
|
+
return;
|
|
217
|
+
if (isSSR) {
|
|
218
|
+
path.replaceWith(
|
|
219
|
+
t.callExpression(t.identifier("__h"), [
|
|
220
|
+
processFragmentSSR(path.node, signals),
|
|
221
|
+
]),
|
|
222
|
+
);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const statements: t.Statement[] = [];
|
|
226
|
+
const genId = (() => {
|
|
227
|
+
let i = 0;
|
|
228
|
+
return () => `_el${i++}`;
|
|
229
|
+
})();
|
|
230
|
+
const root = processFragment(
|
|
231
|
+
path.node,
|
|
232
|
+
statements,
|
|
233
|
+
genId,
|
|
234
|
+
signals,
|
|
235
|
+
scopedHash,
|
|
236
|
+
isHydrate,
|
|
237
|
+
isHydrate ? "__nodes" : undefined,
|
|
238
|
+
);
|
|
239
|
+
path.replaceWith(
|
|
240
|
+
t.callExpression(
|
|
241
|
+
t.arrowFunctionExpression(
|
|
242
|
+
[],
|
|
243
|
+
t.blockStatement([
|
|
244
|
+
...statements,
|
|
245
|
+
t.returnStatement(t.identifier(root)),
|
|
246
|
+
]),
|
|
247
|
+
),
|
|
248
|
+
[],
|
|
249
|
+
),
|
|
250
|
+
);
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
};
|
|
239
254
|
}
|
package/src/babel/jsx/element.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { types as t } from "@babel/core";
|
|
2
|
-
import {
|
|
2
|
+
import {generate} from "@babel/generator"
|
|
3
|
+
import { buildBind, buildProxyBind } from "../util/bind";
|
|
3
4
|
import { buildAnchorMount } from "./anchor-mount";
|
|
4
5
|
import { collectChildren } from "./children";
|
|
5
6
|
import { processFragment } from "./fragment";
|
|
@@ -181,7 +182,29 @@ export function processElement(
|
|
|
181
182
|
),
|
|
182
183
|
);
|
|
183
184
|
} else if (attrName.startsWith("bind")) {
|
|
185
|
+
const signal = (attr.value as t.JSXExpressionContainer)
|
|
186
|
+
.expression as t.Expression;
|
|
187
|
+
|
|
188
|
+
const isTopLevelSignal = t.isCallExpression(signal) &&
|
|
189
|
+
t.isIdentifier((signal as t.CallExpression).callee) &&
|
|
190
|
+
signals.has(((signal as t.CallExpression).callee as t.Identifier).name);
|
|
191
|
+
|
|
192
|
+
const isMemberOfSignal = t.isMemberExpression(signal) &&
|
|
193
|
+
t.isCallExpression((signal as t.MemberExpression).object) &&
|
|
194
|
+
t.isIdentifier(((signal as t.MemberExpression).object as t.CallExpression).callee) &&
|
|
195
|
+
signals.has((((signal as t.MemberExpression).object as t.CallExpression).callee as t.Identifier).name);
|
|
196
|
+
|
|
197
|
+
if (isTopLevelSignal) {
|
|
184
198
|
buildBind(varName, attrName, attr, statements);
|
|
199
|
+
} else if (isMemberOfSignal) {
|
|
200
|
+
buildProxyBind(varName, attrName, attr, statements, signal);
|
|
201
|
+
} else {
|
|
202
|
+
console.warn(
|
|
203
|
+
`[sigil] ${attrName}={...} requires a $state signal or $state object property. ` +
|
|
204
|
+
`Got: ${generate(signal).code}. ` +
|
|
205
|
+
`Hint: change to a $state() call.`
|
|
206
|
+
);
|
|
207
|
+
}
|
|
185
208
|
} else if (t.isStringLiteral(attr.value)) {
|
|
186
209
|
statements.push(
|
|
187
210
|
t.expressionStatement(
|
package/src/babel/util/bind.ts
CHANGED
|
@@ -6,70 +6,130 @@ const BIND_EVENT: Record<string, string> = {
|
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
export function buildBind(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
varName: string,
|
|
10
|
+
attrName: string,
|
|
11
|
+
attr: t.JSXAttribute,
|
|
12
|
+
statements: t.Statement[],
|
|
13
13
|
): void {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const signal = (attr.value as t.JSXExpressionContainer)
|
|
15
|
+
.expression as t.CallExpression; // already text()
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
if (attrName === "bindThis") {
|
|
19
|
-
statements.push(
|
|
20
|
-
t.expressionStatement(
|
|
21
|
-
t.callExpression(t.memberExpression(signal, t.identifier("set")), [
|
|
22
|
-
t.identifier(varName),
|
|
23
|
-
]),
|
|
24
|
-
),
|
|
25
|
-
);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
17
|
+
const signalId = signal.callee as t.Identifier; // text — for .set()
|
|
28
18
|
|
|
29
|
-
|
|
30
|
-
|
|
19
|
+
if (attrName === "bindThis") {
|
|
20
|
+
statements.push(
|
|
21
|
+
t.expressionStatement(
|
|
22
|
+
t.callExpression(
|
|
23
|
+
t.memberExpression(signalId, t.identifier("set")),
|
|
24
|
+
[t.identifier(varName)],
|
|
25
|
+
),
|
|
26
|
+
),
|
|
27
|
+
);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
t.expressionStatement(
|
|
35
|
-
t.callExpression(t.identifier("createEffect"), [
|
|
36
|
-
t.arrowFunctionExpression(
|
|
37
|
-
[],
|
|
38
|
-
t.blockStatement([
|
|
39
|
-
t.expressionStatement(
|
|
40
|
-
t.assignmentExpression(
|
|
41
|
-
"=",
|
|
42
|
-
t.memberExpression(t.identifier(varName), t.identifier(prop)),
|
|
43
|
-
t.callExpression(signal, []),
|
|
44
|
-
),
|
|
45
|
-
),
|
|
46
|
-
]),
|
|
47
|
-
),
|
|
48
|
-
]),
|
|
49
|
-
),
|
|
50
|
-
);
|
|
31
|
+
const prop = attrName.slice(4).toLowerCase();
|
|
32
|
+
const event = BIND_EVENT[prop] ?? "input";
|
|
51
33
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
34
|
+
// createEffect(() => _el.prop = text())
|
|
35
|
+
statements.push(
|
|
36
|
+
t.expressionStatement(
|
|
37
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
38
|
+
t.arrowFunctionExpression(
|
|
39
|
+
[],
|
|
40
|
+
t.blockStatement([
|
|
41
|
+
t.expressionStatement(
|
|
42
|
+
t.assignmentExpression(
|
|
43
|
+
"=",
|
|
44
|
+
t.memberExpression(t.identifier(varName), t.identifier(prop)),
|
|
45
|
+
signal, // text() — already correct
|
|
46
|
+
),
|
|
47
|
+
),
|
|
48
|
+
]),
|
|
49
|
+
),
|
|
50
|
+
]),
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
// addEventListener(event, e => text.set(e.target.prop))
|
|
55
|
+
statements.push(
|
|
56
|
+
t.expressionStatement(
|
|
57
|
+
t.callExpression(
|
|
58
|
+
t.memberExpression(t.identifier(varName), t.identifier("addEventListener")),
|
|
59
|
+
[
|
|
60
|
+
t.stringLiteral(event),
|
|
61
|
+
t.arrowFunctionExpression(
|
|
62
|
+
[t.identifier("e")],
|
|
63
|
+
t.callExpression(
|
|
64
|
+
t.memberExpression(signalId, t.identifier("set")),
|
|
65
|
+
[
|
|
66
|
+
t.memberExpression(
|
|
67
|
+
t.memberExpression(t.identifier("e"), t.identifier("target")),
|
|
68
|
+
t.identifier(prop),
|
|
69
|
+
),
|
|
70
|
+
],
|
|
71
|
+
),
|
|
72
|
+
),
|
|
73
|
+
],
|
|
74
|
+
),
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function buildProxyBind(
|
|
80
|
+
varName: string,
|
|
81
|
+
attrName: string,
|
|
82
|
+
attr: t.JSXAttribute,
|
|
83
|
+
statements: t.Statement[],
|
|
84
|
+
signal: t.MemberExpression, // already form().name
|
|
85
|
+
): void {
|
|
86
|
+
const prop = attrName.slice(4).toLowerCase();
|
|
87
|
+
const event = BIND_EVENT[prop] ?? "input";
|
|
88
|
+
|
|
89
|
+
// createEffect(() => el.prop = form().name)
|
|
90
|
+
statements.push(
|
|
91
|
+
t.expressionStatement(
|
|
92
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
93
|
+
t.arrowFunctionExpression(
|
|
94
|
+
[],
|
|
95
|
+
t.blockStatement([
|
|
96
|
+
t.expressionStatement(
|
|
97
|
+
t.assignmentExpression(
|
|
98
|
+
"=",
|
|
99
|
+
t.memberExpression(t.identifier(varName), t.identifier(prop)),
|
|
100
|
+
signal,
|
|
101
|
+
),
|
|
102
|
+
),
|
|
103
|
+
]),
|
|
104
|
+
),
|
|
105
|
+
]),
|
|
106
|
+
),
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// addEventListener(event, e => { form().name = e.target.prop })
|
|
110
|
+
statements.push(
|
|
111
|
+
t.expressionStatement(
|
|
112
|
+
t.callExpression(
|
|
113
|
+
t.memberExpression(t.identifier(varName), t.identifier("addEventListener")),
|
|
114
|
+
[
|
|
115
|
+
t.stringLiteral(event),
|
|
116
|
+
t.arrowFunctionExpression(
|
|
117
|
+
[t.identifier("e")],
|
|
118
|
+
t.blockStatement([
|
|
119
|
+
t.expressionStatement(
|
|
120
|
+
t.assignmentExpression(
|
|
121
|
+
"=",
|
|
122
|
+
signal as unknown as t.LVal, // form().name — valid LVal
|
|
123
|
+
t.memberExpression(
|
|
124
|
+
t.memberExpression(t.identifier("e"), t.identifier("target")),
|
|
125
|
+
t.identifier(prop),
|
|
126
|
+
),
|
|
127
|
+
),
|
|
128
|
+
),
|
|
129
|
+
]),
|
|
130
|
+
),
|
|
131
|
+
],
|
|
132
|
+
),
|
|
133
|
+
),
|
|
134
|
+
);
|
|
75
135
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { traverse, types as t, NodePath } from "@babel/core";
|
|
2
|
+
|
|
3
|
+
export function warnDeadReactivity(
|
|
4
|
+
path: NodePath,
|
|
5
|
+
signals: Set<string>,
|
|
6
|
+
storeSignals: Set<string>,
|
|
7
|
+
filename?: string,
|
|
8
|
+
): void {
|
|
9
|
+
const used = new Set<string>();
|
|
10
|
+
|
|
11
|
+
path.traverse({
|
|
12
|
+
CallExpression(innerPath: any) {
|
|
13
|
+
if (t.isIdentifier(innerPath.node.callee)) {
|
|
14
|
+
used.add(innerPath.node.callee.name);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
for (const name of signals) {
|
|
20
|
+
if (storeSignals.has(name)) continue; // exported, used across files
|
|
21
|
+
if (!used.has(name)) {
|
|
22
|
+
const tag = filename ? `[sigil:${filename}]` : "[sigil]";
|
|
23
|
+
console.warn(
|
|
24
|
+
`${tag} unused reactive declaration: "${name}" is declared with $state or $derived but never read.`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
package/test/jsx.test.ts
CHANGED
|
@@ -136,4 +136,60 @@ describe("JSX", () => {
|
|
|
136
136
|
expect(result).toContain("createTextNode");
|
|
137
137
|
});
|
|
138
138
|
});
|
|
139
|
+
|
|
140
|
+
describe("Bind", () => {
|
|
141
|
+
test("bindValue compiles to effect + event listener", () => {
|
|
142
|
+
const code = transform(`
|
|
143
|
+
let text = $state("");
|
|
144
|
+
const el = <input bindValue={text} />;
|
|
145
|
+
`);
|
|
146
|
+
// getter: createEffect(() => el.value = text())
|
|
147
|
+
expect(code).toContain("createEffect");
|
|
148
|
+
expect(code).toContain(".value = text()");
|
|
149
|
+
// setter: addEventListener("input", e => text.set(e.target.value))
|
|
150
|
+
expect(code).toContain('"input"');
|
|
151
|
+
expect(code).toContain("text.set(e.target.value)");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("bindChecked compiles to change event", () => {
|
|
155
|
+
const code = transform(`
|
|
156
|
+
let checked = $state(false);
|
|
157
|
+
const el = <input type="checkbox" bindChecked={checked} />;
|
|
158
|
+
`);
|
|
159
|
+
expect(code).toContain(".checked = checked()");
|
|
160
|
+
expect(code).toContain('"change"');
|
|
161
|
+
expect(code).toContain("checked.set(e.target.checked)");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("bindThis assigns element reference", () => {
|
|
165
|
+
const code = transform(`
|
|
166
|
+
let el = $state(null);
|
|
167
|
+
const div = <div bindThis={el} />;
|
|
168
|
+
`);
|
|
169
|
+
expect(code).toContain("el.set(");
|
|
170
|
+
expect(code).not.toContain("addEventListener");
|
|
171
|
+
expect(code).not.toContain("createEffect(");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("bindValue on object property uses proxy assignment", () => {
|
|
175
|
+
const code = transform(`
|
|
176
|
+
let form = $state({ name: "" });
|
|
177
|
+
const el = <input bindValue={form.name} />;
|
|
178
|
+
`);
|
|
179
|
+
// getter: createEffect(() => el.value = form().name)
|
|
180
|
+
expect(code).toContain("form().name");
|
|
181
|
+
expect(code).toContain("createEffect(");
|
|
182
|
+
// setter: proxy assignment, not .set()
|
|
183
|
+
expect(code).toContain("form().name = e.target.value");
|
|
184
|
+
expect(code).not.toContain("form.name.set");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("bindValue does not affect non-signal expressions", () => {
|
|
188
|
+
// static string should not compile — but if it does, at least no crash
|
|
189
|
+
// this is a misuse case, just verify it does not throw
|
|
190
|
+
expect(() => transform(`
|
|
191
|
+
const el = <input bindValue={"static"} />;
|
|
192
|
+
`)).not.toThrow();
|
|
193
|
+
});
|
|
194
|
+
});
|
|
139
195
|
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, spyOn, test } from "bun:test";
|
|
2
|
+
import { transform } from "./helpers/transform.ts";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
describe("Dead code warnings", () => {
|
|
6
|
+
test("warns on unused $state", () => {
|
|
7
|
+
const warn = spyOn(console, "warn");
|
|
8
|
+
transform(`
|
|
9
|
+
let unused = $state(0);
|
|
10
|
+
const el = <div />;
|
|
11
|
+
`);
|
|
12
|
+
expect(warn).toHaveBeenCalledWith(
|
|
13
|
+
expect.stringContaining('unused reactive declaration: "unused"'),
|
|
14
|
+
);
|
|
15
|
+
warn.mockRestore();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("warns on unused $derived", () => {
|
|
19
|
+
const warn = spyOn(console, "warn");
|
|
20
|
+
transform(`
|
|
21
|
+
let count = $state(0);
|
|
22
|
+
let doubled = $derived(count * 2);
|
|
23
|
+
const el = <div>{count}</div>;
|
|
24
|
+
`);
|
|
25
|
+
expect(warn).toHaveBeenCalledWith(
|
|
26
|
+
expect.stringContaining('unused reactive declaration: "doubled"'),
|
|
27
|
+
);
|
|
28
|
+
warn.mockRestore();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("no warning when $state is used in JSX", () => {
|
|
32
|
+
const warn = spyOn(console, "warn");
|
|
33
|
+
transform(`
|
|
34
|
+
let count = $state(0);
|
|
35
|
+
const el = <div>{count}</div>;
|
|
36
|
+
`);
|
|
37
|
+
expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('"count"'));
|
|
38
|
+
warn.mockRestore();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("no warning when $state is used in effect", () => {
|
|
42
|
+
const warn = spyOn(console, "warn");
|
|
43
|
+
transform(`
|
|
44
|
+
let count = $state(0);
|
|
45
|
+
$effect(() => { console.log(count); });
|
|
46
|
+
`);
|
|
47
|
+
expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('"count"'));
|
|
48
|
+
warn.mockRestore();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("$store does not warn even if not used in same file", () => {
|
|
52
|
+
const warn = spyOn(console, "warn");
|
|
53
|
+
transform(`
|
|
54
|
+
export let count = $store(0);
|
|
55
|
+
export let user = $store({ name: "" });
|
|
56
|
+
`);
|
|
57
|
+
expect(warn).not.toHaveBeenCalledWith(
|
|
58
|
+
expect.stringContaining("unused reactive declaration")
|
|
59
|
+
);
|
|
60
|
+
warn.mockRestore();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("$store compiles to createSignal like $state", () => {
|
|
64
|
+
const code = transform(`
|
|
65
|
+
export let count = $store(0);
|
|
66
|
+
`);
|
|
67
|
+
expect(code).toContain("createSignal(0)");
|
|
68
|
+
expect(code).not.toContain("$store");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("$store in JSX works like $state", () => {
|
|
72
|
+
const code = transform(`
|
|
73
|
+
export let count = $store(0);
|
|
74
|
+
const el = <div>{count}</div>;
|
|
75
|
+
`);
|
|
76
|
+
expect(code).toContain("count()");
|
|
77
|
+
expect(code).not.toContain("$store");
|
|
78
|
+
});
|
|
79
|
+
});
|