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