@farm.js/react 0.1.0-beta.41 → 0.1.0-beta.43
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 +61 -10
- package/dist/compiler-runtime.cjs.map +1 -1
- package/dist/compiler-runtime.d.mts +18 -2
- package/dist/compiler-runtime.d.ts +18 -2
- package/dist/compiler-runtime.mjs +61 -11
- package/dist/compiler-runtime.mjs.map +1 -1
- package/dist/vite.cjs +287 -12
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.mjs +287 -12
- 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,156 @@ 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 === "useContext") ? callee.property.name : void 0;
|
|
232
|
+
}
|
|
233
|
+
function isModuleReference(expression, path) {
|
|
234
|
+
let root = expression;
|
|
235
|
+
while (core.types.isMemberExpression(root) && !root.computed) root = root.object;
|
|
236
|
+
if (!core.types.isIdentifier(root)) return false;
|
|
237
|
+
return Boolean(path.scope.getBinding(root.name)?.scope.path.isProgram());
|
|
238
|
+
}
|
|
239
|
+
function isLiteralDefault(expression) {
|
|
240
|
+
return core.types.isStringLiteral(expression) || core.types.isNumericLiteral(expression) || core.types.isBooleanLiteral(expression) || core.types.isNullLiteral(expression) || core.types.isTemplateLiteral(expression) && expression.expressions.length === 0 || core.types.isUnaryExpression(expression, { operator: "-" }) && core.types.isNumericLiteral(expression.argument);
|
|
241
|
+
}
|
|
242
|
+
function contextDeclarationNames(declaration, path) {
|
|
243
|
+
const [context, ...rest] = declaration.init.arguments;
|
|
244
|
+
if (!context || rest.length > 0 || !isModuleReference(context, path)) {
|
|
245
|
+
return "useContext needs one context defined or imported at module level";
|
|
246
|
+
}
|
|
247
|
+
const names = [];
|
|
248
|
+
const collect = (pattern) => {
|
|
249
|
+
if (core.types.isIdentifier(pattern)) {
|
|
250
|
+
names.push(pattern.name);
|
|
251
|
+
return void 0;
|
|
252
|
+
}
|
|
253
|
+
if (core.types.isAssignmentPattern(pattern)) {
|
|
254
|
+
if (!isLiteralDefault(pattern.right)) {
|
|
255
|
+
return "useContext destructuring defaults must be literals";
|
|
256
|
+
}
|
|
257
|
+
return collect(pattern.left);
|
|
258
|
+
}
|
|
259
|
+
if (core.types.isRestElement(pattern)) return collect(pattern.argument);
|
|
260
|
+
if (core.types.isArrayPattern(pattern)) {
|
|
261
|
+
for (const element of pattern.elements) {
|
|
262
|
+
const reason = element ? collect(element) : void 0;
|
|
263
|
+
if (reason) return reason;
|
|
264
|
+
}
|
|
265
|
+
return void 0;
|
|
266
|
+
}
|
|
267
|
+
if (core.types.isObjectPattern(pattern)) {
|
|
268
|
+
for (const property of pattern.properties) {
|
|
269
|
+
if (core.types.isObjectProperty(property) && property.computed) {
|
|
270
|
+
return "computed useContext destructuring is not supported yet";
|
|
271
|
+
}
|
|
272
|
+
const reason = collect(
|
|
273
|
+
core.types.isRestElement(property) ? property : property.value
|
|
274
|
+
);
|
|
275
|
+
if (reason) return reason;
|
|
276
|
+
}
|
|
277
|
+
return void 0;
|
|
278
|
+
}
|
|
279
|
+
return "useContext values must be bound to names";
|
|
280
|
+
};
|
|
281
|
+
return collect(declaration.id) ?? names;
|
|
282
|
+
}
|
|
283
|
+
function memoHookValue(expression, hookNames, reactNames) {
|
|
284
|
+
const hook = reactHookCall(expression, hookNames, reactNames);
|
|
285
|
+
if (hook !== "useMemo" && hook !== "useCallback") return void 0;
|
|
286
|
+
if (!core.types.isCallExpression(expression)) return void 0;
|
|
287
|
+
const [callback, dependencyList, ...rest] = expression.arguments;
|
|
288
|
+
if (rest.length > 0 || !core.types.isArrowFunctionExpression(callback) && !core.types.isFunctionExpression(callback) || callback.async || callback.generator) {
|
|
289
|
+
return `${hook} needs one inline synchronous callback`;
|
|
290
|
+
}
|
|
291
|
+
let dependencies;
|
|
292
|
+
if (dependencyList) {
|
|
293
|
+
if (!core.types.isArrayExpression(dependencyList) || !dependencyList.elements.every((element) => core.types.isIdentifier(element))) {
|
|
294
|
+
return `${hook} dependencies must be an array of names`;
|
|
295
|
+
}
|
|
296
|
+
dependencies = dependencyList.elements.map((element) => element.name);
|
|
297
|
+
}
|
|
298
|
+
if (hook === "useCallback") return { hook, value: callback, dependencies };
|
|
299
|
+
if (callback.params.length > 0) return "useMemo callbacks take no parameters";
|
|
300
|
+
const body = callback.body;
|
|
301
|
+
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;
|
|
302
|
+
if (!value) return "useMemo callbacks must return one expression";
|
|
303
|
+
return { hook, value, dependencies };
|
|
304
|
+
}
|
|
305
|
+
function effectScope(path, effects, statesByValue, locals, propsParameter, stateParameter) {
|
|
306
|
+
const cell = (index, member) => core.types.memberExpression(
|
|
307
|
+
core.types.memberExpression(core.types.cloneNode(stateParameter), core.types.numericLiteral(index), true),
|
|
308
|
+
core.types.identifier(member)
|
|
309
|
+
);
|
|
310
|
+
const statements = [];
|
|
311
|
+
const parameter = path.node.params[0];
|
|
312
|
+
if (parameter && !core.types.isIdentifier(parameter)) {
|
|
313
|
+
statements.push(
|
|
314
|
+
core.types.variableDeclaration("const", [
|
|
315
|
+
core.types.variableDeclarator(core.types.cloneNode(parameter, true), core.types.cloneNode(propsParameter))
|
|
316
|
+
])
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
const localsByName = new Map(locals.map((local) => [local.name, local]));
|
|
320
|
+
for (const statement of path.node.body.body) {
|
|
321
|
+
if (core.types.isFunctionDeclaration(statement)) {
|
|
322
|
+
statements.push(core.types.cloneNode(statement, true));
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
if (!core.types.isVariableDeclaration(statement)) continue;
|
|
326
|
+
const id = statement.declarations[0].id;
|
|
327
|
+
if (core.types.isArrayPattern(id)) {
|
|
328
|
+
const [value2, setter] = id.elements;
|
|
329
|
+
const index = statesByValue.get(value2.name).index;
|
|
330
|
+
statements.push(
|
|
331
|
+
core.types.variableDeclaration("const", [
|
|
332
|
+
core.types.variableDeclarator(core.types.identifier(value2.name), core.types.callExpression(cell(index, "get"), [])),
|
|
333
|
+
core.types.variableDeclarator(core.types.identifier(setter.name), cell(index, "set"))
|
|
334
|
+
])
|
|
335
|
+
);
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
if (!core.types.isIdentifier(id)) continue;
|
|
339
|
+
const ref = statesByValue.get(id.name);
|
|
340
|
+
const local = localsByName.get(id.name);
|
|
341
|
+
const value = ref?.ref ? core.types.callExpression(cell(ref.index, "get"), []) : local ? core.types.cloneNode(local.value, true) : void 0;
|
|
342
|
+
if (value) {
|
|
343
|
+
statements.push(
|
|
344
|
+
core.types.variableDeclaration("const", [core.types.variableDeclarator(core.types.identifier(id.name), value)])
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
statements.push(
|
|
349
|
+
core.types.returnStatement(
|
|
350
|
+
core.types.arrayExpression(
|
|
351
|
+
effects.map(
|
|
352
|
+
(effect) => core.types.arrayExpression([
|
|
353
|
+
core.types.cloneNode(effect.callback, true),
|
|
354
|
+
effect.dependencies ? core.types.cloneNode(effect.dependencies, true) : core.types.identifier("undefined"),
|
|
355
|
+
...effect.layout ? [core.types.booleanLiteral(true)] : []
|
|
356
|
+
])
|
|
357
|
+
)
|
|
358
|
+
)
|
|
359
|
+
)
|
|
360
|
+
);
|
|
361
|
+
return core.types.blockStatement(statements);
|
|
362
|
+
}
|
|
363
|
+
function refReadDuringRender(root, refNames) {
|
|
364
|
+
let read;
|
|
365
|
+
core.traverse(expressionFile(cloneExpression(root)), {
|
|
366
|
+
Identifier(path) {
|
|
367
|
+
if (read || !refNames.has(path.node.name) || !path.isReferencedIdentifier()) return;
|
|
368
|
+
if (path.findParent((parent) => parent.isFunction())) return;
|
|
369
|
+
const attribute = path.parentPath?.isJSXExpressionContainer() ? path.parentPath.parentPath : void 0;
|
|
370
|
+
if (attribute?.isJSXAttribute() && jsxAttributeName(attribute.node) === "ref") return;
|
|
371
|
+
read = path.node.name;
|
|
372
|
+
path.stop();
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
return read;
|
|
376
|
+
}
|
|
226
377
|
function cloneExpression(expression) {
|
|
227
378
|
return core.types.cloneNode(expression, true);
|
|
228
379
|
}
|
|
@@ -6521,13 +6672,18 @@ function lowerStableBindingTargets(root, bindings, blockRuntime, ownedElements)
|
|
|
6521
6672
|
const visit = (element, path) => {
|
|
6522
6673
|
const target = targetByPath.get(path.join("."));
|
|
6523
6674
|
if (target !== void 0 && path.length > 0) {
|
|
6524
|
-
element.openingElement.attributes
|
|
6675
|
+
const attributes = element.openingElement.attributes;
|
|
6676
|
+
const userRefIndex = attributes.findIndex(
|
|
6677
|
+
(attribute) => core.types.isJSXAttribute(attribute) && jsxAttributeName(attribute) === "ref"
|
|
6678
|
+
);
|
|
6679
|
+
const userRef = userRefIndex >= 0 ? attributes.splice(userRefIndex, 1)[0].value.expression : void 0;
|
|
6680
|
+
attributes.push(
|
|
6525
6681
|
core.types.jsxAttribute(
|
|
6526
6682
|
core.types.jsxIdentifier("ref"),
|
|
6527
6683
|
core.types.jsxExpressionContainer(
|
|
6528
6684
|
core.types.callExpression(
|
|
6529
6685
|
core.types.memberExpression(core.types.cloneNode(blockRuntime), core.types.identifier("target")),
|
|
6530
|
-
[core.types.numericLiteral(target)]
|
|
6686
|
+
[core.types.numericLiteral(target), ...userRef && core.types.isExpression(userRef) ? [userRef] : []]
|
|
6531
6687
|
)
|
|
6532
6688
|
)
|
|
6533
6689
|
)
|
|
@@ -6586,9 +6742,14 @@ function analyzeHostTree(root, statesByValue, safeGlobals, conditionalExpression
|
|
|
6586
6742
|
const name = jsxAttributeName(attribute);
|
|
6587
6743
|
if (!name) return "namespaced JSX attributes are not supported yet";
|
|
6588
6744
|
if (name === "is") return "customized built-in elements require React ownership";
|
|
6589
|
-
if (name === "ref"
|
|
6590
|
-
|
|
6745
|
+
if (name === "ref") {
|
|
6746
|
+
const value = core.types.isJSXExpressionContainer(attribute.value) ? attribute.value.expression : void 0;
|
|
6747
|
+
if (path.length > 0 && core.types.isIdentifier(value) && statesByValue.get(value.name)?.ref) {
|
|
6748
|
+
continue;
|
|
6749
|
+
}
|
|
6750
|
+
return "ref requires React ownership";
|
|
6591
6751
|
}
|
|
6752
|
+
if (name === "dangerouslySetInnerHTML") return `${name} requires React ownership`;
|
|
6592
6753
|
if (!core.types.isJSXExpressionContainer(attribute.value) || core.types.isJSXEmptyExpression(attribute.value.expression)) {
|
|
6593
6754
|
continue;
|
|
6594
6755
|
}
|
|
@@ -6823,14 +6984,15 @@ function wrapperElement(definitionIdentifier, props) {
|
|
|
6823
6984
|
const attributes = props ? [core.types.jsxSpreadAttribute(core.types.cloneNode(props))] : [];
|
|
6824
6985
|
return core.types.jsxElement(core.types.jsxOpeningElement(name, attributes, true), null, [], true);
|
|
6825
6986
|
}
|
|
6826
|
-
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) {
|
|
6987
|
+
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) {
|
|
6827
6988
|
const { path, name, statementPath } = candidate;
|
|
6828
6989
|
if (path.node.async || path.node.generator)
|
|
6829
6990
|
return "async and generator components are not supported";
|
|
6830
6991
|
if (path.node.typeParameters) return "generic components are not supported yet";
|
|
6831
6992
|
if (path.node.params.length > 1) return "components must use at most one props parameter";
|
|
6832
|
-
const
|
|
6833
|
-
if (typeof
|
|
6993
|
+
const analyzedPropsPlan = analyzePropsParameter(path);
|
|
6994
|
+
if (typeof analyzedPropsPlan === "string") return analyzedPropsPlan;
|
|
6995
|
+
let propsPlan = analyzedPropsPlan;
|
|
6834
6996
|
if (!core.types.isBlockStatement(path.node.body)) return "components must use a block body";
|
|
6835
6997
|
const safeGlobals = new Set(
|
|
6836
6998
|
[...SAFE_GLOBAL_CALLS, "Math"].filter((name2) => !path.scope.getBinding(name2))
|
|
@@ -6838,6 +7000,10 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6838
7000
|
const globalCollections = new Set(["Set", "Map"].filter((name2) => !path.scope.getBinding(name2)));
|
|
6839
7001
|
const states = [];
|
|
6840
7002
|
const locals = [];
|
|
7003
|
+
const effects = [];
|
|
7004
|
+
const memoDeclarations = [];
|
|
7005
|
+
const contextDeclarations = [];
|
|
7006
|
+
const contextNames = [];
|
|
6841
7007
|
let returned;
|
|
6842
7008
|
for (const statement of path.node.body.body) {
|
|
6843
7009
|
if (core.types.isReturnStatement(statement)) {
|
|
@@ -6845,6 +7011,25 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6845
7011
|
returned = statement;
|
|
6846
7012
|
continue;
|
|
6847
7013
|
}
|
|
7014
|
+
if (core.types.isExpressionStatement(statement)) {
|
|
7015
|
+
const hook = reactHookCall(statement.expression, hookNames, reactNames);
|
|
7016
|
+
if (hook === "useEffect" || hook === "useLayoutEffect") {
|
|
7017
|
+
const [callback, dependencyList, ...rest] = statement.expression.arguments;
|
|
7018
|
+
if (rest.length > 0 || !core.types.isArrowFunctionExpression(callback) && !core.types.isFunctionExpression(callback) || callback.async || callback.generator || callback.params.length > 0) {
|
|
7019
|
+
return `${hook} needs one inline synchronous callback`;
|
|
7020
|
+
}
|
|
7021
|
+
if (containsDirectHookCall(callback)) return `Hooks cannot be called inside ${hook}`;
|
|
7022
|
+
if (dependencyList && (!core.types.isArrayExpression(dependencyList) || !dependencyList.elements.every((element) => core.types.isIdentifier(element)))) {
|
|
7023
|
+
return `${hook} dependencies must be an array of names`;
|
|
7024
|
+
}
|
|
7025
|
+
effects.push({
|
|
7026
|
+
callback,
|
|
7027
|
+
dependencies: dependencyList,
|
|
7028
|
+
layout: hook === "useLayoutEffect"
|
|
7029
|
+
});
|
|
7030
|
+
continue;
|
|
7031
|
+
}
|
|
7032
|
+
}
|
|
6848
7033
|
if (core.types.isFunctionDeclaration(statement)) {
|
|
6849
7034
|
if (!statement.id) return "named event handler declarations must have a name";
|
|
6850
7035
|
if (statement.generator || statement.typeParameters) {
|
|
@@ -6862,6 +7047,13 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6862
7047
|
return "only top-level useState declarations, pure derived const values, named synchronous handlers, and one return are supported by the current compiler";
|
|
6863
7048
|
}
|
|
6864
7049
|
const declaration = statement.declarations[0];
|
|
7050
|
+
if (reactHookCall(declaration.init, hookNames, reactNames) === "useContext") {
|
|
7051
|
+
const names = contextDeclarationNames(declaration, path);
|
|
7052
|
+
if (typeof names === "string") return names;
|
|
7053
|
+
contextDeclarations.push(statement);
|
|
7054
|
+
contextNames.push(...names);
|
|
7055
|
+
continue;
|
|
7056
|
+
}
|
|
6865
7057
|
if (core.types.isArrayPattern(declaration.id)) {
|
|
6866
7058
|
if (declaration.id.elements.length !== 2 || !core.types.isIdentifier(declaration.id.elements[0]) || !core.types.isIdentifier(declaration.id.elements[1]) || !isUseStateCall(declaration.init, useStateNames, reactNames) || declaration.init.arguments.length > 1 || declaration.init.arguments[0] && !core.types.isExpression(declaration.init.arguments[0])) {
|
|
6867
7059
|
return "only const [value, setValue] = useState(initial) state declarations are supported";
|
|
@@ -6877,21 +7069,47 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6877
7069
|
if (!core.types.isIdentifier(declaration.id) || !core.types.isExpression(declaration.init)) {
|
|
6878
7070
|
return "derived const values and event handlers must use one identifier and one expression";
|
|
6879
7071
|
}
|
|
6880
|
-
|
|
6881
|
-
|
|
7072
|
+
let init = declaration.init;
|
|
7073
|
+
if (reactHookCall(init, hookNames, reactNames) === "useRef") {
|
|
7074
|
+
const [initial, ...rest] = init.arguments;
|
|
7075
|
+
if (rest.length > 0 || initial && !core.types.isExpression(initial)) {
|
|
7076
|
+
return "useRef takes at most one initial value";
|
|
7077
|
+
}
|
|
7078
|
+
states.push({
|
|
7079
|
+
valueName: declaration.id.name,
|
|
7080
|
+
setterName: path.scope.generateUidIdentifier("farmRef").name,
|
|
7081
|
+
index: states.length,
|
|
7082
|
+
initialValue: core.types.objectExpression([
|
|
7083
|
+
core.types.objectProperty(
|
|
7084
|
+
core.types.identifier("current"),
|
|
7085
|
+
initial ? initial : core.types.identifier("undefined")
|
|
7086
|
+
)
|
|
7087
|
+
]),
|
|
7088
|
+
ref: true
|
|
7089
|
+
});
|
|
7090
|
+
continue;
|
|
7091
|
+
}
|
|
7092
|
+
const memo = memoHookValue(init, hookNames, reactNames);
|
|
7093
|
+
if (typeof memo === "string") return memo;
|
|
7094
|
+
if (memo) {
|
|
7095
|
+
memoDeclarations.push({ name: declaration.id.name, ...memo });
|
|
7096
|
+
init = memo.value;
|
|
7097
|
+
}
|
|
7098
|
+
if (core.types.isArrowFunctionExpression(init) || core.types.isFunctionExpression(init)) {
|
|
7099
|
+
if (init.generator) {
|
|
6882
7100
|
return `event handler ${declaration.id.name} cannot be a generator`;
|
|
6883
7101
|
}
|
|
6884
7102
|
locals.push({
|
|
6885
7103
|
kind: "handler",
|
|
6886
7104
|
name: declaration.id.name,
|
|
6887
|
-
value:
|
|
7105
|
+
value: init,
|
|
6888
7106
|
statesBefore: states.length
|
|
6889
7107
|
});
|
|
6890
7108
|
} else {
|
|
6891
7109
|
locals.push({
|
|
6892
7110
|
kind: "derived",
|
|
6893
7111
|
name: declaration.id.name,
|
|
6894
|
-
value:
|
|
7112
|
+
value: init,
|
|
6895
7113
|
statesBefore: states.length
|
|
6896
7114
|
});
|
|
6897
7115
|
}
|
|
@@ -6900,10 +7118,52 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6900
7118
|
if (!returned?.argument || !core.types.isJSXElement(returned.argument)) {
|
|
6901
7119
|
return "the component must return one host JSX element";
|
|
6902
7120
|
}
|
|
7121
|
+
if (contextNames.length > 0) {
|
|
7122
|
+
const { wrapperProps } = propsPlan;
|
|
7123
|
+
if (wrapperProps && !core.types.isObjectExpression(wrapperProps)) {
|
|
7124
|
+
return "useContext needs destructured props or no props parameter";
|
|
7125
|
+
}
|
|
7126
|
+
propsPlan = {
|
|
7127
|
+
definitionParameter: propsPlan.definitionParameter,
|
|
7128
|
+
wrapperProps: core.types.objectExpression([
|
|
7129
|
+
...wrapperProps?.properties ?? [],
|
|
7130
|
+
...contextNames.map(
|
|
7131
|
+
(name2) => core.types.objectProperty(core.types.identifier(name2), core.types.identifier(name2), false, true)
|
|
7132
|
+
)
|
|
7133
|
+
]),
|
|
7134
|
+
destructuredNames: /* @__PURE__ */ new Set([...propsPlan.destructuredNames, ...contextNames])
|
|
7135
|
+
};
|
|
7136
|
+
}
|
|
6903
7137
|
const statesByValue = new Map(states.map((state) => [state.valueName, state]));
|
|
6904
7138
|
const statesBySetter = new Map(states.map((state) => [state.setterName, state]));
|
|
6905
7139
|
const localNames = new Set(locals.map((binding) => binding.name));
|
|
6906
7140
|
if (localNames.size !== locals.length) return "local binding names must be unique";
|
|
7141
|
+
const reactiveNames = [
|
|
7142
|
+
...states.map((state) => state.valueName),
|
|
7143
|
+
...propsPlan.destructuredNames,
|
|
7144
|
+
...propsPlan.wrapperProps ? [propsPlan.definitionParameter.name] : [],
|
|
7145
|
+
...localNames
|
|
7146
|
+
];
|
|
7147
|
+
for (const memo of memoDeclarations) {
|
|
7148
|
+
if (!memo.dependencies) continue;
|
|
7149
|
+
const listed = new Set(memo.dependencies);
|
|
7150
|
+
const missing = reactiveNames.find(
|
|
7151
|
+
(name2) => name2 !== memo.name && !listed.has(name2) && referencesIdentifier(memo.value, name2)
|
|
7152
|
+
);
|
|
7153
|
+
if (missing) {
|
|
7154
|
+
return `${memo.hook} ${memo.name} reads ${missing} without listing it as a dependency`;
|
|
7155
|
+
}
|
|
7156
|
+
}
|
|
7157
|
+
const refNames = new Set(states.filter((state) => state.ref).map((state) => state.valueName));
|
|
7158
|
+
if (refNames.size > 0) {
|
|
7159
|
+
for (const binding of locals) {
|
|
7160
|
+
if (binding.kind !== "derived") continue;
|
|
7161
|
+
const ref2 = [...refNames].find((name2) => referencesIdentifier(binding.value, name2));
|
|
7162
|
+
if (ref2) return `derived local ${binding.name} cannot read the ref ${ref2} during render`;
|
|
7163
|
+
}
|
|
7164
|
+
const ref = refReadDuringRender(returned.argument, refNames);
|
|
7165
|
+
if (ref) return `the ref ${ref} cannot be read during render`;
|
|
7166
|
+
}
|
|
6907
7167
|
for (const state of states) {
|
|
6908
7168
|
if (state.initialValue && (collectStateDependencies(state.initialValue, statesByValue).length > 0 || collectReferencedLocals(state.initialValue, localNames).length > 0 || [...statesBySetter].some(
|
|
6909
7169
|
([setterName]) => referencesIdentifier(state.initialValue, setterName)
|
|
@@ -7846,6 +8106,7 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
7846
8106
|
appliedKeyedMapUpdateHints > 0 || appliedKeyedArrayAppendHints > 0,
|
|
7847
8107
|
appliedKeyedArrayAppendHints > 0 && appliedKeyedMapUpdateHints === 0
|
|
7848
8108
|
);
|
|
8109
|
+
if (effects.length > 0) runtimeFeatures.push("effects");
|
|
7849
8110
|
for (const feature of runtimeFeatures) {
|
|
7850
8111
|
if (!runtimeFeatureIdentifiers.has(feature)) {
|
|
7851
8112
|
runtimeFeatureIdentifiers.set(
|
|
@@ -7950,6 +8211,15 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
7950
8211
|
rewrittenRoot
|
|
7951
8212
|
)
|
|
7952
8213
|
),
|
|
8214
|
+
...effects.length > 0 ? [
|
|
8215
|
+
core.types.objectProperty(
|
|
8216
|
+
core.types.identifier("effects"),
|
|
8217
|
+
core.types.arrowFunctionExpression(
|
|
8218
|
+
[core.types.cloneNode(propsParameter), core.types.cloneNode(stateParameter)],
|
|
8219
|
+
effectScope(path, effects, statesByValue, locals, propsParameter, stateParameter)
|
|
8220
|
+
)
|
|
8221
|
+
)
|
|
8222
|
+
] : [],
|
|
7953
8223
|
core.types.objectProperty(
|
|
7954
8224
|
core.types.identifier("bindings"),
|
|
7955
8225
|
core.types.arrayExpression(
|
|
@@ -7973,6 +8243,7 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
7973
8243
|
]);
|
|
7974
8244
|
path.get("body").replaceWith(
|
|
7975
8245
|
core.types.blockStatement([
|
|
8246
|
+
...contextDeclarations,
|
|
7976
8247
|
core.types.returnStatement(wrapperElement(definitionIdentifier, propsPlan.wrapperProps))
|
|
7977
8248
|
])
|
|
7978
8249
|
);
|
|
@@ -8060,6 +8331,7 @@ async function compileReactModule(code, id, options) {
|
|
|
8060
8331
|
const moduleDirectives = new Set(directiveValues(programPath.node));
|
|
8061
8332
|
markForeignContent(programPath);
|
|
8062
8333
|
const useStateNames = /* @__PURE__ */ new Set();
|
|
8334
|
+
const hookNames = /* @__PURE__ */ new Map();
|
|
8063
8335
|
const reactNames = /* @__PURE__ */ new Set();
|
|
8064
8336
|
const listNames = /* @__PURE__ */ new Set();
|
|
8065
8337
|
for (const statement of programPath.node.body) {
|
|
@@ -8078,6 +8350,8 @@ async function compileReactModule(code, id, options) {
|
|
|
8078
8350
|
reactNames.add(specifier.local.name);
|
|
8079
8351
|
} else if (core.types.isImportSpecifier(specifier) && core.types.isIdentifier(specifier.imported, { name: "useState" })) {
|
|
8080
8352
|
useStateNames.add(specifier.local.name);
|
|
8353
|
+
} 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" || specifier.imported.name === "useContext")) {
|
|
8354
|
+
hookNames.set(specifier.local.name, specifier.imported.name);
|
|
8081
8355
|
}
|
|
8082
8356
|
}
|
|
8083
8357
|
}
|
|
@@ -8203,6 +8477,7 @@ async function compileReactModule(code, id, options) {
|
|
|
8203
8477
|
optimizationCounts,
|
|
8204
8478
|
compilerUsage,
|
|
8205
8479
|
useStateNames,
|
|
8480
|
+
hookNames,
|
|
8206
8481
|
reactNames,
|
|
8207
8482
|
listNames,
|
|
8208
8483
|
id,
|