@tamagui/compiler-core 0.0.0-bootstrap.0 → 3.0.0-beta.1093.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/LICENSE +21 -0
- package/README.md +57 -1
- package/dist/cjs/ast.cjs +108 -0
- package/dist/cjs/contracts.cjs +61 -0
- package/dist/cjs/diagnostics.cjs +68 -0
- package/dist/cjs/evaluate.cjs +602 -0
- package/dist/cjs/graph.cjs +335 -0
- package/dist/cjs/hash.cjs +48 -0
- package/dist/cjs/index.cjs +43 -0
- package/dist/cjs/ir.cjs +32 -0
- package/dist/cjs/lower.cjs +232 -0
- package/dist/cjs/materialize.cjs +222 -0
- package/dist/cjs/normalize.cjs +628 -0
- package/dist/cjs/output.cjs +148 -0
- package/dist/cjs/planCache.cjs +262 -0
- package/dist/cjs/session.cjs +127 -0
- package/dist/cjs/yuku.cjs +166 -0
- package/dist/cjs/zero.cjs +389 -0
- package/dist/esm/ast.mjs +79 -0
- package/dist/esm/ast.mjs.map +1 -0
- package/dist/esm/contracts.mjs +34 -0
- package/dist/esm/contracts.mjs.map +1 -0
- package/dist/esm/diagnostics.mjs +44 -0
- package/dist/esm/diagnostics.mjs.map +1 -0
- package/dist/esm/evaluate.mjs +576 -0
- package/dist/esm/evaluate.mjs.map +1 -0
- package/dist/esm/graph.mjs +313 -0
- package/dist/esm/graph.mjs.map +1 -0
- package/dist/esm/hash.mjs +26 -0
- package/dist/esm/hash.mjs.map +1 -0
- package/dist/esm/index.mjs +30 -0
- package/dist/esm/ir.mjs +12 -0
- package/dist/esm/ir.mjs.map +1 -0
- package/dist/esm/lower.mjs +210 -0
- package/dist/esm/lower.mjs.map +1 -0
- package/dist/esm/materialize.mjs +203 -0
- package/dist/esm/materialize.mjs.map +1 -0
- package/dist/esm/normalize.mjs +604 -0
- package/dist/esm/normalize.mjs.map +1 -0
- package/dist/esm/output.mjs +119 -0
- package/dist/esm/output.mjs.map +1 -0
- package/dist/esm/planCache.mjs +232 -0
- package/dist/esm/planCache.mjs.map +1 -0
- package/dist/esm/session.mjs +108 -0
- package/dist/esm/session.mjs.map +1 -0
- package/dist/esm/yuku.mjs +144 -0
- package/dist/esm/yuku.mjs.map +1 -0
- package/dist/esm/zero.mjs +354 -0
- package/dist/esm/zero.mjs.map +1 -0
- package/package.json +42 -4
- package/src/ast.ts +106 -0
- package/src/contracts.ts +150 -0
- package/src/diagnostics.ts +96 -0
- package/src/evaluate.ts +875 -0
- package/src/graph.ts +425 -0
- package/src/hash.ts +36 -0
- package/src/index.ts +15 -0
- package/src/ir.ts +156 -0
- package/src/lower.ts +435 -0
- package/src/materialize.ts +383 -0
- package/src/normalize.ts +834 -0
- package/src/output.ts +175 -0
- package/src/planCache.ts +362 -0
- package/src/session.ts +192 -0
- package/src/yuku.ts +197 -0
- package/src/zero.ts +609 -0
- package/types/ast.d.ts +11 -0
- package/types/ast.d.ts.map +1 -0
- package/types/contracts.d.ts +99 -0
- package/types/contracts.d.ts.map +1 -0
- package/types/diagnostics.d.ts +31 -0
- package/types/diagnostics.d.ts.map +1 -0
- package/types/evaluate.d.ts +48 -0
- package/types/evaluate.d.ts.map +1 -0
- package/types/graph.d.ts +51 -0
- package/types/graph.d.ts.map +1 -0
- package/types/hash.d.ts +8 -0
- package/types/hash.d.ts.map +1 -0
- package/types/index.d.ts +16 -0
- package/types/index.d.ts.map +1 -0
- package/types/ir.d.ts +120 -0
- package/types/ir.d.ts.map +1 -0
- package/types/lower.d.ts +99 -0
- package/types/lower.d.ts.map +1 -0
- package/types/materialize.d.ts +121 -0
- package/types/materialize.d.ts.map +1 -0
- package/types/normalize.d.ts +7 -0
- package/types/normalize.d.ts.map +1 -0
- package/types/output.d.ts +32 -0
- package/types/output.d.ts.map +1 -0
- package/types/planCache.d.ts +123 -0
- package/types/planCache.d.ts.map +1 -0
- package/types/session.d.ts +52 -0
- package/types/session.d.ts.map +1 -0
- package/types/yuku.d.ts +4 -0
- package/types/yuku.d.ts.map +1 -0
- package/types/zero.d.ts +99 -0
- package/types/zero.d.ts.map +1 -0
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
|
+
var evaluate_exports = {};
|
|
22
|
+
__export(evaluate_exports, {
|
|
23
|
+
collectBranchDependencies: () => collectBranchDependencies,
|
|
24
|
+
collectLeaves: () => collectLeaves,
|
|
25
|
+
evaluateBinding: () => evaluateBinding,
|
|
26
|
+
evaluateBranches: () => evaluateBranches,
|
|
27
|
+
evaluateDynamicExpression: () => evaluateDynamicExpression,
|
|
28
|
+
evaluateExpression: () => evaluateExpression
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(evaluate_exports);
|
|
31
|
+
var import_ast = require("./ast.cjs");
|
|
32
|
+
var import_contracts = require("./contracts.cjs");
|
|
33
|
+
var import_diagnostics = require("./diagnostics.cjs");
|
|
34
|
+
function success(value) {
|
|
35
|
+
return {
|
|
36
|
+
ok: true,
|
|
37
|
+
value
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function literal(node) {
|
|
41
|
+
if (node.type === "NullLiteral") return null;
|
|
42
|
+
if (node.type === "Literal" || node.type.endsWith("Literal")) {
|
|
43
|
+
const value = node.value;
|
|
44
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null) {
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return void 0;
|
|
49
|
+
}
|
|
50
|
+
function unsupported(id, node, detail = `Expression ${node.type} is not statically supported`) {
|
|
51
|
+
return {
|
|
52
|
+
ok: false,
|
|
53
|
+
bailout: (0, import_diagnostics.localBailout)("local/unsupported-expression", (0, import_contracts.spanOf)(id, node), detail)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function evaluateDefinition(resolver, definition, useSpan, state) {
|
|
57
|
+
const key = `${definition.id}:${definition.span.start}`;
|
|
58
|
+
if (state.activeDefinitions.has(key)) {
|
|
59
|
+
return {
|
|
60
|
+
ok: false,
|
|
61
|
+
bailout: (0, import_diagnostics.localBailout)("local/static-evaluation-cycle", useSpan, `Static evaluation cycle reached ${definition.name}`)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (!definition.initializer) {
|
|
65
|
+
return {
|
|
66
|
+
ok: false,
|
|
67
|
+
bailout: (0, import_diagnostics.linkedBailout)("linked/missing-initializer", useSpan, `Binding ${definition.name} has no static initializer`, definition.id)
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
if (!definition.constant) {
|
|
71
|
+
return {
|
|
72
|
+
ok: false,
|
|
73
|
+
bailout: (0, import_diagnostics.linkedBailout)("linked/unresolved-binding", useSpan, `Binding ${definition.name} is mutable and cannot be statically evaluated`, definition.id)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const initializer = resolver.expressionNode(definition.initializer);
|
|
77
|
+
if (!initializer) {
|
|
78
|
+
return {
|
|
79
|
+
ok: false,
|
|
80
|
+
bailout: (0, import_diagnostics.linkedBailout)("linked/missing-initializer", useSpan, `Binding ${definition.name} initializer is outside the linked graph`, definition.id)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
state.dependencies.add(definition.id);
|
|
84
|
+
state.activeDefinitions.add(key);
|
|
85
|
+
try {
|
|
86
|
+
return evaluateNode(resolver, definition.id, initializer, state);
|
|
87
|
+
} finally {
|
|
88
|
+
state.activeDefinitions.delete(key);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function evaluateObject(resolver, id, node, state) {
|
|
92
|
+
const output = {};
|
|
93
|
+
for (const property of (0, import_ast.childNodes)(node, "properties")) {
|
|
94
|
+
if (property.type === "SpreadElement") {
|
|
95
|
+
const argument = (0, import_ast.childNode)(property, "argument");
|
|
96
|
+
if (!argument) return unsupported(id, property);
|
|
97
|
+
const spread = evaluateNode(resolver, id, argument, state);
|
|
98
|
+
if (!spread.ok) return spread;
|
|
99
|
+
if (!spread.value || typeof spread.value !== "object" || Array.isArray(spread.value)) {
|
|
100
|
+
return {
|
|
101
|
+
ok: false,
|
|
102
|
+
bailout: (0, import_diagnostics.localBailout)("local/non-object-spread", (0, import_contracts.spanOf)(id, property), "Static object spread did not evaluate to an object")
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
Object.assign(output, spread.value);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
|
|
109
|
+
return unsupported(id, property);
|
|
110
|
+
}
|
|
111
|
+
const keyNode = (0, import_ast.childNode)(property, "key");
|
|
112
|
+
const valueNode = (0, import_ast.childNode)(property, "value");
|
|
113
|
+
if (!keyNode || !valueNode) return unsupported(id, property);
|
|
114
|
+
let key = (0, import_ast.identifierName)(keyNode) ?? void 0;
|
|
115
|
+
if (property.computed === true || key === void 0) {
|
|
116
|
+
const evaluatedKey = evaluateNode(resolver, id, keyNode, state);
|
|
117
|
+
if (!evaluatedKey.ok) return evaluatedKey;
|
|
118
|
+
key = evaluatedKey.value;
|
|
119
|
+
}
|
|
120
|
+
if (typeof key !== "string" && typeof key !== "number") {
|
|
121
|
+
return unsupported(id, keyNode, "Static object key is not a string or number");
|
|
122
|
+
}
|
|
123
|
+
const value = evaluateNode(resolver, id, valueNode, state);
|
|
124
|
+
if (!value.ok) return value;
|
|
125
|
+
output[String(key)] = value.value;
|
|
126
|
+
}
|
|
127
|
+
return success(output);
|
|
128
|
+
}
|
|
129
|
+
function evaluateArray(resolver, id, node, state) {
|
|
130
|
+
const output = [];
|
|
131
|
+
const rawElements = node.elements;
|
|
132
|
+
if (!Array.isArray(rawElements) || rawElements.some((element) => element === null || !(0, import_ast.isAstNode)(element))) {
|
|
133
|
+
return unsupported(id, node, "Sparse arrays are not statically supported");
|
|
134
|
+
}
|
|
135
|
+
for (const element of rawElements.filter(import_ast.isAstNode)) {
|
|
136
|
+
if (element.type === "SpreadElement") {
|
|
137
|
+
const argument = (0, import_ast.childNode)(element, "argument");
|
|
138
|
+
if (!argument) return unsupported(id, element);
|
|
139
|
+
const spread = evaluateNode(resolver, id, argument, state);
|
|
140
|
+
if (!spread.ok) return spread;
|
|
141
|
+
if (!Array.isArray(spread.value)) {
|
|
142
|
+
return unsupported(id, element, "Static array spread did not evaluate to an array");
|
|
143
|
+
}
|
|
144
|
+
output.push(...spread.value);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
const value = evaluateNode(resolver, id, element, state);
|
|
148
|
+
if (!value.ok) return value;
|
|
149
|
+
output.push(value.value);
|
|
150
|
+
}
|
|
151
|
+
return success(output);
|
|
152
|
+
}
|
|
153
|
+
function evaluateNode(resolver, id, input, state) {
|
|
154
|
+
const node = (0, import_ast.unwrapExpression)(input);
|
|
155
|
+
const literalValue = literal(node);
|
|
156
|
+
if (literalValue !== void 0) return success(literalValue);
|
|
157
|
+
switch (node.type) {
|
|
158
|
+
case "Identifier": {
|
|
159
|
+
const name = (0, import_ast.identifierName)(node);
|
|
160
|
+
if (!name) return unsupported(id, node);
|
|
161
|
+
const definition = resolver.resolveReference((0, import_contracts.expressionReference)(id, node));
|
|
162
|
+
if (!definition) {
|
|
163
|
+
return {
|
|
164
|
+
ok: false,
|
|
165
|
+
bailout: (0, import_diagnostics.linkedBailout)("linked/unresolved-binding", (0, import_contracts.spanOf)(id, node), `Binding ${name} has no host-linked definition`)
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
return evaluateDefinition(resolver, definition, (0, import_contracts.spanOf)(id, node), state);
|
|
169
|
+
}
|
|
170
|
+
case "ObjectExpression": return evaluateObject(resolver, id, node, state);
|
|
171
|
+
case "ArrayExpression": return evaluateArray(resolver, id, node, state);
|
|
172
|
+
case "MemberExpression": {
|
|
173
|
+
const objectNode = (0, import_ast.childNode)(node, "object");
|
|
174
|
+
const propertyNode = (0, import_ast.childNode)(node, "property");
|
|
175
|
+
if (!objectNode || !propertyNode) return unsupported(id, node);
|
|
176
|
+
const object = evaluateNode(resolver, id, objectNode, state);
|
|
177
|
+
if (!object.ok) return object;
|
|
178
|
+
let property = (0, import_ast.identifierName)(propertyNode);
|
|
179
|
+
if (node.computed === true || property === null) {
|
|
180
|
+
const evaluatedProperty = evaluateNode(resolver, id, propertyNode, state);
|
|
181
|
+
if (!evaluatedProperty.ok) return evaluatedProperty;
|
|
182
|
+
property = evaluatedProperty.value;
|
|
183
|
+
}
|
|
184
|
+
if ((typeof object.value !== "object" || object.value === null) && typeof object.value !== "string") {
|
|
185
|
+
return unsupported(id, node, "Static member target is not indexable");
|
|
186
|
+
}
|
|
187
|
+
if (typeof property !== "string" && typeof property !== "number") {
|
|
188
|
+
return unsupported(id, propertyNode, "Static member key is not a string or number");
|
|
189
|
+
}
|
|
190
|
+
const value = object.value[String(property)];
|
|
191
|
+
return value === void 0 ? unsupported(id, node, `Static member ${String(property)} does not exist`) : success(value);
|
|
192
|
+
}
|
|
193
|
+
case "UnaryExpression": {
|
|
194
|
+
const argument = (0, import_ast.childNode)(node, "argument");
|
|
195
|
+
if (!argument) return unsupported(id, node);
|
|
196
|
+
const value = evaluateNode(resolver, id, argument, state);
|
|
197
|
+
if (!value.ok) return value;
|
|
198
|
+
if (node.operator === "-" && typeof value.value === "number") {
|
|
199
|
+
return success(-value.value);
|
|
200
|
+
}
|
|
201
|
+
if (node.operator === "+" && typeof value.value === "number") {
|
|
202
|
+
return success(value.value);
|
|
203
|
+
}
|
|
204
|
+
if (node.operator === "!") return success(!value.value);
|
|
205
|
+
return unsupported(id, node, `Unary operator ${String(node.operator)} is unsupported`);
|
|
206
|
+
}
|
|
207
|
+
case "BinaryExpression":
|
|
208
|
+
case "LogicalExpression": {
|
|
209
|
+
const leftNode = (0, import_ast.childNode)(node, "left");
|
|
210
|
+
const rightNode = (0, import_ast.childNode)(node, "right");
|
|
211
|
+
if (!leftNode || !rightNode) return unsupported(id, node);
|
|
212
|
+
const left = evaluateNode(resolver, id, leftNode, state);
|
|
213
|
+
if (!left.ok) return left;
|
|
214
|
+
if (node.type === "LogicalExpression") {
|
|
215
|
+
if (node.operator === "&&" && !left.value) return left;
|
|
216
|
+
if (node.operator === "||" && left.value) return left;
|
|
217
|
+
if (node.operator === "??" && left.value !== null) return left;
|
|
218
|
+
}
|
|
219
|
+
const right = evaluateNode(resolver, id, rightNode, state);
|
|
220
|
+
if (!right.ok) return right;
|
|
221
|
+
switch (node.operator) {
|
|
222
|
+
case "+":
|
|
223
|
+
if (typeof left.value === "number" && typeof right.value === "number") {
|
|
224
|
+
return success(left.value + right.value);
|
|
225
|
+
}
|
|
226
|
+
if (typeof left.value === "string" || typeof right.value === "string") {
|
|
227
|
+
return success(String(left.value) + String(right.value));
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
case "-":
|
|
231
|
+
if (typeof left.value === "number" && typeof right.value === "number") {
|
|
232
|
+
return success(left.value - right.value);
|
|
233
|
+
}
|
|
234
|
+
break;
|
|
235
|
+
case "*":
|
|
236
|
+
if (typeof left.value === "number" && typeof right.value === "number") {
|
|
237
|
+
return success(left.value * right.value);
|
|
238
|
+
}
|
|
239
|
+
break;
|
|
240
|
+
case "/":
|
|
241
|
+
if (typeof left.value === "number" && typeof right.value === "number") {
|
|
242
|
+
return success(left.value / right.value);
|
|
243
|
+
}
|
|
244
|
+
break;
|
|
245
|
+
case "&&":
|
|
246
|
+
case "||":
|
|
247
|
+
case "??": return right;
|
|
248
|
+
}
|
|
249
|
+
return unsupported(id, node, `Binary operator ${String(node.operator)} is unsupported`);
|
|
250
|
+
}
|
|
251
|
+
case "ConditionalExpression": {
|
|
252
|
+
const testNode = (0, import_ast.childNode)(node, "test");
|
|
253
|
+
const consequent = (0, import_ast.childNode)(node, "consequent");
|
|
254
|
+
const alternate = (0, import_ast.childNode)(node, "alternate");
|
|
255
|
+
if (!testNode || !consequent || !alternate) return unsupported(id, node);
|
|
256
|
+
const test = evaluateNode(resolver, id, testNode, state);
|
|
257
|
+
if (!test.ok) return test;
|
|
258
|
+
return evaluateNode(resolver, id, test.value ? consequent : alternate, state);
|
|
259
|
+
}
|
|
260
|
+
case "TemplateLiteral": {
|
|
261
|
+
const quasis = (0, import_ast.childNodes)(node, "quasis");
|
|
262
|
+
const expressions = (0, import_ast.childNodes)(node, "expressions");
|
|
263
|
+
let output = "";
|
|
264
|
+
for (let index = 0; index < quasis.length; index++) {
|
|
265
|
+
output += quasiText(quasis[index]);
|
|
266
|
+
const expression = expressions[index];
|
|
267
|
+
if (expression) {
|
|
268
|
+
const value = evaluateNode(resolver, id, expression, state);
|
|
269
|
+
if (!value.ok) return value;
|
|
270
|
+
output += String(value.value);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return success(output);
|
|
274
|
+
}
|
|
275
|
+
default: return unsupported(id, node);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function quasiText(quasi) {
|
|
279
|
+
const cooked = quasi?.value?.cooked;
|
|
280
|
+
const raw = quasi?.value?.raw;
|
|
281
|
+
return typeof cooked === "string" ? cooked : typeof raw === "string" ? raw : "";
|
|
282
|
+
}
|
|
283
|
+
function dynamicPrimitive(value) {
|
|
284
|
+
if (value === null) return "null";
|
|
285
|
+
if (typeof value === "number") return "number";
|
|
286
|
+
if (typeof value === "string") return "string";
|
|
287
|
+
if (typeof value === "boolean") return "boolean";
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
function evaluateDynamicNode(resolver, id, input, state) {
|
|
291
|
+
const exact = evaluateNode(resolver, id, input, state);
|
|
292
|
+
if (exact.ok) {
|
|
293
|
+
const type = dynamicPrimitive(exact.value);
|
|
294
|
+
return type ? {
|
|
295
|
+
type,
|
|
296
|
+
values: [exact.value]
|
|
297
|
+
} : null;
|
|
298
|
+
}
|
|
299
|
+
const node = (0, import_ast.unwrapExpression)(input);
|
|
300
|
+
if (node.type === "Identifier") {
|
|
301
|
+
if (!(0, import_ast.identifierName)(node)) return null;
|
|
302
|
+
const definition = resolver.resolveReference((0, import_contracts.expressionReference)(id, node));
|
|
303
|
+
if (!definition?.constant || !definition.initializer) return null;
|
|
304
|
+
const key = `${definition.id}:${definition.span.start}`;
|
|
305
|
+
if (state.activeDefinitions.has(key)) return null;
|
|
306
|
+
const initializer = resolver.expressionNode(definition.initializer);
|
|
307
|
+
if (!initializer) return null;
|
|
308
|
+
state.dependencies.add(definition.id);
|
|
309
|
+
state.activeDefinitions.add(key);
|
|
310
|
+
try {
|
|
311
|
+
return evaluateDynamicNode(resolver, definition.id, initializer, state);
|
|
312
|
+
} finally {
|
|
313
|
+
state.activeDefinitions.delete(key);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (node.type === "MemberExpression") {
|
|
317
|
+
const objectNode = (0, import_ast.childNode)(node, "object");
|
|
318
|
+
const propertyNode = (0, import_ast.childNode)(node, "property");
|
|
319
|
+
if (!objectNode || !propertyNode) return null;
|
|
320
|
+
const object = evaluateNode(resolver, id, objectNode, state);
|
|
321
|
+
if (!object.ok || !Array.isArray(object.value)) return null;
|
|
322
|
+
const property = evaluateDynamicNode(resolver, id, propertyNode, state);
|
|
323
|
+
if (property?.type !== "number" || object.value.length > 32) return null;
|
|
324
|
+
const values = [];
|
|
325
|
+
let type = null;
|
|
326
|
+
for (const value of object.value) {
|
|
327
|
+
const nextType = dynamicPrimitive(value);
|
|
328
|
+
if (!nextType || type && type !== nextType) return null;
|
|
329
|
+
type = nextType;
|
|
330
|
+
const primitive = value;
|
|
331
|
+
if (!values.includes(primitive)) values.push(primitive);
|
|
332
|
+
}
|
|
333
|
+
return type ? {
|
|
334
|
+
type,
|
|
335
|
+
values
|
|
336
|
+
} : null;
|
|
337
|
+
}
|
|
338
|
+
if (node.type === "BinaryExpression") {
|
|
339
|
+
const leftNode = (0, import_ast.childNode)(node, "left");
|
|
340
|
+
const rightNode = (0, import_ast.childNode)(node, "right");
|
|
341
|
+
if (!leftNode || !rightNode) return null;
|
|
342
|
+
const left = evaluateDynamicNode(resolver, id, leftNode, state);
|
|
343
|
+
const right = evaluateDynamicNode(resolver, id, rightNode, state);
|
|
344
|
+
if (node.operator === "+") {
|
|
345
|
+
return left?.type === "number" && right?.type === "number" ? { type: "number" } : null;
|
|
346
|
+
}
|
|
347
|
+
if (node.operator === "-" || node.operator === "*" || node.operator === "/" || node.operator === "%" || node.operator === "**") {
|
|
348
|
+
if (left?.type === "number" || right?.type === "number") {
|
|
349
|
+
return { type: "number" };
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (node.type === "TemplateLiteral") {
|
|
354
|
+
const quasis = (0, import_ast.childNodes)(node, "quasis");
|
|
355
|
+
const expressions = (0, import_ast.childNodes)(node, "expressions");
|
|
356
|
+
let values = [""];
|
|
357
|
+
for (let position = 0; position < quasis.length; position++) {
|
|
358
|
+
const text = quasiText(quasis[position]);
|
|
359
|
+
values = values.map((value) => value + text);
|
|
360
|
+
const expression = expressions[position];
|
|
361
|
+
if (!expression) continue;
|
|
362
|
+
const part = evaluateDynamicNode(resolver, id, expression, state);
|
|
363
|
+
if (!part?.values) return null;
|
|
364
|
+
const next = [];
|
|
365
|
+
for (const value of values) {
|
|
366
|
+
for (const primitive of part.values) {
|
|
367
|
+
const joined = value + String(primitive);
|
|
368
|
+
if (!next.includes(joined)) next.push(joined);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (next.length > 32) return null;
|
|
372
|
+
values = next;
|
|
373
|
+
}
|
|
374
|
+
return {
|
|
375
|
+
type: "string",
|
|
376
|
+
values
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
return null;
|
|
380
|
+
}
|
|
381
|
+
function collectLeaves(node) {
|
|
382
|
+
if (node.kind === "leaf") return [node];
|
|
383
|
+
return [...collectLeaves(node.whenTrue), ...collectLeaves(node.whenFalse)];
|
|
384
|
+
}
|
|
385
|
+
function collectBranchDependencies(node) {
|
|
386
|
+
if (node.kind === "leaf") return node.dependencies;
|
|
387
|
+
return [.../* @__PURE__ */ new Set([...collectBranchDependencies(node.whenTrue), ...collectBranchDependencies(node.whenFalse)])].sort();
|
|
388
|
+
}
|
|
389
|
+
function evaluateBranchChild(resolver, id, node, depth, state, sourceId) {
|
|
390
|
+
const leafState = {
|
|
391
|
+
activeDefinitions: new Set(state.activeDefinitions),
|
|
392
|
+
dependencies: new Set(state.dependencies)
|
|
393
|
+
};
|
|
394
|
+
const leafRes = evaluateNode(resolver, id, node, leafState);
|
|
395
|
+
if (leafRes.ok) {
|
|
396
|
+
return {
|
|
397
|
+
kind: "leaf",
|
|
398
|
+
value: leafRes.value,
|
|
399
|
+
dependencies: [...leafState.dependencies].sort()
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
return evaluateBranchesNode(resolver, id, node, depth + 1, leafState, sourceId);
|
|
403
|
+
}
|
|
404
|
+
function evaluateBranchesNode(resolver, id, input, depth, state, sourceId) {
|
|
405
|
+
const node = (0, import_ast.unwrapExpression)(input);
|
|
406
|
+
if (!(0, import_ast.isAstNode)(node)) return null;
|
|
407
|
+
if (node.type === "Identifier") {
|
|
408
|
+
if (!(0, import_ast.identifierName)(node)) return null;
|
|
409
|
+
const definition = resolver.resolveReference((0, import_contracts.expressionReference)(id, node));
|
|
410
|
+
if (!definition || !definition.constant || !definition.initializer) return null;
|
|
411
|
+
const initializer = resolver.expressionNode(definition.initializer);
|
|
412
|
+
if (!initializer) return null;
|
|
413
|
+
const key = `${definition.id}:${definition.span.start}`;
|
|
414
|
+
if (state.activeDefinitions.has(key)) return null;
|
|
415
|
+
state.activeDefinitions.add(key);
|
|
416
|
+
state.dependencies.add(definition.id);
|
|
417
|
+
try {
|
|
418
|
+
return evaluateBranchesNode(resolver, definition.id, initializer, depth, state, sourceId);
|
|
419
|
+
} finally {
|
|
420
|
+
state.activeDefinitions.delete(key);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (depth >= 3) return null;
|
|
424
|
+
if (node.type === "ConditionalExpression") {
|
|
425
|
+
const testNode = (0, import_ast.childNode)(node, "test");
|
|
426
|
+
const consequent = (0, import_ast.childNode)(node, "consequent");
|
|
427
|
+
const alternate = (0, import_ast.childNode)(node, "alternate");
|
|
428
|
+
if (!testNode || !consequent || !alternate) return null;
|
|
429
|
+
const test = (0, import_contracts.spanOf)(id, testNode);
|
|
430
|
+
if (test.id !== sourceId) return null;
|
|
431
|
+
const whenTrue = evaluateBranchChild(resolver, id, consequent, depth, state, sourceId);
|
|
432
|
+
if (!whenTrue) return null;
|
|
433
|
+
const whenFalse = evaluateBranchChild(resolver, id, alternate, depth, state, sourceId);
|
|
434
|
+
if (!whenFalse) return null;
|
|
435
|
+
return {
|
|
436
|
+
kind: "branch",
|
|
437
|
+
test,
|
|
438
|
+
whenTrue,
|
|
439
|
+
whenFalse
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
if (node.type === "TemplateLiteral") {
|
|
443
|
+
return templateBranchesNode(resolver, id, (0, import_ast.childNodes)(node, "quasis"), (0, import_ast.childNodes)(node, "expressions"), 0, "", depth, state, sourceId);
|
|
444
|
+
}
|
|
445
|
+
if (node.type === "LogicalExpression") {
|
|
446
|
+
const left = (0, import_ast.childNode)(node, "left");
|
|
447
|
+
const right = (0, import_ast.childNode)(node, "right");
|
|
448
|
+
if (!left || !right) return null;
|
|
449
|
+
if (node.operator === "&&") {
|
|
450
|
+
const test = (0, import_contracts.spanOf)(id, left);
|
|
451
|
+
if (test.id !== sourceId) return null;
|
|
452
|
+
const whenTrue = evaluateBranchChild(resolver, id, right, depth, state, sourceId);
|
|
453
|
+
if (!whenTrue) return null;
|
|
454
|
+
const whenFalse = {
|
|
455
|
+
kind: "leaf",
|
|
456
|
+
value: void 0,
|
|
457
|
+
dependencies: [...state.dependencies].sort()
|
|
458
|
+
};
|
|
459
|
+
return {
|
|
460
|
+
kind: "branch",
|
|
461
|
+
test,
|
|
462
|
+
whenTrue,
|
|
463
|
+
whenFalse
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
if (node.operator === "||" || node.operator === "??") {
|
|
467
|
+
const test = (0, import_contracts.spanOf)(id, left);
|
|
468
|
+
if (test.id !== sourceId) return null;
|
|
469
|
+
const leftState = {
|
|
470
|
+
activeDefinitions: new Set(state.activeDefinitions),
|
|
471
|
+
dependencies: new Set(state.dependencies)
|
|
472
|
+
};
|
|
473
|
+
const leftRes = evaluateNode(resolver, id, left, leftState);
|
|
474
|
+
if (!leftRes.ok) return null;
|
|
475
|
+
const whenTrue = {
|
|
476
|
+
kind: "leaf",
|
|
477
|
+
value: leftRes.value,
|
|
478
|
+
dependencies: [...leftState.dependencies].sort()
|
|
479
|
+
};
|
|
480
|
+
const whenFalse = evaluateBranchChild(resolver, id, right, depth, state, sourceId);
|
|
481
|
+
if (!whenFalse) return null;
|
|
482
|
+
return {
|
|
483
|
+
kind: "branch",
|
|
484
|
+
test,
|
|
485
|
+
whenTrue,
|
|
486
|
+
whenFalse
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
function mapBranchLeaves(node, map) {
|
|
493
|
+
if (node.kind === "leaf") return map(node);
|
|
494
|
+
const whenTrue = mapBranchLeaves(node.whenTrue, map);
|
|
495
|
+
if (!whenTrue) return null;
|
|
496
|
+
const whenFalse = mapBranchLeaves(node.whenFalse, map);
|
|
497
|
+
if (!whenFalse) return null;
|
|
498
|
+
return {
|
|
499
|
+
kind: "branch",
|
|
500
|
+
test: node.test,
|
|
501
|
+
whenTrue,
|
|
502
|
+
whenFalse
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
function templateBranchesNode(resolver, id, quasis, expressions, index, prefix, depth, state, sourceId) {
|
|
506
|
+
let text = prefix;
|
|
507
|
+
for (let position = index; position < quasis.length; position++) {
|
|
508
|
+
text += quasiText(quasis[position]);
|
|
509
|
+
const expression = expressions[position];
|
|
510
|
+
if (!expression) continue;
|
|
511
|
+
const exact = evaluateNode(resolver, id, expression, state);
|
|
512
|
+
if (exact.ok) {
|
|
513
|
+
const interpolated = templateInterpolation(exact.value);
|
|
514
|
+
if (interpolated === null) return null;
|
|
515
|
+
text += interpolated;
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
const tree = evaluateBranchesNode(resolver, id, expression, depth + 1, state, sourceId);
|
|
519
|
+
if (!tree) return null;
|
|
520
|
+
return mapBranchLeaves(tree, (leaf) => {
|
|
521
|
+
if (leaf.value === void 0) return null;
|
|
522
|
+
const interpolated = templateInterpolation(leaf.value);
|
|
523
|
+
if (interpolated === null) return null;
|
|
524
|
+
return templateBranchesNode(resolver, id, quasis, expressions, position + 1, text + interpolated, depth + 1, state, sourceId);
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
return {
|
|
528
|
+
kind: "leaf",
|
|
529
|
+
value: text,
|
|
530
|
+
dependencies: [...state.dependencies].sort()
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
function templateInterpolation(value) {
|
|
534
|
+
if (value === null) return "null";
|
|
535
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
536
|
+
return String(value);
|
|
537
|
+
}
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
function evaluateBranches(resolver, reference) {
|
|
541
|
+
const node = resolver.expressionNode(reference);
|
|
542
|
+
if (!node) return null;
|
|
543
|
+
const state = {
|
|
544
|
+
activeDefinitions: /* @__PURE__ */ new Set(),
|
|
545
|
+
dependencies: /* @__PURE__ */ new Set()
|
|
546
|
+
};
|
|
547
|
+
const tree = evaluateBranchesNode(resolver, reference.id, node, 0, state, reference.id);
|
|
548
|
+
return tree?.kind === "branch" ? tree : null;
|
|
549
|
+
}
|
|
550
|
+
function evaluateExpression(resolver, reference) {
|
|
551
|
+
const node = resolver.expressionNode(reference);
|
|
552
|
+
if (!node) {
|
|
553
|
+
return {
|
|
554
|
+
ok: false,
|
|
555
|
+
bailout: (0, import_diagnostics.linkedBailout)("linked/missing-initializer", reference, "Expression is outside the linked project graph", reference.id)
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
const state = {
|
|
559
|
+
activeDefinitions: /* @__PURE__ */ new Set(),
|
|
560
|
+
dependencies: /* @__PURE__ */ new Set()
|
|
561
|
+
};
|
|
562
|
+
const result = evaluateNode(resolver, reference.id, node, state);
|
|
563
|
+
return result.ok ? {
|
|
564
|
+
ok: true,
|
|
565
|
+
value: result.value,
|
|
566
|
+
dependencies: [...state.dependencies].sort()
|
|
567
|
+
} : result;
|
|
568
|
+
}
|
|
569
|
+
function evaluateDynamicExpression(resolver, reference) {
|
|
570
|
+
const node = resolver.expressionNode(reference);
|
|
571
|
+
if (!node) return null;
|
|
572
|
+
const state = {
|
|
573
|
+
activeDefinitions: /* @__PURE__ */ new Set(),
|
|
574
|
+
dependencies: /* @__PURE__ */ new Set()
|
|
575
|
+
};
|
|
576
|
+
const result = evaluateDynamicNode(resolver, reference.id, node, state);
|
|
577
|
+
return result ? {
|
|
578
|
+
...result,
|
|
579
|
+
dependencies: [...state.dependencies].sort()
|
|
580
|
+
} : null;
|
|
581
|
+
}
|
|
582
|
+
function evaluateBinding(resolver, id, localName) {
|
|
583
|
+
const definition = resolver.resolveBinding(id, localName);
|
|
584
|
+
if (!definition) {
|
|
585
|
+
const emptySpan = {
|
|
586
|
+
id,
|
|
587
|
+
start: 0,
|
|
588
|
+
end: 0
|
|
589
|
+
};
|
|
590
|
+
return {
|
|
591
|
+
ok: false,
|
|
592
|
+
bailout: (0, import_diagnostics.linkedBailout)("linked/unresolved-binding", emptySpan, `Binding ${localName} has no host-linked definition`)
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
if (!definition.initializer) {
|
|
596
|
+
return {
|
|
597
|
+
ok: false,
|
|
598
|
+
bailout: (0, import_diagnostics.linkedBailout)("linked/missing-initializer", definition.span, `Binding ${localName} has no static initializer`, definition.id)
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
return evaluateExpression(resolver, (0, import_contracts.expressionReference)(definition.id, definition.initializer));
|
|
602
|
+
}
|