eslint-plugin-react-x 5.8.3 → 5.8.4-beta.1

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 +41 -36
  2. package/package.json +8 -8
package/dist/index.js CHANGED
@@ -4,9 +4,9 @@ import { Check, Compare, Extract, Traverse, is, isOneOf } from "@eslint-react/as
4
4
  import * as core from "@eslint-react/core";
5
5
  import { merge } from "@eslint-react/eslint";
6
6
  import { AST_NODE_TYPES } from "@typescript-eslint/types";
7
+ import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
7
8
  import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
8
9
  import { computeObjectType, isAssignmentTargetEqual, resolve, resolveEnclosingAssignmentTarget } from "@eslint-react/var";
9
- import { DefinitionType } from "@typescript-eslint/scope-manager";
10
10
  import { P, isMatching, match } from "ts-pattern";
11
11
  import { findParentAttribute, getElementFullType, hasAttribute } from "@eslint-react/jsx";
12
12
  import { compare } from "compare-versions";
@@ -143,7 +143,7 @@ const rules$6 = {
143
143
  //#endregion
144
144
  //#region package.json
145
145
  var name$6 = "eslint-plugin-react-x";
146
- var version = "5.8.3";
146
+ var version = "5.8.4-beta.1";
147
147
 
148
148
  //#endregion
149
149
  //#region src/utils/create-rule.ts
@@ -185,7 +185,7 @@ function getEnclosingTryBlock(node) {
185
185
  n = n.parent;
186
186
  }
187
187
  }
188
- if (current.type === "Program") return null;
188
+ if (current.type === AST_NODE_TYPES.Program) return null;
189
189
  current = current.parent;
190
190
  }
191
191
  return null;
@@ -1188,7 +1188,7 @@ function create$49(context) {
1188
1188
  if (variable == null) return true;
1189
1189
  if (variable.defs.length === 0) return true;
1190
1190
  const scopeType = variable.scope.type;
1191
- return scopeType === "global" || scopeType === "module";
1191
+ return scopeType === ScopeType.global || scopeType === ScopeType.module;
1192
1192
  }
1193
1193
  /**
1194
1194
  * Return the nearest enclosing function for `node`.
@@ -1326,8 +1326,7 @@ function identifierExistsInPattern(pattern, name) {
1326
1326
  case AST_NODE_TYPES.Identifier: return pattern.name === name;
1327
1327
  case AST_NODE_TYPES.ObjectPattern: return pattern.properties.some((p) => {
1328
1328
  if (p.type === AST_NODE_TYPES.Property) return identifierExistsInPattern(p.value, name);
1329
- if (p.type === AST_NODE_TYPES.RestElement) return identifierExistsInPattern(p.argument, name);
1330
- return false;
1329
+ return identifierExistsInPattern(p.argument, name);
1331
1330
  });
1332
1331
  case AST_NODE_TYPES.ArrayPattern: return pattern.elements.some((el) => el != null && identifierExistsInPattern(el, name));
1333
1332
  case AST_NODE_TYPES.RestElement: return identifierExistsInPattern(pattern.argument, name);
@@ -1405,9 +1404,12 @@ function create$48(context) {
1405
1404
  if (variable == null) return null;
1406
1405
  for (const def of variable.defs) {
1407
1406
  if (def.type !== DefinitionType.Parameter) continue;
1408
- if (def.node == null) continue;
1409
1407
  let fn = def.node;
1410
- while (fn != null && !Check.isFunction(fn)) fn = fn.parent ?? null;
1408
+ for (;;) {
1409
+ if (Check.isFunction(fn)) break;
1410
+ fn = fn.parent ?? null;
1411
+ if (fn == null) break;
1412
+ }
1411
1413
  if (fn == null) continue;
1412
1414
  const func = fn;
1413
1415
  if (func.params.some((param) => identifierExistsInPattern(param, id.name))) return func;
@@ -1428,7 +1430,7 @@ function create$48(context) {
1428
1430
  CallExpression(node) {
1429
1431
  if (isUseStateCall(node) || core.isUseReducerCall(context, node)) {
1430
1432
  const declarator = node.parent;
1431
- if (declarator?.type === AST_NODE_TYPES.VariableDeclarator && declarator.id?.type === AST_NODE_TYPES.ArrayPattern) {
1433
+ if (declarator.type === AST_NODE_TYPES.VariableDeclarator && declarator.id.type === AST_NODE_TYPES.ArrayPattern) {
1432
1434
  const [firstElement] = declarator.id.elements;
1433
1435
  if (firstElement?.type === AST_NODE_TYPES.Identifier && isRefLikeName(firstElement.name)) context.report({
1434
1436
  data: { name: firstElement.name },
@@ -1729,13 +1731,13 @@ function create$47(context) {
1729
1731
  setStateStack.pop();
1730
1732
  },
1731
1733
  ClassDeclaration(node) {
1732
- classStack.push([node, core.isClassComponent(node)]);
1734
+ classStack.push([node, core.isClassComponent(context, node)]);
1733
1735
  },
1734
1736
  "ClassDeclaration:exit"() {
1735
1737
  classStack.pop();
1736
1738
  },
1737
1739
  ClassExpression(node) {
1738
- classStack.push([node, core.isClassComponent(node)]);
1740
+ classStack.push([node, core.isClassComponent(context, node)]);
1739
1741
  },
1740
1742
  "ClassExpression:exit"() {
1741
1743
  classStack.pop();
@@ -1793,7 +1795,7 @@ function create$47(context) {
1793
1795
  function report$2(context) {
1794
1796
  return (descriptor) => {
1795
1797
  if (descriptor == null) return;
1796
- return context.report(descriptor);
1798
+ context.report(descriptor);
1797
1799
  };
1798
1800
  }
1799
1801
  function getIndexParamPosition(methodName) {
@@ -2244,7 +2246,7 @@ var no_create_ref_default = createRule({
2244
2246
  });
2245
2247
  function create$34(context) {
2246
2248
  return merge({ CallExpression(node) {
2247
- if (core.isCreateRefCall(context, node) && Traverse.findParent(node, core.isClassComponent) == null) context.report({
2249
+ if (core.isCreateRefCall(context, node) && Traverse.findParent(node, (n) => core.isClassComponent(context, n)) == null) context.report({
2248
2250
  messageId: "default",
2249
2251
  node
2250
2252
  });
@@ -2273,7 +2275,7 @@ function create$33(context) {
2273
2275
  if (!core.isAssignmentToThisState(node)) return;
2274
2276
  const parentClass = Traverse.findParent(node, isOneOf([AST_NODE_TYPES.ClassDeclaration, AST_NODE_TYPES.ClassExpression]));
2275
2277
  if (parentClass == null) return;
2276
- if (core.isClassComponent(parentClass) && context.sourceCode.getScope(node).block !== Traverse.findParent(node, isConstructorFunction)) context.report({
2278
+ if (core.isClassComponent(context, parentClass) && context.sourceCode.getScope(node).block !== Traverse.findParent(node, isConstructorFunction)) context.report({
2277
2279
  messageId: "default",
2278
2280
  node
2279
2281
  });
@@ -2593,7 +2595,7 @@ function create$28(context) {
2593
2595
  function report$1(context) {
2594
2596
  return (descriptor) => {
2595
2597
  if (descriptor == null) return;
2596
- return context.report(descriptor);
2598
+ context.report(descriptor);
2597
2599
  };
2598
2600
  }
2599
2601
 
@@ -2778,7 +2780,7 @@ function create$25(context) {
2778
2780
  function report(context) {
2779
2781
  return (descriptor) => {
2780
2782
  if (descriptor == null) return;
2781
- return context.report(descriptor);
2783
+ context.report(descriptor);
2782
2784
  };
2783
2785
  }
2784
2786
  /**
@@ -2969,11 +2971,12 @@ function isInsideJSXAttributeValue(node) {
2969
2971
  /**
2970
2972
  * Check whether a given node is declared inside a class component's render block
2971
2973
  * Ex: class C extends React.Component { render() { const Nested = () => <div />; } }
2974
+ * @param context The rule context
2972
2975
  * @param node The AST node being checked
2973
2976
  * @returns `true` if the node is inside a class component's render block
2974
2977
  */
2975
- function isInsideRenderMethod(node) {
2976
- return Traverse.findParent(node, (n) => core.isRenderMethodLike(n) && core.isClassComponent(n.parent.parent)) != null;
2978
+ function isInsideRenderMethod(context, node) {
2979
+ return Traverse.findParent(node, (n) => core.isRenderMethodLike(n) && core.isClassComponent(context, n.parent.parent)) != null;
2977
2980
  }
2978
2981
  /**
2979
2982
  * Determine whether the node is inside `createElement`'s props argument
@@ -3052,7 +3055,7 @@ function create$22(context) {
3052
3055
  });
3053
3056
  continue;
3054
3057
  }
3055
- if (isInsideRenderMethod(component)) context.report({
3058
+ if (isInsideRenderMethod(context, component)) context.report({
3056
3059
  data: {
3057
3060
  name,
3058
3061
  suggestion: "Move it to the top level."
@@ -3105,7 +3108,7 @@ function create$21(context) {
3105
3108
  ...hc.api.getAllHooks(program),
3106
3109
  ...cc.api.getAllComponents(program)
3107
3110
  ];
3108
- for (const lazy of lazyCalls) if (Traverse.findParent(lazy, (n) => significantParents.some((p) => p.node === n))) context.report({
3111
+ for (const lazy of lazyCalls) if (Traverse.findParent(lazy, (n) => significantParents.some((p) => p.node === n)) != null) context.report({
3109
3112
  messageId: "default",
3110
3113
  node: lazy
3111
3114
  });
@@ -3131,7 +3134,7 @@ function create$20(context) {
3131
3134
  if (!context.sourceCode.text.includes("componentDidMount")) return {};
3132
3135
  return merge({ CallExpression(node) {
3133
3136
  if (!core.isThisSetStateCall(node)) return;
3134
- const enclosingClassNode = Traverse.findParent(node, core.isClassComponent);
3137
+ const enclosingClassNode = Traverse.findParent(node, (n) => core.isClassComponent(context, n));
3135
3138
  const enclosingMethodNode = Traverse.findParent(node, (n) => n === enclosingClassNode || core.isComponentDidMount(n));
3136
3139
  if (enclosingClassNode == null || enclosingMethodNode == null || enclosingMethodNode === enclosingClassNode) return;
3137
3140
  const enclosingMethodScope = context.sourceCode.getScope(enclosingMethodNode);
@@ -3161,7 +3164,7 @@ function create$19(context) {
3161
3164
  if (!context.sourceCode.text.includes("componentDidUpdate")) return {};
3162
3165
  return merge({ CallExpression(node) {
3163
3166
  if (!core.isThisSetStateCall(node)) return;
3164
- const enclosingClassNode = Traverse.findParent(node, core.isClassComponent);
3167
+ const enclosingClassNode = Traverse.findParent(node, (n) => core.isClassComponent(context, n));
3165
3168
  const enclosingMethodNode = Traverse.findParent(node, (n) => n === enclosingClassNode || core.isComponentDidUpdate(n));
3166
3169
  if (enclosingClassNode == null || enclosingMethodNode == null || enclosingMethodNode === enclosingClassNode) return;
3167
3170
  const enclosingMethodScope = context.sourceCode.getScope(enclosingMethodNode);
@@ -3191,7 +3194,7 @@ function create$18(context) {
3191
3194
  if (!context.sourceCode.text.includes("componentWillUpdate")) return {};
3192
3195
  return merge({ CallExpression(node) {
3193
3196
  if (!core.isThisSetStateCall(node)) return;
3194
- const enclosingClassNode = Traverse.findParent(node, core.isClassComponent);
3197
+ const enclosingClassNode = Traverse.findParent(node, (n) => core.isClassComponent(context, n));
3195
3198
  const enclosingMethodNode = Traverse.findParent(node, (n) => n === enclosingClassNode || core.isComponentWillUpdate(n));
3196
3199
  if (enclosingClassNode == null || enclosingMethodNode == null || enclosingMethodNode === enclosingClassNode) return;
3197
3200
  const enclosingMethodScope = context.sourceCode.getScope(enclosingMethodNode);
@@ -3537,13 +3540,13 @@ function create$11(context) {
3537
3540
  const propertyUsages = /* @__PURE__ */ new WeakMap();
3538
3541
  function classEnter(node) {
3539
3542
  classStack.push(node);
3540
- if (!core.isClassComponent(node)) return;
3543
+ if (!core.isClassComponent(context, node)) return;
3541
3544
  propertyDefs.set(node, /* @__PURE__ */ new Set());
3542
3545
  propertyUsages.set(node, /* @__PURE__ */ new Set());
3543
3546
  }
3544
3547
  function classExit() {
3545
3548
  const currentClass = classStack.pop();
3546
- if (currentClass == null || !core.isClassComponent(currentClass)) return;
3549
+ if (currentClass == null || !core.isClassComponent(context, currentClass)) return;
3547
3550
  const id = core.getClassId(currentClass);
3548
3551
  const defs = propertyDefs.get(currentClass);
3549
3552
  const usages = propertyUsages.get(currentClass);
@@ -3566,7 +3569,7 @@ function create$11(context) {
3566
3569
  function methodEnter(node) {
3567
3570
  methodStack.push(node);
3568
3571
  const currentClass = classStack.at(-1);
3569
- if (currentClass == null || !core.isClassComponent(currentClass)) return;
3572
+ if (currentClass == null || !core.isClassComponent(context, currentClass)) return;
3570
3573
  if (node.static) return;
3571
3574
  if (isKeyLiteral(node, node.key)) propertyDefs.get(currentClass)?.add(node.key);
3572
3575
  }
@@ -3582,7 +3585,7 @@ function create$11(context) {
3582
3585
  const currentClass = classStack.at(-1);
3583
3586
  const currentMethod = methodStack.at(-1);
3584
3587
  if (currentClass == null || currentMethod == null) return;
3585
- if (!core.isClassComponent(currentClass) || currentMethod.static) return;
3588
+ if (!core.isClassComponent(context, currentClass) || currentMethod.static) return;
3586
3589
  if (node.object.type !== AST_NODE_TYPES.ThisExpression || !isKeyLiteral(node, node.property)) return;
3587
3590
  if (node.parent.type === AST_NODE_TYPES.AssignmentExpression && node.parent.left === node) {
3588
3591
  propertyDefs.get(currentClass)?.add(node.property);
@@ -3599,7 +3602,7 @@ function create$11(context) {
3599
3602
  const currentClass = classStack.at(-1);
3600
3603
  const currentMethod = methodStack.at(-1);
3601
3604
  if (currentClass == null || currentMethod == null) return;
3602
- if (!core.isClassComponent(currentClass) || currentMethod.static) return;
3605
+ if (!core.isClassComponent(context, currentClass) || currentMethod.static) return;
3603
3606
  if (node.init != null && node.init.type === AST_NODE_TYPES.ThisExpression && node.id.type === AST_NODE_TYPES.ObjectPattern) {
3604
3607
  for (const prop of node.id.properties) if (prop.type === AST_NODE_TYPES.Property && isKeyLiteral(prop, prop.key)) {
3605
3608
  const keyName = Extract.getPropertyName(prop.key);
@@ -3749,7 +3752,7 @@ function create$9(context) {
3749
3752
  CallExpression(node) {
3750
3753
  if (!core.isUseStateLikeCall(node, additionalStateHooks)) return;
3751
3754
  const { parent } = node;
3752
- if (parent?.type !== AST_NODE_TYPES.VariableDeclarator || parent.id.type !== AST_NODE_TYPES.ArrayPattern) return;
3755
+ if (parent.type !== AST_NODE_TYPES.VariableDeclarator || parent.id.type !== AST_NODE_TYPES.ArrayPattern) return;
3753
3756
  const [stateEl] = parent.id.elements;
3754
3757
  if (stateEl?.type !== AST_NODE_TYPES.Identifier) return;
3755
3758
  stateEntries.push({
@@ -4159,7 +4162,8 @@ function create$7(context) {
4159
4162
  case expr.type === AST_NODE_TYPES.Identifier: {
4160
4163
  const builtinName = resolveBuiltinObjectName(context, expr);
4161
4164
  if (builtinName == null) return;
4162
- if (!IMPURE_FUNCS.get("globalThis")?.has(builtinName)) return;
4165
+ const globalThisImpure = IMPURE_FUNCS.get("globalThis");
4166
+ if (globalThisImpure == null || !globalThisImpure.has(builtinName)) return;
4163
4167
  const func = Traverse.findParent(node, Check.isFunction);
4164
4168
  if (func == null) return;
4165
4169
  cEntries.push({
@@ -4174,7 +4178,8 @@ function create$7(context) {
4174
4178
  const objectName = resolveBuiltinObjectName(context, rootId);
4175
4179
  if (objectName == null) return;
4176
4180
  const propertyName = expr.property.name;
4177
- if (!IMPURE_FUNCS.get(objectName)?.has(propertyName)) return;
4181
+ const objectImpure = IMPURE_FUNCS.get(objectName);
4182
+ if (objectImpure == null || !objectImpure.has(propertyName)) return;
4178
4183
  const func = Traverse.findParent(node, Check.isFunction);
4179
4184
  if (func == null) return;
4180
4185
  cEntries.push({
@@ -4238,7 +4243,7 @@ function isInNullCheckTest(node) {
4238
4243
  if (!isLiteralNull(parent.left === node || Extract.unwrap(parent.left) === node ? parent.right : parent.left)) return false;
4239
4244
  if (isIfTest(parent)) return true;
4240
4245
  const grandparent = parent.parent;
4241
- if (grandparent?.type === AST_NODE_TYPES.UnaryExpression && grandparent.operator === "!" && isIfTest(grandparent)) return true;
4246
+ if (grandparent.type === AST_NODE_TYPES.UnaryExpression && grandparent.operator === "!" && isIfTest(grandparent)) return true;
4242
4247
  return false;
4243
4248
  }
4244
4249
  if (parent.type === AST_NODE_TYPES.UnaryExpression && parent.operator === "!") return isIfTest(parent);
@@ -4390,7 +4395,7 @@ function create$6(context) {
4390
4395
  let isLazyInit = isInNullCheckTest(node);
4391
4396
  if (!isLazyInit) {
4392
4397
  let current = node.parent;
4393
- findIf: while (current != null) {
4398
+ findIf: for (;;) {
4394
4399
  if (current.type === AST_NODE_TYPES.IfStatement) {
4395
4400
  if (isRefCurrentNullCheck(current.test, refName)) isLazyInit = true;
4396
4401
  break;
@@ -6740,7 +6745,7 @@ function isRefInExpression(context, node) {
6740
6745
  * @returns The actual CallExpression node
6741
6746
  */
6742
6747
  function getSetStateCallExpression(node) {
6743
- return node.type === AST_NODE_TYPES.Identifier && node.parent?.type === AST_NODE_TYPES.CallExpression ? node.parent : node;
6748
+ return node.type === AST_NODE_TYPES.Identifier && node.parent.type === AST_NODE_TYPES.CallExpression ? node.parent : node;
6744
6749
  }
6745
6750
 
6746
6751
  //#endregion
@@ -7174,7 +7179,7 @@ function getDynamicComponentSource(context, variable, isInsideRender, seen = /*
7174
7179
  for (const ref of variable.references) {
7175
7180
  if (!ref.isWrite()) continue;
7176
7181
  const id = ref.identifier;
7177
- if (id.parent?.type === AST_NODE_TYPES.AssignmentExpression && id.parent.left === id) {
7182
+ if (id.parent.type === AST_NODE_TYPES.AssignmentExpression && id.parent.left === id) {
7178
7183
  const source = resolveDynamicValue(context, id.parent.right, isInsideRender, seen);
7179
7184
  if (source != null) return {
7180
7185
  creationNode: source,
@@ -7400,7 +7405,7 @@ function create$1(context) {
7400
7405
  if (node.type !== AST_NODE_TYPES.AssignmentExpression) return;
7401
7406
  const left = Extract.unwrap(node.left);
7402
7407
  if (left.type !== AST_NODE_TYPES.Identifier) return;
7403
- if (Traverse.findParent(node, Check.isFunction, (n) => n === callback)) return;
7408
+ if (Traverse.findParent(node, Check.isFunction, (n) => n === callback) != null) return;
7404
7409
  const variable = findVariable(context.sourceCode.getScope(left), left);
7405
7410
  if (variable != null && variable.defs.length > 0 && isDeclaredInsideCallback(variable, callback)) return;
7406
7411
  violations.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-x",
3
- "version": "5.8.3",
3
+ "version": "5.8.4-beta.1",
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,12 +45,12 @@
45
45
  "string-ts": "^2.3.1",
46
46
  "ts-api-utils": "^2.5.0",
47
47
  "ts-pattern": "^5.9.0",
48
- "@eslint-react/ast": "5.8.3",
49
- "@eslint-react/core": "5.8.3",
50
- "@eslint-react/eslint": "5.8.3",
51
- "@eslint-react/jsx": "5.8.3",
52
- "@eslint-react/shared": "5.8.3",
53
- "@eslint-react/var": "5.8.3"
48
+ "@eslint-react/ast": "5.8.4-beta.1",
49
+ "@eslint-react/core": "5.8.4-beta.1",
50
+ "@eslint-react/eslint": "5.8.4-beta.1",
51
+ "@eslint-react/jsx": "5.8.4-beta.1",
52
+ "@eslint-react/shared": "5.8.4-beta.1",
53
+ "@eslint-react/var": "5.8.4-beta.1"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@types/react": "^19.2.14",
@@ -77,7 +77,7 @@
77
77
  "@local/eff": "workspace:*"
78
78
  },
79
79
  "scripts": {
80
- "build": "tsdown --dts-resolve",
80
+ "build": "tsdown",
81
81
  "lint:publish": "publint",
82
82
  "lint:ts": "tsl"
83
83
  }