@hewel/zgui-build 0.0.0-stage → 0.1.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.
- package/LICENSE +202 -0
- package/NOTICE +95 -0
- package/README.md +84 -2
- package/THIRD_PARTY_NOTICES.md +37 -0
- package/package.json +64 -4
- package/sdk/Cargo.lock +6813 -0
- package/sdk/LICENSE +202 -0
- package/sdk/NOTICE +95 -0
- package/sdk/THIRD_PARTY_NOTICES.md +37 -0
- package/sdk/bun.lock +539 -0
- package/sdk/package.json +60 -0
- package/sdk/preview/fixtures/empty.json +6 -0
- package/sdk/preview/fonts/NotoSans-LICENSE.txt +94 -0
- package/sdk/preview/fonts/NotoSans-Regular.ttf +0 -0
- package/sdk/preview/fonts/NotoSansCJK-LICENSE.txt +92 -0
- package/sdk/preview/fonts/NotoSansCJKsc-Regular.otf +0 -0
- package/sdk/preview/fonts/PROVENANCE.md +25 -0
- package/sdk/preview/fonts/fonts.json +60 -0
- package/sdk/preview/profiles/default.json +32 -0
- package/sdk/preview/scenarios/counter-capture.json +9 -0
- package/sdk/smoke/solid-smoke.ts +53 -0
- package/sdk/smoke/tsconfig.smoke.json +21 -0
- package/src/application.ts +70 -0
- package/src/build.ts +963 -0
- package/src/check-smoke.ts +8 -0
- package/src/compiler.ts +701 -0
- package/src/css.ts +337 -0
- package/src/dev.ts +164 -0
- package/src/host.ts +578 -0
- package/src/identity.ts +60 -0
- package/src/index.ts +27 -0
- package/src/internal.ts +20 -0
- package/src/native-process.ts +178 -0
- package/src/package-app.ts +178 -0
- package/src/preview-check.ts +267 -0
- package/src/preview.ts +69 -0
- package/src/public-api.d.ts +258 -0
- package/src/publication.ts +108 -0
- package/src/sdk-packages.ts +376 -0
- package/src/sdk.ts +11 -0
- package/src/smoke.ts +99 -0
- package/src/sync-ui-resources.ts +171 -0
- package/src/typecheck.ts +177 -0
- package/src/validate.ts +1085 -0
- package/src/watch.ts +531 -0
package/src/validate.ts
ADDED
|
@@ -0,0 +1,1085 @@
|
|
|
1
|
+
import type { BuildInfo } from "./index.ts";
|
|
2
|
+
import { parseSync, type ParseResult, type ParserOptions } from "rolldown/utils";
|
|
3
|
+
import { isBuiltin } from "node:module";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
const globals = new Set([
|
|
7
|
+
"undefined",
|
|
8
|
+
"NaN",
|
|
9
|
+
"Infinity",
|
|
10
|
+
"globalThis",
|
|
11
|
+
"__zgui",
|
|
12
|
+
"console",
|
|
13
|
+
"performance",
|
|
14
|
+
"setTimeout",
|
|
15
|
+
"clearTimeout",
|
|
16
|
+
"requestAnimationFrame",
|
|
17
|
+
"cancelAnimationFrame",
|
|
18
|
+
"queueMicrotask",
|
|
19
|
+
"Object",
|
|
20
|
+
"Function",
|
|
21
|
+
"Boolean",
|
|
22
|
+
"Symbol",
|
|
23
|
+
"Error",
|
|
24
|
+
"AggregateError",
|
|
25
|
+
"EvalError",
|
|
26
|
+
"RangeError",
|
|
27
|
+
"ReferenceError",
|
|
28
|
+
"SyntaxError",
|
|
29
|
+
"TypeError",
|
|
30
|
+
"URIError",
|
|
31
|
+
"Number",
|
|
32
|
+
"BigInt",
|
|
33
|
+
"Math",
|
|
34
|
+
"Date",
|
|
35
|
+
"String",
|
|
36
|
+
"RegExp",
|
|
37
|
+
"Array",
|
|
38
|
+
"Int8Array",
|
|
39
|
+
"Uint8Array",
|
|
40
|
+
"Uint8ClampedArray",
|
|
41
|
+
"Int16Array",
|
|
42
|
+
"Uint16Array",
|
|
43
|
+
"Int32Array",
|
|
44
|
+
"Uint32Array",
|
|
45
|
+
"Float32Array",
|
|
46
|
+
"Float64Array",
|
|
47
|
+
"BigInt64Array",
|
|
48
|
+
"BigUint64Array",
|
|
49
|
+
"Map",
|
|
50
|
+
"Set",
|
|
51
|
+
"WeakMap",
|
|
52
|
+
"WeakSet",
|
|
53
|
+
"ArrayBuffer",
|
|
54
|
+
"SharedArrayBuffer",
|
|
55
|
+
"DataView",
|
|
56
|
+
"Atomics",
|
|
57
|
+
"JSON",
|
|
58
|
+
"Promise",
|
|
59
|
+
"Reflect",
|
|
60
|
+
"Proxy",
|
|
61
|
+
"WeakRef",
|
|
62
|
+
"FinalizationRegistry",
|
|
63
|
+
"decodeURI",
|
|
64
|
+
"decodeURIComponent",
|
|
65
|
+
"encodeURI",
|
|
66
|
+
"encodeURIComponent",
|
|
67
|
+
"escape",
|
|
68
|
+
"unescape",
|
|
69
|
+
"eval",
|
|
70
|
+
"isFinite",
|
|
71
|
+
"isNaN",
|
|
72
|
+
"parseFloat",
|
|
73
|
+
"parseInt",
|
|
74
|
+
]);
|
|
75
|
+
|
|
76
|
+
const workerDisallowedGlobals = new Set([
|
|
77
|
+
"__zgui",
|
|
78
|
+
"performance",
|
|
79
|
+
"setTimeout",
|
|
80
|
+
"clearTimeout",
|
|
81
|
+
"requestAnimationFrame",
|
|
82
|
+
"cancelAnimationFrame",
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
const workerGlobals = new Set([
|
|
86
|
+
...[...globals].filter(name => !workerDisallowedGlobals.has(name)),
|
|
87
|
+
"postMessage",
|
|
88
|
+
"self",
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
export type CompilationGraph = BuildInfo["graph"];
|
|
92
|
+
|
|
93
|
+
type AstNode = Record<string, unknown> & { type: string };
|
|
94
|
+
|
|
95
|
+
interface Binding {
|
|
96
|
+
name: string;
|
|
97
|
+
node: AstNode;
|
|
98
|
+
constant: boolean;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface Scope {
|
|
102
|
+
parent: Scope | null;
|
|
103
|
+
kind: "program" | "function" | "block";
|
|
104
|
+
bindings: Map<string, Binding>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface AncestorFrame {
|
|
108
|
+
node: AstNode;
|
|
109
|
+
key: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isAstNode(value: unknown): value is AstNode {
|
|
113
|
+
return value !== null && typeof value === "object" && typeof (value as AstNode).type === "string";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isStringLiteral(node: unknown): node is AstNode & { value: string } {
|
|
117
|
+
return isAstNode(node) && node.type === "Literal" && typeof node.value === "string";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function isNumberLiteral(node: unknown): node is AstNode & { value: number } {
|
|
121
|
+
return isAstNode(node) && node.type === "Literal" && typeof node.value === "number";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function isFunctionNode(node: AstNode): boolean {
|
|
125
|
+
return (
|
|
126
|
+
node.type === "FunctionDeclaration" ||
|
|
127
|
+
node.type === "FunctionExpression" ||
|
|
128
|
+
node.type === "ArrowFunctionExpression"
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function createScope(parent: Scope | null, kind: Scope["kind"]): Scope {
|
|
133
|
+
return { parent, kind, bindings: new Map() };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function nearestVarScope(scope: Scope): Scope {
|
|
137
|
+
let current: Scope = scope;
|
|
138
|
+
while (current.kind === "block" && current.parent) {
|
|
139
|
+
current = current.parent;
|
|
140
|
+
}
|
|
141
|
+
return current;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function addBinding(scope: Scope, name: string, node: AstNode): void {
|
|
145
|
+
const existing = scope.bindings.get(name);
|
|
146
|
+
if (existing) {
|
|
147
|
+
if (existing.node === node) return;
|
|
148
|
+
existing.constant = false;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
scope.bindings.set(name, { name, node, constant: true });
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function resolveBinding(scope: Scope, name: string): Binding | undefined {
|
|
155
|
+
for (let current: Scope | null = scope; current; current = current.parent) {
|
|
156
|
+
const found = current.bindings.get(name);
|
|
157
|
+
if (found) return found;
|
|
158
|
+
}
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function hasBinding(scope: Scope, name: string): boolean {
|
|
163
|
+
return resolveBinding(scope, name) !== undefined;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function collectPatternNames(pattern: unknown, names: string[]): void {
|
|
167
|
+
if (!isAstNode(pattern)) return;
|
|
168
|
+
switch (pattern.type) {
|
|
169
|
+
case "Identifier":
|
|
170
|
+
names.push(pattern.name as string);
|
|
171
|
+
break;
|
|
172
|
+
case "RestElement":
|
|
173
|
+
collectPatternNames(pattern.argument, names);
|
|
174
|
+
break;
|
|
175
|
+
case "AssignmentPattern":
|
|
176
|
+
collectPatternNames(pattern.left, names);
|
|
177
|
+
break;
|
|
178
|
+
case "ArrayPattern":
|
|
179
|
+
for (const element of (pattern.elements as unknown[]) ?? []) {
|
|
180
|
+
collectPatternNames(element, names);
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
case "ObjectPattern":
|
|
184
|
+
for (const prop of (pattern.properties as unknown[]) ?? []) {
|
|
185
|
+
if (!isAstNode(prop)) continue;
|
|
186
|
+
if (prop.type === "RestElement") collectPatternNames(prop.argument, names);
|
|
187
|
+
else if (prop.type === "Property") collectPatternNames(prop.value, names);
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
case "TSParameterProperty":
|
|
191
|
+
collectPatternNames(pattern.parameter, names);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function bindPattern(scope: Scope, pattern: unknown, node: AstNode): void {
|
|
197
|
+
const names: string[] = [];
|
|
198
|
+
collectPatternNames(pattern, names);
|
|
199
|
+
for (const name of names) {
|
|
200
|
+
addBinding(scope, name, node);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function collectVarDeclarations(node: unknown, varScope: Scope): void {
|
|
205
|
+
if (!isAstNode(node)) return;
|
|
206
|
+
if (isFunctionNode(node) || node.type === "StaticBlock") return;
|
|
207
|
+
if (node.type === "VariableDeclaration") {
|
|
208
|
+
if (node.declare === true) return;
|
|
209
|
+
if (node.kind === "var") {
|
|
210
|
+
for (const decl of (node.declarations as unknown[]) ?? []) {
|
|
211
|
+
if (isAstNode(decl)) bindPattern(varScope, decl.id, decl);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
switch (node.type) {
|
|
217
|
+
case "BlockStatement":
|
|
218
|
+
case "Program":
|
|
219
|
+
for (const stmt of (node.body as unknown[]) ?? []) collectVarDeclarations(stmt, varScope);
|
|
220
|
+
break;
|
|
221
|
+
case "ExportNamedDeclaration":
|
|
222
|
+
case "ExportDefaultDeclaration":
|
|
223
|
+
collectVarDeclarations(node.declaration, varScope);
|
|
224
|
+
break;
|
|
225
|
+
case "IfStatement":
|
|
226
|
+
collectVarDeclarations(node.consequent, varScope);
|
|
227
|
+
collectVarDeclarations(node.alternate, varScope);
|
|
228
|
+
break;
|
|
229
|
+
case "WhileStatement":
|
|
230
|
+
case "DoWhileStatement":
|
|
231
|
+
case "LabeledStatement":
|
|
232
|
+
case "WithStatement":
|
|
233
|
+
collectVarDeclarations(node.body, varScope);
|
|
234
|
+
break;
|
|
235
|
+
case "ForStatement":
|
|
236
|
+
collectVarDeclarations(node.init, varScope);
|
|
237
|
+
collectVarDeclarations(node.body, varScope);
|
|
238
|
+
break;
|
|
239
|
+
case "ForInStatement":
|
|
240
|
+
case "ForOfStatement":
|
|
241
|
+
collectVarDeclarations(node.left, varScope);
|
|
242
|
+
collectVarDeclarations(node.body, varScope);
|
|
243
|
+
break;
|
|
244
|
+
case "SwitchStatement":
|
|
245
|
+
for (const item of (node.cases as unknown[]) ?? []) {
|
|
246
|
+
if (!isAstNode(item)) continue;
|
|
247
|
+
for (const stmt of (item.consequent as unknown[]) ?? []) {
|
|
248
|
+
collectVarDeclarations(stmt, varScope);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
break;
|
|
252
|
+
case "TryStatement":
|
|
253
|
+
collectVarDeclarations(node.block, varScope);
|
|
254
|
+
if (isAstNode(node.handler)) collectVarDeclarations(node.handler.body, varScope);
|
|
255
|
+
collectVarDeclarations(node.finalizer, varScope);
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function hoistStatementList(statements: unknown[], scope: Scope): void {
|
|
261
|
+
const varScope = nearestVarScope(scope);
|
|
262
|
+
for (const stmt of statements) {
|
|
263
|
+
if (!isAstNode(stmt)) continue;
|
|
264
|
+
if (stmt.declare === true) continue;
|
|
265
|
+
switch (stmt.type) {
|
|
266
|
+
case "ImportDeclaration": {
|
|
267
|
+
if (stmt.importKind === "type") break;
|
|
268
|
+
for (const spec of (stmt.specifiers as unknown[]) ?? []) {
|
|
269
|
+
if (!isAstNode(spec)) continue;
|
|
270
|
+
if (spec.importKind === "type") continue;
|
|
271
|
+
if (isAstNode(spec.local) && spec.local.type === "Identifier") {
|
|
272
|
+
addBinding(scope, spec.local.name as string, spec);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
case "VariableDeclaration": {
|
|
278
|
+
const target = stmt.kind === "var" ? varScope : scope;
|
|
279
|
+
for (const decl of (stmt.declarations as unknown[]) ?? []) {
|
|
280
|
+
if (isAstNode(decl)) bindPattern(target, decl.id, decl);
|
|
281
|
+
}
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
case "FunctionDeclaration":
|
|
285
|
+
case "ClassDeclaration":
|
|
286
|
+
case "TSEnumDeclaration":
|
|
287
|
+
case "TSModuleDeclaration":
|
|
288
|
+
if (isAstNode(stmt.id) && stmt.id.type === "Identifier") {
|
|
289
|
+
addBinding(scope, stmt.id.name as string, stmt);
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
case "ExportNamedDeclaration":
|
|
293
|
+
if (stmt.exportKind === "type") break;
|
|
294
|
+
if (isAstNode(stmt.declaration)) hoistStatementList([stmt.declaration], scope);
|
|
295
|
+
break;
|
|
296
|
+
case "ExportDefaultDeclaration":
|
|
297
|
+
if (
|
|
298
|
+
isAstNode(stmt.declaration) &&
|
|
299
|
+
(stmt.declaration.type === "FunctionDeclaration" ||
|
|
300
|
+
stmt.declaration.type === "ClassDeclaration") &&
|
|
301
|
+
isAstNode(stmt.declaration.id) &&
|
|
302
|
+
stmt.declaration.id.type === "Identifier"
|
|
303
|
+
) {
|
|
304
|
+
addBinding(scope, stmt.declaration.id.name as string, stmt.declaration);
|
|
305
|
+
}
|
|
306
|
+
break;
|
|
307
|
+
default:
|
|
308
|
+
collectVarDeclarations(stmt, varScope);
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function executesAtTopLevel(ancestors: readonly AncestorFrame[]): boolean {
|
|
315
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
316
|
+
const frame = ancestors[i]!;
|
|
317
|
+
// Method computed keys execute outside the method, unlike its body/parameters.
|
|
318
|
+
if (isFunctionNode(frame.node) && (frame.key === "body" || frame.key === "params")) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function hasArgumentsBinding(ancestors: readonly AncestorFrame[]): boolean {
|
|
326
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
327
|
+
const frame = ancestors[i]!;
|
|
328
|
+
if (
|
|
329
|
+
(frame.node.type === "FunctionDeclaration" || frame.node.type === "FunctionExpression") &&
|
|
330
|
+
(frame.key === "body" || frame.key === "params")
|
|
331
|
+
) {
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function hasTypeofCheck(expr: unknown, name: string, expectedUndefined: boolean): boolean {
|
|
339
|
+
if (!isAstNode(expr)) return false;
|
|
340
|
+
if (expr.type === "ParenthesizedExpression") {
|
|
341
|
+
return hasTypeofCheck(expr.expression, name, expectedUndefined);
|
|
342
|
+
}
|
|
343
|
+
if (expr.type === "LogicalExpression" && expr.operator === "&&" && !expectedUndefined) {
|
|
344
|
+
return hasTypeofCheck(expr.left, name, false) || hasTypeofCheck(expr.right, name, false);
|
|
345
|
+
}
|
|
346
|
+
if (expr.type === "LogicalExpression" && expr.operator === "||" && expectedUndefined) {
|
|
347
|
+
return hasTypeofCheck(expr.left, name, true) || hasTypeofCheck(expr.right, name, true);
|
|
348
|
+
}
|
|
349
|
+
if (expr.type !== "BinaryExpression") return false;
|
|
350
|
+
const op = expr.operator;
|
|
351
|
+
const matches = expectedUndefined ? op === "===" || op === "==" : op === "!==" || op === "!=";
|
|
352
|
+
if (!matches) return false;
|
|
353
|
+
const left = expr.left;
|
|
354
|
+
const right = expr.right;
|
|
355
|
+
return (
|
|
356
|
+
(isAstNode(left) &&
|
|
357
|
+
left.type === "UnaryExpression" &&
|
|
358
|
+
left.operator === "typeof" &&
|
|
359
|
+
isAstNode(left.argument) &&
|
|
360
|
+
left.argument.type === "Identifier" &&
|
|
361
|
+
left.argument.name === name &&
|
|
362
|
+
isStringLiteral(right) &&
|
|
363
|
+
right.value === "undefined") ||
|
|
364
|
+
(isAstNode(right) &&
|
|
365
|
+
right.type === "UnaryExpression" &&
|
|
366
|
+
right.operator === "typeof" &&
|
|
367
|
+
isAstNode(right.argument) &&
|
|
368
|
+
right.argument.type === "Identifier" &&
|
|
369
|
+
right.argument.name === name &&
|
|
370
|
+
isStringLiteral(left) &&
|
|
371
|
+
left.value === "undefined")
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function isTypeofGuarded(name: string, ancestors: readonly AncestorFrame[]): boolean {
|
|
376
|
+
const parent = ancestors.at(-1);
|
|
377
|
+
if (
|
|
378
|
+
parent &&
|
|
379
|
+
parent.node.type === "UnaryExpression" &&
|
|
380
|
+
parent.node.operator === "typeof" &&
|
|
381
|
+
parent.key === "argument"
|
|
382
|
+
) {
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
386
|
+
const frame = ancestors[i]!;
|
|
387
|
+
if (frame.node.type === "LogicalExpression") {
|
|
388
|
+
if (
|
|
389
|
+
(frame.node.operator === "||" &&
|
|
390
|
+
frame.key === "right" &&
|
|
391
|
+
hasTypeofCheck(frame.node.left, name, true)) ||
|
|
392
|
+
(frame.node.operator === "&&" &&
|
|
393
|
+
frame.key === "right" &&
|
|
394
|
+
hasTypeofCheck(frame.node.left, name, false))
|
|
395
|
+
) {
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (frame.node.type === "IfStatement" || frame.node.type === "ConditionalExpression") {
|
|
400
|
+
if (frame.key === "consequent" && hasTypeofCheck(frame.node.test, name, false)) return true;
|
|
401
|
+
if (frame.key === "alternate" && hasTypeofCheck(frame.node.test, name, true)) return true;
|
|
402
|
+
}
|
|
403
|
+
if (isFunctionNode(frame.node) || frame.node.type === "Program") break;
|
|
404
|
+
}
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export function validateImport(specifier: string): void {
|
|
409
|
+
if (
|
|
410
|
+
isBuiltin(specifier) ||
|
|
411
|
+
/^(?:solid-js\/(?:web|server)|react-dom)(?:\/|$)/.test(specifier) ||
|
|
412
|
+
/(?:^|\/)server(?:\.[cm]?js|\/|$)/.test(specifier)
|
|
413
|
+
) {
|
|
414
|
+
throw new Error(`Incompatible runtime import: ${specifier}`);
|
|
415
|
+
}
|
|
416
|
+
if (/\.module\.css$/i.test(specifier)) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (/\.(?:css|png|jpe?g|svg|woff2?|ttf|gif|webp)(?:[?#]|$)/i.test(specifier)) {
|
|
420
|
+
throw new Error(`Runtime resource imports are unsupported; use app.css/assets: ${specifier}`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function validateWorkerImport(specifier: string): void {
|
|
425
|
+
if (/\.module\.css$/i.test(specifier)) {
|
|
426
|
+
throw new Error(`Compute worker cannot import CSS module: ${specifier}`);
|
|
427
|
+
}
|
|
428
|
+
validateImport(specifier);
|
|
429
|
+
if (
|
|
430
|
+
/^@hewel\/zgui(?:$|\/|-services(?:$|\/))/.test(specifier) ||
|
|
431
|
+
/^solid-js(?:$|\/)/.test(specifier) ||
|
|
432
|
+
/^@solidjs\//.test(specifier)
|
|
433
|
+
) {
|
|
434
|
+
throw new Error(`Compute worker cannot import UI, Solid, or host service module: ${specifier}`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function resolveChunkSpecifier(fromFilename: string, specifier: string): string | null {
|
|
439
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../")) return null;
|
|
440
|
+
const fromDir = path.posix.dirname(fromFilename);
|
|
441
|
+
const resolved = path.posix.normalize(
|
|
442
|
+
fromDir === "." ? specifier : path.posix.join(fromDir, specifier),
|
|
443
|
+
);
|
|
444
|
+
if (resolved.startsWith("..") || resolved.startsWith("/")) return null;
|
|
445
|
+
return resolved;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function inferLang(filename: string, bundled: boolean): NonNullable<ParserOptions["lang"]> {
|
|
449
|
+
if (bundled) return "js";
|
|
450
|
+
if (/\.[cm]?tsx$/i.test(filename)) return "tsx";
|
|
451
|
+
if (/\.[cm]?ts$/i.test(filename)) return "ts";
|
|
452
|
+
if (/\.[cm]?jsx$/i.test(filename)) return "jsx";
|
|
453
|
+
return "js";
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function unwrapTsExpressions(node: unknown): AstNode | null {
|
|
457
|
+
let current = node;
|
|
458
|
+
while (isAstNode(current)) {
|
|
459
|
+
if (
|
|
460
|
+
current.type === "TSAsExpression" ||
|
|
461
|
+
current.type === "TSSatisfiesExpression" ||
|
|
462
|
+
current.type === "TSTypeAssertion" ||
|
|
463
|
+
current.type === "TSNonNullExpression" ||
|
|
464
|
+
current.type === "ParenthesizedExpression"
|
|
465
|
+
) {
|
|
466
|
+
current = current.expression;
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
return current;
|
|
470
|
+
}
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
export function validateModule(
|
|
475
|
+
code: string,
|
|
476
|
+
{
|
|
477
|
+
filename = "app.mjs",
|
|
478
|
+
bundled = false,
|
|
479
|
+
entry = false,
|
|
480
|
+
worker = false,
|
|
481
|
+
allowedChunks,
|
|
482
|
+
lang,
|
|
483
|
+
}: {
|
|
484
|
+
filename?: string;
|
|
485
|
+
bundled?: boolean;
|
|
486
|
+
entry?: boolean;
|
|
487
|
+
worker?: boolean;
|
|
488
|
+
allowedChunks?: ReadonlySet<string>;
|
|
489
|
+
lang?: ParserOptions["lang"];
|
|
490
|
+
} = {},
|
|
491
|
+
): ParseResult {
|
|
492
|
+
const allowedGlobals = worker ? workerGlobals : globals;
|
|
493
|
+
const parsed = parseSync(filename, code, {
|
|
494
|
+
sourceType: "module",
|
|
495
|
+
lang: lang ?? inferLang(filename, bundled),
|
|
496
|
+
});
|
|
497
|
+
if (parsed.errors.length > 0) {
|
|
498
|
+
throw new SyntaxError(
|
|
499
|
+
`Failed to parse ${filename}: ${parsed.errors.map(error => error.message).join("\n")}`,
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const programNode = parsed.program as unknown as AstNode;
|
|
504
|
+
const programScope = createScope(null, "program");
|
|
505
|
+
const exported = new Map<string, string>();
|
|
506
|
+
const ancestors: AncestorFrame[] = [];
|
|
507
|
+
|
|
508
|
+
const assertClosedChunkEdge = (specifier: string, errorMessage: string): void => {
|
|
509
|
+
const resolved = resolveChunkSpecifier(filename, specifier);
|
|
510
|
+
if (!resolved || !allowedChunks?.has(resolved)) {
|
|
511
|
+
throw new Error(errorMessage);
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
function checkIdentifierReference(name: string, scope: Scope): void {
|
|
516
|
+
if (isTypeofGuarded(name, ancestors)) return;
|
|
517
|
+
if (
|
|
518
|
+
bundled &&
|
|
519
|
+
!hasBinding(scope, name) &&
|
|
520
|
+
!allowedGlobals.has(name) &&
|
|
521
|
+
!(name === "arguments" && hasArgumentsBinding(ancestors))
|
|
522
|
+
) {
|
|
523
|
+
throw new Error(`Unsupported runtime global: ${name}`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function markWrite(name: string, scope: Scope): void {
|
|
528
|
+
const binding = resolveBinding(scope, name);
|
|
529
|
+
if (binding) binding.constant = false;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function pushAndWalk(parent: AstNode, key: string, child: unknown, scope: Scope): void {
|
|
533
|
+
if (!isAstNode(child)) return;
|
|
534
|
+
ancestors.push({ node: parent, key });
|
|
535
|
+
try {
|
|
536
|
+
walkNode(child, scope);
|
|
537
|
+
} finally {
|
|
538
|
+
ancestors.pop();
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function walkPattern(
|
|
543
|
+
parent: AstNode,
|
|
544
|
+
key: string,
|
|
545
|
+
pattern: unknown,
|
|
546
|
+
scope: Scope,
|
|
547
|
+
isAssignmentTarget: boolean,
|
|
548
|
+
): void {
|
|
549
|
+
if (!isAstNode(pattern)) return;
|
|
550
|
+
ancestors.push({ node: parent, key });
|
|
551
|
+
try {
|
|
552
|
+
switch (pattern.type) {
|
|
553
|
+
case "Identifier":
|
|
554
|
+
if (isAssignmentTarget) {
|
|
555
|
+
const name = pattern.name as string;
|
|
556
|
+
markWrite(name, scope);
|
|
557
|
+
checkIdentifierReference(name, scope);
|
|
558
|
+
}
|
|
559
|
+
break;
|
|
560
|
+
case "MemberExpression":
|
|
561
|
+
// Assignment target like `obj.x = 1` or `obj[k] = 1` does not reassign `obj`.
|
|
562
|
+
walkNode(pattern, scope);
|
|
563
|
+
break;
|
|
564
|
+
case "RestElement":
|
|
565
|
+
walkPattern(pattern, "argument", pattern.argument, scope, isAssignmentTarget);
|
|
566
|
+
break;
|
|
567
|
+
case "AssignmentPattern":
|
|
568
|
+
walkPattern(pattern, "left", pattern.left, scope, isAssignmentTarget);
|
|
569
|
+
pushAndWalk(pattern, "right", pattern.right, scope);
|
|
570
|
+
break;
|
|
571
|
+
case "ArrayPattern":
|
|
572
|
+
for (const element of (pattern.elements as unknown[]) ?? []) {
|
|
573
|
+
walkPattern(pattern, "elements", element, scope, isAssignmentTarget);
|
|
574
|
+
}
|
|
575
|
+
break;
|
|
576
|
+
case "ObjectPattern":
|
|
577
|
+
for (const prop of (pattern.properties as unknown[]) ?? []) {
|
|
578
|
+
if (!isAstNode(prop)) continue;
|
|
579
|
+
if (prop.type === "RestElement") {
|
|
580
|
+
walkPattern(prop, "argument", prop.argument, scope, isAssignmentTarget);
|
|
581
|
+
} else if (prop.type === "Property") {
|
|
582
|
+
if (prop.computed === true) pushAndWalk(prop, "key", prop.key, scope);
|
|
583
|
+
walkPattern(prop, "value", prop.value, scope, isAssignmentTarget);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
break;
|
|
587
|
+
case "TSParameterProperty":
|
|
588
|
+
walkPattern(pattern, "parameter", pattern.parameter, scope, isAssignmentTarget);
|
|
589
|
+
break;
|
|
590
|
+
case "TSAsExpression":
|
|
591
|
+
case "TSSatisfiesExpression":
|
|
592
|
+
case "TSTypeAssertion":
|
|
593
|
+
case "TSNonNullExpression":
|
|
594
|
+
case "ParenthesizedExpression":
|
|
595
|
+
walkPattern(pattern, "expression", pattern.expression, scope, isAssignmentTarget);
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
} finally {
|
|
599
|
+
ancestors.pop();
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function walkFunction(node: AstNode, outerScope: Scope): void {
|
|
604
|
+
const fnScope = createScope(outerScope, "function");
|
|
605
|
+
if (node.type === "FunctionExpression" && isAstNode(node.id) && node.id.type === "Identifier") {
|
|
606
|
+
addBinding(fnScope, node.id.name as string, node);
|
|
607
|
+
}
|
|
608
|
+
const params = (node.params as unknown[]) ?? [];
|
|
609
|
+
for (const param of params) {
|
|
610
|
+
if (isAstNode(param)) bindPattern(fnScope, param, param);
|
|
611
|
+
}
|
|
612
|
+
for (const param of params) {
|
|
613
|
+
walkPattern(node, "params", param, fnScope, false);
|
|
614
|
+
}
|
|
615
|
+
if (isAstNode(node.body)) {
|
|
616
|
+
if (node.body.type === "BlockStatement") {
|
|
617
|
+
const bodyStatements = (node.body.body as unknown[]) ?? [];
|
|
618
|
+
const bodyScope = createScope(fnScope, "block");
|
|
619
|
+
hoistStatementList(bodyStatements, bodyScope);
|
|
620
|
+
ancestors.push({ node, key: "body" });
|
|
621
|
+
try {
|
|
622
|
+
for (const stmt of bodyStatements) {
|
|
623
|
+
pushAndWalk(node.body, "body", stmt, bodyScope);
|
|
624
|
+
}
|
|
625
|
+
} finally {
|
|
626
|
+
ancestors.pop();
|
|
627
|
+
}
|
|
628
|
+
} else {
|
|
629
|
+
pushAndWalk(node, "body", node.body, fnScope);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function walkClass(node: AstNode, outerScope: Scope): void {
|
|
635
|
+
if (Array.isArray(node.decorators)) {
|
|
636
|
+
for (const dec of node.decorators) pushAndWalk(node, "decorators", dec, outerScope);
|
|
637
|
+
}
|
|
638
|
+
pushAndWalk(node, "superClass", node.superClass, outerScope);
|
|
639
|
+
const classScope =
|
|
640
|
+
node.type === "ClassExpression" && isAstNode(node.id) && node.id.type === "Identifier"
|
|
641
|
+
? createScope(outerScope, "block")
|
|
642
|
+
: outerScope;
|
|
643
|
+
if (classScope !== outerScope && isAstNode(node.id) && node.id.type === "Identifier") {
|
|
644
|
+
addBinding(classScope, node.id.name as string, node);
|
|
645
|
+
}
|
|
646
|
+
if (!isAstNode(node.body)) return;
|
|
647
|
+
ancestors.push({ node, key: "body" });
|
|
648
|
+
try {
|
|
649
|
+
for (const member of (node.body.body as unknown[]) ?? []) {
|
|
650
|
+
if (!isAstNode(member)) continue;
|
|
651
|
+
ancestors.push({ node: node.body, key: "body" });
|
|
652
|
+
try {
|
|
653
|
+
if (Array.isArray(member.decorators)) {
|
|
654
|
+
for (const dec of member.decorators) pushAndWalk(member, "decorators", dec, classScope);
|
|
655
|
+
}
|
|
656
|
+
if (
|
|
657
|
+
member.type === "MethodDefinition" ||
|
|
658
|
+
member.type === "PropertyDefinition" ||
|
|
659
|
+
member.type === "AccessorProperty"
|
|
660
|
+
) {
|
|
661
|
+
if (member.computed === true) pushAndWalk(member, "key", member.key, classScope);
|
|
662
|
+
pushAndWalk(member, "value", member.value, classScope);
|
|
663
|
+
} else if (member.type === "StaticBlock") {
|
|
664
|
+
const staticScope = createScope(classScope, "function");
|
|
665
|
+
const stmts = (member.body as unknown[]) ?? [];
|
|
666
|
+
hoistStatementList(stmts, staticScope);
|
|
667
|
+
for (const stmt of stmts) pushAndWalk(member, "body", stmt, staticScope);
|
|
668
|
+
}
|
|
669
|
+
} finally {
|
|
670
|
+
ancestors.pop();
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
} finally {
|
|
674
|
+
ancestors.pop();
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
function walkNode(node: AstNode, scope: Scope): void {
|
|
679
|
+
if (node.declare === true) return;
|
|
680
|
+
switch (node.type) {
|
|
681
|
+
case "Program": {
|
|
682
|
+
const body = (node.body as unknown[]) ?? [];
|
|
683
|
+
hoistStatementList(body, scope);
|
|
684
|
+
for (const stmt of body) pushAndWalk(node, "body", stmt, scope);
|
|
685
|
+
break;
|
|
686
|
+
}
|
|
687
|
+
case "BlockStatement": {
|
|
688
|
+
const blockScope = createScope(scope, "block");
|
|
689
|
+
const body = (node.body as unknown[]) ?? [];
|
|
690
|
+
hoistStatementList(body, blockScope);
|
|
691
|
+
for (const stmt of body) pushAndWalk(node, "body", stmt, blockScope);
|
|
692
|
+
break;
|
|
693
|
+
}
|
|
694
|
+
case "ImportDeclaration": {
|
|
695
|
+
if (node.importKind === "type") break;
|
|
696
|
+
if (isStringLiteral(node.source)) {
|
|
697
|
+
validateImport(node.source.value);
|
|
698
|
+
if (bundled) {
|
|
699
|
+
assertClosedChunkEdge(
|
|
700
|
+
node.source.value,
|
|
701
|
+
"Bundle contains an unresolved runtime import",
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
break;
|
|
706
|
+
}
|
|
707
|
+
case "ExportAllDeclaration": {
|
|
708
|
+
if (node.exportKind === "type") break;
|
|
709
|
+
if (isStringLiteral(node.source)) {
|
|
710
|
+
validateImport(node.source.value);
|
|
711
|
+
if (bundled) {
|
|
712
|
+
assertClosedChunkEdge(node.source.value, "Bundle contains an external re-export");
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
break;
|
|
716
|
+
}
|
|
717
|
+
case "ExportNamedDeclaration": {
|
|
718
|
+
if (node.exportKind === "type") break;
|
|
719
|
+
const specifiers = (node.specifiers as unknown[]) ?? [];
|
|
720
|
+
const allTypeSpecifiers =
|
|
721
|
+
specifiers.length > 0 &&
|
|
722
|
+
specifiers.every(spec => isAstNode(spec) && spec.exportKind === "type");
|
|
723
|
+
if (isStringLiteral(node.source)) {
|
|
724
|
+
if (allTypeSpecifiers) break;
|
|
725
|
+
validateImport(node.source.value);
|
|
726
|
+
if (bundled) {
|
|
727
|
+
assertClosedChunkEdge(node.source.value, "Bundle contains an external re-export");
|
|
728
|
+
}
|
|
729
|
+
} else {
|
|
730
|
+
for (const spec of specifiers) {
|
|
731
|
+
if (!isAstNode(spec) || spec.type !== "ExportSpecifier") continue;
|
|
732
|
+
if (spec.exportKind === "type") continue;
|
|
733
|
+
if (isAstNode(spec.local) && spec.local.type === "Identifier") {
|
|
734
|
+
const exportedName =
|
|
735
|
+
isAstNode(spec.exported) && spec.exported.type === "Identifier"
|
|
736
|
+
? (spec.exported.name as string)
|
|
737
|
+
: isStringLiteral(spec.exported)
|
|
738
|
+
? spec.exported.value
|
|
739
|
+
: "";
|
|
740
|
+
if (exportedName) exported.set(exportedName, spec.local.name as string);
|
|
741
|
+
pushAndWalk(spec, "local", spec.local, scope);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
if (isAstNode(node.declaration)) {
|
|
746
|
+
const decl = node.declaration;
|
|
747
|
+
if (decl.type === "VariableDeclaration") {
|
|
748
|
+
for (const item of (decl.declarations as unknown[]) ?? []) {
|
|
749
|
+
if (isAstNode(item) && isAstNode(item.id) && item.id.type === "Identifier") {
|
|
750
|
+
exported.set(item.id.name as string, item.id.name as string);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
} else if (isAstNode(decl.id) && decl.id.type === "Identifier") {
|
|
754
|
+
exported.set(decl.id.name as string, decl.id.name as string);
|
|
755
|
+
}
|
|
756
|
+
pushAndWalk(node, "declaration", decl, scope);
|
|
757
|
+
}
|
|
758
|
+
break;
|
|
759
|
+
}
|
|
760
|
+
case "ExportDefaultDeclaration":
|
|
761
|
+
pushAndWalk(node, "declaration", node.declaration, scope);
|
|
762
|
+
break;
|
|
763
|
+
case "ImportExpression": {
|
|
764
|
+
if (worker) {
|
|
765
|
+
throw new Error("Dynamic imports are unsupported in compute workers");
|
|
766
|
+
}
|
|
767
|
+
if (!isStringLiteral(node.source)) {
|
|
768
|
+
throw new Error("Dynamic imports must use a static string literal specifier");
|
|
769
|
+
}
|
|
770
|
+
validateImport(node.source.value);
|
|
771
|
+
if (bundled) {
|
|
772
|
+
assertClosedChunkEdge(node.source.value, "Bundle contains an unresolved dynamic import");
|
|
773
|
+
}
|
|
774
|
+
pushAndWalk(node, "options", node.options, scope);
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
case "AwaitExpression":
|
|
778
|
+
if (executesAtTopLevel(ancestors)) throw new Error("Top-level await is unsupported");
|
|
779
|
+
pushAndWalk(node, "argument", node.argument, scope);
|
|
780
|
+
break;
|
|
781
|
+
case "ForOfStatement": {
|
|
782
|
+
if (node.await === true && executesAtTopLevel(ancestors)) {
|
|
783
|
+
throw new Error("Top-level await is unsupported");
|
|
784
|
+
}
|
|
785
|
+
const forScope = createScope(scope, "block");
|
|
786
|
+
if (isAstNode(node.left) && node.left.type === "VariableDeclaration") {
|
|
787
|
+
hoistStatementList([node.left], forScope);
|
|
788
|
+
pushAndWalk(node, "left", node.left, forScope);
|
|
789
|
+
} else {
|
|
790
|
+
walkPattern(node, "left", node.left, forScope, true);
|
|
791
|
+
}
|
|
792
|
+
pushAndWalk(node, "right", node.right, forScope);
|
|
793
|
+
pushAndWalk(node, "body", node.body, forScope);
|
|
794
|
+
break;
|
|
795
|
+
}
|
|
796
|
+
case "ForInStatement": {
|
|
797
|
+
const forScope = createScope(scope, "block");
|
|
798
|
+
if (isAstNode(node.left) && node.left.type === "VariableDeclaration") {
|
|
799
|
+
hoistStatementList([node.left], forScope);
|
|
800
|
+
pushAndWalk(node, "left", node.left, forScope);
|
|
801
|
+
} else {
|
|
802
|
+
walkPattern(node, "left", node.left, forScope, true);
|
|
803
|
+
}
|
|
804
|
+
pushAndWalk(node, "right", node.right, forScope);
|
|
805
|
+
pushAndWalk(node, "body", node.body, forScope);
|
|
806
|
+
break;
|
|
807
|
+
}
|
|
808
|
+
case "ForStatement": {
|
|
809
|
+
const forScope = createScope(scope, "block");
|
|
810
|
+
if (isAstNode(node.init) && node.init.type === "VariableDeclaration") {
|
|
811
|
+
hoistStatementList([node.init], forScope);
|
|
812
|
+
}
|
|
813
|
+
pushAndWalk(node, "init", node.init, forScope);
|
|
814
|
+
pushAndWalk(node, "test", node.test, forScope);
|
|
815
|
+
pushAndWalk(node, "update", node.update, forScope);
|
|
816
|
+
pushAndWalk(node, "body", node.body, forScope);
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
819
|
+
case "CallExpression": {
|
|
820
|
+
if (
|
|
821
|
+
isAstNode(node.callee) &&
|
|
822
|
+
node.callee.type === "Identifier" &&
|
|
823
|
+
node.callee.name === "require" &&
|
|
824
|
+
!hasBinding(scope, "require")
|
|
825
|
+
) {
|
|
826
|
+
throw new Error("Runtime require is unsupported");
|
|
827
|
+
}
|
|
828
|
+
pushAndWalk(node, "callee", node.callee, scope);
|
|
829
|
+
for (const arg of (node.arguments as unknown[]) ?? []) {
|
|
830
|
+
pushAndWalk(node, "arguments", arg, scope);
|
|
831
|
+
}
|
|
832
|
+
break;
|
|
833
|
+
}
|
|
834
|
+
case "NewExpression":
|
|
835
|
+
pushAndWalk(node, "callee", node.callee, scope);
|
|
836
|
+
for (const arg of (node.arguments as unknown[]) ?? []) {
|
|
837
|
+
pushAndWalk(node, "arguments", arg, scope);
|
|
838
|
+
}
|
|
839
|
+
break;
|
|
840
|
+
case "Identifier":
|
|
841
|
+
checkIdentifierReference(node.name as string, scope);
|
|
842
|
+
break;
|
|
843
|
+
case "VariableDeclaration":
|
|
844
|
+
for (const decl of (node.declarations as unknown[]) ?? []) {
|
|
845
|
+
pushAndWalk(node, "declarations", decl, scope);
|
|
846
|
+
}
|
|
847
|
+
break;
|
|
848
|
+
case "VariableDeclarator":
|
|
849
|
+
walkPattern(node, "id", node.id, scope, false);
|
|
850
|
+
pushAndWalk(node, "init", node.init, scope);
|
|
851
|
+
break;
|
|
852
|
+
case "FunctionDeclaration":
|
|
853
|
+
case "FunctionExpression":
|
|
854
|
+
case "ArrowFunctionExpression":
|
|
855
|
+
walkFunction(node, scope);
|
|
856
|
+
break;
|
|
857
|
+
case "ClassDeclaration":
|
|
858
|
+
case "ClassExpression":
|
|
859
|
+
walkClass(node, scope);
|
|
860
|
+
break;
|
|
861
|
+
case "AssignmentExpression":
|
|
862
|
+
walkPattern(node, "left", node.left, scope, true);
|
|
863
|
+
pushAndWalk(node, "right", node.right, scope);
|
|
864
|
+
break;
|
|
865
|
+
case "UpdateExpression":
|
|
866
|
+
if (isAstNode(node.argument) && node.argument.type === "Identifier") {
|
|
867
|
+
markWrite(node.argument.name as string, scope);
|
|
868
|
+
}
|
|
869
|
+
pushAndWalk(node, "argument", node.argument, scope);
|
|
870
|
+
break;
|
|
871
|
+
case "MemberExpression":
|
|
872
|
+
pushAndWalk(node, "object", node.object, scope);
|
|
873
|
+
if (node.computed === true) pushAndWalk(node, "property", node.property, scope);
|
|
874
|
+
break;
|
|
875
|
+
case "ChainExpression":
|
|
876
|
+
case "ExpressionStatement":
|
|
877
|
+
case "ParenthesizedExpression":
|
|
878
|
+
case "TSAsExpression":
|
|
879
|
+
case "TSSatisfiesExpression":
|
|
880
|
+
case "TSTypeAssertion":
|
|
881
|
+
case "TSNonNullExpression":
|
|
882
|
+
case "TSInstantiationExpression":
|
|
883
|
+
case "TSExportAssignment":
|
|
884
|
+
pushAndWalk(node, "expression", node.expression, scope);
|
|
885
|
+
break;
|
|
886
|
+
case "UnaryExpression":
|
|
887
|
+
case "ReturnStatement":
|
|
888
|
+
case "ThrowStatement":
|
|
889
|
+
case "SpreadElement":
|
|
890
|
+
case "YieldExpression":
|
|
891
|
+
pushAndWalk(node, "argument", node.argument, scope);
|
|
892
|
+
break;
|
|
893
|
+
case "BinaryExpression":
|
|
894
|
+
case "LogicalExpression":
|
|
895
|
+
pushAndWalk(node, "left", node.left, scope);
|
|
896
|
+
pushAndWalk(node, "right", node.right, scope);
|
|
897
|
+
break;
|
|
898
|
+
case "ConditionalExpression":
|
|
899
|
+
case "IfStatement":
|
|
900
|
+
pushAndWalk(node, "test", node.test, scope);
|
|
901
|
+
pushAndWalk(node, "consequent", node.consequent, scope);
|
|
902
|
+
pushAndWalk(node, "alternate", node.alternate, scope);
|
|
903
|
+
break;
|
|
904
|
+
case "WhileStatement":
|
|
905
|
+
case "DoWhileStatement":
|
|
906
|
+
pushAndWalk(node, "test", node.test, scope);
|
|
907
|
+
pushAndWalk(node, "body", node.body, scope);
|
|
908
|
+
break;
|
|
909
|
+
case "LabeledStatement":
|
|
910
|
+
pushAndWalk(node, "body", node.body, scope);
|
|
911
|
+
break;
|
|
912
|
+
case "SwitchStatement": {
|
|
913
|
+
pushAndWalk(node, "discriminant", node.discriminant, scope);
|
|
914
|
+
const switchScope = createScope(scope, "block");
|
|
915
|
+
const cases = (node.cases as unknown[]) ?? [];
|
|
916
|
+
for (const item of cases) {
|
|
917
|
+
if (isAstNode(item))
|
|
918
|
+
hoistStatementList((item.consequent as unknown[]) ?? [], switchScope);
|
|
919
|
+
}
|
|
920
|
+
for (const item of cases) {
|
|
921
|
+
if (!isAstNode(item)) continue;
|
|
922
|
+
pushAndWalk(item, "test", item.test, switchScope);
|
|
923
|
+
for (const stmt of (item.consequent as unknown[]) ?? []) {
|
|
924
|
+
pushAndWalk(item, "consequent", stmt, switchScope);
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
break;
|
|
928
|
+
}
|
|
929
|
+
case "TryStatement":
|
|
930
|
+
pushAndWalk(node, "block", node.block, scope);
|
|
931
|
+
if (isAstNode(node.handler)) {
|
|
932
|
+
const catchScope = createScope(scope, "block");
|
|
933
|
+
if (isAstNode(node.handler.param)) {
|
|
934
|
+
bindPattern(catchScope, node.handler.param, node.handler.param);
|
|
935
|
+
walkPattern(node.handler, "param", node.handler.param, catchScope, false);
|
|
936
|
+
}
|
|
937
|
+
pushAndWalk(node.handler, "body", node.handler.body, catchScope);
|
|
938
|
+
}
|
|
939
|
+
pushAndWalk(node, "finalizer", node.finalizer, scope);
|
|
940
|
+
break;
|
|
941
|
+
case "ArrayExpression":
|
|
942
|
+
for (const element of (node.elements as unknown[]) ?? []) {
|
|
943
|
+
pushAndWalk(node, "elements", element, scope);
|
|
944
|
+
}
|
|
945
|
+
break;
|
|
946
|
+
case "ObjectExpression":
|
|
947
|
+
for (const prop of (node.properties as unknown[]) ?? []) {
|
|
948
|
+
if (!isAstNode(prop)) continue;
|
|
949
|
+
if (prop.type === "SpreadElement") {
|
|
950
|
+
pushAndWalk(prop, "argument", prop.argument, scope);
|
|
951
|
+
} else if (prop.type === "Property") {
|
|
952
|
+
if (prop.computed === true) pushAndWalk(prop, "key", prop.key, scope);
|
|
953
|
+
pushAndWalk(prop, "value", prop.value, scope);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
break;
|
|
957
|
+
case "SequenceExpression":
|
|
958
|
+
case "TemplateLiteral":
|
|
959
|
+
for (const expr of (node.expressions as unknown[]) ?? []) {
|
|
960
|
+
pushAndWalk(node, "expressions", expr, scope);
|
|
961
|
+
}
|
|
962
|
+
break;
|
|
963
|
+
case "TaggedTemplateExpression":
|
|
964
|
+
pushAndWalk(node, "tag", node.tag, scope);
|
|
965
|
+
pushAndWalk(node, "quasi", node.quasi, scope);
|
|
966
|
+
break;
|
|
967
|
+
case "Decorator":
|
|
968
|
+
pushAndWalk(node, "expression", node.expression, scope);
|
|
969
|
+
break;
|
|
970
|
+
case "TSEnumDeclaration":
|
|
971
|
+
for (const member of (node.members as unknown[]) ?? []) {
|
|
972
|
+
if (isAstNode(member)) pushAndWalk(member, "initializer", member.initializer, scope);
|
|
973
|
+
}
|
|
974
|
+
break;
|
|
975
|
+
case "TSModuleDeclaration":
|
|
976
|
+
if (isAstNode(node.body)) {
|
|
977
|
+
const nsScope = createScope(scope, "block");
|
|
978
|
+
if (node.body.type === "TSModuleBlock") {
|
|
979
|
+
const body = (node.body.body as unknown[]) ?? [];
|
|
980
|
+
hoistStatementList(body, nsScope);
|
|
981
|
+
for (const stmt of body) pushAndWalk(node.body, "body", stmt, nsScope);
|
|
982
|
+
} else {
|
|
983
|
+
pushAndWalk(node, "body", node.body, nsScope);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
break;
|
|
987
|
+
case "TSImportEqualsDeclaration":
|
|
988
|
+
if (
|
|
989
|
+
node.importKind !== "type" &&
|
|
990
|
+
isAstNode(node.moduleReference) &&
|
|
991
|
+
node.moduleReference.type === "TSExternalModuleReference" &&
|
|
992
|
+
isStringLiteral(node.moduleReference.expression)
|
|
993
|
+
) {
|
|
994
|
+
validateImport(node.moduleReference.expression.value);
|
|
995
|
+
}
|
|
996
|
+
break;
|
|
997
|
+
case "Literal":
|
|
998
|
+
case "ThisExpression":
|
|
999
|
+
case "Super":
|
|
1000
|
+
case "MetaProperty":
|
|
1001
|
+
case "PrivateIdentifier":
|
|
1002
|
+
case "EmptyStatement":
|
|
1003
|
+
case "DebuggerStatement":
|
|
1004
|
+
case "BreakStatement":
|
|
1005
|
+
case "ContinueStatement":
|
|
1006
|
+
break;
|
|
1007
|
+
default:
|
|
1008
|
+
// Type-only TS* declarations and annotations have no runtime evaluation.
|
|
1009
|
+
if (node.type.startsWith("TS")) break;
|
|
1010
|
+
break;
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
walkNode(programNode, programScope);
|
|
1015
|
+
|
|
1016
|
+
if (entry) {
|
|
1017
|
+
const version = programScope.bindings.get(exported.get("hostApiVersion") ?? "");
|
|
1018
|
+
const versionInit =
|
|
1019
|
+
version?.node.type === "VariableDeclarator" ? unwrapTsExpressions(version.node.init) : null;
|
|
1020
|
+
if (!version?.constant || !isNumberLiteral(versionInit) || versionInit.value !== 1) {
|
|
1021
|
+
throw new Error("Entry must export constant hostApiVersion = 1");
|
|
1022
|
+
}
|
|
1023
|
+
const mount = programScope.bindings.get(exported.get("mount") ?? "");
|
|
1024
|
+
const mountNode =
|
|
1025
|
+
mount?.node.type === "VariableDeclarator"
|
|
1026
|
+
? unwrapTsExpressions(mount.node.init)
|
|
1027
|
+
: (mount?.node ?? null);
|
|
1028
|
+
if (
|
|
1029
|
+
!mount?.constant ||
|
|
1030
|
+
!mountNode ||
|
|
1031
|
+
!isFunctionNode(mountNode) ||
|
|
1032
|
+
mountNode.async === true ||
|
|
1033
|
+
mountNode.generator === true
|
|
1034
|
+
) {
|
|
1035
|
+
throw new Error("Entry must export a synchronous mount function");
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
return parsed;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
export function validateGraph(
|
|
1042
|
+
graph: CompilationGraph,
|
|
1043
|
+
{ allowMultipleChunks = true }: { allowMultipleChunks?: boolean } = {},
|
|
1044
|
+
): string {
|
|
1045
|
+
const runtimes = graph.modules.filter(input =>
|
|
1046
|
+
/\/solid-js\/dist\/(?:solid(?:\.dev|\.observe)?|dev)\.js$/.test(input),
|
|
1047
|
+
);
|
|
1048
|
+
const signals = graph.modules.filter(input =>
|
|
1049
|
+
/\/@solidjs\/signals\/dist\/(?:(?:prod|observe)\/index|dev)\.js$/.test(input),
|
|
1050
|
+
);
|
|
1051
|
+
if (runtimes.length !== 1 && signals.length !== 1) {
|
|
1052
|
+
throw new Error(
|
|
1053
|
+
`Expected one reactive Solid runtime, found solid=${runtimes.length}, signals=${signals.length}`,
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
if (runtimes.length > 1 || signals.length > 1) {
|
|
1057
|
+
throw new Error(
|
|
1058
|
+
`Expected single Solid runtime instances, found solid=${runtimes.length}, signals=${signals.length}`,
|
|
1059
|
+
);
|
|
1060
|
+
}
|
|
1061
|
+
for (const input of graph.modules) {
|
|
1062
|
+
if (/\/solid-js\/(?:web|.*server)|\/react-dom\//.test(input))
|
|
1063
|
+
throw new Error(`Incompatible module graph: ${input}`);
|
|
1064
|
+
}
|
|
1065
|
+
if (graph.chunks.length === 0) {
|
|
1066
|
+
throw new Error("Expected at least one ESM artifact");
|
|
1067
|
+
}
|
|
1068
|
+
if (
|
|
1069
|
+
!allowMultipleChunks &&
|
|
1070
|
+
(graph.chunks.length !== 1 ||
|
|
1071
|
+
graph.chunks[0].imports.length ||
|
|
1072
|
+
graph.chunks[0].dynamicImports.length)
|
|
1073
|
+
) {
|
|
1074
|
+
throw new Error("Expected one self-contained ESM artifact");
|
|
1075
|
+
}
|
|
1076
|
+
const knownChunks = new Set(graph.chunks.map(chunk => chunk.fileName));
|
|
1077
|
+
for (const chunk of graph.chunks) {
|
|
1078
|
+
for (const edge of [...chunk.imports, ...chunk.dynamicImports]) {
|
|
1079
|
+
if (!knownChunks.has(edge)) {
|
|
1080
|
+
throw new Error(`Chunk graph contains an unresolved external edge: ${edge}`);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
return runtimes[0] ?? signals[0];
|
|
1085
|
+
}
|