eslint-plugin-react-x 5.24.5 → 5.24.7

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 +30 -25
  2. package/package.json +11 -11
package/dist/index.js CHANGED
@@ -135,7 +135,7 @@ const rules$6 = {
135
135
  //#endregion
136
136
  //#region package.json
137
137
  var name$6 = "eslint-plugin-react-x";
138
- var version = "5.24.5";
138
+ var version = "5.24.7";
139
139
  //#endregion
140
140
  //#region src/utils/create-rule.ts
141
141
  function getDocsUrl(ruleName) {
@@ -1335,15 +1335,16 @@ function resolveToFunctionNode(context, node, seen = /* @__PURE__ */ new Set())
1335
1335
  const resolved = resolveOrigin(context, expr);
1336
1336
  return resolved == null ? null : resolveToFunctionNode(context, resolved, seen);
1337
1337
  }
1338
- function resolveVariableOrigin(context, variable, seen = /* @__PURE__ */ new Set()) {
1338
+ function resolveVariableOrigin(context, variable, mode = "alias", seen = /* @__PURE__ */ new Set()) {
1339
1339
  if (seen.has(variable)) return variable;
1340
1340
  seen.add(variable);
1341
1341
  const def = variable.defs.length === 1 ? variable.defs[0] : null;
1342
1342
  if (def?.type !== DefinitionType.Variable || def.node.init == null) return variable;
1343
+ if (mode === "binding" && def.node.id.type === AST_NODE_TYPES.Identifier) return variable;
1343
1344
  const init = Extract.unwrap(def.node.init);
1344
1345
  if (!Check.isIdentifier(init)) return variable;
1345
1346
  const source = findVariable(context.sourceCode.getScope(init), init);
1346
- return source == null ? variable : resolveVariableOrigin(context, source, seen);
1347
+ return source == null ? variable : resolveVariableOrigin(context, source, mode, seen);
1347
1348
  }
1348
1349
  function isRefLikeName$1(name) {
1349
1350
  return name === "ref" || name.endsWith("Ref");
@@ -1467,10 +1468,10 @@ function createFactCollector$5() {
1467
1468
  }
1468
1469
  //#endregion
1469
1470
  //#region src/rules/immutability/origins.ts
1470
- function classifyFrozenOrigin(context, variable, components, seen = /* @__PURE__ */ new Set()) {
1471
+ function classifyFrozenOrigin(context, variable, components, mode = "alias", seen = /* @__PURE__ */ new Set()) {
1471
1472
  if (seen.has(variable)) return null;
1472
1473
  seen.add(variable);
1473
- const origin = resolveVariableOrigin(context, variable);
1474
+ const origin = resolveVariableOrigin(context, variable, mode);
1474
1475
  const def = origin.defs.length === 1 ? origin.defs[0] : null;
1475
1476
  if (def == null) return null;
1476
1477
  if (isComponentPropsDefinition(def, components)) return {
@@ -1485,7 +1486,7 @@ function classifyFrozenOrigin(context, variable, components, seen = /* @__PURE__
1485
1486
  if (root == null || !Check.isIdentifier(root)) return null;
1486
1487
  const source = findVariable(context.sourceCode.getScope(root), root);
1487
1488
  if (source == null) return null;
1488
- const inner = classifyFrozenOrigin(context, source, components, seen);
1489
+ const inner = classifyFrozenOrigin(context, source, components, mode, seen);
1489
1490
  return inner == null ? null : {
1490
1491
  kind: "iterator",
1491
1492
  name: origin.name,
@@ -1515,7 +1516,7 @@ function classifyFrozenOrigin(context, variable, components, seen = /* @__PURE__
1515
1516
  if (!Check.isIdentifier(argument)) continue;
1516
1517
  const source = findVariable(context.sourceCode.getScope(argument), argument);
1517
1518
  if (source == null) continue;
1518
- const inner = classifyFrozenOrigin(context, source, components, seen);
1519
+ const inner = classifyFrozenOrigin(context, source, components, mode, seen);
1519
1520
  if (inner != null) return {
1520
1521
  kind: "shallow-copy",
1521
1522
  name: origin.name,
@@ -1570,7 +1571,7 @@ function inferDirectMutations(context, mutations, components) {
1570
1571
  if (isRefMutation(context, mutation)) continue;
1571
1572
  const variable = findVariable(context.sourceCode.getScope(mutation.root), mutation.root);
1572
1573
  if (variable == null) continue;
1573
- const origin = classifyFrozenOrigin(context, variable, components);
1574
+ const origin = classifyFrozenOrigin(context, variable, components, mutation.kind === "binding" ? "binding" : "alias");
1574
1575
  if (origin == null) continue;
1575
1576
  if (mutation.kind === "binding") {
1576
1577
  switch (origin.kind) {
@@ -3463,7 +3464,7 @@ function collectUsedPropKeysOfParameter(context, usedPropKeys, parameter) {
3463
3464
  function collectUsedPropKeysOfObjectPattern(context, usedPropKeys, objectPattern) {
3464
3465
  for (const property of objectPattern.properties) switch (property.type) {
3465
3466
  case AST_NODE_TYPES.Property: {
3466
- const key = getKeyOfExpression(property.key);
3467
+ const key = getKeyOfExpression(property.key, property.computed);
3467
3468
  if (key == null) return false;
3468
3469
  usedPropKeys.add(key);
3469
3470
  break;
@@ -3494,7 +3495,7 @@ function collectUsedPropKeysOfReference(context, usedPropKeys, ref) {
3494
3495
  switch (parent.type) {
3495
3496
  case AST_NODE_TYPES.MemberExpression:
3496
3497
  if (parent.object === valueNode) {
3497
- const key = getKeyOfExpression(parent.property);
3498
+ const key = getKeyOfExpression(parent.property, parent.computed);
3498
3499
  if (key == null) return false;
3499
3500
  usedPropKeys.add(key);
3500
3501
  return true;
@@ -3504,9 +3505,9 @@ function collectUsedPropKeysOfReference(context, usedPropKeys, ref) {
3504
3505
  }
3505
3506
  return false;
3506
3507
  }
3507
- function getKeyOfExpression(expr) {
3508
+ function getKeyOfExpression(expr, computed = false) {
3508
3509
  switch (expr.type) {
3509
- case AST_NODE_TYPES.Identifier: return expr.name;
3510
+ case AST_NODE_TYPES.Identifier: return computed ? null : expr.name;
3510
3511
  case AST_NODE_TYPES.Literal: if (isString(expr.value)) return expr.value;
3511
3512
  }
3512
3513
  return null;
@@ -6509,7 +6510,9 @@ function createSetStateResolver$1(context) {
6509
6510
  if (initNode.type !== AST_NODE_TYPES.CallExpression) return false;
6510
6511
  if (!isUseStateCall(initNode)) return false;
6511
6512
  const variableNodeParent = initNode.parent;
6512
- if (!hasProperty(variableNodeParent, "id") || variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return true;
6513
+ if (!hasProperty(variableNodeParent, "id")) return true;
6514
+ if (variableNodeParent.id?.type === AST_NODE_TYPES.ObjectPattern) return variableNodeParent.id.properties.some((p) => p.type === AST_NODE_TYPES.Property && p.key.type === AST_NODE_TYPES.Literal && p.key.value === (at ?? 1) && Check.isIdentifier(p.value, id.name));
6515
+ if (variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return true;
6513
6516
  return variableNodeParent.id.elements.findIndex((e) => e != null && Check.isIdentifier(e, id.name)) === at;
6514
6517
  }
6515
6518
  function isSetStateCall(node) {
@@ -6763,9 +6766,9 @@ function create$5(context) {
6763
6766
  //#endregion
6764
6767
  //#region src/rules/set-state-in-render/helpers.ts
6765
6768
  const isComponentOrHookName = or(core.isFunctionComponentName, core.isHookName);
6766
- function isComponentOrHookLikeFunction(node) {
6769
+ function isComponentOrHookLikeFunction(context, node) {
6767
6770
  const id = core.getFunctionId(node);
6768
- if (id == null) return false;
6771
+ if (id == null) return core.isFunctionComponentWrapperCallback(context, node);
6769
6772
  if (Check.isIdentifier(id)) return isComponentOrHookName(id.name);
6770
6773
  if (id.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(id.property)) return isComponentOrHookName(id.property.name);
6771
6774
  return false;
@@ -6786,18 +6789,19 @@ function isInsideConditional(node, stopAt) {
6786
6789
  }
6787
6790
  //#endregion
6788
6791
  //#region src/rules/set-state-in-render/collect.ts
6789
- function createFactCollector$2(additionalStateHooks) {
6792
+ function createFactCollector$2(context) {
6793
+ const { additionalStateHooks } = getSettingsFromContext(context);
6790
6794
  const facts = {
6791
6795
  calls: [],
6792
6796
  earlyReturns: []
6793
6797
  };
6794
6798
  const functionEntries = [];
6795
- const componentFnRef = { current: null };
6799
+ const componentFnStack = [];
6796
6800
  function isUseStateCall(node) {
6797
6801
  return core.isUseStateLikeCall(node, additionalStateHooks);
6798
6802
  }
6799
6803
  function getFunctionKind(node) {
6800
- if (isComponentOrHookLikeFunction(node)) return "component";
6804
+ if (isComponentOrHookLikeFunction(context, node)) return "component";
6801
6805
  const parent = Traverse.findParent(node, not(Check.isTypeExpression)) ?? node.parent;
6802
6806
  if (parent.type === AST_NODE_TYPES.CallExpression && parent.callee !== node) return "callback";
6803
6807
  return "other";
@@ -6811,10 +6815,10 @@ function createFactCollector$2(additionalStateHooks) {
6811
6815
  kind,
6812
6816
  node
6813
6817
  });
6814
- if (kind === "component") componentFnRef.current = node;
6818
+ if (kind === "component") componentFnStack.push(node);
6815
6819
  },
6816
6820
  ":function:exit"(node) {
6817
- if (functionEntries.at(-1)?.kind === "component" && componentFnRef.current === node) componentFnRef.current = null;
6821
+ if (functionEntries.at(-1)?.kind === "component" && componentFnStack.at(-1) === node) componentFnStack.pop();
6818
6822
  functionEntries.pop();
6819
6823
  },
6820
6824
  CallExpression(node) {
@@ -6822,14 +6826,14 @@ function createFactCollector$2(additionalStateHooks) {
6822
6826
  if (entry == null) return;
6823
6827
  if (isUseStateCall(node)) return;
6824
6828
  facts.calls.push({
6825
- componentFunction: componentFnRef.current,
6829
+ componentFunction: componentFnStack.at(-1) ?? null,
6826
6830
  enclosingFunction: entry.node,
6827
6831
  enclosingFunctionKind: entry.kind,
6828
6832
  node
6829
6833
  });
6830
6834
  },
6831
6835
  ReturnStatement(node) {
6832
- const componentFn = componentFnRef.current;
6836
+ const componentFn = componentFnStack.at(-1);
6833
6837
  if (componentFn == null) return;
6834
6838
  const entry = functionEntries.at(-1);
6835
6839
  if (entry == null || entry.node !== componentFn) return;
@@ -6862,7 +6866,9 @@ function createSetStateResolver(context) {
6862
6866
  if (initNode.type !== AST_NODE_TYPES.CallExpression) return false;
6863
6867
  if (!isUseStateCall(initNode)) return false;
6864
6868
  const variableNodeParent = initNode.parent;
6865
- if (!hasProperty(variableNodeParent, "id") || variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return true;
6869
+ if (!hasProperty(variableNodeParent, "id")) return true;
6870
+ if (variableNodeParent.id?.type === AST_NODE_TYPES.ObjectPattern) return variableNodeParent.id.properties.some((p) => p.type === AST_NODE_TYPES.Property && p.key.type === AST_NODE_TYPES.Literal && p.key.value === (at ?? 1) && Check.isIdentifier(p.value, id.name));
6871
+ if (variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return true;
6866
6872
  return variableNodeParent.id.elements.findIndex((e) => e != null && Check.isIdentifier(e, id.name)) === at;
6867
6873
  }
6868
6874
  function isSetStateCall(node) {
@@ -6926,8 +6932,7 @@ var set_state_in_render_default = createRule({
6926
6932
  create: create$4
6927
6933
  });
6928
6934
  function create$4(context) {
6929
- const { additionalStateHooks } = getSettingsFromContext(context);
6930
- const facts = createFactCollector$2(additionalStateHooks);
6935
+ const facts = createFactCollector$2(context);
6931
6936
  return merge(facts.visitor, { "Program:exit"() {
6932
6937
  for (const violation of inferViolations$1(context, facts.facts)) context.report({
6933
6938
  data: { name: violation.name },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-x",
3
- "version": "5.24.5",
3
+ "version": "5.24.7",
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",
@@ -36,16 +36,16 @@
36
36
  "dist"
37
37
  ],
38
38
  "dependencies": {
39
- "@eslint-react/ast": "5.24.5",
40
- "@eslint-react/core": "5.24.5",
41
- "@eslint-react/eslint": "5.24.5",
42
- "@eslint-react/jsx": "5.24.5",
43
- "@eslint-react/shared": "5.24.5",
44
- "@eslint-react/var": "5.24.5",
45
- "@typescript-eslint/scope-manager": "^8.71.0",
46
- "@typescript-eslint/types": "^8.71.0",
47
- "@typescript-eslint/typescript-estree": "^8.71.0",
48
- "@typescript-eslint/utils": "^8.71.0",
39
+ "@eslint-react/ast": "5.24.7",
40
+ "@eslint-react/core": "5.24.7",
41
+ "@eslint-react/eslint": "5.24.7",
42
+ "@eslint-react/jsx": "5.24.7",
43
+ "@eslint-react/shared": "5.24.7",
44
+ "@eslint-react/var": "5.24.7",
45
+ "@typescript-eslint/scope-manager": "^8.71.1",
46
+ "@typescript-eslint/types": "^8.71.1",
47
+ "@typescript-eslint/typescript-estree": "^8.71.1",
48
+ "@typescript-eslint/utils": "^8.71.1",
49
49
  "compare-versions": "^6.1.1",
50
50
  "string-ts": "^2.3.1",
51
51
  "ts-api-utils": "^2.5.0",