eslint-plugin-react-x 3.0.0-next.12 → 3.0.0-next.14

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.
Files changed (2) hide show
  1. package/dist/index.js +26 -10
  2. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ import { ESLintUtils } from "@typescript-eslint/utils";
5
5
  import { P, isMatching, match } from "ts-pattern";
6
6
  import ts from "typescript";
7
7
  import { AST_NODE_TYPES } from "@typescript-eslint/types";
8
- import { findEnclosingAssignmentTarget, findVariable, getChildScopes, getObjectType, getVariableDefinitionNode, isAssignmentTargetEqual } from "@eslint-react/var";
8
+ import { findEnclosingAssignmentTarget, findVariable, getChildScopes, getObjectType, getVariableInitializer, isAssignmentTargetEqual } from "@eslint-react/var";
9
9
  import { DefinitionType } from "@typescript-eslint/scope-manager";
10
10
  import { constFalse, constTrue, constVoid, flow, getOrElseUpdate, identity, not, unit } from "@eslint-react/eff";
11
11
  import { compare } from "compare-versions";
@@ -68,7 +68,7 @@ const rules$7 = {
68
68
  //#endregion
69
69
  //#region package.json
70
70
  var name$6 = "eslint-plugin-react-x";
71
- var version = "3.0.0-next.12";
71
+ var version = "3.0.0-next.14";
72
72
 
73
73
  //#endregion
74
74
  //#region src/utils/create-rule.ts
@@ -2499,7 +2499,7 @@ function create$23(context) {
2499
2499
  const [arg0, arg1] = init.arguments;
2500
2500
  if (arg0 == null || arg1 == null) return;
2501
2501
  if (!match(arg1).with({ type: AST_NODE_TYPES.ArrayExpression }, (n) => n.elements.length === 0).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
2502
- const variableNode = getVariableDefinitionNode(findVariable(n.name, scope), 0);
2502
+ const variableNode = getVariableInitializer(findVariable(n.name, scope), 0);
2503
2503
  if (variableNode?.type !== AST_NODE_TYPES.ArrayExpression) return false;
2504
2504
  return variableNode.elements.length === 0;
2505
2505
  }).otherwise(() => false)) {
@@ -2510,7 +2510,7 @@ function create$23(context) {
2510
2510
  if (n.body.type === AST_NODE_TYPES.ArrowFunctionExpression) return n.body;
2511
2511
  return n;
2512
2512
  }).with({ type: AST_NODE_TYPES.FunctionExpression }, identity).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
2513
- const variableNode = getVariableDefinitionNode(findVariable(n.name, scope), 0);
2513
+ const variableNode = getVariableInitializer(findVariable(n.name, scope), 0);
2514
2514
  if (variableNode?.type !== AST_NODE_TYPES.ArrowFunctionExpression && variableNode?.type !== AST_NODE_TYPES.FunctionExpression) return null;
2515
2515
  return variableNode;
2516
2516
  }).otherwise(() => null);
@@ -2580,7 +2580,7 @@ function create$22(context) {
2580
2580
  return;
2581
2581
  }
2582
2582
  if (!match(arg1).with({ type: AST_NODE_TYPES.ArrayExpression }, (n) => n.elements.length === 0).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
2583
- const variableNode = getVariableDefinitionNode(findVariable(n.name, scope), 0);
2583
+ const variableNode = getVariableInitializer(findVariable(n.name, scope), 0);
2584
2584
  if (variableNode?.type !== AST_NODE_TYPES.ArrayExpression) return false;
2585
2585
  return variableNode.elements.length === 0;
2586
2586
  }).otherwise(() => false)) {
@@ -2591,7 +2591,7 @@ function create$22(context) {
2591
2591
  if (n.body.type === AST_NODE_TYPES.ArrowFunctionExpression) return n.body;
2592
2592
  return n;
2593
2593
  }).with({ type: AST_NODE_TYPES.FunctionExpression }, identity).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
2594
- const variableNode = getVariableDefinitionNode(findVariable(n.name, scope), 0);
2594
+ const variableNode = getVariableInitializer(findVariable(n.name, scope), 0);
2595
2595
  if (variableNode?.type !== AST_NODE_TYPES.ArrowFunctionExpression && variableNode?.type !== AST_NODE_TYPES.FunctionExpression) return null;
2596
2596
  return variableNode;
2597
2597
  }).otherwise(() => null);
@@ -4180,7 +4180,7 @@ function create$2(context) {
4180
4180
  }
4181
4181
  }
4182
4182
  function isIdFromUseStateCall(topLevelId, at) {
4183
- const variableNode = getVariableDefinitionNode(findVariable(topLevelId, context.sourceCode.getScope(topLevelId)), 0);
4183
+ const variableNode = getVariableInitializer(findVariable(topLevelId, context.sourceCode.getScope(topLevelId)), 0);
4184
4184
  if (variableNode == null) return false;
4185
4185
  if (variableNode.type !== AST_NODE_TYPES.CallExpression) return false;
4186
4186
  if (!isUseStateCall(variableNode)) return false;
@@ -4231,13 +4231,17 @@ function create$2(context) {
4231
4231
  case entry.kind === "deferred":
4232
4232
  case entry.node.async: break;
4233
4233
  case entry.node === setupFunction:
4234
- case entry.kind === "immediate" && ast.findParentNode(entry.node, ast.isFunction) === setupFunction:
4234
+ case entry.kind === "immediate" && ast.findParentNode(entry.node, ast.isFunction) === setupFunction: {
4235
+ const args0 = node.arguments.at(0);
4236
+ if (args0 == null) return;
4237
+ if (isSetterUsingRefValue(context, args0)) return;
4235
4238
  context.report({
4236
4239
  messageId: "default",
4237
4240
  node,
4238
4241
  data: { name: context.sourceCode.getText(node.callee) }
4239
4242
  });
4240
4243
  return;
4244
+ }
4241
4245
  default: {
4242
4246
  const init = ast.findParentNode(node, isVariableDeclaratorFromHookCall)?.init;
4243
4247
  if (init == null) getOrElseUpdate(setStateCallsByFn, entry.node, () => []).push(node);
@@ -4276,7 +4280,7 @@ function create$2(context) {
4276
4280
  },
4277
4281
  "Program:exit"() {
4278
4282
  const getSetStateCalls = (id, initialScope) => {
4279
- const node = getVariableDefinitionNode(findVariable(id, initialScope), 0);
4283
+ const node = getVariableInitializer(findVariable(id, initialScope), 0);
4280
4284
  switch (node?.type) {
4281
4285
  case AST_NODE_TYPES.ArrowFunctionExpression:
4282
4286
  case AST_NODE_TYPES.FunctionDeclaration:
@@ -4311,6 +4315,18 @@ function create$2(context) {
4311
4315
  }
4312
4316
  });
4313
4317
  }
4318
+ function isSetterUsingRefValue(context, node) {
4319
+ const isUsingRefValue = (n) => {
4320
+ switch (n.type) {
4321
+ case AST_NODE_TYPES.Identifier: return core.isInitializedFromRef(n.name, context.sourceCode.getScope(n));
4322
+ case AST_NODE_TYPES.MemberExpression: return isUsingRefValue(n.object);
4323
+ case AST_NODE_TYPES.CallExpression: return isUsingRefValue(n.callee) || ast.getNestedIdentifiers(n).some(isUsingRefValue);
4324
+ default: return false;
4325
+ }
4326
+ };
4327
+ if (isUsingRefValue(node)) return true;
4328
+ return ast.isFunction(node) && context.sourceCode.getScope(node.body).references.some((r) => isUsingRefValue(r.identifier));
4329
+ }
4314
4330
  function isInitFromHookCall(init) {
4315
4331
  if (init?.type !== AST_NODE_TYPES.CallExpression) return false;
4316
4332
  switch (init.callee.type) {
@@ -4348,7 +4364,7 @@ function create$1(context) {
4348
4364
  return core.isUseStateLikeCall(node, additionalStateHooks);
4349
4365
  }
4350
4366
  function isIdFromUseStateCall(topLevelId, at) {
4351
- const variableNode = getVariableDefinitionNode(findVariable(topLevelId, context.sourceCode.getScope(topLevelId)), 0);
4367
+ const variableNode = getVariableInitializer(findVariable(topLevelId, context.sourceCode.getScope(topLevelId)), 0);
4352
4368
  if (variableNode == null) return false;
4353
4369
  if (variableNode.type !== AST_NODE_TYPES.CallExpression) return false;
4354
4370
  if (!isUseStateCall(variableNode)) return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-x",
3
- "version": "3.0.0-next.12",
3
+ "version": "3.0.0-next.14",
4
4
  "description": "A set of composable ESLint rules for libraries and frameworks that use React as a UI runtime.",
5
5
  "keywords": [
6
6
  "react",
@@ -45,11 +45,11 @@
45
45
  "is-immutable-type": "^5.0.1",
46
46
  "ts-api-utils": "^2.4.0",
47
47
  "ts-pattern": "^5.9.0",
48
- "@eslint-react/core": "3.0.0-next.12",
49
- "@eslint-react/eff": "3.0.0-next.12",
50
- "@eslint-react/ast": "3.0.0-next.12",
51
- "@eslint-react/shared": "3.0.0-next.12",
52
- "@eslint-react/var": "3.0.0-next.12"
48
+ "@eslint-react/ast": "3.0.0-next.14",
49
+ "@eslint-react/core": "3.0.0-next.14",
50
+ "@eslint-react/eff": "3.0.0-next.14",
51
+ "@eslint-react/shared": "3.0.0-next.14",
52
+ "@eslint-react/var": "3.0.0-next.14"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/react": "^19.2.14",