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