@farm.js/react 0.1.0-beta.42 → 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/vite.cjs +81 -4
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.mjs +81 -4
- package/dist/vite.mjs.map +1 -1
- package/package.json +2 -2
package/dist/vite.mjs
CHANGED
|
@@ -226,7 +226,57 @@ function reactHookCall(expression, hookNames, reactNames) {
|
|
|
226
226
|
if (!types.isCallExpression(expression)) return void 0;
|
|
227
227
|
const callee = expression.callee;
|
|
228
228
|
if (types.isIdentifier(callee)) return hookNames.get(callee.name);
|
|
229
|
-
return types.isMemberExpression(callee) && !callee.computed && types.isIdentifier(callee.object) && reactNames.has(callee.object.name) && types.isIdentifier(callee.property) && (callee.property.name === "useMemo" || callee.property.name === "useCallback" || callee.property.name === "useRef" || callee.property.name === "useEffect" || callee.property.name === "useLayoutEffect") ? callee.property.name : void 0;
|
|
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;
|
|
230
280
|
}
|
|
231
281
|
function memoHookValue(expression, hookNames, reactNames) {
|
|
232
282
|
const hook = reactHookCall(expression, hookNames, reactNames);
|
|
@@ -6938,8 +6988,9 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6938
6988
|
return "async and generator components are not supported";
|
|
6939
6989
|
if (path.node.typeParameters) return "generic components are not supported yet";
|
|
6940
6990
|
if (path.node.params.length > 1) return "components must use at most one props parameter";
|
|
6941
|
-
const
|
|
6942
|
-
if (typeof
|
|
6991
|
+
const analyzedPropsPlan = analyzePropsParameter(path);
|
|
6992
|
+
if (typeof analyzedPropsPlan === "string") return analyzedPropsPlan;
|
|
6993
|
+
let propsPlan = analyzedPropsPlan;
|
|
6943
6994
|
if (!types.isBlockStatement(path.node.body)) return "components must use a block body";
|
|
6944
6995
|
const safeGlobals = new Set(
|
|
6945
6996
|
[...SAFE_GLOBAL_CALLS, "Math"].filter((name2) => !path.scope.getBinding(name2))
|
|
@@ -6949,6 +7000,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6949
7000
|
const locals = [];
|
|
6950
7001
|
const effects = [];
|
|
6951
7002
|
const memoDeclarations = [];
|
|
7003
|
+
const contextDeclarations = [];
|
|
7004
|
+
const contextNames = [];
|
|
6952
7005
|
let returned;
|
|
6953
7006
|
for (const statement of path.node.body.body) {
|
|
6954
7007
|
if (types.isReturnStatement(statement)) {
|
|
@@ -6992,6 +7045,13 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
6992
7045
|
return "only top-level useState declarations, pure derived const values, named synchronous handlers, and one return are supported by the current compiler";
|
|
6993
7046
|
}
|
|
6994
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
|
+
}
|
|
6995
7055
|
if (types.isArrayPattern(declaration.id)) {
|
|
6996
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])) {
|
|
6997
7057
|
return "only const [value, setValue] = useState(initial) state declarations are supported";
|
|
@@ -7056,6 +7116,22 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
7056
7116
|
if (!returned?.argument || !types.isJSXElement(returned.argument)) {
|
|
7057
7117
|
return "the component must return one host JSX element";
|
|
7058
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
|
+
}
|
|
7059
7135
|
const statesByValue = new Map(states.map((state) => [state.valueName, state]));
|
|
7060
7136
|
const statesBySetter = new Map(states.map((state) => [state.setterName, state]));
|
|
7061
7137
|
const localNames = new Set(locals.map((binding) => binding.name));
|
|
@@ -8165,6 +8241,7 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
|
|
|
8165
8241
|
]);
|
|
8166
8242
|
path.get("body").replaceWith(
|
|
8167
8243
|
types.blockStatement([
|
|
8244
|
+
...contextDeclarations,
|
|
8168
8245
|
types.returnStatement(wrapperElement(definitionIdentifier, propsPlan.wrapperProps))
|
|
8169
8246
|
])
|
|
8170
8247
|
);
|
|
@@ -8271,7 +8348,7 @@ async function compileReactModule(code, id, options) {
|
|
|
8271
8348
|
reactNames.add(specifier.local.name);
|
|
8272
8349
|
} else if (types.isImportSpecifier(specifier) && types.isIdentifier(specifier.imported, { name: "useState" })) {
|
|
8273
8350
|
useStateNames.add(specifier.local.name);
|
|
8274
|
-
} else if (types.isImportSpecifier(specifier) && types.isIdentifier(specifier.imported) && (specifier.imported.name === "useMemo" || specifier.imported.name === "useCallback" || specifier.imported.name === "useRef" || specifier.imported.name === "useEffect" || specifier.imported.name === "useLayoutEffect")) {
|
|
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")) {
|
|
8275
8352
|
hookNames.set(specifier.local.name, specifier.imported.name);
|
|
8276
8353
|
}
|
|
8277
8354
|
}
|