@farm.js/react 0.1.0-beta.0 → 0.1.0-beta.2
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/README.md +277 -11
- package/dist/compiler-runtime.cjs +1290 -17
- package/dist/compiler-runtime.cjs.map +1 -1
- package/dist/compiler-runtime.d.mts +132 -5
- package/dist/compiler-runtime.d.ts +132 -5
- package/dist/compiler-runtime.mjs +1290 -17
- package/dist/compiler-runtime.mjs.map +1 -1
- package/dist/index.cjs +24 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.mjs +24 -2
- package/dist/index.mjs.map +1 -1
- package/dist/list.cjs +27 -0
- package/dist/list.cjs.map +1 -0
- package/dist/list.d.mts +20 -0
- package/dist/list.d.ts +20 -0
- package/dist/list.mjs +21 -0
- package/dist/list.mjs.map +1 -0
- package/dist/vite.cjs +2332 -140
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.mjs +2332 -140
- package/dist/vite.mjs.map +1 -1
- package/package.json +10 -4
package/dist/vite.mjs
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { normalizePath } from 'vite';
|
|
2
2
|
import { transformAsync, types, traverse } from '@babel/core';
|
|
3
|
+
import { mkdir, writeFile } from 'fs/promises';
|
|
4
|
+
import { resolve, dirname, relative } from 'path';
|
|
3
5
|
|
|
4
6
|
// src/vite.ts
|
|
7
|
+
var SAFE_GLOBAL_CALLS = /* @__PURE__ */ new Set(["Boolean", "Number", "String"]);
|
|
8
|
+
var SAFE_MATH_CALLS = /* @__PURE__ */ new Set(["abs", "ceil", "floor", "max", "min", "round", "sign", "trunc"]);
|
|
5
9
|
function isComponentName(name) {
|
|
6
10
|
return /^[A-Z]/.test(name);
|
|
7
11
|
}
|
|
12
|
+
function isHostElement(element) {
|
|
13
|
+
const name = element.openingElement.name;
|
|
14
|
+
return types.isJSXIdentifier(name) && /^[a-z]/.test(name.name);
|
|
15
|
+
}
|
|
8
16
|
function directiveValues(node) {
|
|
9
17
|
return (node.directives || []).map((directive) => directive.value.value);
|
|
10
18
|
}
|
|
@@ -38,108 +46,1963 @@ function referencesIdentifier(expression, name) {
|
|
|
38
46
|
referenced = true;
|
|
39
47
|
}
|
|
40
48
|
}
|
|
41
|
-
});
|
|
42
|
-
return referenced;
|
|
49
|
+
});
|
|
50
|
+
return referenced;
|
|
51
|
+
}
|
|
52
|
+
function collectReferencedLocals(expression, names) {
|
|
53
|
+
const references = /* @__PURE__ */ new Set();
|
|
54
|
+
traverse(expressionFile(cloneExpression(expression)), {
|
|
55
|
+
ReferencedIdentifier(path) {
|
|
56
|
+
if (names.has(path.node.name) && !path.scope.hasBinding(path.node.name)) {
|
|
57
|
+
references.add(path.node.name);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return [...references];
|
|
62
|
+
}
|
|
63
|
+
function rewriteDerivedAccess(expression, derivedByName) {
|
|
64
|
+
const file = expressionFile(cloneExpression(expression));
|
|
65
|
+
traverse(file, {
|
|
66
|
+
ReferencedIdentifier(path) {
|
|
67
|
+
if (path.scope.hasBinding(path.node.name)) return;
|
|
68
|
+
const derived = derivedByName.get(path.node.name);
|
|
69
|
+
if (!derived) return;
|
|
70
|
+
path.replaceWith(cloneExpression(derived));
|
|
71
|
+
path.skip();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
return file.program.body[0].expression;
|
|
75
|
+
}
|
|
76
|
+
function rewriteDestructuredPropAccess(expression, names, propsParameter) {
|
|
77
|
+
if (names.size === 0) return cloneExpression(expression);
|
|
78
|
+
const file = expressionFile(cloneExpression(expression));
|
|
79
|
+
traverse(file, {
|
|
80
|
+
ReferencedIdentifier(path) {
|
|
81
|
+
if (!names.has(path.node.name) || path.scope.hasBinding(path.node.name)) return;
|
|
82
|
+
if (path.parentPath.isJSXOpeningElement() || path.parentPath.isJSXClosingElement()) return;
|
|
83
|
+
path.replaceWith(
|
|
84
|
+
types.memberExpression(types.cloneNode(propsParameter), types.identifier(path.node.name))
|
|
85
|
+
);
|
|
86
|
+
path.skip();
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
return file.program.body[0].expression;
|
|
90
|
+
}
|
|
91
|
+
function rewriteHandlerAccess(root, handlersByName) {
|
|
92
|
+
if (handlersByName.size === 0) return { root: types.cloneNode(root, true) };
|
|
93
|
+
const file = expressionFile(types.cloneNode(root, true));
|
|
94
|
+
let reason;
|
|
95
|
+
traverse(file, {
|
|
96
|
+
ReferencedIdentifier(path) {
|
|
97
|
+
if (reason || path.scope.hasBinding(path.node.name)) return;
|
|
98
|
+
const handler = handlersByName.get(path.node.name);
|
|
99
|
+
if (!handler) return;
|
|
100
|
+
const container = path.parentPath;
|
|
101
|
+
const attribute = container.parentPath;
|
|
102
|
+
const eventName = attribute?.isJSXAttribute() && jsxAttributeName(attribute.node);
|
|
103
|
+
if (container.isJSXExpressionContainer() && container.node.expression === path.node && eventName && /^on[A-Z]/.test(eventName)) {
|
|
104
|
+
path.replaceWith(types.cloneNode(handler.value, true));
|
|
105
|
+
path.skip();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const call = container.isCallExpression() && container.node.callee === path.node ? container : null;
|
|
109
|
+
const event = call && findContainingEvent(call);
|
|
110
|
+
const eventExpression = event?.node.value && types.isJSXExpressionContainer(event.node.value) && event.node.value.expression;
|
|
111
|
+
const isInsideInlineHandler = eventExpression && (types.isArrowFunctionExpression(eventExpression) || types.isFunctionExpression(eventExpression)) && Boolean(call?.findParent((parent) => parent.node === eventExpression));
|
|
112
|
+
if (!call || !event || !isInsideInlineHandler) {
|
|
113
|
+
reason = `event handler ${handler.name} must be passed directly to a JSX event or called inside its inline handler`;
|
|
114
|
+
path.stop();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
path.replaceWith(types.cloneNode(handler.value, true));
|
|
118
|
+
path.skip();
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
return reason ? { reason } : { root: file.program.body[0].expression };
|
|
122
|
+
}
|
|
123
|
+
function isSafeCompilerCall(expression, safeGlobals) {
|
|
124
|
+
if (types.isIdentifier(expression.callee)) {
|
|
125
|
+
return safeGlobals.has(expression.callee.name) && SAFE_GLOBAL_CALLS.has(expression.callee.name);
|
|
126
|
+
}
|
|
127
|
+
return types.isMemberExpression(expression.callee) && !expression.callee.computed && types.isIdentifier(expression.callee.object, { name: "Math" }) && safeGlobals.has("Math") && types.isIdentifier(expression.callee.property) && SAFE_MATH_CALLS.has(expression.callee.property.name);
|
|
128
|
+
}
|
|
129
|
+
function validateDerivedExpression(expression, safeGlobals) {
|
|
130
|
+
let unsupported;
|
|
131
|
+
const reject = (reason, path) => {
|
|
132
|
+
unsupported = reason;
|
|
133
|
+
path.stop();
|
|
134
|
+
};
|
|
135
|
+
traverse(expressionFile(cloneExpression(expression)), {
|
|
136
|
+
ArrayExpression: (path) => reject("array literals", path),
|
|
137
|
+
AssignmentExpression: (path) => reject("assignments", path),
|
|
138
|
+
AwaitExpression: (path) => reject("await expressions", path),
|
|
139
|
+
CallExpression(path) {
|
|
140
|
+
if (!isSafeCompilerCall(path.node, safeGlobals)) reject("function calls", path);
|
|
141
|
+
},
|
|
142
|
+
ClassExpression: (path) => reject("class expressions", path),
|
|
143
|
+
Function: (path) => reject("function expressions", path),
|
|
144
|
+
JSXElement: (path) => reject("JSX", path),
|
|
145
|
+
JSXFragment: (path) => reject("JSX", path),
|
|
146
|
+
NewExpression: (path) => reject("constructor calls", path),
|
|
147
|
+
ObjectExpression: (path) => reject("object literals", path),
|
|
148
|
+
OptionalCallExpression: (path) => reject("function calls", path),
|
|
149
|
+
TaggedTemplateExpression: (path) => reject("tagged templates", path),
|
|
150
|
+
ThisExpression: (path) => reject("this expressions", path),
|
|
151
|
+
UnaryExpression(path) {
|
|
152
|
+
if (path.node.operator === "delete") reject("delete expressions", path);
|
|
153
|
+
},
|
|
154
|
+
UpdateExpression: (path) => reject("update expressions", path),
|
|
155
|
+
YieldExpression: (path) => reject("yield expressions", path)
|
|
156
|
+
});
|
|
157
|
+
return unsupported;
|
|
158
|
+
}
|
|
159
|
+
function findContainingEvent(path) {
|
|
160
|
+
const attribute = path.findParent((parent) => parent.isJSXAttribute());
|
|
161
|
+
if (!attribute?.isJSXAttribute() || !types.isJSXIdentifier(attribute.node.name)) return null;
|
|
162
|
+
return /^on[A-Z]/.test(attribute.node.name.name) ? attribute : null;
|
|
163
|
+
}
|
|
164
|
+
function validateSetterUsage(root, statesBySetter) {
|
|
165
|
+
const file = expressionFile(types.cloneNode(root, true));
|
|
166
|
+
let reason;
|
|
167
|
+
traverse(file, {
|
|
168
|
+
ReferencedIdentifier(path) {
|
|
169
|
+
if (reason) return;
|
|
170
|
+
const state = statesBySetter.get(path.node.name);
|
|
171
|
+
if (!state || path.scope.hasBinding(path.node.name)) return;
|
|
172
|
+
const parent = path.parentPath;
|
|
173
|
+
if (!parent.isCallExpression() || parent.node.callee !== path.node || !findContainingEvent(parent)) {
|
|
174
|
+
reason = `${state.setterName} must be called inside a JSX event handler`;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return reason;
|
|
179
|
+
}
|
|
180
|
+
function rewriteStateAccess(expression, stateParameter, statesByValue, statesBySetter) {
|
|
181
|
+
const file = expressionFile(cloneExpression(expression));
|
|
182
|
+
traverse(file, {
|
|
183
|
+
CallExpression(path) {
|
|
184
|
+
const callee = path.get("callee");
|
|
185
|
+
if (!callee.isIdentifier() || callee.scope.hasBinding(callee.node.name)) return;
|
|
186
|
+
const state = statesBySetter.get(callee.node.name);
|
|
187
|
+
if (!state) return;
|
|
188
|
+
callee.replaceWith(
|
|
189
|
+
types.memberExpression(
|
|
190
|
+
types.memberExpression(types.cloneNode(stateParameter), types.numericLiteral(state.index), true),
|
|
191
|
+
types.identifier("set")
|
|
192
|
+
)
|
|
193
|
+
);
|
|
194
|
+
},
|
|
195
|
+
ReferencedIdentifier(path) {
|
|
196
|
+
const state = statesByValue.get(path.node.name);
|
|
197
|
+
if (!state || path.scope.hasBinding(path.node.name)) return;
|
|
198
|
+
path.replaceWith(
|
|
199
|
+
types.callExpression(
|
|
200
|
+
types.memberExpression(
|
|
201
|
+
types.memberExpression(types.cloneNode(stateParameter), types.numericLiteral(state.index), true),
|
|
202
|
+
types.identifier("get")
|
|
203
|
+
),
|
|
204
|
+
[]
|
|
205
|
+
)
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
return file.program.body[0].expression;
|
|
210
|
+
}
|
|
211
|
+
function cleanJsxText(value) {
|
|
212
|
+
const lines = value.replace(/\r/g, "").split("\n");
|
|
213
|
+
let result = "";
|
|
214
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
215
|
+
const lastNonEmptyLine = lines.slice(index + 1).every((line2) => line2.trim().length === 0);
|
|
216
|
+
let line = lines[index].replace(/\t/g, " ").replace(/ +/g, " ");
|
|
217
|
+
if (index > 0) line = line.replace(/^ /, "");
|
|
218
|
+
if (!lastNonEmptyLine) line = line.replace(/ $/, "");
|
|
219
|
+
if (line) result += line + (!lastNonEmptyLine ? " " : "");
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
function isTextExpression(expression, statesByValue, safeGlobals) {
|
|
224
|
+
if (collectStateDependencies(expression, statesByValue).length > 0) {
|
|
225
|
+
return validateDerivedExpression(expression, safeGlobals) === void 0;
|
|
226
|
+
}
|
|
227
|
+
let supported = true;
|
|
228
|
+
traverse(expressionFile(cloneExpression(expression)), {
|
|
229
|
+
CallExpression(path) {
|
|
230
|
+
if (!isSafeCompilerCall(path.node, safeGlobals)) {
|
|
231
|
+
supported = false;
|
|
232
|
+
path.stop();
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
OptionalCallExpression(path) {
|
|
236
|
+
supported = false;
|
|
237
|
+
path.stop();
|
|
238
|
+
},
|
|
239
|
+
JSXElement(path) {
|
|
240
|
+
supported = false;
|
|
241
|
+
path.stop();
|
|
242
|
+
},
|
|
243
|
+
JSXFragment(path) {
|
|
244
|
+
supported = false;
|
|
245
|
+
path.stop();
|
|
246
|
+
},
|
|
247
|
+
ObjectExpression(path) {
|
|
248
|
+
supported = false;
|
|
249
|
+
path.stop();
|
|
250
|
+
},
|
|
251
|
+
Function(path) {
|
|
252
|
+
supported = false;
|
|
253
|
+
path.stop();
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
return supported;
|
|
257
|
+
}
|
|
258
|
+
function jsxAttributeName(attribute) {
|
|
259
|
+
return types.isJSXIdentifier(attribute.name) ? attribute.name.name : void 0;
|
|
260
|
+
}
|
|
261
|
+
function jsxAttribute(element, name) {
|
|
262
|
+
return element.openingElement.attributes.find(
|
|
263
|
+
(attribute) => types.isJSXAttribute(attribute) && jsxAttributeName(attribute) === name
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
function staticJsxAttributeString(attribute) {
|
|
267
|
+
if (!attribute?.value) return attribute ? "" : void 0;
|
|
268
|
+
if (types.isStringLiteral(attribute.value)) return attribute.value.value;
|
|
269
|
+
if (types.isJSXExpressionContainer(attribute.value) && types.isStringLiteral(attribute.value.expression)) {
|
|
270
|
+
return attribute.value.expression.value;
|
|
271
|
+
}
|
|
272
|
+
return void 0;
|
|
273
|
+
}
|
|
274
|
+
function controlledSelectHasDynamicOptions(element) {
|
|
275
|
+
let dynamic = false;
|
|
276
|
+
types.traverseFast(element, (node) => {
|
|
277
|
+
if (dynamic || !types.isJSXElement(node)) return;
|
|
278
|
+
const tag = node.openingElement.name;
|
|
279
|
+
if (!types.isJSXIdentifier(tag) || tag.name !== "option" && tag.name !== "optgroup") return;
|
|
280
|
+
dynamic = node.openingElement.attributes.some(
|
|
281
|
+
(attribute) => types.isJSXSpreadAttribute(attribute) || types.isJSXAttribute(attribute) && types.isJSXExpressionContainer(attribute.value) && !types.isJSXEmptyExpression(attribute.value.expression)
|
|
282
|
+
);
|
|
283
|
+
});
|
|
284
|
+
return dynamic;
|
|
285
|
+
}
|
|
286
|
+
function isEmptyConditionalBranch(expression) {
|
|
287
|
+
return types.isNullLiteral(expression) || types.isBooleanLiteral(expression, { value: false });
|
|
288
|
+
}
|
|
289
|
+
function conditionalBlockShape(expression) {
|
|
290
|
+
if (types.isLogicalExpression(expression, { operator: "&&" }) && types.isExpression(expression.left) && types.isJSXElement(expression.right)) {
|
|
291
|
+
return {
|
|
292
|
+
test: expression.left,
|
|
293
|
+
logical: true,
|
|
294
|
+
truthy: expression.right,
|
|
295
|
+
branches: [expression.right]
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
if (!types.isConditionalExpression(expression)) return null;
|
|
299
|
+
const branches = [expression.consequent, expression.alternate];
|
|
300
|
+
if (!branches.every((branch) => types.isJSXElement(branch) || isEmptyConditionalBranch(branch)) || !branches.some((branch) => types.isJSXElement(branch))) {
|
|
301
|
+
return null;
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
test: expression.test,
|
|
305
|
+
logical: false,
|
|
306
|
+
truthy: types.isJSXElement(expression.consequent) ? expression.consequent : void 0,
|
|
307
|
+
falsy: types.isJSXElement(expression.alternate) ? expression.alternate : void 0,
|
|
308
|
+
branches: branches.filter((branch) => types.isJSXElement(branch))
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function isMapCall(expression) {
|
|
312
|
+
return types.isCallExpression(expression) && types.isMemberExpression(expression.callee) && !expression.callee.computed && types.isExpression(expression.callee.object) && types.isIdentifier(expression.callee.property, { name: "map" });
|
|
313
|
+
}
|
|
314
|
+
function returnedExpression(callback) {
|
|
315
|
+
if (types.isExpression(callback.body)) return callback.body;
|
|
316
|
+
if (callback.body.body.length !== 1 || !types.isReturnStatement(callback.body.body[0])) {
|
|
317
|
+
return void 0;
|
|
318
|
+
}
|
|
319
|
+
const value = callback.body.body[0].argument;
|
|
320
|
+
return value && types.isExpression(value) ? value : void 0;
|
|
321
|
+
}
|
|
322
|
+
var SAFE_COLLECTION_PIPELINE_METHODS = /* @__PURE__ */ new Set(["filter", "slice", "toReversed", "toSorted"]);
|
|
323
|
+
function collectionPipelineMethod(expression) {
|
|
324
|
+
if (!types.isCallExpression(expression) || !types.isMemberExpression(expression.callee) || expression.callee.computed || !types.isExpression(expression.callee.object) || !types.isIdentifier(expression.callee.property)) {
|
|
325
|
+
return void 0;
|
|
326
|
+
}
|
|
327
|
+
return SAFE_COLLECTION_PIPELINE_METHODS.has(expression.callee.property.name) ? expression.callee.property.name : void 0;
|
|
328
|
+
}
|
|
329
|
+
function validateCollectionCallback(callback, method, safeGlobals) {
|
|
330
|
+
const expectedParameters = method === "filter" ? "one item and an optional index" : "two items";
|
|
331
|
+
const validParameterCount = method === "filter" ? callback.params.length >= 1 && callback.params.length <= 2 : callback.params.length === 2;
|
|
332
|
+
if (callback.async || callback.generator || !validParameterCount || callback.params.some((parameter) => !types.isIdentifier(parameter))) {
|
|
333
|
+
return `${method} callbacks must be synchronous and use ${expectedParameters}`;
|
|
334
|
+
}
|
|
335
|
+
if (containsDirectHookCall(callback)) {
|
|
336
|
+
return `Hooks cannot be called inside a ${method} callback`;
|
|
337
|
+
}
|
|
338
|
+
const value = returnedExpression(callback);
|
|
339
|
+
if (!value) return `${method} callbacks must return one expression`;
|
|
340
|
+
const unsupported = validateDerivedExpression(value, safeGlobals);
|
|
341
|
+
return unsupported ? `${method} callbacks cannot use ${unsupported}` : void 0;
|
|
342
|
+
}
|
|
343
|
+
function validateCollectionPipeline(expression, safeGlobals) {
|
|
344
|
+
const method = collectionPipelineMethod(expression);
|
|
345
|
+
if (!method || !types.isCallExpression(expression)) {
|
|
346
|
+
return validateDerivedExpression(expression, safeGlobals);
|
|
347
|
+
}
|
|
348
|
+
const source = expression.callee.object;
|
|
349
|
+
const sourceUnsupported = collectionPipelineMethod(source) ? validateCollectionPipeline(source, safeGlobals) : validateDerivedExpression(source, safeGlobals);
|
|
350
|
+
if (sourceUnsupported) return sourceUnsupported;
|
|
351
|
+
if (method === "filter") {
|
|
352
|
+
if (expression.arguments.length !== 1 || !types.isArrowFunctionExpression(expression.arguments[0]) && !types.isFunctionExpression(expression.arguments[0])) {
|
|
353
|
+
return "filter requires one inline callback";
|
|
354
|
+
}
|
|
355
|
+
return validateCollectionCallback(expression.arguments[0], "filter", safeGlobals);
|
|
356
|
+
}
|
|
357
|
+
if (method === "toSorted") {
|
|
358
|
+
if (expression.arguments.length === 0) return void 0;
|
|
359
|
+
if (expression.arguments.length !== 1 || !types.isArrowFunctionExpression(expression.arguments[0]) && !types.isFunctionExpression(expression.arguments[0])) {
|
|
360
|
+
return "toSorted requires an optional inline comparator";
|
|
361
|
+
}
|
|
362
|
+
return validateCollectionCallback(expression.arguments[0], "toSorted", safeGlobals);
|
|
363
|
+
}
|
|
364
|
+
const maximumArguments = method === "slice" ? 2 : 0;
|
|
365
|
+
if (expression.arguments.length > maximumArguments) {
|
|
366
|
+
return `${method} accepts at most ${maximumArguments} compiler-safe arguments`;
|
|
367
|
+
}
|
|
368
|
+
for (const argument of expression.arguments) {
|
|
369
|
+
if (!types.isExpression(argument)) return `${method} does not support spread arguments`;
|
|
370
|
+
const unsupported = validateDerivedExpression(argument, safeGlobals);
|
|
371
|
+
if (unsupported) return `${method} arguments cannot use ${unsupported}`;
|
|
372
|
+
}
|
|
373
|
+
return void 0;
|
|
374
|
+
}
|
|
375
|
+
function containsDirectHookCall(callback) {
|
|
376
|
+
let found = false;
|
|
377
|
+
traverse(expressionFile(types.cloneNode(callback, true)), {
|
|
378
|
+
CallExpression(path) {
|
|
379
|
+
const callee = path.node.callee;
|
|
380
|
+
const name = types.isIdentifier(callee) ? callee.name : types.isMemberExpression(callee) && !callee.computed && types.isIdentifier(callee.property) ? callee.property.name : "";
|
|
381
|
+
if (/^use[A-Z0-9]/.test(name)) {
|
|
382
|
+
found = true;
|
|
383
|
+
path.stop();
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
return found;
|
|
388
|
+
}
|
|
389
|
+
function jsxKeyExpression(element) {
|
|
390
|
+
for (const attribute of element.openingElement.attributes) {
|
|
391
|
+
if (!types.isJSXAttribute(attribute) || !types.isJSXIdentifier(attribute.name, { name: "key" })) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
if (types.isStringLiteral(attribute.value)) return types.stringLiteral(attribute.value.value);
|
|
395
|
+
if (types.isJSXExpressionContainer(attribute.value) && !types.isJSXEmptyExpression(attribute.value.expression))
|
|
396
|
+
return attribute.value.expression;
|
|
397
|
+
}
|
|
398
|
+
return void 0;
|
|
399
|
+
}
|
|
400
|
+
function validateKeyFunction(callback, keyExpression, safeGlobals) {
|
|
401
|
+
if (callback.async || callback.generator || callback.params.length < 1 || callback.params.length > 2 || !types.isIdentifier(callback.params[0]) || callback.params[1] !== void 0 && !types.isIdentifier(callback.params[1])) {
|
|
402
|
+
return "keyed list callbacks must be synchronous and use item and optional index identifiers";
|
|
403
|
+
}
|
|
404
|
+
const item = callback.params[0].name;
|
|
405
|
+
const index = types.isIdentifier(callback.params[1]) ? callback.params[1].name : void 0;
|
|
406
|
+
if (index && referencesIdentifier(keyExpression, index)) {
|
|
407
|
+
return "keyed list keys cannot depend on the array index";
|
|
408
|
+
}
|
|
409
|
+
if (!referencesIdentifier(keyExpression, item)) {
|
|
410
|
+
return "keyed list keys must depend on the mapped item";
|
|
411
|
+
}
|
|
412
|
+
const unsupported = validateDerivedExpression(keyExpression, safeGlobals);
|
|
413
|
+
return unsupported ? `keyed list keys cannot use ${unsupported}` : void 0;
|
|
414
|
+
}
|
|
415
|
+
function isPublicListElement(element, listNames) {
|
|
416
|
+
const name = element.openingElement.name;
|
|
417
|
+
return types.isJSXIdentifier(name) && listNames.has(name.name);
|
|
418
|
+
}
|
|
419
|
+
function meaningfulJsxChildren(element) {
|
|
420
|
+
return element.children.filter((child) => {
|
|
421
|
+
if (types.isJSXText(child)) return child.value.trim().length > 0;
|
|
422
|
+
return !(types.isJSXExpressionContainer(child) && types.isJSXEmptyExpression(child.expression));
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
function analyzeMapList(expression, statesByValue, safeGlobals) {
|
|
426
|
+
const callee = expression.callee;
|
|
427
|
+
const collection = callee.object;
|
|
428
|
+
const collectionUnsupported = validateCollectionPipeline(collection, safeGlobals);
|
|
429
|
+
if (collectionUnsupported) {
|
|
430
|
+
return { reason: `keyed list collection cannot use ${collectionUnsupported}` };
|
|
431
|
+
}
|
|
432
|
+
if (expression.arguments.length !== 1 || !types.isArrowFunctionExpression(expression.arguments[0]) && !types.isFunctionExpression(expression.arguments[0])) {
|
|
433
|
+
return { reason: "keyed list map must use one inline render callback" };
|
|
434
|
+
}
|
|
435
|
+
const callback = expression.arguments[0];
|
|
436
|
+
if (containsDirectHookCall(callback)) {
|
|
437
|
+
return { reason: "Hooks cannot be called directly inside a keyed list callback" };
|
|
438
|
+
}
|
|
439
|
+
const row = returnedExpression(callback);
|
|
440
|
+
if (!row || !types.isJSXElement(row)) {
|
|
441
|
+
return { reason: "keyed list map callbacks must return one React element" };
|
|
442
|
+
}
|
|
443
|
+
const key = jsxKeyExpression(row);
|
|
444
|
+
if (!key) return { reason: "automatically compiled lists require an explicit item key" };
|
|
445
|
+
const keyReason = validateKeyFunction(callback, key, safeGlobals);
|
|
446
|
+
if (keyReason) return { reason: keyReason };
|
|
447
|
+
return { dependencies: collectStateDependencies(expression, statesByValue) };
|
|
448
|
+
}
|
|
449
|
+
function listAttributeExpression(element, name) {
|
|
450
|
+
for (const attribute of element.openingElement.attributes) {
|
|
451
|
+
if (types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name, { name }) && types.isJSXExpressionContainer(attribute.value) && !types.isJSXEmptyExpression(attribute.value.expression)) {
|
|
452
|
+
return attribute.value.expression;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return void 0;
|
|
456
|
+
}
|
|
457
|
+
function analyzePublicList(element, statesByValue, safeGlobals) {
|
|
458
|
+
if (element.openingElement.attributes.some((attribute) => types.isJSXSpreadAttribute(attribute))) {
|
|
459
|
+
return { reason: "compiled List boundaries do not support JSX attribute spreads" };
|
|
460
|
+
}
|
|
461
|
+
const each = listAttributeExpression(element, "each");
|
|
462
|
+
const by = listAttributeExpression(element, "by");
|
|
463
|
+
if (!each || !by) return { reason: "List requires explicit each and by properties" };
|
|
464
|
+
const collectionUnsupported = validateCollectionPipeline(each, safeGlobals);
|
|
465
|
+
if (collectionUnsupported) {
|
|
466
|
+
return { reason: `List each cannot use ${collectionUnsupported}` };
|
|
467
|
+
}
|
|
468
|
+
if (!types.isArrowFunctionExpression(by) && !types.isFunctionExpression(by)) {
|
|
469
|
+
return { reason: "compiled List by must use an inline key function" };
|
|
470
|
+
}
|
|
471
|
+
const key = returnedExpression(by);
|
|
472
|
+
if (!key) return { reason: "compiled List by must return one key expression" };
|
|
473
|
+
const keyReason = validateKeyFunction(by, key, safeGlobals);
|
|
474
|
+
if (keyReason) return { reason: keyReason };
|
|
475
|
+
const children = meaningfulJsxChildren(element);
|
|
476
|
+
if (children.length !== 1 || !types.isJSXExpressionContainer(children[0]) || types.isJSXEmptyExpression(children[0].expression) || !types.isArrowFunctionExpression(children[0].expression) && !types.isFunctionExpression(children[0].expression)) {
|
|
477
|
+
return { reason: "compiled List children must use one inline render function" };
|
|
478
|
+
}
|
|
479
|
+
const render = children[0].expression;
|
|
480
|
+
if (!returnedExpression(render)) {
|
|
481
|
+
return { reason: "compiled List children must return one React element" };
|
|
482
|
+
}
|
|
483
|
+
if (containsDirectHookCall(render)) {
|
|
484
|
+
return { reason: "Hooks cannot be called directly inside List children" };
|
|
485
|
+
}
|
|
486
|
+
return { dependencies: collectStateDependencies(element, statesByValue) };
|
|
487
|
+
}
|
|
488
|
+
function validateKeyedRowEventHandler(handler) {
|
|
489
|
+
let reason;
|
|
490
|
+
traverse(expressionFile(types.cloneNode(handler, true)), {
|
|
491
|
+
MetaProperty(path) {
|
|
492
|
+
reason = "meta properties";
|
|
493
|
+
path.stop();
|
|
494
|
+
},
|
|
495
|
+
ReferencedIdentifier(path) {
|
|
496
|
+
if (path.node.name !== "arguments") return;
|
|
497
|
+
reason = "arguments access";
|
|
498
|
+
path.stop();
|
|
499
|
+
},
|
|
500
|
+
Super(path) {
|
|
501
|
+
reason = "super access";
|
|
502
|
+
path.stop();
|
|
503
|
+
},
|
|
504
|
+
ThisExpression(path) {
|
|
505
|
+
reason = "this access";
|
|
506
|
+
path.stop();
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
return reason;
|
|
510
|
+
}
|
|
511
|
+
function stabilizeDeferredEventCurrentTarget(handler, compilerSetters) {
|
|
512
|
+
const cloned = types.cloneNode(handler, true);
|
|
513
|
+
const eventParameter = cloned.params[0];
|
|
514
|
+
if (!types.isIdentifier(eventParameter) || compilerSetters.size === 0) return cloned;
|
|
515
|
+
const file = expressionFile(cloned);
|
|
516
|
+
const expression = file.program.body[0].expression;
|
|
517
|
+
if (!types.isArrowFunctionExpression(expression) && !types.isFunctionExpression(expression)) {
|
|
518
|
+
return cloned;
|
|
519
|
+
}
|
|
520
|
+
const rootBindingIdentifier = expression.params[0];
|
|
521
|
+
const snapshots = [];
|
|
522
|
+
const snapshotsByProperty = /* @__PURE__ */ new Map();
|
|
523
|
+
const replaceCurrentTarget = (path) => {
|
|
524
|
+
const property = path.node.property;
|
|
525
|
+
const isCurrentTarget = path.node.computed ? types.isStringLiteral(property, { value: "currentTarget" }) : types.isIdentifier(property, { name: "currentTarget" });
|
|
526
|
+
if (!isCurrentTarget || !types.isIdentifier(path.node.object, { name: eventParameter.name }))
|
|
527
|
+
return;
|
|
528
|
+
const binding = path.scope.getBinding(eventParameter.name);
|
|
529
|
+
if (!binding || binding.identifier !== rootBindingIdentifier) return;
|
|
530
|
+
const setterCall = path.findParent(
|
|
531
|
+
(parent2) => parent2.isCallExpression() && types.isIdentifier(parent2.node.callee) && compilerSetters.has(parent2.node.callee.name)
|
|
532
|
+
);
|
|
533
|
+
if (!setterCall) return;
|
|
534
|
+
const deferredFunction = path.findParent(
|
|
535
|
+
(parent2) => parent2.isFunction() && parent2.node !== expression
|
|
536
|
+
);
|
|
537
|
+
if (!deferredFunction || !deferredFunction.findParent((parent2) => parent2 === setterCall))
|
|
538
|
+
return;
|
|
539
|
+
const parent = path.parentPath;
|
|
540
|
+
const propertyRead = (parent?.isMemberExpression() || parent?.isOptionalMemberExpression()) && parent.node.object === path.node ? parent : path;
|
|
541
|
+
const snapshotProperty = propertyRead === path ? "element" : propertyRead.node.computed ? types.isStringLiteral(propertyRead.node.property) ? propertyRead.node.property.value : `computed${snapshots.length}` : types.isIdentifier(propertyRead.node.property) ? propertyRead.node.property.name : `property${snapshots.length}`;
|
|
542
|
+
let snapshot = snapshotsByProperty.get(snapshotProperty);
|
|
543
|
+
if (!snapshot) {
|
|
544
|
+
const suffix = snapshotProperty.replace(/[^A-Za-z0-9_$]/g, " ").replace(
|
|
545
|
+
/(?:^|\s+)([A-Za-z0-9_$])/g,
|
|
546
|
+
(_match, character) => character.toUpperCase()
|
|
547
|
+
).replace(/\s/g, "");
|
|
548
|
+
snapshot = uniqueLocalIdentifier(`_farmCurrentTarget${suffix}`, [
|
|
549
|
+
expression,
|
|
550
|
+
...snapshots.map((entry) => entry.identifier)
|
|
551
|
+
]);
|
|
552
|
+
snapshotsByProperty.set(snapshotProperty, snapshot);
|
|
553
|
+
snapshots.push({
|
|
554
|
+
identifier: snapshot,
|
|
555
|
+
value: types.cloneNode(propertyRead.node, true)
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
propertyRead.replaceWith(types.cloneNode(snapshot));
|
|
559
|
+
propertyRead.skip();
|
|
560
|
+
};
|
|
561
|
+
traverse(file, {
|
|
562
|
+
MemberExpression: replaceCurrentTarget,
|
|
563
|
+
OptionalMemberExpression: replaceCurrentTarget
|
|
564
|
+
});
|
|
565
|
+
if (snapshots.length === 0) return expression;
|
|
566
|
+
const declarations = snapshots.map(
|
|
567
|
+
({ identifier, value }) => types.variableDeclaration("const", [types.variableDeclarator(types.cloneNode(identifier), value)])
|
|
568
|
+
);
|
|
569
|
+
if (types.isBlockStatement(expression.body)) {
|
|
570
|
+
expression.body.body.unshift(...declarations);
|
|
571
|
+
} else {
|
|
572
|
+
expression.body = types.blockStatement([...declarations, types.returnStatement(expression.body)]);
|
|
573
|
+
}
|
|
574
|
+
return expression;
|
|
575
|
+
}
|
|
576
|
+
function stabilizeDeferredEventCurrentTargets(root, compilerSetters) {
|
|
577
|
+
const file = expressionFile(types.cloneNode(root, true));
|
|
578
|
+
traverse(file, {
|
|
579
|
+
JSXAttribute(path) {
|
|
580
|
+
if (!types.isJSXIdentifier(path.node.name) || !/^on[A-Z]/.test(path.node.name.name)) return;
|
|
581
|
+
const value = path.node.value;
|
|
582
|
+
if (!types.isJSXExpressionContainer(value) || !types.isArrowFunctionExpression(value.expression) && !types.isFunctionExpression(value.expression)) {
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
value.expression = stabilizeDeferredEventCurrentTarget(value.expression, compilerSetters);
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
const expression = file.program.body[0].expression;
|
|
589
|
+
return types.isJSXElement(expression) ? expression : types.cloneNode(root, true);
|
|
590
|
+
}
|
|
591
|
+
function analyzeKeyedRowTree(row, renderCallback, safeGlobals, compilerSetters) {
|
|
592
|
+
if (renderCallback.async || renderCallback.generator || renderCallback.params.length < 1 || renderCallback.params.length > 2 || !types.isIdentifier(renderCallback.params[0]) || renderCallback.params[1] !== void 0 && !types.isIdentifier(renderCallback.params[1])) {
|
|
593
|
+
return { reason: "compiled keyed rows require item and optional index identifiers" };
|
|
594
|
+
}
|
|
595
|
+
const bindings = [];
|
|
596
|
+
const events = [];
|
|
597
|
+
const conditionals = [];
|
|
598
|
+
const visit = (element, path) => {
|
|
599
|
+
const tag = element.openingElement.name;
|
|
600
|
+
if (!types.isJSXIdentifier(tag) || !/^[a-z]/.test(tag.name)) {
|
|
601
|
+
return "compiled keyed rows support host elements only";
|
|
602
|
+
}
|
|
603
|
+
if (tag.name === "svg") return "compiled keyed rows do not support SVG yet";
|
|
604
|
+
const attributeNames = new Set(
|
|
605
|
+
element.openingElement.attributes.filter((attribute) => types.isJSXAttribute(attribute)).map(jsxAttributeName).filter((name) => name !== void 0)
|
|
606
|
+
);
|
|
607
|
+
const controlled = attributeNames.has("value") || attributeNames.has("checked");
|
|
608
|
+
if (attributeNames.has("contentEditable")) {
|
|
609
|
+
return "content-editable keyed rows require React ownership";
|
|
610
|
+
}
|
|
611
|
+
if (tag.name === "input" && controlled) {
|
|
612
|
+
const type = jsxAttribute(element, "type");
|
|
613
|
+
const staticType = staticJsxAttributeString(type);
|
|
614
|
+
if (type && staticType === void 0) {
|
|
615
|
+
return "controlled keyed row inputs require a static type";
|
|
616
|
+
}
|
|
617
|
+
if (attributeNames.has("value") && staticType?.toLowerCase() === "file") {
|
|
618
|
+
return "file inputs require React ownership";
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (tag.name === "textarea" && controlled && meaningfulJsxChildren(element).length > 0) {
|
|
622
|
+
return "controlled keyed row textareas cannot also use children";
|
|
623
|
+
}
|
|
624
|
+
if (tag.name === "select" && controlled && controlledSelectHasDynamicOptions(element)) {
|
|
625
|
+
return "controlled keyed row selects require static options";
|
|
626
|
+
}
|
|
627
|
+
for (const attribute of element.openingElement.attributes) {
|
|
628
|
+
if (types.isJSXSpreadAttribute(attribute)) {
|
|
629
|
+
return "compiled keyed rows do not support JSX attribute spreads";
|
|
630
|
+
}
|
|
631
|
+
const name = jsxAttributeName(attribute);
|
|
632
|
+
if (!name) return "compiled keyed rows do not support namespaced JSX attributes";
|
|
633
|
+
if (name === "ref" || name === "dangerouslySetInnerHTML") {
|
|
634
|
+
return `compiled keyed row ${name} requires React ownership`;
|
|
635
|
+
}
|
|
636
|
+
if (name === "key") continue;
|
|
637
|
+
if (/^on[A-Z]/.test(name)) {
|
|
638
|
+
if (!types.isJSXExpressionContainer(attribute.value) || !types.isArrowFunctionExpression(attribute.value.expression) && !types.isFunctionExpression(attribute.value.expression)) {
|
|
639
|
+
return "interactive compiled keyed rows require inline synchronous event handlers";
|
|
640
|
+
}
|
|
641
|
+
const handler = attribute.value.expression;
|
|
642
|
+
if (handler.async || handler.generator) {
|
|
643
|
+
return "interactive compiled keyed row handlers must be synchronous";
|
|
644
|
+
}
|
|
645
|
+
if (containsDirectHookCall(handler)) {
|
|
646
|
+
return "Hooks cannot be called inside compiled keyed row handlers";
|
|
647
|
+
}
|
|
648
|
+
const unsupported2 = validateKeyedRowEventHandler(handler);
|
|
649
|
+
if (unsupported2) {
|
|
650
|
+
return `interactive compiled keyed row handlers cannot use ${unsupported2}`;
|
|
651
|
+
}
|
|
652
|
+
events.push({
|
|
653
|
+
id: events.length,
|
|
654
|
+
path: [...path],
|
|
655
|
+
name,
|
|
656
|
+
value: stabilizeDeferredEventCurrentTarget(handler, compilerSetters)
|
|
657
|
+
});
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
if (!types.isJSXExpressionContainer(attribute.value) || types.isJSXEmptyExpression(attribute.value.expression)) {
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
const expression = attribute.value.expression;
|
|
664
|
+
if (name === "style") {
|
|
665
|
+
if (!types.isObjectExpression(expression)) {
|
|
666
|
+
return "compiled keyed row styles must use one inline object literal";
|
|
667
|
+
}
|
|
668
|
+
for (const property of expression.properties) {
|
|
669
|
+
if (!types.isObjectProperty(property) || property.computed || !types.isExpression(property.value)) {
|
|
670
|
+
return "compiled keyed row styles do not support spreads, methods, or computed properties";
|
|
671
|
+
}
|
|
672
|
+
const propertyName = types.isIdentifier(property.key) ? property.key.name : types.isStringLiteral(property.key) ? property.key.value : void 0;
|
|
673
|
+
if (!propertyName || propertyName.includes("-") && !propertyName.startsWith("--")) {
|
|
674
|
+
return "compiled keyed row style names must use camelCase or a CSS custom property";
|
|
675
|
+
}
|
|
676
|
+
const unsupported2 = validateDerivedExpression(property.value, safeGlobals);
|
|
677
|
+
if (unsupported2) {
|
|
678
|
+
return `compiled keyed row style ${propertyName} cannot use ${unsupported2}`;
|
|
679
|
+
}
|
|
680
|
+
bindings.push({
|
|
681
|
+
kind: "style",
|
|
682
|
+
path: [...path],
|
|
683
|
+
name: propertyName,
|
|
684
|
+
value: cloneExpression(property.value)
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
if (name === "children") return "compiled keyed row children props are not supported yet";
|
|
690
|
+
const unsupported = validateDerivedExpression(expression, safeGlobals);
|
|
691
|
+
if (unsupported) return `compiled keyed row ${name} cannot use ${unsupported}`;
|
|
692
|
+
bindings.push({
|
|
693
|
+
kind: "attribute",
|
|
694
|
+
path: [...path],
|
|
695
|
+
name,
|
|
696
|
+
value: cloneExpression(expression)
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
const meaningfulChildren = meaningfulJsxChildren(element);
|
|
700
|
+
const conditionalChild = meaningfulChildren.find(
|
|
701
|
+
(child) => types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression) && conditionalBlockShape(child.expression) !== null
|
|
702
|
+
);
|
|
703
|
+
if (conditionalChild && !types.isJSXEmptyExpression(conditionalChild.expression)) {
|
|
704
|
+
if (meaningfulChildren.length !== 1) {
|
|
705
|
+
return "compiled keyed row conditionals must be the only child of a host container";
|
|
706
|
+
}
|
|
707
|
+
const shape = conditionalBlockShape(conditionalChild.expression);
|
|
708
|
+
if (!shape) return "compiled keyed row conditional shape is not supported";
|
|
709
|
+
const testUnsupported = validateDerivedExpression(shape.test, safeGlobals);
|
|
710
|
+
if (testUnsupported) {
|
|
711
|
+
return `compiled keyed row conditional test cannot use ${testUnsupported}`;
|
|
712
|
+
}
|
|
713
|
+
const truthyAnalysis = shape.truthy ? analyzeHostConditionalTree(shape.truthy, safeGlobals) : { bindings: [] };
|
|
714
|
+
const falsyAnalysis = shape.falsy ? analyzeHostConditionalTree(shape.falsy, safeGlobals) : { bindings: [] };
|
|
715
|
+
if (truthyAnalysis.reason || falsyAnalysis.reason) {
|
|
716
|
+
return truthyAnalysis.reason || falsyAnalysis.reason || "compiled keyed row conditional branch is not supported";
|
|
717
|
+
}
|
|
718
|
+
conditionals.push({
|
|
719
|
+
id: conditionals.length,
|
|
720
|
+
path: [...path],
|
|
721
|
+
test: cloneExpression(shape.test),
|
|
722
|
+
logical: shape.logical,
|
|
723
|
+
truthy: shape.truthy ? types.cloneNode(shape.truthy, true) : void 0,
|
|
724
|
+
falsy: shape.falsy ? types.cloneNode(shape.falsy, true) : void 0,
|
|
725
|
+
truthyBindings: truthyAnalysis.bindings || [],
|
|
726
|
+
falsyBindings: falsyAnalysis.bindings || []
|
|
727
|
+
});
|
|
728
|
+
return void 0;
|
|
729
|
+
}
|
|
730
|
+
const nestedElements = element.children.filter(
|
|
731
|
+
(child) => types.isJSXElement(child)
|
|
732
|
+
);
|
|
733
|
+
if (element.children.some((child) => types.isJSXFragment(child))) {
|
|
734
|
+
return "compiled keyed rows do not support fragments yet";
|
|
735
|
+
}
|
|
736
|
+
const expressions = element.children.filter(
|
|
737
|
+
(child) => types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression)
|
|
738
|
+
);
|
|
739
|
+
for (const child of expressions) {
|
|
740
|
+
const unsupported = validateDerivedExpression(child.expression, safeGlobals);
|
|
741
|
+
if (unsupported) return `compiled keyed row text cannot use ${unsupported}`;
|
|
742
|
+
}
|
|
743
|
+
if (nestedElements.length > 0 && expressions.length > 0) {
|
|
744
|
+
return "compiled keyed rows do not support dynamic text beside nested elements yet";
|
|
745
|
+
}
|
|
746
|
+
if (nestedElements.length === 0 && expressions.length > 0) {
|
|
747
|
+
const parts = [];
|
|
748
|
+
for (const child of element.children) {
|
|
749
|
+
if (types.isJSXText(child)) {
|
|
750
|
+
const text = cleanJsxText(child.value);
|
|
751
|
+
if (text) parts.push(types.stringLiteral(text));
|
|
752
|
+
} else if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression)) {
|
|
753
|
+
parts.push(cloneExpression(child.expression));
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
bindings.push({ kind: "text", path: [...path], value: types.arrayExpression(parts) });
|
|
757
|
+
}
|
|
758
|
+
let elementIndex = 0;
|
|
759
|
+
for (const child of element.children) {
|
|
760
|
+
if (!types.isJSXElement(child)) continue;
|
|
761
|
+
const reason2 = visit(child, [...path, elementIndex]);
|
|
762
|
+
if (reason2) return reason2;
|
|
763
|
+
elementIndex += 1;
|
|
764
|
+
}
|
|
765
|
+
return void 0;
|
|
766
|
+
};
|
|
767
|
+
const reason = visit(row, []);
|
|
768
|
+
return reason ? { reason } : { bindings, events, conditionals };
|
|
769
|
+
}
|
|
770
|
+
function analyzeKeyedRowChild(child, statesByValue, safeGlobals, listNames) {
|
|
771
|
+
let collection;
|
|
772
|
+
let keyCallback;
|
|
773
|
+
let renderCallback;
|
|
774
|
+
let row;
|
|
775
|
+
let dependencies;
|
|
776
|
+
let syntax;
|
|
777
|
+
let source;
|
|
778
|
+
if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression) && isMapCall(child.expression)) {
|
|
779
|
+
const result = analyzeMapList(child.expression, statesByValue, safeGlobals);
|
|
780
|
+
if (result.reason) return void 0;
|
|
781
|
+
const callback = child.expression.arguments[0];
|
|
782
|
+
if (!types.isArrowFunctionExpression(callback) && !types.isFunctionExpression(callback))
|
|
783
|
+
return void 0;
|
|
784
|
+
const returned = returnedExpression(callback);
|
|
785
|
+
if (!returned || !types.isJSXElement(returned)) return void 0;
|
|
786
|
+
const key = jsxKeyExpression(returned);
|
|
787
|
+
if (!key) return void 0;
|
|
788
|
+
collection = child.expression.callee.object;
|
|
789
|
+
keyCallback = types.arrowFunctionExpression(
|
|
790
|
+
callback.params.map((parameter) => types.cloneNode(parameter, true)),
|
|
791
|
+
cloneExpression(key)
|
|
792
|
+
);
|
|
793
|
+
renderCallback = callback;
|
|
794
|
+
row = returned;
|
|
795
|
+
dependencies = result.dependencies || [];
|
|
796
|
+
syntax = "map";
|
|
797
|
+
source = child.expression;
|
|
798
|
+
} else if (types.isJSXElement(child) && isPublicListElement(child, listNames)) {
|
|
799
|
+
const result = analyzePublicList(child, statesByValue, safeGlobals);
|
|
800
|
+
if (result.reason) return void 0;
|
|
801
|
+
const each = listAttributeExpression(child, "each");
|
|
802
|
+
const by = listAttributeExpression(child, "by");
|
|
803
|
+
const renderChild = meaningfulJsxChildren(child)[0];
|
|
804
|
+
if (!each || !types.isArrowFunctionExpression(by) && !types.isFunctionExpression(by) || !types.isJSXExpressionContainer(renderChild) || types.isJSXEmptyExpression(renderChild.expression) || !types.isArrowFunctionExpression(renderChild.expression) && !types.isFunctionExpression(renderChild.expression)) {
|
|
805
|
+
return void 0;
|
|
806
|
+
}
|
|
807
|
+
const returned = returnedExpression(renderChild.expression);
|
|
808
|
+
if (!returned || !types.isJSXElement(returned)) return void 0;
|
|
809
|
+
collection = each;
|
|
810
|
+
keyCallback = by;
|
|
811
|
+
renderCallback = renderChild.expression;
|
|
812
|
+
row = returned;
|
|
813
|
+
dependencies = result.dependencies || [];
|
|
814
|
+
syntax = "list";
|
|
815
|
+
source = child;
|
|
816
|
+
} else {
|
|
817
|
+
return void 0;
|
|
818
|
+
}
|
|
819
|
+
const rowAnalysis = analyzeKeyedRowTree(
|
|
820
|
+
row,
|
|
821
|
+
renderCallback,
|
|
822
|
+
safeGlobals,
|
|
823
|
+
new Set([...statesByValue.values()].map((state) => state.setterName))
|
|
824
|
+
);
|
|
825
|
+
if (rowAnalysis.reason) return void 0;
|
|
826
|
+
return {
|
|
827
|
+
source,
|
|
828
|
+
collection,
|
|
829
|
+
keyCallback,
|
|
830
|
+
renderCallback,
|
|
831
|
+
row,
|
|
832
|
+
dependencies,
|
|
833
|
+
bindings: rowAnalysis.bindings || [],
|
|
834
|
+
events: rowAnalysis.events || [],
|
|
835
|
+
conditionals: rowAnalysis.conditionals || [],
|
|
836
|
+
syntax
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
function analyzeKeyedRowsContainer(container, statesByValue, safeGlobals, listNames) {
|
|
840
|
+
if (container.openingElement.attributes.some(
|
|
841
|
+
(attribute) => types.isJSXSpreadAttribute(attribute) || types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && (attribute.name.name === "ref" || attribute.name.name === "dangerouslySetInnerHTML")
|
|
842
|
+
)) {
|
|
843
|
+
return void 0;
|
|
844
|
+
}
|
|
845
|
+
for (const attribute of container.openingElement.attributes) {
|
|
846
|
+
if (types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && types.isJSXExpressionContainer(attribute.value) && !types.isJSXEmptyExpression(attribute.value.expression) && !/^on[A-Z]/.test(attribute.name.name) && collectStateDependencies(attribute.value.expression, statesByValue).length > 0) {
|
|
847
|
+
return void 0;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
const children = meaningfulJsxChildren(container);
|
|
851
|
+
if (children.length !== 1) return void 0;
|
|
852
|
+
return analyzeKeyedRowChild(children[0], statesByValue, safeGlobals, listNames);
|
|
853
|
+
}
|
|
854
|
+
function isStaticKeyedRangeSibling(element, statesByValue, safeGlobals) {
|
|
855
|
+
for (const child of meaningfulJsxChildren(element)) {
|
|
856
|
+
if (types.isJSXFragment(child)) return false;
|
|
857
|
+
if (types.isJSXElement(child)) {
|
|
858
|
+
if (!isHostElement(child) || !isStaticKeyedRangeSibling(child, statesByValue, safeGlobals)) {
|
|
859
|
+
return false;
|
|
860
|
+
}
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression) && !isTextExpression(child.expression, statesByValue, safeGlobals)) {
|
|
864
|
+
return false;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
return true;
|
|
868
|
+
}
|
|
869
|
+
function analyzeKeyedRangesContainer(container, statesByValue, safeGlobals, listNames, allowSingleRange = false) {
|
|
870
|
+
if (container.openingElement.attributes.some(
|
|
871
|
+
(attribute) => types.isJSXSpreadAttribute(attribute) || types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && (attribute.name.name === "ref" || attribute.name.name === "dangerouslySetInnerHTML")
|
|
872
|
+
)) {
|
|
873
|
+
return void 0;
|
|
874
|
+
}
|
|
875
|
+
const children = meaningfulJsxChildren(container);
|
|
876
|
+
if (children.length < (allowSingleRange ? 1 : 2)) return void 0;
|
|
877
|
+
const ranges = [];
|
|
878
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
879
|
+
let staticBefore = 0;
|
|
880
|
+
for (const child of children) {
|
|
881
|
+
const range = analyzeKeyedRowChild(child, statesByValue, safeGlobals, listNames);
|
|
882
|
+
if (range) {
|
|
883
|
+
if (range.events.length > 0 || range.conditionals.length > 0) return void 0;
|
|
884
|
+
ranges.push({
|
|
885
|
+
before: staticBefore,
|
|
886
|
+
source: range.source,
|
|
887
|
+
collection: range.collection,
|
|
888
|
+
keyCallback: range.keyCallback,
|
|
889
|
+
renderCallback: range.renderCallback,
|
|
890
|
+
row: range.row,
|
|
891
|
+
bindings: range.bindings,
|
|
892
|
+
syntax: range.syntax
|
|
893
|
+
});
|
|
894
|
+
for (const dependency of range.dependencies) dependencies.add(dependency);
|
|
895
|
+
staticBefore = 0;
|
|
896
|
+
continue;
|
|
897
|
+
}
|
|
898
|
+
if (!types.isJSXElement(child) || !isHostElement(child)) return void 0;
|
|
899
|
+
if (!isStaticKeyedRangeSibling(child, statesByValue, safeGlobals)) return void 0;
|
|
900
|
+
staticBefore += 1;
|
|
901
|
+
}
|
|
902
|
+
if (ranges.length === 0) return void 0;
|
|
903
|
+
return {
|
|
904
|
+
ranges,
|
|
905
|
+
trailing: staticBefore,
|
|
906
|
+
dependencies: [...dependencies].sort((left, right) => left - right)
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
function analyzeHostConditionalTree(branch, safeGlobals) {
|
|
910
|
+
const bindings = [];
|
|
911
|
+
const visit = (element, path) => {
|
|
912
|
+
const tag = element.openingElement.name;
|
|
913
|
+
if (!types.isJSXIdentifier(tag) || !/^[a-z]/.test(tag.name)) {
|
|
914
|
+
return "compiler-owned conditionals support host elements only";
|
|
915
|
+
}
|
|
916
|
+
if (tag.name === "svg") return "compiler-owned conditionals do not support SVG yet";
|
|
917
|
+
for (const attribute of element.openingElement.attributes) {
|
|
918
|
+
if (types.isJSXSpreadAttribute(attribute)) {
|
|
919
|
+
return "compiler-owned conditionals do not support JSX attribute spreads";
|
|
920
|
+
}
|
|
921
|
+
const name = jsxAttributeName(attribute);
|
|
922
|
+
if (!name) return "compiler-owned conditionals do not support namespaced JSX attributes";
|
|
923
|
+
if (name === "ref" || name === "dangerouslySetInnerHTML" || name === "key") {
|
|
924
|
+
return `compiler-owned conditional ${name} requires React ownership`;
|
|
925
|
+
}
|
|
926
|
+
if (/^on[A-Z]/.test(name)) {
|
|
927
|
+
return "compiler-owned conditional events require React ownership";
|
|
928
|
+
}
|
|
929
|
+
if (!types.isJSXExpressionContainer(attribute.value) || types.isJSXEmptyExpression(attribute.value.expression)) {
|
|
930
|
+
continue;
|
|
931
|
+
}
|
|
932
|
+
const expression = attribute.value.expression;
|
|
933
|
+
if (name === "style") {
|
|
934
|
+
if (!types.isObjectExpression(expression)) {
|
|
935
|
+
return "compiler-owned conditional styles must use one inline object literal";
|
|
936
|
+
}
|
|
937
|
+
for (const property of expression.properties) {
|
|
938
|
+
if (!types.isObjectProperty(property) || property.computed || !types.isExpression(property.value)) {
|
|
939
|
+
return "compiler-owned conditional styles do not support spreads, methods, or computed properties";
|
|
940
|
+
}
|
|
941
|
+
const propertyName = types.isIdentifier(property.key) ? property.key.name : types.isStringLiteral(property.key) ? property.key.value : void 0;
|
|
942
|
+
if (!propertyName || propertyName.includes("-") && !propertyName.startsWith("--")) {
|
|
943
|
+
return "compiler-owned conditional style names must use camelCase or a CSS custom property";
|
|
944
|
+
}
|
|
945
|
+
const unsupported2 = validateDerivedExpression(property.value, safeGlobals);
|
|
946
|
+
if (unsupported2) {
|
|
947
|
+
return `compiler-owned conditional style ${propertyName} cannot use ${unsupported2}`;
|
|
948
|
+
}
|
|
949
|
+
bindings.push({
|
|
950
|
+
kind: "style",
|
|
951
|
+
path: [...path],
|
|
952
|
+
name: propertyName,
|
|
953
|
+
value: cloneExpression(property.value)
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
continue;
|
|
957
|
+
}
|
|
958
|
+
if (name === "children") {
|
|
959
|
+
return "compiler-owned conditional children props are not supported yet";
|
|
960
|
+
}
|
|
961
|
+
const unsupported = validateDerivedExpression(expression, safeGlobals);
|
|
962
|
+
if (unsupported) return `compiler-owned conditional ${name} cannot use ${unsupported}`;
|
|
963
|
+
bindings.push({
|
|
964
|
+
kind: "attribute",
|
|
965
|
+
path: [...path],
|
|
966
|
+
name,
|
|
967
|
+
value: cloneExpression(expression)
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
const nestedElements = element.children.filter(
|
|
971
|
+
(child) => types.isJSXElement(child)
|
|
972
|
+
);
|
|
973
|
+
if (element.children.some((child) => types.isJSXFragment(child))) {
|
|
974
|
+
return "compiler-owned conditionals do not support fragments yet";
|
|
975
|
+
}
|
|
976
|
+
const expressions = element.children.filter(
|
|
977
|
+
(child) => types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression)
|
|
978
|
+
);
|
|
979
|
+
for (const child of expressions) {
|
|
980
|
+
const unsupported = validateDerivedExpression(child.expression, safeGlobals);
|
|
981
|
+
if (unsupported) return `compiler-owned conditional text cannot use ${unsupported}`;
|
|
982
|
+
}
|
|
983
|
+
if (nestedElements.length > 0 && expressions.length > 0) {
|
|
984
|
+
return "compiler-owned conditionals do not support dynamic text beside nested elements yet";
|
|
985
|
+
}
|
|
986
|
+
if (nestedElements.length === 0 && expressions.length > 0) {
|
|
987
|
+
const parts = [];
|
|
988
|
+
for (const child of element.children) {
|
|
989
|
+
if (types.isJSXText(child)) {
|
|
990
|
+
const text = cleanJsxText(child.value);
|
|
991
|
+
if (text) parts.push(types.stringLiteral(text));
|
|
992
|
+
} else if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression)) {
|
|
993
|
+
parts.push(cloneExpression(child.expression));
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
bindings.push({ kind: "text", path: [...path], value: types.arrayExpression(parts) });
|
|
997
|
+
}
|
|
998
|
+
let elementIndex = 0;
|
|
999
|
+
for (const child of element.children) {
|
|
1000
|
+
if (!types.isJSXElement(child)) continue;
|
|
1001
|
+
const reason2 = visit(child, [...path, elementIndex]);
|
|
1002
|
+
if (reason2) return reason2;
|
|
1003
|
+
elementIndex += 1;
|
|
1004
|
+
}
|
|
1005
|
+
return void 0;
|
|
1006
|
+
};
|
|
1007
|
+
const reason = visit(branch, []);
|
|
1008
|
+
return reason ? { reason } : { bindings };
|
|
1009
|
+
}
|
|
1010
|
+
function analyzeHostConditionalContainer(container, statesByValue, safeGlobals) {
|
|
1011
|
+
const containerName = container.openingElement.name;
|
|
1012
|
+
if (!types.isJSXIdentifier(containerName) || !/^[a-z]/.test(containerName.name) || containerName.name === "svg") {
|
|
1013
|
+
return void 0;
|
|
1014
|
+
}
|
|
1015
|
+
if (container.openingElement.attributes.some(
|
|
1016
|
+
(attribute) => types.isJSXSpreadAttribute(attribute) || types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && (attribute.name.name === "ref" || attribute.name.name === "dangerouslySetInnerHTML")
|
|
1017
|
+
)) {
|
|
1018
|
+
return void 0;
|
|
1019
|
+
}
|
|
1020
|
+
for (const attribute of container.openingElement.attributes) {
|
|
1021
|
+
if (!types.isJSXAttribute(attribute) || !types.isJSXIdentifier(attribute.name)) return void 0;
|
|
1022
|
+
if (/^on[A-Z]/.test(attribute.name.name) || attribute.name.name === "key") return void 0;
|
|
1023
|
+
if (types.isJSXExpressionContainer(attribute.value) && !types.isJSXEmptyExpression(attribute.value.expression))
|
|
1024
|
+
return void 0;
|
|
1025
|
+
}
|
|
1026
|
+
const children = meaningfulJsxChildren(container);
|
|
1027
|
+
if (children.length !== 1 || !types.isJSXExpressionContainer(children[0]) || types.isJSXEmptyExpression(children[0].expression)) {
|
|
1028
|
+
return void 0;
|
|
1029
|
+
}
|
|
1030
|
+
const shape = conditionalBlockShape(children[0].expression);
|
|
1031
|
+
if (!shape || validateDerivedExpression(shape.test, safeGlobals)) return void 0;
|
|
1032
|
+
const truthyAnalysis = shape.truthy ? analyzeHostConditionalTree(shape.truthy, safeGlobals) : { bindings: [] };
|
|
1033
|
+
const falsyAnalysis = shape.falsy ? analyzeHostConditionalTree(shape.falsy, safeGlobals) : { bindings: [] };
|
|
1034
|
+
if (truthyAnalysis.reason || falsyAnalysis.reason) return void 0;
|
|
1035
|
+
const dependencies = new Set(collectStateDependencies(shape.test, statesByValue));
|
|
1036
|
+
for (const binding of [...truthyAnalysis.bindings || [], ...falsyAnalysis.bindings || []]) {
|
|
1037
|
+
for (const dependency of collectStateDependencies(binding.value, statesByValue)) {
|
|
1038
|
+
dependencies.add(dependency);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
return {
|
|
1042
|
+
dependencies: [...dependencies].sort((left, right) => left - right),
|
|
1043
|
+
test: cloneExpression(shape.test),
|
|
1044
|
+
logical: shape.logical,
|
|
1045
|
+
truthy: shape.truthy,
|
|
1046
|
+
falsy: shape.falsy,
|
|
1047
|
+
truthyBindings: truthyAnalysis.bindings || [],
|
|
1048
|
+
falsyBindings: falsyAnalysis.bindings || []
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
function keyedListBoundary(blockRuntime, plan, source) {
|
|
1052
|
+
const name = types.jsxMemberExpression(
|
|
1053
|
+
types.jsxIdentifier(blockRuntime.name),
|
|
1054
|
+
types.jsxIdentifier("KeyedList")
|
|
1055
|
+
);
|
|
1056
|
+
return types.jsxElement(
|
|
1057
|
+
types.jsxOpeningElement(
|
|
1058
|
+
name,
|
|
1059
|
+
[
|
|
1060
|
+
types.jsxAttribute(types.jsxIdentifier("id"), types.jsxExpressionContainer(types.numericLiteral(plan.id))),
|
|
1061
|
+
types.jsxAttribute(
|
|
1062
|
+
types.jsxIdentifier("render"),
|
|
1063
|
+
types.jsxExpressionContainer(types.arrowFunctionExpression([], cloneExpression(source)))
|
|
1064
|
+
)
|
|
1065
|
+
],
|
|
1066
|
+
true
|
|
1067
|
+
),
|
|
1068
|
+
null,
|
|
1069
|
+
[],
|
|
1070
|
+
true
|
|
1071
|
+
);
|
|
1072
|
+
}
|
|
1073
|
+
function hostElementDescriptor(element) {
|
|
1074
|
+
const tag = element.openingElement.name;
|
|
1075
|
+
const attributes = [];
|
|
1076
|
+
const styles = [];
|
|
1077
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1078
|
+
if (!types.isJSXAttribute(attribute)) continue;
|
|
1079
|
+
const name = jsxAttributeName(attribute);
|
|
1080
|
+
if (!name || name === "key" || /^on[A-Z]/.test(name)) continue;
|
|
1081
|
+
if (name === "style") {
|
|
1082
|
+
if (types.isJSXExpressionContainer(attribute.value) && types.isObjectExpression(attribute.value.expression)) {
|
|
1083
|
+
for (const property of attribute.value.expression.properties) {
|
|
1084
|
+
if (!types.isObjectProperty(property) || !types.isExpression(property.value)) continue;
|
|
1085
|
+
const propertyName = types.isIdentifier(property.key) ? property.key.name : property.key.value;
|
|
1086
|
+
styles.push(
|
|
1087
|
+
types.objectExpression([
|
|
1088
|
+
types.objectProperty(types.identifier("name"), types.stringLiteral(propertyName)),
|
|
1089
|
+
types.objectProperty(types.identifier("value"), cloneExpression(property.value))
|
|
1090
|
+
])
|
|
1091
|
+
);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
continue;
|
|
1095
|
+
}
|
|
1096
|
+
const value = attribute.value ? types.isStringLiteral(attribute.value) ? types.stringLiteral(attribute.value.value) : types.isJSXExpressionContainer(attribute.value) && !types.isJSXEmptyExpression(attribute.value.expression) ? cloneExpression(attribute.value.expression) : types.identifier("undefined") : types.booleanLiteral(true);
|
|
1097
|
+
attributes.push(
|
|
1098
|
+
types.objectExpression([
|
|
1099
|
+
types.objectProperty(types.identifier("name"), types.stringLiteral(name)),
|
|
1100
|
+
types.objectProperty(types.identifier("value"), value)
|
|
1101
|
+
])
|
|
1102
|
+
);
|
|
1103
|
+
}
|
|
1104
|
+
const children = [];
|
|
1105
|
+
for (const child of element.children) {
|
|
1106
|
+
if (types.isJSXText(child)) {
|
|
1107
|
+
const text = cleanJsxText(child.value);
|
|
1108
|
+
if (text) children.push(types.stringLiteral(text));
|
|
1109
|
+
} else if (types.isJSXElement(child)) {
|
|
1110
|
+
children.push(hostElementDescriptor(child));
|
|
1111
|
+
} else if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression)) {
|
|
1112
|
+
children.push(cloneExpression(child.expression));
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
return types.objectExpression([
|
|
1116
|
+
types.objectProperty(types.identifier("kind"), types.stringLiteral("element")),
|
|
1117
|
+
types.objectProperty(types.identifier("tag"), types.stringLiteral(tag.name)),
|
|
1118
|
+
types.objectProperty(types.identifier("attributes"), types.arrayExpression(attributes)),
|
|
1119
|
+
types.objectProperty(types.identifier("styles"), types.arrayExpression(styles)),
|
|
1120
|
+
types.objectProperty(types.identifier("children"), types.arrayExpression(children))
|
|
1121
|
+
]);
|
|
1122
|
+
}
|
|
1123
|
+
function keyedRowBindingObject(binding, parameters) {
|
|
1124
|
+
const properties = [
|
|
1125
|
+
types.objectProperty(types.identifier("kind"), types.stringLiteral(binding.kind)),
|
|
1126
|
+
types.objectProperty(
|
|
1127
|
+
types.identifier("path"),
|
|
1128
|
+
types.arrayExpression(binding.path.map((part) => types.numericLiteral(part)))
|
|
1129
|
+
)
|
|
1130
|
+
];
|
|
1131
|
+
if (binding.name) {
|
|
1132
|
+
properties.push(types.objectProperty(types.identifier("name"), types.stringLiteral(binding.name)));
|
|
1133
|
+
}
|
|
1134
|
+
properties.push(
|
|
1135
|
+
types.objectProperty(
|
|
1136
|
+
types.identifier("read"),
|
|
1137
|
+
types.arrowFunctionExpression(
|
|
1138
|
+
parameters.map((parameter) => types.cloneNode(parameter, true)),
|
|
1139
|
+
cloneExpression(binding.value)
|
|
1140
|
+
)
|
|
1141
|
+
)
|
|
1142
|
+
);
|
|
1143
|
+
return types.objectExpression(properties);
|
|
1144
|
+
}
|
|
1145
|
+
function uniqueLocalIdentifier(base, nodes) {
|
|
1146
|
+
const names = /* @__PURE__ */ new Set();
|
|
1147
|
+
for (const node of nodes) {
|
|
1148
|
+
types.traverseFast(node, (child) => {
|
|
1149
|
+
if (types.isIdentifier(child)) names.add(child.name);
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
let name = base;
|
|
1153
|
+
let suffix = 2;
|
|
1154
|
+
while (names.has(name)) name = `${base}${suffix++}`;
|
|
1155
|
+
return types.identifier(name);
|
|
1156
|
+
}
|
|
1157
|
+
function rewriteKeyedRowEventAttributes(row, events, eventFactory, item, index) {
|
|
1158
|
+
const eventByLocation = new Map(
|
|
1159
|
+
events.map((event) => [`${event.path.join(".")}:${event.name}`, event])
|
|
1160
|
+
);
|
|
1161
|
+
const visit = (element, path) => {
|
|
1162
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1163
|
+
if (!types.isJSXAttribute(attribute)) continue;
|
|
1164
|
+
const name = jsxAttributeName(attribute);
|
|
1165
|
+
if (!name || !/^on[A-Z]/.test(name)) continue;
|
|
1166
|
+
const event = eventByLocation.get(`${path.join(".")}:${name}`);
|
|
1167
|
+
if (!event) continue;
|
|
1168
|
+
attribute.value = types.jsxExpressionContainer(
|
|
1169
|
+
types.callExpression(types.cloneNode(eventFactory), [
|
|
1170
|
+
types.cloneNode(item),
|
|
1171
|
+
types.cloneNode(index),
|
|
1172
|
+
types.numericLiteral(event.id)
|
|
1173
|
+
])
|
|
1174
|
+
);
|
|
1175
|
+
}
|
|
1176
|
+
let elementIndex = 0;
|
|
1177
|
+
for (const child of element.children) {
|
|
1178
|
+
if (!types.isJSXElement(child)) continue;
|
|
1179
|
+
visit(child, [...path, elementIndex]);
|
|
1180
|
+
elementIndex += 1;
|
|
1181
|
+
}
|
|
1182
|
+
};
|
|
1183
|
+
visit(row, []);
|
|
1184
|
+
}
|
|
1185
|
+
function rewriteKeyedRowConditionalExpressions(row, conditionals, conditionalFactory, item, index, renderParameters) {
|
|
1186
|
+
const conditionalByLocation = new Map(
|
|
1187
|
+
conditionals.map((conditional) => [conditional.path.join("."), conditional])
|
|
1188
|
+
);
|
|
1189
|
+
const visit = (element, path) => {
|
|
1190
|
+
const conditional = conditionalByLocation.get(path.join("."));
|
|
1191
|
+
if (conditional) {
|
|
1192
|
+
element.children = element.children.map((child) => {
|
|
1193
|
+
if (!types.isJSXExpressionContainer(child) || types.isJSXEmptyExpression(child.expression) || !conditionalBlockShape(child.expression)) {
|
|
1194
|
+
return child;
|
|
1195
|
+
}
|
|
1196
|
+
return types.jsxExpressionContainer(
|
|
1197
|
+
types.callExpression(types.cloneNode(conditionalFactory), [
|
|
1198
|
+
types.cloneNode(item),
|
|
1199
|
+
types.cloneNode(index),
|
|
1200
|
+
types.numericLiteral(conditional.id),
|
|
1201
|
+
types.arrowFunctionExpression(
|
|
1202
|
+
renderParameters.map((parameter) => types.cloneNode(parameter, true)),
|
|
1203
|
+
cloneExpression(child.expression)
|
|
1204
|
+
)
|
|
1205
|
+
])
|
|
1206
|
+
);
|
|
1207
|
+
});
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
let elementIndex = 0;
|
|
1211
|
+
for (const child of element.children) {
|
|
1212
|
+
if (!types.isJSXElement(child)) continue;
|
|
1213
|
+
visit(child, [...path, elementIndex]);
|
|
1214
|
+
elementIndex += 1;
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1217
|
+
visit(row, []);
|
|
1218
|
+
}
|
|
1219
|
+
function keyedRowsRenderSource(plan, eventFactory, conditionalFactory) {
|
|
1220
|
+
const source = types.cloneNode(plan.source, true);
|
|
1221
|
+
const child = meaningfulJsxChildren(source)[0];
|
|
1222
|
+
let callback;
|
|
1223
|
+
if (plan.syntax === "map" && types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression) && isMapCall(child.expression)) {
|
|
1224
|
+
const candidate = child.expression.arguments[0];
|
|
1225
|
+
if (types.isArrowFunctionExpression(candidate) || types.isFunctionExpression(candidate)) {
|
|
1226
|
+
callback = candidate;
|
|
1227
|
+
}
|
|
1228
|
+
} else if (plan.syntax === "list" && types.isJSXElement(child)) {
|
|
1229
|
+
const renderChild = meaningfulJsxChildren(child)[0];
|
|
1230
|
+
if (types.isJSXExpressionContainer(renderChild) && !types.isJSXEmptyExpression(renderChild.expression) && (types.isArrowFunctionExpression(renderChild.expression) || types.isFunctionExpression(renderChild.expression))) {
|
|
1231
|
+
callback = renderChild.expression;
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
const row = callback && returnedExpression(callback);
|
|
1235
|
+
const item = callback?.params[0];
|
|
1236
|
+
const index = callback?.params[1];
|
|
1237
|
+
if (!callback || !types.isJSXElement(row) || !types.isIdentifier(item)) return source;
|
|
1238
|
+
const runtimeIndex = types.isIdentifier(index) ? index : types.numericLiteral(0);
|
|
1239
|
+
if (plan.events.length > 0) {
|
|
1240
|
+
rewriteKeyedRowEventAttributes(row, plan.events, eventFactory, item, runtimeIndex);
|
|
1241
|
+
}
|
|
1242
|
+
if (plan.conditionals.length > 0) {
|
|
1243
|
+
rewriteKeyedRowConditionalExpressions(
|
|
1244
|
+
row,
|
|
1245
|
+
plan.conditionals,
|
|
1246
|
+
conditionalFactory,
|
|
1247
|
+
item,
|
|
1248
|
+
runtimeIndex,
|
|
1249
|
+
callback.params
|
|
1250
|
+
);
|
|
1251
|
+
}
|
|
1252
|
+
return source;
|
|
1253
|
+
}
|
|
1254
|
+
function keyedRowEventObject(event, renderParameters) {
|
|
1255
|
+
const nodes = [event.value, ...renderParameters];
|
|
1256
|
+
const index = renderParameters[1] ? types.cloneNode(renderParameters[1], true) : uniqueLocalIdentifier("_farmRowIndex", nodes);
|
|
1257
|
+
const syntheticEvent = uniqueLocalIdentifier("_farmEvent", [...nodes, index]);
|
|
1258
|
+
const parameters = [types.cloneNode(renderParameters[0], true), index, syntheticEvent];
|
|
1259
|
+
return types.objectExpression([
|
|
1260
|
+
types.objectProperty(types.identifier("name"), types.stringLiteral(event.name)),
|
|
1261
|
+
types.objectProperty(
|
|
1262
|
+
types.identifier("invoke"),
|
|
1263
|
+
types.arrowFunctionExpression(
|
|
1264
|
+
parameters,
|
|
1265
|
+
types.callExpression(types.parenthesizedExpression(types.cloneNode(event.value, true)), [
|
|
1266
|
+
types.cloneNode(syntheticEvent)
|
|
1267
|
+
])
|
|
1268
|
+
)
|
|
1269
|
+
)
|
|
1270
|
+
]);
|
|
1271
|
+
}
|
|
1272
|
+
function keyedRowConditionalBranchObject(bindings, parameters) {
|
|
1273
|
+
return types.objectExpression([
|
|
1274
|
+
types.objectProperty(
|
|
1275
|
+
types.identifier("bindings"),
|
|
1276
|
+
types.arrayExpression(bindings.map((binding) => keyedRowBindingObject(binding, parameters)))
|
|
1277
|
+
)
|
|
1278
|
+
]);
|
|
1279
|
+
}
|
|
1280
|
+
function keyedRowConditionalObject(conditional, parameters) {
|
|
1281
|
+
const properties = [
|
|
1282
|
+
types.objectProperty(types.identifier("id"), types.numericLiteral(conditional.id)),
|
|
1283
|
+
types.objectProperty(
|
|
1284
|
+
types.identifier("path"),
|
|
1285
|
+
types.arrayExpression(conditional.path.map((part) => types.numericLiteral(part)))
|
|
1286
|
+
),
|
|
1287
|
+
types.objectProperty(
|
|
1288
|
+
types.identifier("test"),
|
|
1289
|
+
types.arrowFunctionExpression(
|
|
1290
|
+
parameters.map((parameter) => types.cloneNode(parameter, true)),
|
|
1291
|
+
cloneExpression(conditional.test)
|
|
1292
|
+
)
|
|
1293
|
+
),
|
|
1294
|
+
types.objectProperty(types.identifier("logical"), types.booleanLiteral(conditional.logical))
|
|
1295
|
+
];
|
|
1296
|
+
if (conditional.truthy) {
|
|
1297
|
+
properties.push(
|
|
1298
|
+
types.objectProperty(
|
|
1299
|
+
types.identifier("truthy"),
|
|
1300
|
+
keyedRowConditionalBranchObject(conditional.truthyBindings, parameters)
|
|
1301
|
+
)
|
|
1302
|
+
);
|
|
1303
|
+
}
|
|
1304
|
+
if (conditional.falsy) {
|
|
1305
|
+
properties.push(
|
|
1306
|
+
types.objectProperty(
|
|
1307
|
+
types.identifier("falsy"),
|
|
1308
|
+
keyedRowConditionalBranchObject(conditional.falsyBindings, parameters)
|
|
1309
|
+
)
|
|
1310
|
+
);
|
|
1311
|
+
}
|
|
1312
|
+
return types.objectExpression(properties);
|
|
1313
|
+
}
|
|
1314
|
+
function keyedRowsBoundary(blockRuntime, plan) {
|
|
1315
|
+
const name = types.jsxMemberExpression(
|
|
1316
|
+
types.jsxIdentifier(blockRuntime.name),
|
|
1317
|
+
types.jsxIdentifier("KeyedRows")
|
|
1318
|
+
);
|
|
1319
|
+
const parameters = plan.renderCallback.params.map(
|
|
1320
|
+
(parameter) => types.cloneNode(parameter, true)
|
|
1321
|
+
);
|
|
1322
|
+
const eventFactory = uniqueLocalIdentifier("_farmRowEvent", [
|
|
1323
|
+
plan.source,
|
|
1324
|
+
...plan.events.map((event) => event.value)
|
|
1325
|
+
]);
|
|
1326
|
+
const conditionalFactory = uniqueLocalIdentifier("_farmRowConditional", [
|
|
1327
|
+
plan.source,
|
|
1328
|
+
...plan.conditionals.map((conditional) => conditional.test)
|
|
1329
|
+
]);
|
|
1330
|
+
const renderSource = keyedRowsRenderSource(plan, eventFactory, conditionalFactory);
|
|
1331
|
+
const renderFactoryParameters = plan.conditionals.length > 0 ? [types.cloneNode(eventFactory), types.cloneNode(conditionalFactory)] : plan.events.length > 0 ? [types.cloneNode(eventFactory)] : [];
|
|
1332
|
+
return types.jsxElement(
|
|
1333
|
+
types.jsxOpeningElement(
|
|
1334
|
+
name,
|
|
1335
|
+
[
|
|
1336
|
+
types.jsxAttribute(types.jsxIdentifier("id"), types.jsxExpressionContainer(types.numericLiteral(plan.id))),
|
|
1337
|
+
types.jsxAttribute(
|
|
1338
|
+
types.jsxIdentifier("render"),
|
|
1339
|
+
types.jsxExpressionContainer(
|
|
1340
|
+
types.arrowFunctionExpression(renderFactoryParameters, renderSource)
|
|
1341
|
+
)
|
|
1342
|
+
),
|
|
1343
|
+
types.jsxAttribute(
|
|
1344
|
+
types.jsxIdentifier("items"),
|
|
1345
|
+
types.jsxExpressionContainer(types.arrowFunctionExpression([], cloneExpression(plan.collection)))
|
|
1346
|
+
),
|
|
1347
|
+
types.jsxAttribute(
|
|
1348
|
+
types.jsxIdentifier("rowKey"),
|
|
1349
|
+
types.jsxExpressionContainer(types.cloneNode(plan.keyCallback, true))
|
|
1350
|
+
),
|
|
1351
|
+
types.jsxAttribute(
|
|
1352
|
+
types.jsxIdentifier("create"),
|
|
1353
|
+
types.jsxExpressionContainer(
|
|
1354
|
+
types.arrowFunctionExpression(
|
|
1355
|
+
parameters.map((parameter) => types.cloneNode(parameter, true)),
|
|
1356
|
+
hostElementDescriptor(plan.row)
|
|
1357
|
+
)
|
|
1358
|
+
)
|
|
1359
|
+
),
|
|
1360
|
+
types.jsxAttribute(
|
|
1361
|
+
types.jsxIdentifier("bindings"),
|
|
1362
|
+
types.jsxExpressionContainer(
|
|
1363
|
+
types.arrayExpression(
|
|
1364
|
+
plan.bindings.map((binding) => keyedRowBindingObject(binding, parameters))
|
|
1365
|
+
)
|
|
1366
|
+
)
|
|
1367
|
+
),
|
|
1368
|
+
...plan.events.length > 0 ? [
|
|
1369
|
+
types.jsxAttribute(
|
|
1370
|
+
types.jsxIdentifier("events"),
|
|
1371
|
+
types.jsxExpressionContainer(
|
|
1372
|
+
types.arrayExpression(
|
|
1373
|
+
plan.events.map((event) => keyedRowEventObject(event, parameters))
|
|
1374
|
+
)
|
|
1375
|
+
)
|
|
1376
|
+
)
|
|
1377
|
+
] : [],
|
|
1378
|
+
...plan.conditionals.length > 0 ? [
|
|
1379
|
+
types.jsxAttribute(
|
|
1380
|
+
types.jsxIdentifier("conditionals"),
|
|
1381
|
+
types.jsxExpressionContainer(
|
|
1382
|
+
types.arrayExpression(
|
|
1383
|
+
plan.conditionals.map(
|
|
1384
|
+
(conditional) => keyedRowConditionalObject(conditional, parameters)
|
|
1385
|
+
)
|
|
1386
|
+
)
|
|
1387
|
+
)
|
|
1388
|
+
)
|
|
1389
|
+
] : []
|
|
1390
|
+
],
|
|
1391
|
+
true
|
|
1392
|
+
),
|
|
1393
|
+
null,
|
|
1394
|
+
[],
|
|
1395
|
+
true
|
|
1396
|
+
);
|
|
1397
|
+
}
|
|
1398
|
+
function keyedRangeObject(range) {
|
|
1399
|
+
const parameters = range.renderCallback.params.map(
|
|
1400
|
+
(parameter) => types.cloneNode(parameter, true)
|
|
1401
|
+
);
|
|
1402
|
+
return types.objectExpression([
|
|
1403
|
+
types.objectProperty(types.identifier("before"), types.numericLiteral(range.before)),
|
|
1404
|
+
types.objectProperty(
|
|
1405
|
+
types.identifier("items"),
|
|
1406
|
+
types.arrowFunctionExpression([], cloneExpression(range.collection))
|
|
1407
|
+
),
|
|
1408
|
+
types.objectProperty(types.identifier("rowKey"), types.cloneNode(range.keyCallback, true)),
|
|
1409
|
+
types.objectProperty(
|
|
1410
|
+
types.identifier("create"),
|
|
1411
|
+
types.arrowFunctionExpression(
|
|
1412
|
+
parameters.map((parameter) => types.cloneNode(parameter, true)),
|
|
1413
|
+
hostElementDescriptor(range.row)
|
|
1414
|
+
)
|
|
1415
|
+
),
|
|
1416
|
+
types.objectProperty(
|
|
1417
|
+
types.identifier("bindings"),
|
|
1418
|
+
types.arrayExpression(
|
|
1419
|
+
range.bindings.map((binding) => keyedRowBindingObject(binding, parameters))
|
|
1420
|
+
)
|
|
1421
|
+
)
|
|
1422
|
+
]);
|
|
1423
|
+
}
|
|
1424
|
+
function keyedRangesBoundary(blockRuntime, plan) {
|
|
1425
|
+
const name = types.jsxMemberExpression(
|
|
1426
|
+
types.jsxIdentifier(blockRuntime.name),
|
|
1427
|
+
types.jsxIdentifier("KeyedRanges")
|
|
1428
|
+
);
|
|
1429
|
+
const source = types.cloneNode(plan.source, true);
|
|
1430
|
+
let rootRef;
|
|
1431
|
+
source.openingElement.attributes = source.openingElement.attributes.filter((attribute) => {
|
|
1432
|
+
if (!types.isJSXAttribute(attribute) || jsxAttributeName(attribute) !== "ref" || !types.isJSXExpressionContainer(attribute.value) || types.isJSXEmptyExpression(attribute.value.expression)) {
|
|
1433
|
+
return true;
|
|
1434
|
+
}
|
|
1435
|
+
rootRef = cloneExpression(attribute.value.expression);
|
|
1436
|
+
return false;
|
|
1437
|
+
});
|
|
1438
|
+
return types.jsxElement(
|
|
1439
|
+
types.jsxOpeningElement(
|
|
1440
|
+
name,
|
|
1441
|
+
[
|
|
1442
|
+
types.jsxAttribute(types.jsxIdentifier("id"), types.jsxExpressionContainer(types.numericLiteral(plan.id))),
|
|
1443
|
+
types.jsxAttribute(
|
|
1444
|
+
types.jsxIdentifier("render"),
|
|
1445
|
+
types.jsxExpressionContainer(types.arrowFunctionExpression([], source))
|
|
1446
|
+
),
|
|
1447
|
+
...rootRef ? [types.jsxAttribute(types.jsxIdentifier("rootRef"), types.jsxExpressionContainer(rootRef))] : [],
|
|
1448
|
+
types.jsxAttribute(
|
|
1449
|
+
types.jsxIdentifier("ranges"),
|
|
1450
|
+
types.jsxExpressionContainer(
|
|
1451
|
+
types.arrayExpression(plan.ranges.map((range) => keyedRangeObject(range)))
|
|
1452
|
+
)
|
|
1453
|
+
),
|
|
1454
|
+
types.jsxAttribute(
|
|
1455
|
+
types.jsxIdentifier("trailing"),
|
|
1456
|
+
types.jsxExpressionContainer(types.numericLiteral(plan.trailing))
|
|
1457
|
+
)
|
|
1458
|
+
],
|
|
1459
|
+
true
|
|
1460
|
+
),
|
|
1461
|
+
null,
|
|
1462
|
+
[],
|
|
1463
|
+
true
|
|
1464
|
+
);
|
|
1465
|
+
}
|
|
1466
|
+
function hostConditionalBranchObject(branch, bindings) {
|
|
1467
|
+
return types.objectExpression([
|
|
1468
|
+
types.objectProperty(
|
|
1469
|
+
types.identifier("create"),
|
|
1470
|
+
types.arrowFunctionExpression([], hostElementDescriptor(branch))
|
|
1471
|
+
),
|
|
1472
|
+
types.objectProperty(
|
|
1473
|
+
types.identifier("bindings"),
|
|
1474
|
+
types.arrayExpression(bindings.map((binding) => keyedRowBindingObject(binding, [])))
|
|
1475
|
+
)
|
|
1476
|
+
]);
|
|
1477
|
+
}
|
|
1478
|
+
function hostConditionalBoundary(blockRuntime, plan) {
|
|
1479
|
+
const name = types.jsxMemberExpression(
|
|
1480
|
+
types.jsxIdentifier(blockRuntime.name),
|
|
1481
|
+
types.jsxIdentifier("HostConditional")
|
|
1482
|
+
);
|
|
1483
|
+
const attributes = [
|
|
1484
|
+
types.jsxAttribute(types.jsxIdentifier("id"), types.jsxExpressionContainer(types.numericLiteral(plan.id))),
|
|
1485
|
+
types.jsxAttribute(
|
|
1486
|
+
types.jsxIdentifier("render"),
|
|
1487
|
+
types.jsxExpressionContainer(types.arrowFunctionExpression([], types.cloneNode(plan.source, true)))
|
|
1488
|
+
),
|
|
1489
|
+
types.jsxAttribute(
|
|
1490
|
+
types.jsxIdentifier("test"),
|
|
1491
|
+
types.jsxExpressionContainer(types.arrowFunctionExpression([], cloneExpression(plan.test)))
|
|
1492
|
+
)
|
|
1493
|
+
];
|
|
1494
|
+
if (plan.logical) attributes.push(types.jsxAttribute(types.jsxIdentifier("logical")));
|
|
1495
|
+
if (plan.truthy) {
|
|
1496
|
+
attributes.push(
|
|
1497
|
+
types.jsxAttribute(
|
|
1498
|
+
types.jsxIdentifier("truthy"),
|
|
1499
|
+
types.jsxExpressionContainer(hostConditionalBranchObject(plan.truthy, plan.truthyBindings))
|
|
1500
|
+
)
|
|
1501
|
+
);
|
|
1502
|
+
}
|
|
1503
|
+
if (plan.falsy) {
|
|
1504
|
+
attributes.push(
|
|
1505
|
+
types.jsxAttribute(
|
|
1506
|
+
types.jsxIdentifier("falsy"),
|
|
1507
|
+
types.jsxExpressionContainer(hostConditionalBranchObject(plan.falsy, plan.falsyBindings))
|
|
1508
|
+
)
|
|
1509
|
+
);
|
|
1510
|
+
}
|
|
1511
|
+
return types.jsxElement(types.jsxOpeningElement(name, attributes, true), null, [], true);
|
|
1512
|
+
}
|
|
1513
|
+
function analyzeComponentIslandElement(element, statesByValue, safeGlobals, allowedComponentNames) {
|
|
1514
|
+
const name = element.openingElement.name;
|
|
1515
|
+
if (!types.isJSXIdentifier(name) || !isComponentName(name.name)) {
|
|
1516
|
+
return { reason: "component islands require a direct component identifier" };
|
|
1517
|
+
}
|
|
1518
|
+
if (!allowedComponentNames.has(name.name)) {
|
|
1519
|
+
return {
|
|
1520
|
+
reason: `component island ${name.name} must reference a stable module-level component`
|
|
1521
|
+
};
|
|
1522
|
+
}
|
|
1523
|
+
if (meaningfulJsxChildren(element).length > 0) {
|
|
1524
|
+
return { reason: `component island ${name.name} does not support children yet` };
|
|
1525
|
+
}
|
|
1526
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
1527
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1528
|
+
if (types.isJSXSpreadAttribute(attribute)) {
|
|
1529
|
+
return { reason: `component island ${name.name} does not support JSX attribute spreads` };
|
|
1530
|
+
}
|
|
1531
|
+
const attributeName = jsxAttributeName(attribute);
|
|
1532
|
+
if (!attributeName) {
|
|
1533
|
+
return { reason: `component island ${name.name} does not support namespaced JSX attributes` };
|
|
1534
|
+
}
|
|
1535
|
+
if (attributeName === "ref" || attributeName === "key" || attributeName === "children") {
|
|
1536
|
+
return { reason: `component island ${name.name} does not support ${attributeName} yet` };
|
|
1537
|
+
}
|
|
1538
|
+
if (!types.isJSXExpressionContainer(attribute.value) || types.isJSXEmptyExpression(attribute.value.expression)) {
|
|
1539
|
+
continue;
|
|
1540
|
+
}
|
|
1541
|
+
const expression = attribute.value.expression;
|
|
1542
|
+
const isEventFunction = /^on[A-Z]/.test(attributeName) && (types.isArrowFunctionExpression(expression) || types.isFunctionExpression(expression));
|
|
1543
|
+
if (!isEventFunction) {
|
|
1544
|
+
const unsupported = validateDerivedExpression(expression, safeGlobals);
|
|
1545
|
+
if (unsupported) {
|
|
1546
|
+
return {
|
|
1547
|
+
reason: `component island ${name.name} prop ${attributeName} cannot use ${unsupported}`
|
|
1548
|
+
};
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
collectStateDependencies(expression, statesByValue).forEach(
|
|
1552
|
+
(dependency) => dependencies.add(dependency)
|
|
1553
|
+
);
|
|
1554
|
+
}
|
|
1555
|
+
return { dependencies: [...dependencies].sort((left, right) => left - right) };
|
|
1556
|
+
}
|
|
1557
|
+
function componentIslandBoundary(blockRuntime, plan, source) {
|
|
1558
|
+
const name = types.jsxMemberExpression(
|
|
1559
|
+
types.jsxIdentifier(blockRuntime.name),
|
|
1560
|
+
types.jsxIdentifier("Component")
|
|
1561
|
+
);
|
|
1562
|
+
return types.jsxElement(
|
|
1563
|
+
types.jsxOpeningElement(
|
|
1564
|
+
name,
|
|
1565
|
+
[
|
|
1566
|
+
types.jsxAttribute(types.jsxIdentifier("id"), types.jsxExpressionContainer(types.numericLiteral(plan.id))),
|
|
1567
|
+
types.jsxAttribute(
|
|
1568
|
+
types.jsxIdentifier("render"),
|
|
1569
|
+
types.jsxExpressionContainer(types.arrowFunctionExpression([], types.cloneNode(source, true)))
|
|
1570
|
+
)
|
|
1571
|
+
],
|
|
1572
|
+
true
|
|
1573
|
+
),
|
|
1574
|
+
null,
|
|
1575
|
+
[],
|
|
1576
|
+
true
|
|
1577
|
+
);
|
|
1578
|
+
}
|
|
1579
|
+
function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, allowedComponentNames) {
|
|
1580
|
+
const plans = [];
|
|
1581
|
+
const componentElements = /* @__PURE__ */ new Set();
|
|
1582
|
+
const conditionalExpressions = /* @__PURE__ */ new Set();
|
|
1583
|
+
const ownedElements = /* @__PURE__ */ new Set();
|
|
1584
|
+
const keyedExpressions = /* @__PURE__ */ new Set();
|
|
1585
|
+
let nextId = 0;
|
|
1586
|
+
const mergeDependencies = (target, dependencies) => {
|
|
1587
|
+
for (const dependency of dependencies) target.add(dependency);
|
|
1588
|
+
};
|
|
1589
|
+
const visitHost = (element, parent, insideConditional) => {
|
|
1590
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
1591
|
+
if (!isHostElement(element)) {
|
|
1592
|
+
return {
|
|
1593
|
+
dependencies,
|
|
1594
|
+
reason: insideConditional ? "conditional blocks currently support host elements only" : "compiled trees currently support host elements only"
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
if (insideConditional) {
|
|
1598
|
+
for (const attribute of element.openingElement.attributes) {
|
|
1599
|
+
if (types.isJSXSpreadAttribute(attribute)) {
|
|
1600
|
+
return {
|
|
1601
|
+
dependencies,
|
|
1602
|
+
reason: "conditional blocks do not support JSX attribute spreads"
|
|
1603
|
+
};
|
|
1604
|
+
}
|
|
1605
|
+
const name = jsxAttributeName(attribute);
|
|
1606
|
+
if (!name) {
|
|
1607
|
+
return {
|
|
1608
|
+
dependencies,
|
|
1609
|
+
reason: "conditional blocks do not support namespaced JSX attributes"
|
|
1610
|
+
};
|
|
1611
|
+
}
|
|
1612
|
+
if (name === "ref" || name === "dangerouslySetInnerHTML") {
|
|
1613
|
+
return {
|
|
1614
|
+
dependencies,
|
|
1615
|
+
reason: `conditional block ${name} requires React ownership`
|
|
1616
|
+
};
|
|
1617
|
+
}
|
|
1618
|
+
if (!types.isJSXExpressionContainer(attribute.value) || types.isJSXEmptyExpression(attribute.value.expression) || /^on[A-Z]/.test(name)) {
|
|
1619
|
+
continue;
|
|
1620
|
+
}
|
|
1621
|
+
const expression = attribute.value.expression;
|
|
1622
|
+
if (name === "style") {
|
|
1623
|
+
if (!types.isObjectExpression(expression)) {
|
|
1624
|
+
return {
|
|
1625
|
+
dependencies,
|
|
1626
|
+
reason: "conditional block styles must use one inline object literal"
|
|
1627
|
+
};
|
|
1628
|
+
}
|
|
1629
|
+
for (const property of expression.properties) {
|
|
1630
|
+
if (!types.isObjectProperty(property) || property.computed) {
|
|
1631
|
+
return {
|
|
1632
|
+
dependencies,
|
|
1633
|
+
reason: "conditional block styles do not support spreads, methods, or computed properties"
|
|
1634
|
+
};
|
|
1635
|
+
}
|
|
1636
|
+
if (!types.isExpression(property.value)) {
|
|
1637
|
+
return {
|
|
1638
|
+
dependencies,
|
|
1639
|
+
reason: "conditional block style properties must use expression values"
|
|
1640
|
+
};
|
|
1641
|
+
}
|
|
1642
|
+
const unsupported2 = validateDerivedExpression(property.value, safeGlobals);
|
|
1643
|
+
if (unsupported2) {
|
|
1644
|
+
return {
|
|
1645
|
+
dependencies,
|
|
1646
|
+
reason: `conditional block style cannot use ${unsupported2}`
|
|
1647
|
+
};
|
|
1648
|
+
}
|
|
1649
|
+
mergeDependencies(
|
|
1650
|
+
dependencies,
|
|
1651
|
+
collectStateDependencies(property.value, statesByValue)
|
|
1652
|
+
);
|
|
1653
|
+
}
|
|
1654
|
+
continue;
|
|
1655
|
+
}
|
|
1656
|
+
const unsupported = validateDerivedExpression(expression, safeGlobals);
|
|
1657
|
+
if (unsupported) {
|
|
1658
|
+
return {
|
|
1659
|
+
dependencies,
|
|
1660
|
+
reason: `conditional block attribute ${name} cannot use ${unsupported}`
|
|
1661
|
+
};
|
|
1662
|
+
}
|
|
1663
|
+
mergeDependencies(dependencies, collectStateDependencies(expression, statesByValue));
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
for (const child of element.children) {
|
|
1667
|
+
if (types.isJSXFragment(child)) {
|
|
1668
|
+
if (insideConditional) {
|
|
1669
|
+
return {
|
|
1670
|
+
dependencies,
|
|
1671
|
+
reason: "conditional blocks do not support fragments yet"
|
|
1672
|
+
};
|
|
1673
|
+
}
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1676
|
+
if (types.isJSXElement(child)) {
|
|
1677
|
+
if (isHostElement(child)) {
|
|
1678
|
+
const keyedRanges = insideConditional ? void 0 : analyzeKeyedRangesContainer(child, statesByValue, safeGlobals, listNames);
|
|
1679
|
+
if (keyedRanges) {
|
|
1680
|
+
plans.push({
|
|
1681
|
+
kind: "keyed-ranges",
|
|
1682
|
+
id: nextId++,
|
|
1683
|
+
parent,
|
|
1684
|
+
dependencies: keyedRanges.dependencies,
|
|
1685
|
+
source: child,
|
|
1686
|
+
ranges: keyedRanges.ranges,
|
|
1687
|
+
trailing: keyedRanges.trailing
|
|
1688
|
+
});
|
|
1689
|
+
for (const range of keyedRanges.ranges) {
|
|
1690
|
+
if (types.isJSXElement(range.source)) ownedElements.add(range.source);
|
|
1691
|
+
else keyedExpressions.add(range.source);
|
|
1692
|
+
}
|
|
1693
|
+
continue;
|
|
1694
|
+
}
|
|
1695
|
+
const hostConditional = analyzeHostConditionalContainer(
|
|
1696
|
+
child,
|
|
1697
|
+
statesByValue,
|
|
1698
|
+
safeGlobals
|
|
1699
|
+
);
|
|
1700
|
+
if (hostConditional) {
|
|
1701
|
+
plans.push({
|
|
1702
|
+
kind: "host-conditional",
|
|
1703
|
+
id: nextId++,
|
|
1704
|
+
parent,
|
|
1705
|
+
source: child,
|
|
1706
|
+
...hostConditional
|
|
1707
|
+
});
|
|
1708
|
+
ownedElements.add(child);
|
|
1709
|
+
continue;
|
|
1710
|
+
}
|
|
1711
|
+
const keyedRows = analyzeKeyedRowsContainer(child, statesByValue, safeGlobals, listNames);
|
|
1712
|
+
if (keyedRows) {
|
|
1713
|
+
plans.push({
|
|
1714
|
+
kind: "keyed-rows",
|
|
1715
|
+
id: nextId++,
|
|
1716
|
+
parent,
|
|
1717
|
+
dependencies: keyedRows.dependencies,
|
|
1718
|
+
source: child,
|
|
1719
|
+
collection: keyedRows.collection,
|
|
1720
|
+
keyCallback: keyedRows.keyCallback,
|
|
1721
|
+
renderCallback: keyedRows.renderCallback,
|
|
1722
|
+
row: keyedRows.row,
|
|
1723
|
+
bindings: keyedRows.bindings,
|
|
1724
|
+
events: keyedRows.events,
|
|
1725
|
+
conditionals: keyedRows.conditionals,
|
|
1726
|
+
syntax: keyedRows.syntax
|
|
1727
|
+
});
|
|
1728
|
+
ownedElements.add(child);
|
|
1729
|
+
continue;
|
|
1730
|
+
}
|
|
1731
|
+
const nested = visitHost(child, parent, insideConditional);
|
|
1732
|
+
if (nested.reason) return { dependencies, reason: nested.reason };
|
|
1733
|
+
mergeDependencies(dependencies, nested.dependencies);
|
|
1734
|
+
continue;
|
|
1735
|
+
}
|
|
1736
|
+
if (isPublicListElement(child, listNames)) {
|
|
1737
|
+
const result3 = analyzePublicList(child, statesByValue, safeGlobals);
|
|
1738
|
+
if (result3.reason) return { dependencies, reason: result3.reason };
|
|
1739
|
+
const plan = {
|
|
1740
|
+
kind: "keyed-list",
|
|
1741
|
+
id: nextId++,
|
|
1742
|
+
parent,
|
|
1743
|
+
dependencies: result3.dependencies || [],
|
|
1744
|
+
source: child,
|
|
1745
|
+
syntax: "list"
|
|
1746
|
+
};
|
|
1747
|
+
plans.push(plan);
|
|
1748
|
+
ownedElements.add(child);
|
|
1749
|
+
continue;
|
|
1750
|
+
}
|
|
1751
|
+
const result2 = analyzeComponentIslandElement(
|
|
1752
|
+
child,
|
|
1753
|
+
statesByValue,
|
|
1754
|
+
safeGlobals,
|
|
1755
|
+
allowedComponentNames
|
|
1756
|
+
);
|
|
1757
|
+
if (result2.reason) return { dependencies, reason: result2.reason };
|
|
1758
|
+
componentElements.add(child);
|
|
1759
|
+
if ((result2.dependencies || []).length > 0) {
|
|
1760
|
+
plans.push({
|
|
1761
|
+
kind: "component",
|
|
1762
|
+
id: nextId++,
|
|
1763
|
+
parent,
|
|
1764
|
+
dependencies: result2.dependencies || [],
|
|
1765
|
+
source: child
|
|
1766
|
+
});
|
|
1767
|
+
}
|
|
1768
|
+
continue;
|
|
1769
|
+
}
|
|
1770
|
+
if (!types.isJSXExpressionContainer(child) || types.isJSXEmptyExpression(child.expression)) {
|
|
1771
|
+
continue;
|
|
1772
|
+
}
|
|
1773
|
+
const expression = child.expression;
|
|
1774
|
+
const shape = conditionalBlockShape(expression);
|
|
1775
|
+
if (shape) {
|
|
1776
|
+
const unsupported = validateDerivedExpression(shape.test, safeGlobals);
|
|
1777
|
+
if (unsupported) {
|
|
1778
|
+
return {
|
|
1779
|
+
dependencies,
|
|
1780
|
+
reason: `conditional block test cannot use ${unsupported}`
|
|
1781
|
+
};
|
|
1782
|
+
}
|
|
1783
|
+
const plan = {
|
|
1784
|
+
kind: "conditional",
|
|
1785
|
+
id: nextId++,
|
|
1786
|
+
parent,
|
|
1787
|
+
dependencies: collectStateDependencies(shape.test, statesByValue),
|
|
1788
|
+
source: expression
|
|
1789
|
+
};
|
|
1790
|
+
plans.push(plan);
|
|
1791
|
+
conditionalExpressions.add(expression);
|
|
1792
|
+
const ownedDependencies = new Set(plan.dependencies);
|
|
1793
|
+
for (const branch of shape.branches) {
|
|
1794
|
+
const nested = visitHost(branch, plan.id, true);
|
|
1795
|
+
if (nested.reason) return { dependencies, reason: nested.reason };
|
|
1796
|
+
mergeDependencies(ownedDependencies, nested.dependencies);
|
|
1797
|
+
}
|
|
1798
|
+
plan.dependencies = [...ownedDependencies].sort((left, right) => left - right);
|
|
1799
|
+
continue;
|
|
1800
|
+
}
|
|
1801
|
+
if (isMapCall(expression)) {
|
|
1802
|
+
const result2 = analyzeMapList(expression, statesByValue, safeGlobals);
|
|
1803
|
+
if (result2.reason) return { dependencies, reason: result2.reason };
|
|
1804
|
+
plans.push({
|
|
1805
|
+
kind: "keyed-list",
|
|
1806
|
+
id: nextId++,
|
|
1807
|
+
parent,
|
|
1808
|
+
dependencies: result2.dependencies || [],
|
|
1809
|
+
source: expression,
|
|
1810
|
+
syntax: "map"
|
|
1811
|
+
});
|
|
1812
|
+
keyedExpressions.add(expression);
|
|
1813
|
+
continue;
|
|
1814
|
+
}
|
|
1815
|
+
if (insideConditional) {
|
|
1816
|
+
if (!isTextExpression(expression, statesByValue, safeGlobals)) {
|
|
1817
|
+
return {
|
|
1818
|
+
dependencies,
|
|
1819
|
+
reason: "conditional block children must keep one supported block or static host tree"
|
|
1820
|
+
};
|
|
1821
|
+
}
|
|
1822
|
+
mergeDependencies(dependencies, collectStateDependencies(expression, statesByValue));
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
return { dependencies };
|
|
1826
|
+
};
|
|
1827
|
+
const rootRanges = analyzeKeyedRangesContainer(root, statesByValue, safeGlobals, listNames, true);
|
|
1828
|
+
if (rootRanges) {
|
|
1829
|
+
plans.push({
|
|
1830
|
+
kind: "keyed-ranges",
|
|
1831
|
+
id: nextId++,
|
|
1832
|
+
dependencies: rootRanges.dependencies,
|
|
1833
|
+
source: root,
|
|
1834
|
+
ranges: rootRanges.ranges,
|
|
1835
|
+
trailing: rootRanges.trailing
|
|
1836
|
+
});
|
|
1837
|
+
for (const range of rootRanges.ranges) {
|
|
1838
|
+
if (types.isJSXElement(range.source)) ownedElements.add(range.source);
|
|
1839
|
+
else keyedExpressions.add(range.source);
|
|
1840
|
+
}
|
|
1841
|
+
return {
|
|
1842
|
+
plans,
|
|
1843
|
+
componentElements,
|
|
1844
|
+
conditionalExpressions,
|
|
1845
|
+
ownedElements,
|
|
1846
|
+
keyedExpressions
|
|
1847
|
+
};
|
|
1848
|
+
}
|
|
1849
|
+
const result = visitHost(root, void 0, false);
|
|
1850
|
+
if (result.reason) return { reason: result.reason };
|
|
1851
|
+
return {
|
|
1852
|
+
plans,
|
|
1853
|
+
componentElements,
|
|
1854
|
+
conditionalExpressions,
|
|
1855
|
+
ownedElements,
|
|
1856
|
+
keyedExpressions
|
|
1857
|
+
};
|
|
43
1858
|
}
|
|
44
|
-
function
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
1859
|
+
function conditionalBlockBoundary(blockRuntime, plan, source) {
|
|
1860
|
+
const name = types.jsxMemberExpression(
|
|
1861
|
+
types.jsxIdentifier(blockRuntime.name),
|
|
1862
|
+
types.jsxIdentifier("Conditional")
|
|
1863
|
+
);
|
|
1864
|
+
return types.jsxElement(
|
|
1865
|
+
types.jsxOpeningElement(
|
|
1866
|
+
name,
|
|
1867
|
+
[
|
|
1868
|
+
types.jsxAttribute(types.jsxIdentifier("id"), types.jsxExpressionContainer(types.numericLiteral(plan.id))),
|
|
1869
|
+
types.jsxAttribute(
|
|
1870
|
+
types.jsxIdentifier("render"),
|
|
1871
|
+
types.jsxExpressionContainer(types.arrowFunctionExpression([], cloneExpression(source)))
|
|
1872
|
+
)
|
|
1873
|
+
],
|
|
1874
|
+
true
|
|
1875
|
+
),
|
|
1876
|
+
null,
|
|
1877
|
+
[],
|
|
1878
|
+
true
|
|
1879
|
+
);
|
|
48
1880
|
}
|
|
49
|
-
function
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
1881
|
+
function lowerComposableBlocks(root, plans, blockRuntime, listNames) {
|
|
1882
|
+
const planBySource = new Map(
|
|
1883
|
+
plans.map((plan) => [plan.source, plan])
|
|
1884
|
+
);
|
|
1885
|
+
const rootPlan = planBySource.get(root);
|
|
1886
|
+
if (rootPlan?.kind === "keyed-ranges") {
|
|
1887
|
+
return keyedRangesBoundary(blockRuntime, rootPlan);
|
|
1888
|
+
}
|
|
1889
|
+
const visit = (element) => {
|
|
1890
|
+
element.children = element.children.map((child) => {
|
|
1891
|
+
if (types.isJSXElement(child)) {
|
|
1892
|
+
const plan2 = planBySource.get(child);
|
|
1893
|
+
if (plan2?.kind === "host-conditional") {
|
|
1894
|
+
return hostConditionalBoundary(blockRuntime, plan2);
|
|
1895
|
+
}
|
|
1896
|
+
if (plan2?.kind === "keyed-rows") {
|
|
1897
|
+
return keyedRowsBoundary(blockRuntime, plan2);
|
|
1898
|
+
}
|
|
1899
|
+
if (plan2?.kind === "keyed-ranges") {
|
|
1900
|
+
return keyedRangesBoundary(blockRuntime, plan2);
|
|
1901
|
+
}
|
|
1902
|
+
if (plan2?.kind === "component") {
|
|
1903
|
+
return componentIslandBoundary(blockRuntime, plan2, child);
|
|
1904
|
+
}
|
|
1905
|
+
if (plan2?.kind === "keyed-list" && isPublicListElement(child, listNames)) {
|
|
1906
|
+
return keyedListBoundary(blockRuntime, plan2, child);
|
|
1907
|
+
}
|
|
1908
|
+
if (isHostElement(child)) visit(child);
|
|
1909
|
+
return child;
|
|
60
1910
|
}
|
|
1911
|
+
if (!types.isJSXExpressionContainer(child) || types.isJSXEmptyExpression(child.expression)) {
|
|
1912
|
+
return child;
|
|
1913
|
+
}
|
|
1914
|
+
const plan = planBySource.get(child.expression);
|
|
1915
|
+
if (plan?.kind === "conditional") {
|
|
1916
|
+
const shape = conditionalBlockShape(child.expression);
|
|
1917
|
+
for (const branch of shape?.branches || []) visit(branch);
|
|
1918
|
+
return conditionalBlockBoundary(blockRuntime, plan, child.expression);
|
|
1919
|
+
}
|
|
1920
|
+
if (plan?.kind === "keyed-list") {
|
|
1921
|
+
return keyedListBoundary(blockRuntime, plan, child.expression);
|
|
1922
|
+
}
|
|
1923
|
+
return child;
|
|
1924
|
+
});
|
|
1925
|
+
};
|
|
1926
|
+
visit(root);
|
|
1927
|
+
return root;
|
|
1928
|
+
}
|
|
1929
|
+
function assignStableBindingTargets(bindings) {
|
|
1930
|
+
const targetByPath = /* @__PURE__ */ new Map();
|
|
1931
|
+
for (const binding of bindings) {
|
|
1932
|
+
if (binding.kind === "block") continue;
|
|
1933
|
+
const key = binding.path.join(".");
|
|
1934
|
+
let target = targetByPath.get(key);
|
|
1935
|
+
if (target === void 0) {
|
|
1936
|
+
target = targetByPath.size;
|
|
1937
|
+
targetByPath.set(key, target);
|
|
61
1938
|
}
|
|
62
|
-
|
|
63
|
-
|
|
1939
|
+
binding.target = target;
|
|
1940
|
+
}
|
|
64
1941
|
}
|
|
65
|
-
function
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
types.callExpression(
|
|
85
|
-
types.memberExpression(
|
|
86
|
-
types.memberExpression(types.cloneNode(stateParameter), types.numericLiteral(state.index), true),
|
|
87
|
-
types.identifier("get")
|
|
88
|
-
),
|
|
89
|
-
[]
|
|
1942
|
+
function lowerStableBindingTargets(root, bindings, blockRuntime, ownedElements) {
|
|
1943
|
+
const targetByPath = /* @__PURE__ */ new Map();
|
|
1944
|
+
for (const binding of bindings) {
|
|
1945
|
+
if (binding.kind !== "block" && binding.target !== void 0) {
|
|
1946
|
+
targetByPath.set(binding.path.join("."), binding.target);
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
const visit = (element, path) => {
|
|
1950
|
+
const target = targetByPath.get(path.join("."));
|
|
1951
|
+
if (target !== void 0 && path.length > 0) {
|
|
1952
|
+
element.openingElement.attributes.push(
|
|
1953
|
+
types.jsxAttribute(
|
|
1954
|
+
types.jsxIdentifier("ref"),
|
|
1955
|
+
types.jsxExpressionContainer(
|
|
1956
|
+
types.callExpression(
|
|
1957
|
+
types.memberExpression(types.cloneNode(blockRuntime), types.identifier("target")),
|
|
1958
|
+
[types.numericLiteral(target)]
|
|
1959
|
+
)
|
|
1960
|
+
)
|
|
90
1961
|
)
|
|
91
1962
|
);
|
|
92
1963
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
for (let index = 0; index < lines.length; index += 1) {
|
|
100
|
-
const lastNonEmptyLine = lines.slice(index + 1).every((line2) => line2.trim().length === 0);
|
|
101
|
-
let line = lines[index].replace(/\t/g, " ").replace(/ +/g, " ");
|
|
102
|
-
if (index > 0) line = line.replace(/^ /, "");
|
|
103
|
-
if (!lastNonEmptyLine) line = line.replace(/ $/, "");
|
|
104
|
-
if (line) result += line + (!lastNonEmptyLine ? " " : "");
|
|
105
|
-
}
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
108
|
-
function isTextExpression(expression) {
|
|
109
|
-
let supported = true;
|
|
110
|
-
traverse(expressionFile(cloneExpression(expression)), {
|
|
111
|
-
CallExpression(path) {
|
|
112
|
-
supported = false;
|
|
113
|
-
path.stop();
|
|
114
|
-
},
|
|
115
|
-
OptionalCallExpression(path) {
|
|
116
|
-
supported = false;
|
|
117
|
-
path.stop();
|
|
118
|
-
},
|
|
119
|
-
JSXElement(path) {
|
|
120
|
-
supported = false;
|
|
121
|
-
path.stop();
|
|
122
|
-
},
|
|
123
|
-
JSXFragment(path) {
|
|
124
|
-
supported = false;
|
|
125
|
-
path.stop();
|
|
126
|
-
},
|
|
127
|
-
ObjectExpression(path) {
|
|
128
|
-
supported = false;
|
|
129
|
-
path.stop();
|
|
130
|
-
},
|
|
131
|
-
Function(path) {
|
|
132
|
-
supported = false;
|
|
133
|
-
path.stop();
|
|
1964
|
+
let elementIndex = 0;
|
|
1965
|
+
for (const child of element.children) {
|
|
1966
|
+
if (!types.isJSXElement(child) || !isHostElement(child)) continue;
|
|
1967
|
+
if (ownedElements.has(child)) continue;
|
|
1968
|
+
visit(child, [...path, elementIndex]);
|
|
1969
|
+
elementIndex += 1;
|
|
134
1970
|
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
function jsxAttributeName(attribute) {
|
|
139
|
-
return types.isJSXIdentifier(attribute.name) ? attribute.name.name : void 0;
|
|
1971
|
+
};
|
|
1972
|
+
visit(root, []);
|
|
1973
|
+
return root;
|
|
140
1974
|
}
|
|
141
|
-
function analyzeHostTree(root, statesByValue) {
|
|
1975
|
+
function analyzeHostTree(root, statesByValue, safeGlobals, conditionalExpressions, keyedExpressions, ownedElements, componentElements) {
|
|
142
1976
|
const bindings = [];
|
|
1977
|
+
const analyzeStyle = (expression, path) => {
|
|
1978
|
+
if (!types.isObjectExpression(expression)) {
|
|
1979
|
+
return "stateful style bindings must use one inline object literal";
|
|
1980
|
+
}
|
|
1981
|
+
for (const property of expression.properties) {
|
|
1982
|
+
if (!types.isObjectProperty(property) || property.computed) {
|
|
1983
|
+
return "stateful style bindings do not support spreads, methods, or computed properties";
|
|
1984
|
+
}
|
|
1985
|
+
const name = types.isIdentifier(property.key) ? property.key.name : types.isStringLiteral(property.key) ? property.key.value : void 0;
|
|
1986
|
+
if (!name || name.includes("-") && !name.startsWith("--")) {
|
|
1987
|
+
return "stateful style property names must use camelCase or a CSS custom property";
|
|
1988
|
+
}
|
|
1989
|
+
if (!types.isExpression(property.value)) {
|
|
1990
|
+
return `stateful style property ${name} must use an expression value`;
|
|
1991
|
+
}
|
|
1992
|
+
const dependencies = collectStateDependencies(property.value, statesByValue);
|
|
1993
|
+
if (dependencies.length === 0) continue;
|
|
1994
|
+
const unsupported = validateDerivedExpression(property.value, safeGlobals);
|
|
1995
|
+
if (unsupported) return `stateful style property ${name} cannot use ${unsupported}`;
|
|
1996
|
+
bindings.push({
|
|
1997
|
+
kind: "style",
|
|
1998
|
+
path: [...path],
|
|
1999
|
+
dependencies,
|
|
2000
|
+
name,
|
|
2001
|
+
value: cloneExpression(property.value)
|
|
2002
|
+
});
|
|
2003
|
+
}
|
|
2004
|
+
return void 0;
|
|
2005
|
+
};
|
|
143
2006
|
const visit = (element, path) => {
|
|
144
2007
|
const tag = element.openingElement.name;
|
|
145
2008
|
if (!types.isJSXIdentifier(tag) || !/^[a-z]/.test(tag.name)) {
|
|
@@ -158,9 +2021,16 @@ function analyzeHostTree(root, statesByValue) {
|
|
|
158
2021
|
const expression = attribute.value.expression;
|
|
159
2022
|
const dependencies = collectStateDependencies(expression, statesByValue);
|
|
160
2023
|
if (dependencies.length === 0 || /^on[A-Z]/.test(name)) continue;
|
|
161
|
-
if (name === "style"
|
|
2024
|
+
if (name === "style") {
|
|
2025
|
+
const reason2 = analyzeStyle(expression, path);
|
|
2026
|
+
if (reason2) return reason2;
|
|
2027
|
+
continue;
|
|
2028
|
+
}
|
|
2029
|
+
if (name === "children" || name === "key") {
|
|
162
2030
|
return `stateful ${name} bindings are not supported yet`;
|
|
163
2031
|
}
|
|
2032
|
+
const unsupported = validateDerivedExpression(expression, safeGlobals);
|
|
2033
|
+
if (unsupported) return `stateful ${name} binding cannot use ${unsupported}`;
|
|
164
2034
|
bindings.push({
|
|
165
2035
|
kind: "attribute",
|
|
166
2036
|
path: [...path],
|
|
@@ -170,18 +2040,29 @@ function analyzeHostTree(root, statesByValue) {
|
|
|
170
2040
|
});
|
|
171
2041
|
}
|
|
172
2042
|
const nestedElements = element.children.filter(
|
|
173
|
-
(child) => types.isJSXElement(child)
|
|
2043
|
+
(child) => types.isJSXElement(child) && isHostElement(child) && !ownedElements.has(child)
|
|
174
2044
|
);
|
|
175
2045
|
const expressionChildren = element.children.filter(
|
|
176
2046
|
(child) => types.isJSXExpressionContainer(child)
|
|
177
2047
|
);
|
|
178
2048
|
for (const child of expressionChildren) {
|
|
179
2049
|
if (types.isJSXEmptyExpression(child.expression)) continue;
|
|
180
|
-
if (
|
|
2050
|
+
if (conditionalExpressions.has(child.expression)) continue;
|
|
2051
|
+
if (keyedExpressions.has(child.expression)) continue;
|
|
2052
|
+
if (!isTextExpression(child.expression, statesByValue, safeGlobals)) {
|
|
181
2053
|
return "dynamic child structures require React reconciliation";
|
|
182
2054
|
}
|
|
183
2055
|
}
|
|
184
|
-
|
|
2056
|
+
const hasOwnedElement = element.children.some(
|
|
2057
|
+
(child) => types.isJSXElement(child) && ownedElements.has(child)
|
|
2058
|
+
);
|
|
2059
|
+
const hasComponentElement = element.children.some(
|
|
2060
|
+
(child) => types.isJSXElement(child) && componentElements.has(child)
|
|
2061
|
+
);
|
|
2062
|
+
const hasDynamicBlocks = hasOwnedElement || hasComponentElement || expressionChildren.some(
|
|
2063
|
+
(child) => !types.isJSXEmptyExpression(child.expression) && (conditionalExpressions.has(child.expression) || keyedExpressions.has(child.expression))
|
|
2064
|
+
);
|
|
2065
|
+
if (nestedElements.length === 0 && !hasDynamicBlocks) {
|
|
185
2066
|
const dependencies = /* @__PURE__ */ new Set();
|
|
186
2067
|
const parts = [];
|
|
187
2068
|
for (const child of element.children) {
|
|
@@ -205,13 +2086,13 @@ function analyzeHostTree(root, statesByValue) {
|
|
|
205
2086
|
}
|
|
206
2087
|
} else {
|
|
207
2088
|
for (const child of expressionChildren) {
|
|
208
|
-
if (!types.isJSXEmptyExpression(child.expression) && collectStateDependencies(child.expression, statesByValue).length > 0) {
|
|
2089
|
+
if (!types.isJSXEmptyExpression(child.expression) && !conditionalExpressions.has(child.expression) && !keyedExpressions.has(child.expression) && collectStateDependencies(child.expression, statesByValue).length > 0) {
|
|
209
2090
|
return "mixed element and stateful text children are not supported yet";
|
|
210
2091
|
}
|
|
211
2092
|
}
|
|
212
2093
|
let elementIndex = 0;
|
|
213
2094
|
for (const child of element.children) {
|
|
214
|
-
if (!types.isJSXElement(child)) continue;
|
|
2095
|
+
if (!types.isJSXElement(child) || !isHostElement(child) || ownedElements.has(child)) continue;
|
|
215
2096
|
const reason2 = visit(child, [...path, elementIndex]);
|
|
216
2097
|
if (reason2) return reason2;
|
|
217
2098
|
elementIndex += 1;
|
|
@@ -246,29 +2127,94 @@ function lazyInitialValue(expression) {
|
|
|
246
2127
|
[]
|
|
247
2128
|
);
|
|
248
2129
|
}
|
|
249
|
-
function
|
|
2130
|
+
function analyzePropsParameter(path) {
|
|
250
2131
|
const parameter = path.node.params[0];
|
|
251
|
-
|
|
2132
|
+
if (!parameter) {
|
|
2133
|
+
return {
|
|
2134
|
+
definitionParameter: path.scope.generateUidIdentifier("props"),
|
|
2135
|
+
destructuredNames: /* @__PURE__ */ new Set()
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
if (types.isIdentifier(parameter)) {
|
|
2139
|
+
return {
|
|
2140
|
+
definitionParameter: types.cloneNode(parameter, true),
|
|
2141
|
+
wrapperProps: types.cloneNode(parameter, true),
|
|
2142
|
+
destructuredNames: /* @__PURE__ */ new Set()
|
|
2143
|
+
};
|
|
2144
|
+
}
|
|
2145
|
+
if (!types.isObjectPattern(parameter)) {
|
|
2146
|
+
return "components must use zero parameters, one props identifier, or flat object props destructuring";
|
|
2147
|
+
}
|
|
2148
|
+
const localNames = /* @__PURE__ */ new Set();
|
|
2149
|
+
for (const property of parameter.properties) {
|
|
2150
|
+
if (types.isRestElement(property)) {
|
|
2151
|
+
return "rest properties in component props destructuring are not supported yet";
|
|
2152
|
+
}
|
|
2153
|
+
if (property.computed) {
|
|
2154
|
+
return "computed component props destructuring is not supported yet";
|
|
2155
|
+
}
|
|
2156
|
+
let local;
|
|
2157
|
+
if (types.isIdentifier(property.value)) {
|
|
2158
|
+
local = property.value;
|
|
2159
|
+
} else if (types.isAssignmentPattern(property.value) && types.isIdentifier(property.value.left)) {
|
|
2160
|
+
local = property.value.left;
|
|
2161
|
+
}
|
|
2162
|
+
if (!local) {
|
|
2163
|
+
return "nested component props destructuring is not supported yet";
|
|
2164
|
+
}
|
|
2165
|
+
if (localNames.has(local.name)) {
|
|
2166
|
+
return "component props destructuring local names must be unique";
|
|
2167
|
+
}
|
|
2168
|
+
localNames.add(local.name);
|
|
2169
|
+
}
|
|
2170
|
+
const definitionParameter = path.scope.generateUidIdentifier("props");
|
|
2171
|
+
return {
|
|
2172
|
+
definitionParameter,
|
|
2173
|
+
wrapperProps: types.objectExpression(
|
|
2174
|
+
[...localNames].map(
|
|
2175
|
+
(name) => types.objectProperty(types.identifier(name), types.identifier(name), false, true)
|
|
2176
|
+
)
|
|
2177
|
+
),
|
|
2178
|
+
destructuredNames: localNames
|
|
2179
|
+
};
|
|
252
2180
|
}
|
|
253
|
-
function bindingObject(binding,
|
|
2181
|
+
function bindingObject(binding, propsParameter, stateParameter, statesByValue, statesBySetter) {
|
|
254
2182
|
const properties = [
|
|
255
2183
|
types.objectProperty(types.identifier("kind"), types.stringLiteral(binding.kind)),
|
|
256
|
-
types.objectProperty(
|
|
257
|
-
types.identifier("path"),
|
|
258
|
-
types.arrayExpression(binding.path.map((part) => types.numericLiteral(part)))
|
|
259
|
-
),
|
|
260
2184
|
types.objectProperty(
|
|
261
2185
|
types.identifier("dependencies"),
|
|
262
2186
|
types.arrayExpression(binding.dependencies.map((part) => types.numericLiteral(part)))
|
|
263
2187
|
)
|
|
264
2188
|
];
|
|
2189
|
+
if (binding.kind === "block") {
|
|
2190
|
+
properties.push(types.objectProperty(types.identifier("id"), types.numericLiteral(binding.id)));
|
|
2191
|
+
if (binding.parent !== void 0) {
|
|
2192
|
+
properties.push(types.objectProperty(types.identifier("parent"), types.numericLiteral(binding.parent)));
|
|
2193
|
+
}
|
|
2194
|
+
return types.objectExpression(properties);
|
|
2195
|
+
}
|
|
2196
|
+
properties.splice(
|
|
2197
|
+
1,
|
|
2198
|
+
0,
|
|
2199
|
+
types.objectProperty(
|
|
2200
|
+
types.identifier("path"),
|
|
2201
|
+
types.arrayExpression(binding.path.map((part) => types.numericLiteral(part)))
|
|
2202
|
+
)
|
|
2203
|
+
);
|
|
2204
|
+
if (binding.target !== void 0) {
|
|
2205
|
+
properties.splice(
|
|
2206
|
+
2,
|
|
2207
|
+
0,
|
|
2208
|
+
types.objectProperty(types.identifier("target"), types.numericLiteral(binding.target))
|
|
2209
|
+
);
|
|
2210
|
+
}
|
|
265
2211
|
if (binding.name)
|
|
266
2212
|
properties.push(types.objectProperty(types.identifier("name"), types.stringLiteral(binding.name)));
|
|
267
2213
|
properties.push(
|
|
268
2214
|
types.objectProperty(
|
|
269
2215
|
types.identifier("read"),
|
|
270
2216
|
types.arrowFunctionExpression(
|
|
271
|
-
[
|
|
2217
|
+
[types.cloneNode(propsParameter), types.cloneNode(stateParameter)],
|
|
272
2218
|
rewriteStateAccess(binding.value, stateParameter, statesByValue, statesBySetter)
|
|
273
2219
|
)
|
|
274
2220
|
)
|
|
@@ -280,16 +2226,20 @@ function wrapperElement(definitionIdentifier, props) {
|
|
|
280
2226
|
const attributes = props ? [types.jsxSpreadAttribute(types.cloneNode(props))] : [];
|
|
281
2227
|
return types.jsxElement(types.jsxOpeningElement(name, attributes, true), null, [], true);
|
|
282
2228
|
}
|
|
283
|
-
function compileCandidate(candidate, createComponentIdentifier, useStateNames, reactNames) {
|
|
2229
|
+
function compileCandidate(candidate, createComponentIdentifier, useStateNames, reactNames, listNames, moduleId) {
|
|
284
2230
|
const { path, name, statementPath } = candidate;
|
|
285
2231
|
if (path.node.async || path.node.generator)
|
|
286
2232
|
return "async and generator components are not supported";
|
|
287
2233
|
if (path.node.typeParameters) return "generic components are not supported yet";
|
|
288
|
-
if (path.node.params.length > 1
|
|
289
|
-
|
|
290
|
-
|
|
2234
|
+
if (path.node.params.length > 1) return "components must use at most one props parameter";
|
|
2235
|
+
const propsPlan = analyzePropsParameter(path);
|
|
2236
|
+
if (typeof propsPlan === "string") return propsPlan;
|
|
291
2237
|
if (!types.isBlockStatement(path.node.body)) return "components must use a block body";
|
|
2238
|
+
const safeGlobals = new Set(
|
|
2239
|
+
[...SAFE_GLOBAL_CALLS, "Math"].filter((name2) => !path.scope.getBinding(name2))
|
|
2240
|
+
);
|
|
292
2241
|
const states = [];
|
|
2242
|
+
const locals = [];
|
|
293
2243
|
let returned;
|
|
294
2244
|
for (const statement of path.node.body.body) {
|
|
295
2245
|
if (types.isReturnStatement(statement)) {
|
|
@@ -297,19 +2247,48 @@ function compileCandidate(candidate, createComponentIdentifier, useStateNames, r
|
|
|
297
2247
|
returned = statement;
|
|
298
2248
|
continue;
|
|
299
2249
|
}
|
|
300
|
-
if (
|
|
301
|
-
return "
|
|
2250
|
+
if (types.isFunctionDeclaration(statement)) {
|
|
2251
|
+
if (!statement.id) return "named event handler declarations must have a name";
|
|
2252
|
+
if (statement.async || statement.generator || statement.typeParameters) {
|
|
2253
|
+
return `event handler ${statement.id.name} must be synchronous and non-generic`;
|
|
2254
|
+
}
|
|
2255
|
+
locals.push({
|
|
2256
|
+
kind: "handler",
|
|
2257
|
+
name: statement.id.name,
|
|
2258
|
+
value: types.toExpression(types.cloneNode(statement, true))
|
|
2259
|
+
});
|
|
2260
|
+
continue;
|
|
2261
|
+
}
|
|
2262
|
+
if (!types.isVariableDeclaration(statement) || statement.kind !== "const" || statement.declarations.length !== 1) {
|
|
2263
|
+
return "only top-level useState declarations, pure derived const values, named synchronous handlers, and one return are supported by the current compiler";
|
|
302
2264
|
}
|
|
303
2265
|
const declaration = statement.declarations[0];
|
|
304
|
-
if (
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
2266
|
+
if (types.isArrayPattern(declaration.id)) {
|
|
2267
|
+
if (declaration.id.elements.length !== 2 || !types.isIdentifier(declaration.id.elements[0]) || !types.isIdentifier(declaration.id.elements[1]) || !isUseStateCall(declaration.init, useStateNames, reactNames) || declaration.init.arguments.length > 1 || declaration.init.arguments[0] && !types.isExpression(declaration.init.arguments[0])) {
|
|
2268
|
+
return "only const [value, setValue] = useState(initial) state declarations are supported";
|
|
2269
|
+
}
|
|
2270
|
+
if (locals.length > 0) {
|
|
2271
|
+
return "useState declarations must appear before derived local values and event handlers";
|
|
2272
|
+
}
|
|
2273
|
+
states.push({
|
|
2274
|
+
valueName: declaration.id.elements[0].name,
|
|
2275
|
+
setterName: declaration.id.elements[1].name,
|
|
2276
|
+
index: states.length,
|
|
2277
|
+
initialValue: declaration.init.arguments[0]
|
|
2278
|
+
});
|
|
2279
|
+
continue;
|
|
2280
|
+
}
|
|
2281
|
+
if (!types.isIdentifier(declaration.id) || !types.isExpression(declaration.init)) {
|
|
2282
|
+
return "derived const values and event handlers must use one identifier and one expression";
|
|
2283
|
+
}
|
|
2284
|
+
if (types.isArrowFunctionExpression(declaration.init) || types.isFunctionExpression(declaration.init)) {
|
|
2285
|
+
if (declaration.init.async || declaration.init.generator) {
|
|
2286
|
+
return `event handler ${declaration.id.name} must be synchronous`;
|
|
2287
|
+
}
|
|
2288
|
+
locals.push({ kind: "handler", name: declaration.id.name, value: declaration.init });
|
|
2289
|
+
} else {
|
|
2290
|
+
locals.push({ kind: "derived", name: declaration.id.name, value: declaration.init });
|
|
2291
|
+
}
|
|
313
2292
|
}
|
|
314
2293
|
if (states.length === 0) return "no local useState binding was found";
|
|
315
2294
|
if (!returned?.argument || !types.isJSXElement(returned.argument)) {
|
|
@@ -317,22 +2296,109 @@ function compileCandidate(candidate, createComponentIdentifier, useStateNames, r
|
|
|
317
2296
|
}
|
|
318
2297
|
const statesByValue = new Map(states.map((state) => [state.valueName, state]));
|
|
319
2298
|
const statesBySetter = new Map(states.map((state) => [state.setterName, state]));
|
|
2299
|
+
const localNames = new Set(locals.map((binding) => binding.name));
|
|
2300
|
+
if (localNames.size !== locals.length) return "local binding names must be unique";
|
|
320
2301
|
for (const state of states) {
|
|
321
|
-
if (state.initialValue && (collectStateDependencies(state.initialValue, statesByValue).length > 0 || [...statesBySetter].some(
|
|
2302
|
+
if (state.initialValue && (collectStateDependencies(state.initialValue, statesByValue).length > 0 || collectReferencedLocals(state.initialValue, localNames).length > 0 || [...statesBySetter].some(
|
|
322
2303
|
([setterName]) => referencesIdentifier(state.initialValue, setterName)
|
|
323
2304
|
))) {
|
|
324
|
-
return "useState initializers cannot reference another local
|
|
2305
|
+
return "useState initializers cannot reference another local binding";
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
const derivedByName = /* @__PURE__ */ new Map();
|
|
2309
|
+
const handlersByName = /* @__PURE__ */ new Map();
|
|
2310
|
+
for (const binding of locals) {
|
|
2311
|
+
const forwardReferences = collectReferencedLocals(binding.value, localNames).filter(
|
|
2312
|
+
(reference) => !derivedByName.has(reference)
|
|
2313
|
+
);
|
|
2314
|
+
if (forwardReferences.length > 0) {
|
|
2315
|
+
return `${binding.kind === "handler" ? "event handler" : "derived local"} ${binding.name} can only reference earlier derived local values`;
|
|
2316
|
+
}
|
|
2317
|
+
const expanded = rewriteDerivedAccess(binding.value, derivedByName);
|
|
2318
|
+
if (binding.kind === "handler") {
|
|
2319
|
+
handlersByName.set(binding.name, {
|
|
2320
|
+
name: binding.name,
|
|
2321
|
+
value: expanded
|
|
2322
|
+
});
|
|
2323
|
+
continue;
|
|
325
2324
|
}
|
|
2325
|
+
const unsupported = collectionPipelineMethod(expanded) ? validateCollectionPipeline(expanded, safeGlobals) : validateDerivedExpression(expanded, safeGlobals);
|
|
2326
|
+
if (unsupported) {
|
|
2327
|
+
return `derived local ${binding.name} cannot use ${unsupported}`;
|
|
2328
|
+
}
|
|
2329
|
+
derivedByName.set(binding.name, expanded);
|
|
326
2330
|
}
|
|
327
|
-
const
|
|
2331
|
+
const rootWithDerived = rewriteDerivedAccess(returned.argument, derivedByName);
|
|
2332
|
+
const handlerRewrite = rewriteHandlerAccess(rootWithDerived, handlersByName);
|
|
2333
|
+
if (handlerRewrite.reason) return handlerRewrite.reason;
|
|
2334
|
+
if (!handlerRewrite.root) return "event handlers could not be prepared safely";
|
|
2335
|
+
const expandedRootWithDeferredEvents = rewriteDestructuredPropAccess(
|
|
2336
|
+
handlerRewrite.root,
|
|
2337
|
+
propsPlan.destructuredNames,
|
|
2338
|
+
propsPlan.definitionParameter
|
|
2339
|
+
);
|
|
2340
|
+
const expandedRoot = stabilizeDeferredEventCurrentTargets(
|
|
2341
|
+
expandedRootWithDeferredEvents,
|
|
2342
|
+
new Set(states.map((state) => state.setterName))
|
|
2343
|
+
);
|
|
2344
|
+
const setterReason = validateSetterUsage(expandedRoot, statesBySetter);
|
|
328
2345
|
if (setterReason) return setterReason;
|
|
329
|
-
const
|
|
2346
|
+
const referencedComponentNames = /* @__PURE__ */ new Set();
|
|
2347
|
+
traverse(expressionFile(types.cloneNode(expandedRoot, true)), {
|
|
2348
|
+
JSXOpeningElement(componentPath) {
|
|
2349
|
+
const componentName = componentPath.node.name;
|
|
2350
|
+
if (types.isJSXIdentifier(componentName) && isComponentName(componentName.name)) {
|
|
2351
|
+
referencedComponentNames.add(componentName.name);
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
});
|
|
2355
|
+
const allowedComponentNames = new Set(
|
|
2356
|
+
[...referencedComponentNames].filter((componentName) => {
|
|
2357
|
+
if (listNames.has(componentName)) return false;
|
|
2358
|
+
const binding = path.scope.getBinding(componentName);
|
|
2359
|
+
return Boolean(
|
|
2360
|
+
binding?.constant && binding.scope.path.isProgram() && binding.kind !== "let" && binding.kind !== "var"
|
|
2361
|
+
);
|
|
2362
|
+
})
|
|
2363
|
+
);
|
|
2364
|
+
const blockAnalysis = analyzeComposableBlocks(
|
|
2365
|
+
expandedRoot,
|
|
2366
|
+
statesByValue,
|
|
2367
|
+
safeGlobals,
|
|
2368
|
+
listNames,
|
|
2369
|
+
allowedComponentNames
|
|
2370
|
+
);
|
|
2371
|
+
if (blockAnalysis.reason) return blockAnalysis.reason;
|
|
2372
|
+
const blockPlans = blockAnalysis.plans || [];
|
|
2373
|
+
const analysis = analyzeHostTree(
|
|
2374
|
+
expandedRoot,
|
|
2375
|
+
statesByValue,
|
|
2376
|
+
safeGlobals,
|
|
2377
|
+
blockAnalysis.conditionalExpressions || /* @__PURE__ */ new Set(),
|
|
2378
|
+
blockAnalysis.keyedExpressions || /* @__PURE__ */ new Set(),
|
|
2379
|
+
blockAnalysis.ownedElements || /* @__PURE__ */ new Set(),
|
|
2380
|
+
blockAnalysis.componentElements || /* @__PURE__ */ new Set()
|
|
2381
|
+
);
|
|
330
2382
|
if (analysis.reason) return analysis.reason;
|
|
2383
|
+
assignStableBindingTargets(analysis.bindings || []);
|
|
331
2384
|
const stateParameter = path.scope.generateUidIdentifier("farmState");
|
|
2385
|
+
const blockParameter = path.scope.generateUidIdentifier("farmBlocks");
|
|
332
2386
|
const definitionIdentifier = path.scope.generateUidIdentifier(`${name}Compiled`);
|
|
333
|
-
const propsParameter =
|
|
2387
|
+
const propsParameter = propsPlan.definitionParameter;
|
|
2388
|
+
const rootWithTargets = lowerStableBindingTargets(
|
|
2389
|
+
expandedRoot,
|
|
2390
|
+
analysis.bindings || [],
|
|
2391
|
+
blockParameter,
|
|
2392
|
+
blockAnalysis.ownedElements || /* @__PURE__ */ new Set()
|
|
2393
|
+
);
|
|
2394
|
+
const rootWithBlocks = lowerComposableBlocks(
|
|
2395
|
+
rootWithTargets,
|
|
2396
|
+
blockPlans,
|
|
2397
|
+
blockParameter,
|
|
2398
|
+
listNames
|
|
2399
|
+
);
|
|
334
2400
|
const rewrittenRoot = rewriteStateAccess(
|
|
335
|
-
|
|
2401
|
+
rootWithBlocks,
|
|
336
2402
|
stateParameter,
|
|
337
2403
|
statesByValue,
|
|
338
2404
|
statesBySetter
|
|
@@ -340,39 +2406,67 @@ function compileCandidate(candidate, createComponentIdentifier, useStateNames, r
|
|
|
340
2406
|
const definition = types.callExpression(types.cloneNode(createComponentIdentifier), [
|
|
341
2407
|
types.objectExpression([
|
|
342
2408
|
types.objectProperty(types.identifier("displayName"), types.stringLiteral(name)),
|
|
2409
|
+
types.spreadElement(
|
|
2410
|
+
types.conditionalExpression(
|
|
2411
|
+
types.memberExpression(
|
|
2412
|
+
types.metaProperty(types.identifier("import"), types.identifier("meta")),
|
|
2413
|
+
types.identifier("hot")
|
|
2414
|
+
),
|
|
2415
|
+
types.objectExpression([
|
|
2416
|
+
types.objectProperty(types.identifier("hmrId"), types.stringLiteral(`${moduleId}#${name}`)),
|
|
2417
|
+
types.objectProperty(
|
|
2418
|
+
types.identifier("stateSignature"),
|
|
2419
|
+
types.stringLiteral(String(states.length))
|
|
2420
|
+
)
|
|
2421
|
+
]),
|
|
2422
|
+
types.objectExpression([])
|
|
2423
|
+
)
|
|
2424
|
+
),
|
|
343
2425
|
types.objectProperty(
|
|
344
2426
|
types.identifier("initialize"),
|
|
345
2427
|
types.arrowFunctionExpression(
|
|
346
2428
|
[types.cloneNode(propsParameter)],
|
|
347
|
-
types.arrayExpression(
|
|
2429
|
+
types.arrayExpression(
|
|
2430
|
+
states.map(
|
|
2431
|
+
(state) => lazyInitialValue(
|
|
2432
|
+
state.initialValue ? rewriteDestructuredPropAccess(
|
|
2433
|
+
state.initialValue,
|
|
2434
|
+
propsPlan.destructuredNames,
|
|
2435
|
+
propsParameter
|
|
2436
|
+
) : void 0
|
|
2437
|
+
)
|
|
2438
|
+
)
|
|
2439
|
+
)
|
|
348
2440
|
)
|
|
349
2441
|
),
|
|
350
2442
|
types.objectProperty(
|
|
351
2443
|
types.identifier("render"),
|
|
352
2444
|
types.arrowFunctionExpression(
|
|
353
|
-
[types.cloneNode(propsParameter), types.cloneNode(stateParameter)],
|
|
2445
|
+
[types.cloneNode(propsParameter), types.cloneNode(stateParameter), types.cloneNode(blockParameter)],
|
|
354
2446
|
rewrittenRoot
|
|
355
2447
|
)
|
|
356
2448
|
),
|
|
357
2449
|
types.objectProperty(
|
|
358
2450
|
types.identifier("bindings"),
|
|
359
2451
|
types.arrayExpression(
|
|
360
|
-
|
|
361
|
-
|
|
2452
|
+
[
|
|
2453
|
+
...analysis.bindings || [],
|
|
2454
|
+
...blockPlans.map((block) => ({
|
|
2455
|
+
kind: "block",
|
|
2456
|
+
id: block.id,
|
|
2457
|
+
parent: block.parent,
|
|
2458
|
+
dependencies: block.dependencies
|
|
2459
|
+
}))
|
|
2460
|
+
].map(
|
|
2461
|
+
(binding) => bindingObject(binding, propsParameter, stateParameter, statesByValue, statesBySetter)
|
|
362
2462
|
)
|
|
363
2463
|
)
|
|
364
2464
|
)
|
|
365
2465
|
])
|
|
366
2466
|
]);
|
|
367
|
-
const originalProps = path.node.params[0];
|
|
368
2467
|
path.get("body").replaceWith(
|
|
369
2468
|
types.blockStatement([
|
|
370
|
-
types.returnStatement(
|
|
371
|
-
wrapperElement(
|
|
372
|
-
definitionIdentifier,
|
|
373
|
-
originalProps && types.isIdentifier(originalProps) ? originalProps : void 0
|
|
374
|
-
)
|
|
375
|
-
)
|
|
2469
|
+
types.returnStatement(wrapperElement(definitionIdentifier, propsPlan.wrapperProps))
|
|
376
2470
|
])
|
|
377
2471
|
);
|
|
378
2472
|
statementPath.insertAfter(
|
|
@@ -423,8 +2517,18 @@ async function compileReactModule(code, id, options) {
|
|
|
423
2517
|
const moduleDirectives = new Set(directiveValues(programPath.node));
|
|
424
2518
|
const useStateNames = /* @__PURE__ */ new Set();
|
|
425
2519
|
const reactNames = /* @__PURE__ */ new Set();
|
|
2520
|
+
const listNames = /* @__PURE__ */ new Set();
|
|
426
2521
|
for (const statement of programPath.node.body) {
|
|
427
|
-
if (!types.isImportDeclaration(statement)
|
|
2522
|
+
if (!types.isImportDeclaration(statement)) continue;
|
|
2523
|
+
if (statement.source.value === "@farm.js/react/list") {
|
|
2524
|
+
for (const specifier of statement.specifiers) {
|
|
2525
|
+
if (types.isImportSpecifier(specifier) && types.isIdentifier(specifier.imported, { name: "List" })) {
|
|
2526
|
+
listNames.add(specifier.local.name);
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
continue;
|
|
2530
|
+
}
|
|
2531
|
+
if (statement.source.value !== "react") continue;
|
|
428
2532
|
for (const specifier of statement.specifiers) {
|
|
429
2533
|
if (types.isImportDefaultSpecifier(specifier) || types.isImportNamespaceSpecifier(specifier)) {
|
|
430
2534
|
reactNames.add(specifier.local.name);
|
|
@@ -446,7 +2550,9 @@ async function compileReactModule(code, id, options) {
|
|
|
446
2550
|
candidate,
|
|
447
2551
|
createComponentIdentifier,
|
|
448
2552
|
useStateNames,
|
|
449
|
-
reactNames
|
|
2553
|
+
reactNames,
|
|
2554
|
+
listNames,
|
|
2555
|
+
id
|
|
450
2556
|
);
|
|
451
2557
|
if (reason) {
|
|
452
2558
|
diagnostics.push({
|
|
@@ -500,9 +2606,65 @@ async function compileReactModule(code, id, options) {
|
|
|
500
2606
|
diagnostics
|
|
501
2607
|
};
|
|
502
2608
|
}
|
|
2609
|
+
function projectRelativeId(projectRoot, id) {
|
|
2610
|
+
return relative(projectRoot, id).replace(/\\/g, "/");
|
|
2611
|
+
}
|
|
2612
|
+
function createReactCompilerReport(projectRoot, observations) {
|
|
2613
|
+
const reasonCounts = /* @__PURE__ */ new Map();
|
|
2614
|
+
let componentsConsidered = 0;
|
|
2615
|
+
let compiled = 0;
|
|
2616
|
+
let fallback = 0;
|
|
2617
|
+
const modules = [...observations].map(({ id, result }) => {
|
|
2618
|
+
componentsConsidered += result.compiled.length + result.diagnostics.length;
|
|
2619
|
+
compiled += result.compiled.length;
|
|
2620
|
+
fallback += result.diagnostics.length;
|
|
2621
|
+
const moduleId = projectRelativeId(projectRoot, id);
|
|
2622
|
+
const fallbacks = result.diagnostics.map((diagnostic) => {
|
|
2623
|
+
reasonCounts.set(diagnostic.reason, (reasonCounts.get(diagnostic.reason) || 0) + 1);
|
|
2624
|
+
return { module: moduleId, ...diagnostic };
|
|
2625
|
+
});
|
|
2626
|
+
return {
|
|
2627
|
+
id: moduleId,
|
|
2628
|
+
compiled: [...result.compiled].sort(),
|
|
2629
|
+
fallbacks
|
|
2630
|
+
};
|
|
2631
|
+
}).sort((left, right) => left.id.localeCompare(right.id));
|
|
2632
|
+
const fallbackReasons = [...reasonCounts].map(([reason, count]) => ({ count, reason })).sort((left, right) => right.count - left.count || left.reason.localeCompare(right.reason));
|
|
2633
|
+
return {
|
|
2634
|
+
version: 1,
|
|
2635
|
+
summary: {
|
|
2636
|
+
modules: modules.length,
|
|
2637
|
+
componentsConsidered,
|
|
2638
|
+
compiled,
|
|
2639
|
+
fallback
|
|
2640
|
+
},
|
|
2641
|
+
fallbackReasons,
|
|
2642
|
+
modules
|
|
2643
|
+
};
|
|
2644
|
+
}
|
|
2645
|
+
async function writeReactCompilerReport(projectRoot, reportFile, observations) {
|
|
2646
|
+
const outputPath = resolve(projectRoot, reportFile);
|
|
2647
|
+
const report = createReactCompilerReport(projectRoot, observations);
|
|
2648
|
+
await mkdir(dirname(outputPath), { recursive: true });
|
|
2649
|
+
await writeFile(outputPath, `${JSON.stringify(report, null, 2)}
|
|
2650
|
+
`, "utf8");
|
|
2651
|
+
return outputPath;
|
|
2652
|
+
}
|
|
503
2653
|
|
|
504
2654
|
// src/index.ts
|
|
505
2655
|
var DEFAULT_COMPILER_DIRECTIVE = "use compiler";
|
|
2656
|
+
var DEFAULT_COMPILER_REPORT_FILE = ".farm/react-compiler.json";
|
|
2657
|
+
function normalizeCompilerReportFile(value) {
|
|
2658
|
+
if (value === void 0) return DEFAULT_COMPILER_REPORT_FILE;
|
|
2659
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
2660
|
+
throw new TypeError("The React compiler report file must be a non-empty relative path.");
|
|
2661
|
+
}
|
|
2662
|
+
const normalized = value.trim().replace(/\\/g, "/");
|
|
2663
|
+
if (normalized.startsWith("/") || /^[A-Za-z]:\//.test(normalized) || normalized.split("/").includes("..")) {
|
|
2664
|
+
throw new TypeError("The React compiler report file must stay inside the project root.");
|
|
2665
|
+
}
|
|
2666
|
+
return normalized;
|
|
2667
|
+
}
|
|
506
2668
|
function normalizeReactCompilerOptions(value) {
|
|
507
2669
|
const options = value === true ? {} : value;
|
|
508
2670
|
const mode = options.mode || "infer";
|
|
@@ -517,7 +2679,17 @@ function normalizeReactCompilerOptions(value) {
|
|
|
517
2679
|
if (!["fallback", "warn", "error"].includes(onUnsupported)) {
|
|
518
2680
|
throw new TypeError(`Unknown unsupported-component behavior: ${String(onUnsupported)}`);
|
|
519
2681
|
}
|
|
520
|
-
|
|
2682
|
+
if (options.report !== void 0 && typeof options.report !== "boolean") {
|
|
2683
|
+
throw new TypeError("The React compiler report option must be a boolean.");
|
|
2684
|
+
}
|
|
2685
|
+
if (options.report === false && options.reportFile !== void 0) {
|
|
2686
|
+
throw new TypeError(
|
|
2687
|
+
"A React compiler report file cannot be configured when reporting is false."
|
|
2688
|
+
);
|
|
2689
|
+
}
|
|
2690
|
+
const reportFile = normalizeCompilerReportFile(options.reportFile);
|
|
2691
|
+
const report = options.report === true || options.reportFile !== void 0;
|
|
2692
|
+
return { mode, directive, onUnsupported, report, reportFile };
|
|
521
2693
|
}
|
|
522
2694
|
|
|
523
2695
|
// src/vite.ts
|
|
@@ -542,6 +2714,8 @@ function createFarmRendererPlugin(options = {}) {
|
|
|
542
2714
|
const compilerOptions = readCompilerOptions(options.rendererOptions);
|
|
543
2715
|
if (!compilerOptions) return [];
|
|
544
2716
|
let projectRoot = "";
|
|
2717
|
+
const reportEnabled = compilerOptions.report && options.ssr !== true;
|
|
2718
|
+
const reportObservations = /* @__PURE__ */ new Map();
|
|
545
2719
|
return [
|
|
546
2720
|
{
|
|
547
2721
|
name: "farm:react-aot-compiler",
|
|
@@ -549,11 +2723,21 @@ function createFarmRendererPlugin(options = {}) {
|
|
|
549
2723
|
configResolved(config) {
|
|
550
2724
|
projectRoot = normalizePath(config.root).replace(/\/$/, "");
|
|
551
2725
|
},
|
|
2726
|
+
buildStart() {
|
|
2727
|
+
reportObservations.clear();
|
|
2728
|
+
},
|
|
552
2729
|
async transform(code, id) {
|
|
553
2730
|
const cleanId = normalizePath(id.split("?", 1)[0]);
|
|
554
2731
|
if (!/\.[jt]sx$/.test(cleanId) || cleanId.includes("/node_modules/") || projectRoot && !cleanId.startsWith(`${projectRoot}/`))
|
|
555
2732
|
return null;
|
|
556
2733
|
const result = await compileReactModule(code, cleanId, compilerOptions);
|
|
2734
|
+
if (reportEnabled) {
|
|
2735
|
+
if (result.compiled.length > 0 || result.diagnostics.length > 0) {
|
|
2736
|
+
reportObservations.set(cleanId, { id: cleanId, result });
|
|
2737
|
+
} else {
|
|
2738
|
+
reportObservations.delete(cleanId);
|
|
2739
|
+
}
|
|
2740
|
+
}
|
|
557
2741
|
for (const diagnostic of result.diagnostics) {
|
|
558
2742
|
const message = `[react-compiler] ${diagnostic.component}: ${diagnostic.reason}; using React.`;
|
|
559
2743
|
if (compilerOptions.onUnsupported === "error") this.error(message);
|
|
@@ -561,6 +2745,14 @@ function createFarmRendererPlugin(options = {}) {
|
|
|
561
2745
|
}
|
|
562
2746
|
if (result.compiled.length === 0) return null;
|
|
563
2747
|
return { code: result.code, map: result.map };
|
|
2748
|
+
},
|
|
2749
|
+
async buildEnd(error) {
|
|
2750
|
+
if (error || !reportEnabled || !projectRoot) return;
|
|
2751
|
+
await writeReactCompilerReport(
|
|
2752
|
+
projectRoot,
|
|
2753
|
+
compilerOptions.reportFile,
|
|
2754
|
+
reportObservations.values()
|
|
2755
|
+
);
|
|
564
2756
|
}
|
|
565
2757
|
}
|
|
566
2758
|
];
|