@farm.js/react 0.1.0-beta.40 → 0.1.0-beta.42
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/dist/compiler-runtime.cjs +170 -28
- package/dist/compiler-runtime.cjs.map +1 -1
- package/dist/compiler-runtime.d.mts +26 -3
- package/dist/compiler-runtime.d.ts +26 -3
- package/dist/compiler-runtime.mjs +170 -29
- package/dist/compiler-runtime.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.mjs.map +1 -1
- package/dist/vite.cjs +404 -144
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.mjs +404 -144
- package/dist/vite.mjs.map +1 -1
- package/package.json +2 -2
package/dist/vite.mjs
CHANGED
|
@@ -153,7 +153,8 @@ var COMPILER_RUNTIME_FEATURE_EXPORTS = {
|
|
|
153
153
|
"keyed-rows-complete-filter-prepend-hinted": "keyedRowsCompleteFilterPrependHintedRuntimeFeature",
|
|
154
154
|
"keyed-ranges": "keyedRangesRuntimeFeature",
|
|
155
155
|
"mixed-ranges": "mixedRangesRuntimeFeature",
|
|
156
|
-
component: "componentRuntimeFeature"
|
|
156
|
+
component: "componentRuntimeFeature",
|
|
157
|
+
effects: "effectsRuntimeFeature"
|
|
157
158
|
};
|
|
158
159
|
function runtimeFeaturesForPlans(plans, keyedUpdateHints, keyedArrayFilterHints, keyedArrayStructuralAppendHints, keyedArrayStructuralAppendMapHints, keyedArrayPrependHints, keyedArrayStructuralPrependHints, keyedArrayStructuralPrependMapHints, keyedArrayPositionHints, keyedArrayBatchInsertHints, keyedArrayWindowReplaceHints, keyedArrayReorderHints, keyedArrayMapReorderHints, keyedArrayMappedRollingWindowChainHints, keyedArrayRollingWindowHints, keyedMapOrAppendHints, keyedArrayAppendOnlyHints) {
|
|
159
160
|
const features = /* @__PURE__ */ new Set();
|
|
@@ -221,6 +222,106 @@ function isUseStateCall(expression, useStateNames, reactNames) {
|
|
|
221
222
|
if (types.isIdentifier(expression.callee)) return useStateNames.has(expression.callee.name);
|
|
222
223
|
return types.isMemberExpression(expression.callee) && !expression.callee.computed && types.isIdentifier(expression.callee.object) && reactNames.has(expression.callee.object.name) && types.isIdentifier(expression.callee.property, { name: "useState" });
|
|
223
224
|
}
|
|
225
|
+
function reactHookCall(expression, hookNames, reactNames) {
|
|
226
|
+
if (!types.isCallExpression(expression)) return void 0;
|
|
227
|
+
const callee = expression.callee;
|
|
228
|
+
if (types.isIdentifier(callee)) return hookNames.get(callee.name);
|
|
229
|
+
return types.isMemberExpression(callee) && !callee.computed && types.isIdentifier(callee.object) && reactNames.has(callee.object.name) && types.isIdentifier(callee.property) && (callee.property.name === "useMemo" || callee.property.name === "useCallback" || callee.property.name === "useRef" || callee.property.name === "useEffect" || callee.property.name === "useLayoutEffect") ? callee.property.name : void 0;
|
|
230
|
+
}
|
|
231
|
+
function memoHookValue(expression, hookNames, reactNames) {
|
|
232
|
+
const hook = reactHookCall(expression, hookNames, reactNames);
|
|
233
|
+
if (hook !== "useMemo" && hook !== "useCallback") return void 0;
|
|
234
|
+
if (!types.isCallExpression(expression)) return void 0;
|
|
235
|
+
const [callback, dependencyList, ...rest] = expression.arguments;
|
|
236
|
+
if (rest.length > 0 || !types.isArrowFunctionExpression(callback) && !types.isFunctionExpression(callback) || callback.async || callback.generator) {
|
|
237
|
+
return `${hook} needs one inline synchronous callback`;
|
|
238
|
+
}
|
|
239
|
+
let dependencies;
|
|
240
|
+
if (dependencyList) {
|
|
241
|
+
if (!types.isArrayExpression(dependencyList) || !dependencyList.elements.every((element) => types.isIdentifier(element))) {
|
|
242
|
+
return `${hook} dependencies must be an array of names`;
|
|
243
|
+
}
|
|
244
|
+
dependencies = dependencyList.elements.map((element) => element.name);
|
|
245
|
+
}
|
|
246
|
+
if (hook === "useCallback") return { hook, value: callback, dependencies };
|
|
247
|
+
if (callback.params.length > 0) return "useMemo callbacks take no parameters";
|
|
248
|
+
const body = callback.body;
|
|
249
|
+
const value = types.isExpression(body) ? body : body.body.length === 1 && types.isReturnStatement(body.body[0]) && body.body[0].argument ? body.body[0].argument : void 0;
|
|
250
|
+
if (!value) return "useMemo callbacks must return one expression";
|
|
251
|
+
return { hook, value, dependencies };
|
|
252
|
+
}
|
|
253
|
+
function effectScope(path, effects, statesByValue, locals, propsParameter, stateParameter) {
|
|
254
|
+
const cell = (index, member) => types.memberExpression(
|
|
255
|
+
types.memberExpression(types.cloneNode(stateParameter), types.numericLiteral(index), true),
|
|
256
|
+
types.identifier(member)
|
|
257
|
+
);
|
|
258
|
+
const statements = [];
|
|
259
|
+
const parameter = path.node.params[0];
|
|
260
|
+
if (parameter && !types.isIdentifier(parameter)) {
|
|
261
|
+
statements.push(
|
|
262
|
+
types.variableDeclaration("const", [
|
|
263
|
+
types.variableDeclarator(types.cloneNode(parameter, true), types.cloneNode(propsParameter))
|
|
264
|
+
])
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
const localsByName = new Map(locals.map((local) => [local.name, local]));
|
|
268
|
+
for (const statement of path.node.body.body) {
|
|
269
|
+
if (types.isFunctionDeclaration(statement)) {
|
|
270
|
+
statements.push(types.cloneNode(statement, true));
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (!types.isVariableDeclaration(statement)) continue;
|
|
274
|
+
const id = statement.declarations[0].id;
|
|
275
|
+
if (types.isArrayPattern(id)) {
|
|
276
|
+
const [value2, setter] = id.elements;
|
|
277
|
+
const index = statesByValue.get(value2.name).index;
|
|
278
|
+
statements.push(
|
|
279
|
+
types.variableDeclaration("const", [
|
|
280
|
+
types.variableDeclarator(types.identifier(value2.name), types.callExpression(cell(index, "get"), [])),
|
|
281
|
+
types.variableDeclarator(types.identifier(setter.name), cell(index, "set"))
|
|
282
|
+
])
|
|
283
|
+
);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (!types.isIdentifier(id)) continue;
|
|
287
|
+
const ref = statesByValue.get(id.name);
|
|
288
|
+
const local = localsByName.get(id.name);
|
|
289
|
+
const value = ref?.ref ? types.callExpression(cell(ref.index, "get"), []) : local ? types.cloneNode(local.value, true) : void 0;
|
|
290
|
+
if (value) {
|
|
291
|
+
statements.push(
|
|
292
|
+
types.variableDeclaration("const", [types.variableDeclarator(types.identifier(id.name), value)])
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
statements.push(
|
|
297
|
+
types.returnStatement(
|
|
298
|
+
types.arrayExpression(
|
|
299
|
+
effects.map(
|
|
300
|
+
(effect) => types.arrayExpression([
|
|
301
|
+
types.cloneNode(effect.callback, true),
|
|
302
|
+
effect.dependencies ? types.cloneNode(effect.dependencies, true) : types.identifier("undefined"),
|
|
303
|
+
...effect.layout ? [types.booleanLiteral(true)] : []
|
|
304
|
+
])
|
|
305
|
+
)
|
|
306
|
+
)
|
|
307
|
+
)
|
|
308
|
+
);
|
|
309
|
+
return types.blockStatement(statements);
|
|
310
|
+
}
|
|
311
|
+
function refReadDuringRender(root, refNames) {
|
|
312
|
+
let read;
|
|
313
|
+
traverse(expressionFile(cloneExpression(root)), {
|
|
314
|
+
Identifier(path) {
|
|
315
|
+
if (read || !refNames.has(path.node.name) || !path.isReferencedIdentifier()) return;
|
|
316
|
+
if (path.findParent((parent) => parent.isFunction())) return;
|
|
317
|
+
const attribute = path.parentPath?.isJSXExpressionContainer() ? path.parentPath.parentPath : void 0;
|
|
318
|
+
if (attribute?.isJSXAttribute() && jsxAttributeName(attribute.node) === "ref") return;
|
|
319
|
+
read = path.node.name;
|
|
320
|
+
path.stop();
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
return read;
|
|
324
|
+
}
|
|
224
325
|
function cloneExpression(expression) {
|
|
225
326
|
return types.cloneNode(expression, true);
|
|
226
327
|
}
|
|
@@ -3499,6 +3600,33 @@ function reactOwnedAttribute(name, attribute) {
|
|
|
3499
3600
|
if (name === "defaultValue" || name === "defaultChecked") return true;
|
|
3500
3601
|
return (name === "spellCheck" || name === "draggable" || name === "contentEditable") && !types.isStringLiteral(attribute.value);
|
|
3501
3602
|
}
|
|
3603
|
+
function keyedRowFieldReason(element) {
|
|
3604
|
+
const tag = element.openingElement.name.name;
|
|
3605
|
+
const attributeNames = new Set(
|
|
3606
|
+
element.openingElement.attributes.filter((attribute) => types.isJSXAttribute(attribute)).map(jsxAttributeName).filter((name) => name !== void 0)
|
|
3607
|
+
);
|
|
3608
|
+
const controlled = attributeNames.has("value") || attributeNames.has("checked");
|
|
3609
|
+
if (attributeNames.has("contentEditable")) {
|
|
3610
|
+
return "content-editable keyed rows require React ownership";
|
|
3611
|
+
}
|
|
3612
|
+
if (tag === "input" && controlled) {
|
|
3613
|
+
const type = jsxAttribute(element, "type");
|
|
3614
|
+
const staticType = staticJsxAttributeString(type);
|
|
3615
|
+
if (type && staticType === void 0) {
|
|
3616
|
+
return "controlled keyed row inputs require a static type";
|
|
3617
|
+
}
|
|
3618
|
+
if (attributeNames.has("value") && staticType?.toLowerCase() === "file") {
|
|
3619
|
+
return "file inputs require React ownership";
|
|
3620
|
+
}
|
|
3621
|
+
}
|
|
3622
|
+
if (tag === "textarea" && controlled && meaningfulJsxChildren(element).length > 0) {
|
|
3623
|
+
return "controlled keyed row textareas cannot also use children";
|
|
3624
|
+
}
|
|
3625
|
+
if (tag === "select" && controlled && controlledSelectHasDynamicOptions(element)) {
|
|
3626
|
+
return "controlled keyed row selects require static options";
|
|
3627
|
+
}
|
|
3628
|
+
return void 0;
|
|
3629
|
+
}
|
|
3502
3630
|
function keyedRowEventHandler(attribute) {
|
|
3503
3631
|
if (!types.isJSXExpressionContainer(attribute.value) || !types.isArrowFunctionExpression(attribute.value.expression) && !types.isFunctionExpression(attribute.value.expression)) {
|
|
3504
3632
|
return "interactive compiled keyed rows require inline synchronous event handlers";
|
|
@@ -3514,31 +3642,52 @@ function keyedRowEventHandler(attribute) {
|
|
|
3514
3642
|
if (unsupported) return `interactive compiled keyed row handlers cannot use ${unsupported}`;
|
|
3515
3643
|
return handler;
|
|
3516
3644
|
}
|
|
3517
|
-
function
|
|
3645
|
+
function forEachKeyedRowElement(row, visit) {
|
|
3646
|
+
const walk = (element, location) => {
|
|
3647
|
+
visit(element, location);
|
|
3648
|
+
let elementIndex = 0;
|
|
3649
|
+
element.children.forEach((child, childIndex) => {
|
|
3650
|
+
if (types.isJSXElement(child)) {
|
|
3651
|
+
if (isHostElement(child))
|
|
3652
|
+
walk(child, location ? `${location}.${elementIndex}` : `${elementIndex}`);
|
|
3653
|
+
elementIndex += 1;
|
|
3654
|
+
return;
|
|
3655
|
+
}
|
|
3656
|
+
if (!types.isJSXExpressionContainer(child) || types.isJSXEmptyExpression(child.expression)) return;
|
|
3657
|
+
const shape = conditionalBlockShape(child.expression);
|
|
3658
|
+
if (shape?.truthy) walk(shape.truthy, `${location}/${childIndex}t`);
|
|
3659
|
+
if (shape?.falsy) walk(shape.falsy, `${location}/${childIndex}f`);
|
|
3660
|
+
});
|
|
3661
|
+
};
|
|
3662
|
+
walk(row, "");
|
|
3663
|
+
}
|
|
3664
|
+
function hostBlockKeyedRowEvents(row, compilerSetters) {
|
|
3518
3665
|
const events = [];
|
|
3519
|
-
const
|
|
3666
|
+
const elements = /* @__PURE__ */ new Map();
|
|
3667
|
+
const attributes = /* @__PURE__ */ new Set();
|
|
3668
|
+
let supported = true;
|
|
3669
|
+
forEachKeyedRowElement(row, (element, location) => {
|
|
3670
|
+
if (keyedRowFieldReason(element)) supported = false;
|
|
3520
3671
|
for (const attribute of element.openingElement.attributes) {
|
|
3521
3672
|
if (!types.isJSXAttribute(attribute)) continue;
|
|
3522
3673
|
const name = jsxAttributeName(attribute);
|
|
3523
3674
|
if (!name || !/^on[A-Z]/.test(name)) continue;
|
|
3524
3675
|
const handler = keyedRowEventHandler(attribute);
|
|
3525
|
-
if (typeof handler === "string")
|
|
3676
|
+
if (typeof handler === "string") {
|
|
3677
|
+
supported = false;
|
|
3678
|
+
continue;
|
|
3679
|
+
}
|
|
3680
|
+
elements.set(element, [...elements.get(element) || [], events.length]);
|
|
3681
|
+
attributes.add(attribute);
|
|
3526
3682
|
events.push({
|
|
3527
3683
|
id: events.length,
|
|
3528
|
-
|
|
3684
|
+
location,
|
|
3529
3685
|
name,
|
|
3530
3686
|
value: stabilizeDeferredEventCurrentTarget(handler, compilerSetters)
|
|
3531
3687
|
});
|
|
3532
3688
|
}
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
if (!types.isJSXElement(child)) continue;
|
|
3536
|
-
if (!visit(child, [...path, elementIndex])) return false;
|
|
3537
|
-
elementIndex += 1;
|
|
3538
|
-
}
|
|
3539
|
-
return true;
|
|
3540
|
-
};
|
|
3541
|
-
return visit(row, []) ? events : void 0;
|
|
3689
|
+
});
|
|
3690
|
+
return supported ? { events, elements, attributes } : void 0;
|
|
3542
3691
|
}
|
|
3543
3692
|
function analyzeKeyedRowTree(row, renderCallback, safeGlobals, compilerSetters, keyedTargetReactiveByValue) {
|
|
3544
3693
|
if (renderCallback.async || renderCallback.generator || renderCallback.params.length < 1 || renderCallback.params.length > 2 || !types.isIdentifier(renderCallback.params[0]) || renderCallback.params[1] !== void 0 && !types.isIdentifier(renderCallback.params[1])) {
|
|
@@ -3565,29 +3714,8 @@ function analyzeKeyedRowTree(row, renderCallback, safeGlobals, compilerSetters,
|
|
|
3565
3714
|
if (tag.name === "title" || tag.name === "meta") {
|
|
3566
3715
|
return "document metadata requires React ownership";
|
|
3567
3716
|
}
|
|
3568
|
-
const
|
|
3569
|
-
|
|
3570
|
-
);
|
|
3571
|
-
const controlled = attributeNames.has("value") || attributeNames.has("checked");
|
|
3572
|
-
if (attributeNames.has("contentEditable")) {
|
|
3573
|
-
return "content-editable keyed rows require React ownership";
|
|
3574
|
-
}
|
|
3575
|
-
if (tag.name === "input" && controlled) {
|
|
3576
|
-
const type = jsxAttribute(element, "type");
|
|
3577
|
-
const staticType = staticJsxAttributeString(type);
|
|
3578
|
-
if (type && staticType === void 0) {
|
|
3579
|
-
return "controlled keyed row inputs require a static type";
|
|
3580
|
-
}
|
|
3581
|
-
if (attributeNames.has("value") && staticType?.toLowerCase() === "file") {
|
|
3582
|
-
return "file inputs require React ownership";
|
|
3583
|
-
}
|
|
3584
|
-
}
|
|
3585
|
-
if (tag.name === "textarea" && controlled && meaningfulJsxChildren(element).length > 0) {
|
|
3586
|
-
return "controlled keyed row textareas cannot also use children";
|
|
3587
|
-
}
|
|
3588
|
-
if (tag.name === "select" && controlled && controlledSelectHasDynamicOptions(element)) {
|
|
3589
|
-
return "controlled keyed row selects require static options";
|
|
3590
|
-
}
|
|
3717
|
+
const fieldReason = keyedRowFieldReason(element);
|
|
3718
|
+
if (fieldReason) return fieldReason;
|
|
3591
3719
|
for (const attribute of element.openingElement.attributes) {
|
|
3592
3720
|
if (types.isJSXSpreadAttribute(attribute)) {
|
|
3593
3721
|
return "compiled keyed rows do not support JSX attribute spreads";
|
|
@@ -3605,6 +3733,7 @@ function analyzeKeyedRowTree(row, renderCallback, safeGlobals, compilerSetters,
|
|
|
3605
3733
|
events.push({
|
|
3606
3734
|
id: events.length,
|
|
3607
3735
|
path: [...path],
|
|
3736
|
+
location: path.join("."),
|
|
3608
3737
|
name,
|
|
3609
3738
|
value: stabilizeDeferredEventCurrentTarget(handler, compilerSetters)
|
|
3610
3739
|
});
|
|
@@ -4249,7 +4378,8 @@ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames,
|
|
|
4249
4378
|
safeGlobals,
|
|
4250
4379
|
listNames,
|
|
4251
4380
|
id,
|
|
4252
|
-
allocateId
|
|
4381
|
+
allocateId,
|
|
4382
|
+
delegatedEvents
|
|
4253
4383
|
) : {
|
|
4254
4384
|
bindings: [],
|
|
4255
4385
|
plans: [],
|
|
@@ -4261,7 +4391,8 @@ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames,
|
|
|
4261
4391
|
safeGlobals,
|
|
4262
4392
|
listNames,
|
|
4263
4393
|
id,
|
|
4264
|
-
allocateId
|
|
4394
|
+
allocateId,
|
|
4395
|
+
delegatedEvents
|
|
4265
4396
|
) : {
|
|
4266
4397
|
bindings: [],
|
|
4267
4398
|
plans: [],
|
|
@@ -4443,71 +4574,20 @@ function analyzeRecursiveHostTree(branch, statesByValue, safeGlobals, listNames,
|
|
|
4443
4574
|
const reason = visit(branch, [], parent);
|
|
4444
4575
|
return { bindings, plans, blocks, reason };
|
|
4445
4576
|
}
|
|
4446
|
-
function jsxCanRenderElements(expression) {
|
|
4447
|
-
let found = false;
|
|
4448
|
-
traverse(expressionFile(cloneExpression(expression)), {
|
|
4449
|
-
JSXElement(path) {
|
|
4450
|
-
found = true;
|
|
4451
|
-
path.stop();
|
|
4452
|
-
},
|
|
4453
|
-
JSXFragment(path) {
|
|
4454
|
-
found = true;
|
|
4455
|
-
path.stop();
|
|
4456
|
-
}
|
|
4457
|
-
});
|
|
4458
|
-
return found;
|
|
4459
|
-
}
|
|
4460
|
-
function keyedRowEventPathIsStable(row, path) {
|
|
4461
|
-
let element = row;
|
|
4462
|
-
for (const index of path) {
|
|
4463
|
-
let seen = 0;
|
|
4464
|
-
let next;
|
|
4465
|
-
for (const child of element.children) {
|
|
4466
|
-
if (types.isJSXElement(child)) {
|
|
4467
|
-
if (!isHostElement(child)) return false;
|
|
4468
|
-
if (seen === index) {
|
|
4469
|
-
next = child;
|
|
4470
|
-
break;
|
|
4471
|
-
}
|
|
4472
|
-
seen += 1;
|
|
4473
|
-
continue;
|
|
4474
|
-
}
|
|
4475
|
-
if (types.isJSXFragment(child)) return false;
|
|
4476
|
-
if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression) && jsxCanRenderElements(child.expression)) {
|
|
4477
|
-
return false;
|
|
4478
|
-
}
|
|
4479
|
-
}
|
|
4480
|
-
if (!next) return false;
|
|
4481
|
-
element = next;
|
|
4482
|
-
}
|
|
4483
|
-
return true;
|
|
4484
|
-
}
|
|
4485
4577
|
function analyzeCompilerOwnedKeyedRowHostBlocks(shape, container, statesByValue, safeGlobals, listNames, parent, allocateId, delegateWithoutReact = false) {
|
|
4486
|
-
const events = shape.rowReason ? staticKeyedRowEvents(
|
|
4487
|
-
shape.row,
|
|
4488
|
-
new Set([...statesByValue.values()].map((state) => state.setterName))
|
|
4489
|
-
) : shape.events;
|
|
4490
|
-
if (!events || events.length > 0 && (!canDelegateKeyedRowEvents(
|
|
4491
|
-
{ events, conditionals: [], source: container },
|
|
4492
|
-
delegateWithoutReact
|
|
4493
|
-
) || !events.every((event) => keyedRowEventPathIsStable(shape.row, event.path)))) {
|
|
4494
|
-
return void 0;
|
|
4495
|
-
}
|
|
4496
4578
|
const row = types.cloneNode(shape.row, true);
|
|
4497
4579
|
row.openingElement.attributes = row.openingElement.attributes.filter(
|
|
4498
4580
|
(attribute) => !types.isJSXAttribute(attribute) || jsxAttributeName(attribute) !== "key"
|
|
4499
4581
|
);
|
|
4500
|
-
const
|
|
4501
|
-
|
|
4502
|
-
|
|
4503
|
-
|
|
4504
|
-
|
|
4505
|
-
}
|
|
4506
|
-
|
|
4507
|
-
|
|
4508
|
-
|
|
4509
|
-
}
|
|
4510
|
-
}
|
|
4582
|
+
const rowEvents = hostBlockKeyedRowEvents(
|
|
4583
|
+
row,
|
|
4584
|
+
new Set([...statesByValue.values()].map((state) => state.setterName))
|
|
4585
|
+
);
|
|
4586
|
+
if (!rowEvents || rowEvents.events.length > 0 && !canDelegateKeyedRowEvents(
|
|
4587
|
+
{ events: rowEvents.events, conditionals: [], source: container },
|
|
4588
|
+
delegateWithoutReact
|
|
4589
|
+
)) {
|
|
4590
|
+
return void 0;
|
|
4511
4591
|
}
|
|
4512
4592
|
const analysis = analyzeRecursiveHostTree(
|
|
4513
4593
|
row,
|
|
@@ -4516,7 +4596,7 @@ function analyzeCompilerOwnedKeyedRowHostBlocks(shape, container, statesByValue,
|
|
|
4516
4596
|
listNames,
|
|
4517
4597
|
parent,
|
|
4518
4598
|
allocateId,
|
|
4519
|
-
|
|
4599
|
+
rowEvents.attributes
|
|
4520
4600
|
);
|
|
4521
4601
|
if (analysis.reason || analysis.plans.length === 0) {
|
|
4522
4602
|
return void 0;
|
|
@@ -4526,7 +4606,8 @@ function analyzeCompilerOwnedKeyedRowHostBlocks(shape, container, statesByValue,
|
|
|
4526
4606
|
bindings: analysis.bindings,
|
|
4527
4607
|
descriptorBlocks: analysis.blocks,
|
|
4528
4608
|
nestedPlans: analysis.plans,
|
|
4529
|
-
events
|
|
4609
|
+
events: rowEvents.events,
|
|
4610
|
+
eventElements: rowEvents.elements
|
|
4530
4611
|
};
|
|
4531
4612
|
}
|
|
4532
4613
|
function analyzeHostConditionalContainer(container, statesByValue, safeGlobals, listNames, parent, allocateId) {
|
|
@@ -4728,7 +4809,8 @@ function analyzeMixedRangesContainer(container, statesByValue, safeGlobals, list
|
|
|
4728
4809
|
safeGlobals,
|
|
4729
4810
|
listNames,
|
|
4730
4811
|
parent,
|
|
4731
|
-
allocateId
|
|
4812
|
+
allocateId,
|
|
4813
|
+
delegatedEvents
|
|
4732
4814
|
) : {
|
|
4733
4815
|
bindings: [],
|
|
4734
4816
|
plans: [],
|
|
@@ -4740,7 +4822,8 @@ function analyzeMixedRangesContainer(container, statesByValue, safeGlobals, list
|
|
|
4740
4822
|
safeGlobals,
|
|
4741
4823
|
listNames,
|
|
4742
4824
|
parent,
|
|
4743
|
-
allocateId
|
|
4825
|
+
allocateId,
|
|
4826
|
+
delegatedEvents
|
|
4744
4827
|
) : {
|
|
4745
4828
|
bindings: [],
|
|
4746
4829
|
plans: [],
|
|
@@ -4851,7 +4934,7 @@ function keyedListBoundary(blockRuntime, plan, source) {
|
|
|
4851
4934
|
true
|
|
4852
4935
|
);
|
|
4853
4936
|
}
|
|
4854
|
-
function hostElementDescriptor(element, descriptorBlocks = /* @__PURE__ */ new Map()) {
|
|
4937
|
+
function hostElementDescriptor(element, descriptorBlocks = /* @__PURE__ */ new Map(), eventElements) {
|
|
4855
4938
|
const tag = element.openingElement.name;
|
|
4856
4939
|
const attributes = [];
|
|
4857
4940
|
const styles = [];
|
|
@@ -4907,13 +4990,15 @@ function hostElementDescriptor(element, descriptorBlocks = /* @__PURE__ */ new M
|
|
|
4907
4990
|
children.push(keyedRangeDescriptorChildren(keyedRange, descriptorBlocks));
|
|
4908
4991
|
}
|
|
4909
4992
|
} else {
|
|
4910
|
-
children.push(hostElementDescriptor(child, descriptorBlocks));
|
|
4993
|
+
children.push(hostElementDescriptor(child, descriptorBlocks, eventElements));
|
|
4911
4994
|
}
|
|
4912
4995
|
} else if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression)) {
|
|
4913
4996
|
const conditionalRange = conditionalBySource.get(child.expression);
|
|
4914
4997
|
const keyedRange = keyedBySource.get(child.expression);
|
|
4915
4998
|
if (conditionalRange) {
|
|
4916
|
-
children.push(
|
|
4999
|
+
children.push(
|
|
5000
|
+
conditionalRangeDescriptorChild(conditionalRange, descriptorBlocks, eventElements)
|
|
5001
|
+
);
|
|
4917
5002
|
} else if (keyedRange) {
|
|
4918
5003
|
if (block?.kind !== "nested-host-keyed-ranges") {
|
|
4919
5004
|
children.push(keyedRangeDescriptorChildren(keyedRange, descriptorBlocks));
|
|
@@ -4930,22 +5015,34 @@ function hostElementDescriptor(element, descriptorBlocks = /* @__PURE__ */ new M
|
|
|
4930
5015
|
types.objectProperty(types.identifier("styles"), types.arrayExpression(styles)),
|
|
4931
5016
|
types.objectProperty(types.identifier("children"), types.arrayExpression(children))
|
|
4932
5017
|
];
|
|
5018
|
+
const events = eventElements?.get(element);
|
|
5019
|
+
if (events) {
|
|
5020
|
+
properties.push(
|
|
5021
|
+
types.objectProperty(
|
|
5022
|
+
types.identifier("events"),
|
|
5023
|
+
types.arrayExpression(events.map((id) => types.numericLiteral(id)))
|
|
5024
|
+
)
|
|
5025
|
+
);
|
|
5026
|
+
}
|
|
4933
5027
|
if (block) {
|
|
4934
5028
|
properties.push(
|
|
4935
|
-
types.objectProperty(
|
|
5029
|
+
types.objectProperty(
|
|
5030
|
+
types.identifier("block"),
|
|
5031
|
+
nestedHostBlockObject(block, descriptorBlocks, eventElements)
|
|
5032
|
+
)
|
|
4936
5033
|
);
|
|
4937
5034
|
}
|
|
4938
5035
|
return types.objectExpression(properties);
|
|
4939
5036
|
}
|
|
4940
|
-
function conditionalRangeDescriptorChild(range, descriptorBlocks) {
|
|
4941
|
-
const truthy = range.truthy ? hostElementDescriptor(range.truthy, descriptorBlocks) : types.nullLiteral();
|
|
5037
|
+
function conditionalRangeDescriptorChild(range, descriptorBlocks, eventElements) {
|
|
5038
|
+
const truthy = range.truthy ? hostElementDescriptor(range.truthy, descriptorBlocks, eventElements) : types.nullLiteral();
|
|
4942
5039
|
if (range.logical) {
|
|
4943
5040
|
return types.logicalExpression("&&", cloneExpression(range.test), truthy);
|
|
4944
5041
|
}
|
|
4945
5042
|
return types.conditionalExpression(
|
|
4946
5043
|
cloneExpression(range.test),
|
|
4947
5044
|
truthy,
|
|
4948
|
-
range.falsy ? hostElementDescriptor(range.falsy, descriptorBlocks) : types.nullLiteral()
|
|
5045
|
+
range.falsy ? hostElementDescriptor(range.falsy, descriptorBlocks, eventElements) : types.nullLiteral()
|
|
4949
5046
|
);
|
|
4950
5047
|
}
|
|
4951
5048
|
function keyedRangeDescriptorChildren(range, descriptorBlocks) {
|
|
@@ -5073,14 +5170,14 @@ function uniqueLocalIdentifier(base, nodes) {
|
|
|
5073
5170
|
}
|
|
5074
5171
|
function rewriteKeyedRowEventAttributes(row, events, eventFactory, item, index, extraArguments = []) {
|
|
5075
5172
|
const eventByLocation = new Map(
|
|
5076
|
-
events.map((event) => [`${event.
|
|
5173
|
+
events.map((event) => [`${event.location}:${event.name}`, event])
|
|
5077
5174
|
);
|
|
5078
|
-
|
|
5175
|
+
forEachKeyedRowElement(row, (element, location) => {
|
|
5079
5176
|
for (const attribute of element.openingElement.attributes) {
|
|
5080
5177
|
if (!types.isJSXAttribute(attribute)) continue;
|
|
5081
5178
|
const name = jsxAttributeName(attribute);
|
|
5082
5179
|
if (!name || !/^on[A-Z]/.test(name)) continue;
|
|
5083
|
-
const event = eventByLocation.get(`${
|
|
5180
|
+
const event = eventByLocation.get(`${location}:${name}`);
|
|
5084
5181
|
if (!event) continue;
|
|
5085
5182
|
attribute.value = types.jsxExpressionContainer(
|
|
5086
5183
|
types.callExpression(types.cloneNode(eventFactory), [
|
|
@@ -5091,14 +5188,7 @@ function rewriteKeyedRowEventAttributes(row, events, eventFactory, item, index,
|
|
|
5091
5188
|
])
|
|
5092
5189
|
);
|
|
5093
5190
|
}
|
|
5094
|
-
|
|
5095
|
-
for (const child of element.children) {
|
|
5096
|
-
if (!types.isJSXElement(child)) continue;
|
|
5097
|
-
visit(child, [...path, elementIndex]);
|
|
5098
|
-
elementIndex += 1;
|
|
5099
|
-
}
|
|
5100
|
-
};
|
|
5101
|
-
visit(row, []);
|
|
5191
|
+
});
|
|
5102
5192
|
}
|
|
5103
5193
|
function rewriteKeyedRowConditionalExpressions(row, conditionals, conditionalFactory, item, index, renderParameters) {
|
|
5104
5194
|
const conditionalByLocation = new Map(
|
|
@@ -5176,10 +5266,12 @@ function keyedRowEventObject(event, renderParameters) {
|
|
|
5176
5266
|
const parameters = [types.cloneNode(renderParameters[0], true), index, syntheticEvent];
|
|
5177
5267
|
return types.objectExpression([
|
|
5178
5268
|
types.objectProperty(types.identifier("name"), types.stringLiteral(event.name)),
|
|
5179
|
-
|
|
5180
|
-
types.
|
|
5181
|
-
|
|
5182
|
-
|
|
5269
|
+
...event.path ? [
|
|
5270
|
+
types.objectProperty(
|
|
5271
|
+
types.identifier("path"),
|
|
5272
|
+
types.arrayExpression(event.path.map((part) => types.numericLiteral(part)))
|
|
5273
|
+
)
|
|
5274
|
+
] : [],
|
|
5183
5275
|
types.objectProperty(
|
|
5184
5276
|
types.identifier("invoke"),
|
|
5185
5277
|
types.arrowFunctionExpression(
|
|
@@ -5390,7 +5482,7 @@ function keyedRowsBoundary(blockRuntime, plan, keyedMapUpdateHints, keyedArrayFi
|
|
|
5390
5482
|
types.jsxExpressionContainer(
|
|
5391
5483
|
types.arrowFunctionExpression(
|
|
5392
5484
|
parameters.map((parameter) => types.cloneNode(parameter, true)),
|
|
5393
|
-
hostElementDescriptor(plan.row, plan.descriptorBlocks)
|
|
5485
|
+
hostElementDescriptor(plan.row, plan.descriptorBlocks, plan.eventElements)
|
|
5394
5486
|
)
|
|
5395
5487
|
)
|
|
5396
5488
|
),
|
|
@@ -5586,11 +5678,11 @@ function keyedRangesBoundary(blockRuntime, plan) {
|
|
|
5586
5678
|
true
|
|
5587
5679
|
);
|
|
5588
5680
|
}
|
|
5589
|
-
function hostConditionalBranchObject(branch, bindings, descriptorBlocks = /* @__PURE__ */ new Map()) {
|
|
5681
|
+
function hostConditionalBranchObject(branch, bindings, descriptorBlocks = /* @__PURE__ */ new Map(), eventElements) {
|
|
5590
5682
|
return types.objectExpression([
|
|
5591
5683
|
types.objectProperty(
|
|
5592
5684
|
types.identifier("create"),
|
|
5593
|
-
types.arrowFunctionExpression([], hostElementDescriptor(branch, descriptorBlocks))
|
|
5685
|
+
types.arrowFunctionExpression([], hostElementDescriptor(branch, descriptorBlocks, eventElements))
|
|
5594
5686
|
),
|
|
5595
5687
|
types.objectProperty(
|
|
5596
5688
|
types.identifier("bindings"),
|
|
@@ -5637,7 +5729,7 @@ function hostConditionalBoundary(blockRuntime, plan) {
|
|
|
5637
5729
|
}
|
|
5638
5730
|
return types.jsxElement(types.jsxOpeningElement(name, attributes, true), null, [], true);
|
|
5639
5731
|
}
|
|
5640
|
-
function conditionalRangeObject(range, descriptorBlocks = /* @__PURE__ */ new Map()) {
|
|
5732
|
+
function conditionalRangeObject(range, descriptorBlocks = /* @__PURE__ */ new Map(), eventElements) {
|
|
5641
5733
|
const properties = [
|
|
5642
5734
|
types.objectProperty(types.identifier("before"), types.numericLiteral(range.before)),
|
|
5643
5735
|
types.objectProperty(
|
|
@@ -5652,7 +5744,12 @@ function conditionalRangeObject(range, descriptorBlocks = /* @__PURE__ */ new Ma
|
|
|
5652
5744
|
properties.push(
|
|
5653
5745
|
types.objectProperty(
|
|
5654
5746
|
types.identifier("truthy"),
|
|
5655
|
-
hostConditionalBranchObject(
|
|
5747
|
+
hostConditionalBranchObject(
|
|
5748
|
+
range.truthy,
|
|
5749
|
+
range.truthyBindings,
|
|
5750
|
+
descriptorBlocks,
|
|
5751
|
+
eventElements
|
|
5752
|
+
)
|
|
5656
5753
|
)
|
|
5657
5754
|
);
|
|
5658
5755
|
}
|
|
@@ -5660,13 +5757,18 @@ function conditionalRangeObject(range, descriptorBlocks = /* @__PURE__ */ new Ma
|
|
|
5660
5757
|
properties.push(
|
|
5661
5758
|
types.objectProperty(
|
|
5662
5759
|
types.identifier("falsy"),
|
|
5663
|
-
hostConditionalBranchObject(
|
|
5760
|
+
hostConditionalBranchObject(
|
|
5761
|
+
range.falsy,
|
|
5762
|
+
range.falsyBindings,
|
|
5763
|
+
descriptorBlocks,
|
|
5764
|
+
eventElements
|
|
5765
|
+
)
|
|
5664
5766
|
)
|
|
5665
5767
|
);
|
|
5666
5768
|
}
|
|
5667
5769
|
return types.objectExpression(properties);
|
|
5668
5770
|
}
|
|
5669
|
-
function nestedHostBlockObject(plan, descriptorBlocks) {
|
|
5771
|
+
function nestedHostBlockObject(plan, descriptorBlocks, eventElements) {
|
|
5670
5772
|
const kind = plan.kind === "nested-host-conditional-ranges" ? "conditional-ranges" : plan.kind === "nested-host-keyed-ranges" ? "keyed-ranges" : "mixed-ranges";
|
|
5671
5773
|
return types.objectExpression([
|
|
5672
5774
|
types.objectProperty(types.identifier("kind"), types.stringLiteral(kind)),
|
|
@@ -5674,7 +5776,9 @@ function nestedHostBlockObject(plan, descriptorBlocks) {
|
|
|
5674
5776
|
types.objectProperty(
|
|
5675
5777
|
types.identifier("ranges"),
|
|
5676
5778
|
types.arrayExpression(
|
|
5677
|
-
plan.kind === "nested-host-conditional-ranges" ? plan.ranges.map(
|
|
5779
|
+
plan.kind === "nested-host-conditional-ranges" ? plan.ranges.map(
|
|
5780
|
+
(range) => conditionalRangeObject(range, descriptorBlocks, eventElements)
|
|
5781
|
+
) : plan.kind === "nested-host-keyed-ranges" ? plan.ranges.map((range) => keyedRangeObject(range, descriptorBlocks)) : plan.ranges.map((range) => mixedRangeObject(range, descriptorBlocks, eventElements))
|
|
5678
5782
|
)
|
|
5679
5783
|
),
|
|
5680
5784
|
types.objectProperty(types.identifier("trailing"), types.numericLiteral(plan.trailing)),
|
|
@@ -5685,8 +5789,8 @@ function nestedHostBlockObject(plan, descriptorBlocks) {
|
|
|
5685
5789
|
...plan.kind === "nested-host-keyed-ranges" ? [types.objectProperty(types.identifier("staticChildrenOnly"), types.booleanLiteral(true))] : []
|
|
5686
5790
|
]);
|
|
5687
5791
|
}
|
|
5688
|
-
function mixedRangeObject(range, descriptorBlocks) {
|
|
5689
|
-
const value = range.rangeKind === "conditional" ? conditionalRangeObject(range, descriptorBlocks) : keyedRangeObject(range, descriptorBlocks);
|
|
5792
|
+
function mixedRangeObject(range, descriptorBlocks, eventElements) {
|
|
5793
|
+
const value = range.rangeKind === "conditional" ? conditionalRangeObject(range, descriptorBlocks, eventElements) : keyedRangeObject(range, descriptorBlocks);
|
|
5690
5794
|
value.properties.unshift(
|
|
5691
5795
|
types.objectProperty(types.identifier("kind"), types.stringLiteral(range.rangeKind))
|
|
5692
5796
|
);
|
|
@@ -6101,6 +6205,7 @@ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, al
|
|
|
6101
6205
|
events: hostBlocks?.events || keyedRows.events,
|
|
6102
6206
|
conditionals: hostBlocks ? [] : keyedRows.conditionals,
|
|
6103
6207
|
descriptorBlocks: hostBlocks?.descriptorBlocks,
|
|
6208
|
+
eventElements: hostBlocks?.eventElements,
|
|
6104
6209
|
syntax: keyedRows.syntax
|
|
6105
6210
|
});
|
|
6106
6211
|
plans.push(...hostBlocks?.nestedPlans || []);
|
|
@@ -6274,6 +6379,53 @@ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, al
|
|
|
6274
6379
|
keyedExpressions
|
|
6275
6380
|
};
|
|
6276
6381
|
}
|
|
6382
|
+
const rootKeyedRowsStart = nextId;
|
|
6383
|
+
const rootKeyedRowsId = nextId++;
|
|
6384
|
+
const rootKeyedRows = analyzeKeyedRowsContainer(root, statesByValue, safeGlobals, listNames);
|
|
6385
|
+
const rootHostBlocks = rootKeyedRows && analyzeCompilerOwnedKeyedRowHostBlocks(
|
|
6386
|
+
rootKeyedRows,
|
|
6387
|
+
root,
|
|
6388
|
+
statesByValue,
|
|
6389
|
+
safeGlobals,
|
|
6390
|
+
listNames,
|
|
6391
|
+
rootKeyedRowsId,
|
|
6392
|
+
() => nextId++,
|
|
6393
|
+
delegateWithoutReact
|
|
6394
|
+
);
|
|
6395
|
+
if (rootKeyedRows && rootHostBlocks) {
|
|
6396
|
+
plans.push({
|
|
6397
|
+
kind: "keyed-rows",
|
|
6398
|
+
id: rootKeyedRowsId,
|
|
6399
|
+
dependencies: rootKeyedRows.dependencies,
|
|
6400
|
+
source: root,
|
|
6401
|
+
collection: rootKeyedRows.collection,
|
|
6402
|
+
collectionDependency: rootKeyedRows.collectionDependency,
|
|
6403
|
+
structureDependencies: rootKeyedRows.structureDependencies,
|
|
6404
|
+
keyCallback: rootKeyedRows.keyCallback,
|
|
6405
|
+
renderCallback: rootKeyedRows.renderCallback,
|
|
6406
|
+
row: rootHostBlocks.row,
|
|
6407
|
+
bindings: rootHostBlocks.bindings,
|
|
6408
|
+
events: rootHostBlocks.events,
|
|
6409
|
+
conditionals: [],
|
|
6410
|
+
descriptorBlocks: rootHostBlocks.descriptorBlocks,
|
|
6411
|
+
eventElements: rootHostBlocks.eventElements,
|
|
6412
|
+
syntax: rootKeyedRows.syntax
|
|
6413
|
+
});
|
|
6414
|
+
plans.push(...rootHostBlocks.nestedPlans);
|
|
6415
|
+
const list = meaningfulJsxChildren(root)[0];
|
|
6416
|
+
if (types.isJSXElement(list)) ownedElements.add(list);
|
|
6417
|
+
else if (types.isJSXExpressionContainer(list) && !types.isJSXEmptyExpression(list.expression)) {
|
|
6418
|
+
keyedExpressions.add(list.expression);
|
|
6419
|
+
}
|
|
6420
|
+
return {
|
|
6421
|
+
plans,
|
|
6422
|
+
componentElements,
|
|
6423
|
+
conditionalExpressions,
|
|
6424
|
+
ownedElements,
|
|
6425
|
+
keyedExpressions
|
|
6426
|
+
};
|
|
6427
|
+
}
|
|
6428
|
+
nextId = rootKeyedRowsStart;
|
|
6277
6429
|
const rootConditionalRangeStart = nextId;
|
|
6278
6430
|
const rootConditionalRangeId = nextId++;
|
|
6279
6431
|
const rootConditionalRanges = analyzeConditionalRangesContainer(
|
|
@@ -6355,6 +6507,17 @@ function lowerComposableBlocks(root, plans, blockRuntime, listNames, keyedMapUpd
|
|
|
6355
6507
|
if (rootPlan?.kind === "conditional-ranges") {
|
|
6356
6508
|
return conditionalRangesBoundary(blockRuntime, rootPlan);
|
|
6357
6509
|
}
|
|
6510
|
+
if (rootPlan?.kind === "keyed-rows") {
|
|
6511
|
+
return keyedRowsBoundary(
|
|
6512
|
+
blockRuntime,
|
|
6513
|
+
rootPlan,
|
|
6514
|
+
keyedMapUpdateHints,
|
|
6515
|
+
keyedArrayFilterHintedStateIndices,
|
|
6516
|
+
keyedArrayPrependHintedStateIndices,
|
|
6517
|
+
keyedArrayPositionHintedStateIndices,
|
|
6518
|
+
keyedArrayReorderHintedStateIndices
|
|
6519
|
+
);
|
|
6520
|
+
}
|
|
6358
6521
|
const visit = (element) => {
|
|
6359
6522
|
element.children = element.children.map((child) => {
|
|
6360
6523
|
if (types.isJSXElement(child)) {
|
|
@@ -6457,13 +6620,18 @@ function lowerStableBindingTargets(root, bindings, blockRuntime, ownedElements)
|
|
|
6457
6620
|
const visit = (element, path) => {
|
|
6458
6621
|
const target = targetByPath.get(path.join("."));
|
|
6459
6622
|
if (target !== void 0 && path.length > 0) {
|
|
6460
|
-
element.openingElement.attributes
|
|
6623
|
+
const attributes = element.openingElement.attributes;
|
|
6624
|
+
const userRefIndex = attributes.findIndex(
|
|
6625
|
+
(attribute) => types.isJSXAttribute(attribute) && jsxAttributeName(attribute) === "ref"
|
|
6626
|
+
);
|
|
6627
|
+
const userRef = userRefIndex >= 0 ? attributes.splice(userRefIndex, 1)[0].value.expression : void 0;
|
|
6628
|
+
attributes.push(
|
|
6461
6629
|
types.jsxAttribute(
|
|
6462
6630
|
types.jsxIdentifier("ref"),
|
|
6463
6631
|
types.jsxExpressionContainer(
|
|
6464
6632
|
types.callExpression(
|
|
6465
6633
|
types.memberExpression(types.cloneNode(blockRuntime), types.identifier("target")),
|
|
6466
|
-
[types.numericLiteral(target)]
|
|
6634
|
+
[types.numericLiteral(target), ...userRef && types.isExpression(userRef) ? [userRef] : []]
|
|
6467
6635
|
)
|
|
6468
6636
|
)
|
|
6469
6637
|
)
|
|
@@ -6522,9 +6690,14 @@ function analyzeHostTree(root, statesByValue, safeGlobals, conditionalExpression
|
|
|
6522
6690
|
const name = jsxAttributeName(attribute);
|
|
6523
6691
|
if (!name) return "namespaced JSX attributes are not supported yet";
|
|
6524
6692
|
if (name === "is") return "customized built-in elements require React ownership";
|
|
6525
|
-
if (name === "ref"
|
|
6526
|
-
|
|
6693
|
+
if (name === "ref") {
|
|
6694
|
+
const value = types.isJSXExpressionContainer(attribute.value) ? attribute.value.expression : void 0;
|
|
6695
|
+
if (path.length > 0 && types.isIdentifier(value) && statesByValue.get(value.name)?.ref) {
|
|
6696
|
+
continue;
|
|
6697
|
+
}
|
|
6698
|
+
return "ref requires React ownership";
|
|
6527
6699
|
}
|
|
6700
|
+
if (name === "dangerouslySetInnerHTML") return `${name} requires React ownership`;
|
|
6528
6701
|
if (!types.isJSXExpressionContainer(attribute.value) || types.isJSXEmptyExpression(attribute.value.expression)) {
|
|
6529
6702
|
continue;
|
|
6530
6703
|
}
|
|
@@ -6759,7 +6932,7 @@ function wrapperElement(definitionIdentifier, props) {
|
|
|
6759
6932
|
const attributes = props ? [types.jsxSpreadAttribute(types.cloneNode(props))] : [];
|
|
6760
6933
|
return types.jsxElement(types.jsxOpeningElement(name, attributes, true), null, [], true);
|
|
6761
6934
|
}
|
|
6762
|
-
function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateIdentifier, keyedArrayAppendIdentifier, keyedArrayFilterIdentifier, keyedArrayMapPipelineIdentifier, keyedArrayMappedStructuralAppendIdentifier, keyedArrayStructuralAppendMapPipelineIdentifier, keyedArrayMappedStructuralPrependIdentifier, keyedArrayStructuralPrependMapPipelineIdentifier, keyedArrayQueuedMapPipelineIdentifier, keyedArrayRollingWindowMapPipelineIdentifier, keyedArrayMappedRollingWindowIdentifier, keyedArrayMapReorderIdentifier, keyedArrayMappedStructuralIdentifier, keyedArrayPrependIdentifier, keyedArrayPositionIdentifier, keyedArrayBatchInsertIdentifier, keyedArrayWindowReplaceIdentifier, keyedArrayReorderIdentifier, keyedArrayStructuralReorderIdentifier, keyedArraySortIdentifier, keyedArrayStructuralSortIdentifier, keyedArrayRollingWindowIdentifier, keyedArraySliceIdentifier, keyedCollectionUpdateIdentifier, keyedCollectionMutationIdentifier, runtimeFeatureIdentifiers, usedRuntimeFeatures, optimizationCounts, compilerUsage, useStateNames, reactNames, listNames, moduleId, reactivity, delegateWithoutReact) {
|
|
6935
|
+
function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateIdentifier, keyedArrayAppendIdentifier, keyedArrayFilterIdentifier, keyedArrayMapPipelineIdentifier, keyedArrayMappedStructuralAppendIdentifier, keyedArrayStructuralAppendMapPipelineIdentifier, keyedArrayMappedStructuralPrependIdentifier, keyedArrayStructuralPrependMapPipelineIdentifier, keyedArrayQueuedMapPipelineIdentifier, keyedArrayRollingWindowMapPipelineIdentifier, keyedArrayMappedRollingWindowIdentifier, keyedArrayMapReorderIdentifier, keyedArrayMappedStructuralIdentifier, keyedArrayPrependIdentifier, keyedArrayPositionIdentifier, keyedArrayBatchInsertIdentifier, keyedArrayWindowReplaceIdentifier, keyedArrayReorderIdentifier, keyedArrayStructuralReorderIdentifier, keyedArraySortIdentifier, keyedArrayStructuralSortIdentifier, keyedArrayRollingWindowIdentifier, keyedArraySliceIdentifier, keyedCollectionUpdateIdentifier, keyedCollectionMutationIdentifier, runtimeFeatureIdentifiers, usedRuntimeFeatures, optimizationCounts, compilerUsage, useStateNames, hookNames, reactNames, listNames, moduleId, reactivity, delegateWithoutReact) {
|
|
6763
6936
|
const { path, name, statementPath } = candidate;
|
|
6764
6937
|
if (path.node.async || path.node.generator)
|
|
6765
6938
|
return "async and generator components are not supported";
|
|
@@ -6774,6 +6947,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6774
6947
|
const globalCollections = new Set(["Set", "Map"].filter((name2) => !path.scope.getBinding(name2)));
|
|
6775
6948
|
const states = [];
|
|
6776
6949
|
const locals = [];
|
|
6950
|
+
const effects = [];
|
|
6951
|
+
const memoDeclarations = [];
|
|
6777
6952
|
let returned;
|
|
6778
6953
|
for (const statement of path.node.body.body) {
|
|
6779
6954
|
if (types.isReturnStatement(statement)) {
|
|
@@ -6781,6 +6956,25 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6781
6956
|
returned = statement;
|
|
6782
6957
|
continue;
|
|
6783
6958
|
}
|
|
6959
|
+
if (types.isExpressionStatement(statement)) {
|
|
6960
|
+
const hook = reactHookCall(statement.expression, hookNames, reactNames);
|
|
6961
|
+
if (hook === "useEffect" || hook === "useLayoutEffect") {
|
|
6962
|
+
const [callback, dependencyList, ...rest] = statement.expression.arguments;
|
|
6963
|
+
if (rest.length > 0 || !types.isArrowFunctionExpression(callback) && !types.isFunctionExpression(callback) || callback.async || callback.generator || callback.params.length > 0) {
|
|
6964
|
+
return `${hook} needs one inline synchronous callback`;
|
|
6965
|
+
}
|
|
6966
|
+
if (containsDirectHookCall(callback)) return `Hooks cannot be called inside ${hook}`;
|
|
6967
|
+
if (dependencyList && (!types.isArrayExpression(dependencyList) || !dependencyList.elements.every((element) => types.isIdentifier(element)))) {
|
|
6968
|
+
return `${hook} dependencies must be an array of names`;
|
|
6969
|
+
}
|
|
6970
|
+
effects.push({
|
|
6971
|
+
callback,
|
|
6972
|
+
dependencies: dependencyList,
|
|
6973
|
+
layout: hook === "useLayoutEffect"
|
|
6974
|
+
});
|
|
6975
|
+
continue;
|
|
6976
|
+
}
|
|
6977
|
+
}
|
|
6784
6978
|
if (types.isFunctionDeclaration(statement)) {
|
|
6785
6979
|
if (!statement.id) return "named event handler declarations must have a name";
|
|
6786
6980
|
if (statement.generator || statement.typeParameters) {
|
|
@@ -6813,21 +7007,47 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6813
7007
|
if (!types.isIdentifier(declaration.id) || !types.isExpression(declaration.init)) {
|
|
6814
7008
|
return "derived const values and event handlers must use one identifier and one expression";
|
|
6815
7009
|
}
|
|
6816
|
-
|
|
6817
|
-
|
|
7010
|
+
let init = declaration.init;
|
|
7011
|
+
if (reactHookCall(init, hookNames, reactNames) === "useRef") {
|
|
7012
|
+
const [initial, ...rest] = init.arguments;
|
|
7013
|
+
if (rest.length > 0 || initial && !types.isExpression(initial)) {
|
|
7014
|
+
return "useRef takes at most one initial value";
|
|
7015
|
+
}
|
|
7016
|
+
states.push({
|
|
7017
|
+
valueName: declaration.id.name,
|
|
7018
|
+
setterName: path.scope.generateUidIdentifier("farmRef").name,
|
|
7019
|
+
index: states.length,
|
|
7020
|
+
initialValue: types.objectExpression([
|
|
7021
|
+
types.objectProperty(
|
|
7022
|
+
types.identifier("current"),
|
|
7023
|
+
initial ? initial : types.identifier("undefined")
|
|
7024
|
+
)
|
|
7025
|
+
]),
|
|
7026
|
+
ref: true
|
|
7027
|
+
});
|
|
7028
|
+
continue;
|
|
7029
|
+
}
|
|
7030
|
+
const memo = memoHookValue(init, hookNames, reactNames);
|
|
7031
|
+
if (typeof memo === "string") return memo;
|
|
7032
|
+
if (memo) {
|
|
7033
|
+
memoDeclarations.push({ name: declaration.id.name, ...memo });
|
|
7034
|
+
init = memo.value;
|
|
7035
|
+
}
|
|
7036
|
+
if (types.isArrowFunctionExpression(init) || types.isFunctionExpression(init)) {
|
|
7037
|
+
if (init.generator) {
|
|
6818
7038
|
return `event handler ${declaration.id.name} cannot be a generator`;
|
|
6819
7039
|
}
|
|
6820
7040
|
locals.push({
|
|
6821
7041
|
kind: "handler",
|
|
6822
7042
|
name: declaration.id.name,
|
|
6823
|
-
value:
|
|
7043
|
+
value: init,
|
|
6824
7044
|
statesBefore: states.length
|
|
6825
7045
|
});
|
|
6826
7046
|
} else {
|
|
6827
7047
|
locals.push({
|
|
6828
7048
|
kind: "derived",
|
|
6829
7049
|
name: declaration.id.name,
|
|
6830
|
-
value:
|
|
7050
|
+
value: init,
|
|
6831
7051
|
statesBefore: states.length
|
|
6832
7052
|
});
|
|
6833
7053
|
}
|
|
@@ -6840,6 +7060,32 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6840
7060
|
const statesBySetter = new Map(states.map((state) => [state.setterName, state]));
|
|
6841
7061
|
const localNames = new Set(locals.map((binding) => binding.name));
|
|
6842
7062
|
if (localNames.size !== locals.length) return "local binding names must be unique";
|
|
7063
|
+
const reactiveNames = [
|
|
7064
|
+
...states.map((state) => state.valueName),
|
|
7065
|
+
...propsPlan.destructuredNames,
|
|
7066
|
+
...propsPlan.wrapperProps ? [propsPlan.definitionParameter.name] : [],
|
|
7067
|
+
...localNames
|
|
7068
|
+
];
|
|
7069
|
+
for (const memo of memoDeclarations) {
|
|
7070
|
+
if (!memo.dependencies) continue;
|
|
7071
|
+
const listed = new Set(memo.dependencies);
|
|
7072
|
+
const missing = reactiveNames.find(
|
|
7073
|
+
(name2) => name2 !== memo.name && !listed.has(name2) && referencesIdentifier(memo.value, name2)
|
|
7074
|
+
);
|
|
7075
|
+
if (missing) {
|
|
7076
|
+
return `${memo.hook} ${memo.name} reads ${missing} without listing it as a dependency`;
|
|
7077
|
+
}
|
|
7078
|
+
}
|
|
7079
|
+
const refNames = new Set(states.filter((state) => state.ref).map((state) => state.valueName));
|
|
7080
|
+
if (refNames.size > 0) {
|
|
7081
|
+
for (const binding of locals) {
|
|
7082
|
+
if (binding.kind !== "derived") continue;
|
|
7083
|
+
const ref2 = [...refNames].find((name2) => referencesIdentifier(binding.value, name2));
|
|
7084
|
+
if (ref2) return `derived local ${binding.name} cannot read the ref ${ref2} during render`;
|
|
7085
|
+
}
|
|
7086
|
+
const ref = refReadDuringRender(returned.argument, refNames);
|
|
7087
|
+
if (ref) return `the ref ${ref} cannot be read during render`;
|
|
7088
|
+
}
|
|
6843
7089
|
for (const state of states) {
|
|
6844
7090
|
if (state.initialValue && (collectStateDependencies(state.initialValue, statesByValue).length > 0 || collectReferencedLocals(state.initialValue, localNames).length > 0 || [...statesBySetter].some(
|
|
6845
7091
|
([setterName]) => referencesIdentifier(state.initialValue, setterName)
|
|
@@ -7782,6 +8028,7 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
7782
8028
|
appliedKeyedMapUpdateHints > 0 || appliedKeyedArrayAppendHints > 0,
|
|
7783
8029
|
appliedKeyedArrayAppendHints > 0 && appliedKeyedMapUpdateHints === 0
|
|
7784
8030
|
);
|
|
8031
|
+
if (effects.length > 0) runtimeFeatures.push("effects");
|
|
7785
8032
|
for (const feature of runtimeFeatures) {
|
|
7786
8033
|
if (!runtimeFeatureIdentifiers.has(feature)) {
|
|
7787
8034
|
runtimeFeatureIdentifiers.set(
|
|
@@ -7886,6 +8133,15 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
7886
8133
|
rewrittenRoot
|
|
7887
8134
|
)
|
|
7888
8135
|
),
|
|
8136
|
+
...effects.length > 0 ? [
|
|
8137
|
+
types.objectProperty(
|
|
8138
|
+
types.identifier("effects"),
|
|
8139
|
+
types.arrowFunctionExpression(
|
|
8140
|
+
[types.cloneNode(propsParameter), types.cloneNode(stateParameter)],
|
|
8141
|
+
effectScope(path, effects, statesByValue, locals, propsParameter, stateParameter)
|
|
8142
|
+
)
|
|
8143
|
+
)
|
|
8144
|
+
] : [],
|
|
7889
8145
|
types.objectProperty(
|
|
7890
8146
|
types.identifier("bindings"),
|
|
7891
8147
|
types.arrayExpression(
|
|
@@ -7996,6 +8252,7 @@ async function compileReactModule(code, id, options) {
|
|
|
7996
8252
|
const moduleDirectives = new Set(directiveValues(programPath.node));
|
|
7997
8253
|
markForeignContent(programPath);
|
|
7998
8254
|
const useStateNames = /* @__PURE__ */ new Set();
|
|
8255
|
+
const hookNames = /* @__PURE__ */ new Map();
|
|
7999
8256
|
const reactNames = /* @__PURE__ */ new Set();
|
|
8000
8257
|
const listNames = /* @__PURE__ */ new Set();
|
|
8001
8258
|
for (const statement of programPath.node.body) {
|
|
@@ -8014,6 +8271,8 @@ async function compileReactModule(code, id, options) {
|
|
|
8014
8271
|
reactNames.add(specifier.local.name);
|
|
8015
8272
|
} else if (types.isImportSpecifier(specifier) && types.isIdentifier(specifier.imported, { name: "useState" })) {
|
|
8016
8273
|
useStateNames.add(specifier.local.name);
|
|
8274
|
+
} else if (types.isImportSpecifier(specifier) && types.isIdentifier(specifier.imported) && (specifier.imported.name === "useMemo" || specifier.imported.name === "useCallback" || specifier.imported.name === "useRef" || specifier.imported.name === "useEffect" || specifier.imported.name === "useLayoutEffect")) {
|
|
8275
|
+
hookNames.set(specifier.local.name, specifier.imported.name);
|
|
8017
8276
|
}
|
|
8018
8277
|
}
|
|
8019
8278
|
}
|
|
@@ -8139,6 +8398,7 @@ async function compileReactModule(code, id, options) {
|
|
|
8139
8398
|
optimizationCounts,
|
|
8140
8399
|
compilerUsage,
|
|
8141
8400
|
useStateNames,
|
|
8401
|
+
hookNames,
|
|
8142
8402
|
reactNames,
|
|
8143
8403
|
listNames,
|
|
8144
8404
|
id,
|