@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 CHANGED
@@ -228,7 +228,57 @@ function reactHookCall(expression, hookNames, reactNames) {
228
228
  if (!core.types.isCallExpression(expression)) return void 0;
229
229
  const callee = expression.callee;
230
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;
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;
232
282
  }
233
283
  function memoHookValue(expression, hookNames, reactNames) {
234
284
  const hook = reactHookCall(expression, hookNames, reactNames);
@@ -6940,8 +6990,9 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6940
6990
  return "async and generator components are not supported";
6941
6991
  if (path.node.typeParameters) return "generic components are not supported yet";
6942
6992
  if (path.node.params.length > 1) return "components must use at most one props parameter";
6943
- const propsPlan = analyzePropsParameter(path);
6944
- if (typeof propsPlan === "string") return propsPlan;
6993
+ const analyzedPropsPlan = analyzePropsParameter(path);
6994
+ if (typeof analyzedPropsPlan === "string") return analyzedPropsPlan;
6995
+ let propsPlan = analyzedPropsPlan;
6945
6996
  if (!core.types.isBlockStatement(path.node.body)) return "components must use a block body";
6946
6997
  const safeGlobals = new Set(
6947
6998
  [...SAFE_GLOBAL_CALLS, "Math"].filter((name2) => !path.scope.getBinding(name2))
@@ -6951,6 +7002,8 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6951
7002
  const locals = [];
6952
7003
  const effects = [];
6953
7004
  const memoDeclarations = [];
7005
+ const contextDeclarations = [];
7006
+ const contextNames = [];
6954
7007
  let returned;
6955
7008
  for (const statement of path.node.body.body) {
6956
7009
  if (core.types.isReturnStatement(statement)) {
@@ -6994,6 +7047,13 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
6994
7047
  return "only top-level useState declarations, pure derived const values, named synchronous handlers, and one return are supported by the current compiler";
6995
7048
  }
6996
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
+ }
6997
7057
  if (core.types.isArrayPattern(declaration.id)) {
6998
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])) {
6999
7059
  return "only const [value, setValue] = useState(initial) state declarations are supported";
@@ -7058,6 +7118,22 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
7058
7118
  if (!returned?.argument || !core.types.isJSXElement(returned.argument)) {
7059
7119
  return "the component must return one host JSX element";
7060
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
+ }
7061
7137
  const statesByValue = new Map(states.map((state) => [state.valueName, state]));
7062
7138
  const statesBySetter = new Map(states.map((state) => [state.setterName, state]));
7063
7139
  const localNames = new Set(locals.map((binding) => binding.name));
@@ -8167,6 +8243,7 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
8167
8243
  ]);
8168
8244
  path.get("body").replaceWith(
8169
8245
  core.types.blockStatement([
8246
+ ...contextDeclarations,
8170
8247
  core.types.returnStatement(wrapperElement(definitionIdentifier, propsPlan.wrapperProps))
8171
8248
  ])
8172
8249
  );
@@ -8273,7 +8350,7 @@ async function compileReactModule(code, id, options) {
8273
8350
  reactNames.add(specifier.local.name);
8274
8351
  } else if (core.types.isImportSpecifier(specifier) && core.types.isIdentifier(specifier.imported, { name: "useState" })) {
8275
8352
  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")) {
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")) {
8277
8354
  hookNames.set(specifier.local.name, specifier.imported.name);
8278
8355
  }
8279
8356
  }