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