eslint-plugin-react-x 3.0.0-beta.60 → 3.0.0-beta.61

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 +102 -26
  2. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -5,9 +5,9 @@ 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, getObjectType, getVariableInitializer, isAssignmentTargetEqual } from "@eslint-react/var";
9
- import { DefinitionType } from "@typescript-eslint/scope-manager";
10
8
  import { constFalse, constTrue, constVoid, flow, getOrElseUpdate, identity, not, unit } from "@eslint-react/eff";
9
+ import { findEnclosingAssignmentTarget, findVariable, getObjectType, isAssignmentTargetEqual } from "@eslint-react/var";
10
+ import { DefinitionType } from "@typescript-eslint/scope-manager";
11
11
  import { compare } from "compare-versions";
12
12
  import { getConstrainedTypeAtLocation } from "@typescript-eslint/type-utils";
13
13
  import { unionConstituents } from "ts-api-utils";
@@ -69,7 +69,7 @@ const rules$7 = {
69
69
  //#endregion
70
70
  //#region package.json
71
71
  var name$6 = "eslint-plugin-react-x";
72
- var version = "3.0.0-beta.60";
72
+ var version = "3.0.0-beta.61";
73
73
 
74
74
  //#endregion
75
75
  //#region src/utils/create-rule.ts
@@ -1289,7 +1289,14 @@ function create$61(context) {
1289
1289
  * @returns True if `id` is a state variable, false otherwise.
1290
1290
  */
1291
1291
  function isStateValue(id) {
1292
- const initNode = getVariableInitializer(findVariable(id, context.sourceCode.getScope(id)), 0);
1292
+ function resolve(v) {
1293
+ if (v == null) return unit;
1294
+ const def = v.defs.at(0);
1295
+ if (def == null) return unit;
1296
+ if ("init" in def.node && def.node.init != null && !("declarations" in def.node.init)) return def.node.init;
1297
+ return def.node;
1298
+ }
1299
+ const initNode = resolve(findVariable(id, context.sourceCode.getScope(id)));
1293
1300
  if (initNode == null || initNode.type !== AST_NODE_TYPES.CallExpression) return false;
1294
1301
  if (!isUseStateCall(initNode)) return false;
1295
1302
  const declarator = initNode.parent;
@@ -3144,7 +3151,19 @@ function create$22(context) {
3144
3151
  const [arg0, arg1] = init.arguments;
3145
3152
  if (arg0 == null || arg1 == null) return;
3146
3153
  if (!match(arg1).with({ type: AST_NODE_TYPES.ArrayExpression }, (n) => n.elements.length === 0).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
3147
- const initNode = getVariableInitializer(findVariable(n.name, scope), 0);
3154
+ const variable = findVariable(n.name, scope);
3155
+ function resolve(v) {
3156
+ if (v == null) return unit;
3157
+ const def = v.defs.at(0);
3158
+ if (def == null) return unit;
3159
+ switch (true) {
3160
+ case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
3161
+ case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
3162
+ case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
3163
+ default: return unit;
3164
+ }
3165
+ }
3166
+ const initNode = resolve(variable);
3148
3167
  if (initNode?.type !== AST_NODE_TYPES.ArrayExpression) return false;
3149
3168
  return initNode.elements.length === 0;
3150
3169
  }).otherwise(() => false)) {
@@ -3155,7 +3174,19 @@ function create$22(context) {
3155
3174
  if (n.body.type === AST_NODE_TYPES.ArrowFunctionExpression) return n.body;
3156
3175
  return n;
3157
3176
  }).with({ type: AST_NODE_TYPES.FunctionExpression }, identity).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
3158
- const initNode = getVariableInitializer(findVariable(n.name, scope), 0);
3177
+ const variable = findVariable(n.name, scope);
3178
+ function resolve(v) {
3179
+ if (v == null) return unit;
3180
+ const def = v.defs.at(0);
3181
+ if (def == null) return unit;
3182
+ switch (true) {
3183
+ case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
3184
+ case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
3185
+ case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
3186
+ default: return unit;
3187
+ }
3188
+ }
3189
+ const initNode = resolve(variable);
3159
3190
  if (initNode?.type !== AST_NODE_TYPES.ArrowFunctionExpression && initNode?.type !== AST_NODE_TYPES.FunctionExpression) return null;
3160
3191
  return initNode;
3161
3192
  }).otherwise(() => null);
@@ -3228,7 +3259,19 @@ function create$21(context) {
3228
3259
  return;
3229
3260
  }
3230
3261
  if (!match(arg1).with({ type: AST_NODE_TYPES.ArrayExpression }, (n) => n.elements.length === 0).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
3231
- const initNode = getVariableInitializer(findVariable(n.name, scope), 0);
3262
+ const variable = findVariable(n.name, scope);
3263
+ function resolve(v) {
3264
+ if (v == null) return unit;
3265
+ const def = v.defs.at(0);
3266
+ if (def == null) return unit;
3267
+ switch (true) {
3268
+ case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
3269
+ case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
3270
+ case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
3271
+ default: return unit;
3272
+ }
3273
+ }
3274
+ const initNode = resolve(variable);
3232
3275
  if (initNode?.type !== AST_NODE_TYPES.ArrayExpression) return false;
3233
3276
  return initNode.elements.length === 0;
3234
3277
  }).otherwise(() => false)) {
@@ -3239,7 +3282,14 @@ function create$21(context) {
3239
3282
  if (n.body.type === AST_NODE_TYPES.ArrowFunctionExpression) return n.body;
3240
3283
  return n;
3241
3284
  }).with({ type: AST_NODE_TYPES.FunctionExpression }, identity).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
3242
- const variableNode = getVariableInitializer(findVariable(n.name, scope), 0);
3285
+ function resolve(v) {
3286
+ if (v == null) return unit;
3287
+ const def = v.defs.at(0);
3288
+ if (def == null) return unit;
3289
+ if ("init" in def.node && def.node.init != null && !("declarations" in def.node.init)) return def.node.init;
3290
+ return def.node;
3291
+ }
3292
+ const variableNode = resolve(findVariable(n.name, scope));
3243
3293
  if (variableNode?.type !== AST_NODE_TYPES.ArrowFunctionExpression && variableNode?.type !== AST_NODE_TYPES.FunctionExpression) return null;
3244
3294
  return variableNode;
3245
3295
  }).otherwise(() => null);
@@ -6545,7 +6595,19 @@ function create$5(context) {
6545
6595
  }
6546
6596
  }
6547
6597
  function isIdFromUseStateCall(id, at) {
6548
- const initNode = getVariableInitializer(findVariable(id, context.sourceCode.getScope(id)), 0);
6598
+ const variable = findVariable(id, context.sourceCode.getScope(id));
6599
+ function resolve(v) {
6600
+ if (v == null) return unit;
6601
+ const def = v.defs.at(0);
6602
+ if (def == null) return unit;
6603
+ switch (true) {
6604
+ case def.type === DefinitionType.FunctionName && def.node.type === AST_NODE_TYPES.FunctionDeclaration: return def.node;
6605
+ case def.type === DefinitionType.ClassName && def.node.type === AST_NODE_TYPES.ClassDeclaration: return def.node;
6606
+ case "init" in def.node && def.node.init != null && !("declarations" in def.node.init): return def.node.init;
6607
+ default: return unit;
6608
+ }
6609
+ }
6610
+ const initNode = resolve(variable);
6549
6611
  if (initNode == null) return false;
6550
6612
  if (initNode.type !== AST_NODE_TYPES.CallExpression) return false;
6551
6613
  if (!isUseStateCall(initNode)) return false;
@@ -6668,7 +6730,14 @@ function create$5(context) {
6668
6730
  },
6669
6731
  "Program:exit"() {
6670
6732
  const getSetStateCalls = (id, initialScope) => {
6671
- const node = getVariableInitializer(findVariable(id, initialScope), 0);
6733
+ function resolve(v) {
6734
+ if (v == null) return unit;
6735
+ const def = v.defs.at(0);
6736
+ if (def == null) return unit;
6737
+ if ("init" in def.node && def.node.init != null && !("declarations" in def.node.init)) return def.node.init;
6738
+ return def.node;
6739
+ }
6740
+ const node = resolve(findVariable(id, initialScope));
6672
6741
  switch (node?.type) {
6673
6742
  case AST_NODE_TYPES.ArrowFunctionExpression:
6674
6743
  case AST_NODE_TYPES.FunctionDeclaration:
@@ -6727,7 +6796,14 @@ function create$4(context) {
6727
6796
  return core.isUseStateLikeCall(node, additionalStateHooks);
6728
6797
  }
6729
6798
  function isIdFromUseStateCall(topLevelId, at) {
6730
- const initNode = getVariableInitializer(findVariable(topLevelId, context.sourceCode.getScope(topLevelId)), 0);
6799
+ function resolve(v) {
6800
+ if (v == null) return unit;
6801
+ const def = v.defs.at(0);
6802
+ if (def == null) return unit;
6803
+ if ("init" in def.node && def.node.init != null && !("declarations" in def.node.init)) return def.node.init;
6804
+ return def.node;
6805
+ }
6806
+ const initNode = resolve(findVariable(topLevelId, context.sourceCode.getScope(topLevelId)));
6731
6807
  if (initNode == null) return false;
6732
6808
  if (initNode.type !== AST_NODE_TYPES.CallExpression) return false;
6733
6809
  if (!isUseStateCall(initNode)) return false;
@@ -7097,14 +7173,6 @@ function create$1(context) {
7097
7173
  //#endregion
7098
7174
  //#region src/rules/unstable-rules-of-props/unstable-rules-of-props.ts
7099
7175
  const RULE_NAME = "unstable-rules-of-props";
7100
- /**
7101
- * Pairs of [controlled prop, uncontrolled prop] that must not appear together
7102
- * on the same JSX element.
7103
- *
7104
- * - `value` → controlled; `defaultValue` → uncontrolled
7105
- * - `checked` → controlled; `defaultChecked` → uncontrolled
7106
- */
7107
- const CONTROLLED_UNCONTROLLED_PAIRS = [["value", "defaultValue"], ["checked", "defaultChecked"]];
7108
7176
  var unstable_rules_of_props_default = createRule({
7109
7177
  meta: {
7110
7178
  type: "problem",
@@ -7117,25 +7185,33 @@ var unstable_rules_of_props_default = createRule({
7117
7185
  defaultOptions: []
7118
7186
  });
7119
7187
  function create(context) {
7188
+ /**
7189
+ * Pairs of [controlled prop, uncontrolled prop] that must not appear together
7190
+ * on the same JSX element.
7191
+ *
7192
+ * - `value` → controlled; `defaultValue` → uncontrolled
7193
+ * - `checked` → controlled; `defaultChecked` → uncontrolled
7194
+ */
7195
+ const pairs = [["value", "defaultValue"], ["checked", "defaultChecked"]];
7120
7196
  return defineRuleListener({ JSXOpeningElement(node) {
7121
- const propMap = /* @__PURE__ */ new Map();
7197
+ const map = /* @__PURE__ */ new Map();
7122
7198
  for (const attr of node.attributes) {
7123
7199
  if (attr.type === AST_NODE_TYPES.JSXSpreadAttribute) continue;
7124
7200
  const { name } = attr.name;
7125
7201
  if (typeof name !== "string") continue;
7126
- propMap.set(name, attr);
7202
+ map.set(name, attr);
7127
7203
  }
7128
- for (const [controlled, uncontrolled] of CONTROLLED_UNCONTROLLED_PAIRS) {
7129
- if (!propMap.has(controlled) || !propMap.has(uncontrolled)) continue;
7130
- const uncontrolledAttr = propMap.get(uncontrolled);
7131
- if (uncontrolledAttr == null) continue;
7204
+ for (const [controlled, uncontrolled] of pairs) {
7205
+ if (!map.has(controlled) || !map.has(uncontrolled)) continue;
7206
+ const attr = map.get(uncontrolled);
7207
+ if (attr == null) continue;
7132
7208
  context.report({
7133
7209
  data: {
7134
7210
  controlled,
7135
7211
  uncontrolled
7136
7212
  },
7137
7213
  messageId: "noControlledAndUncontrolledTogether",
7138
- node: uncontrolledAttr
7214
+ node: attr
7139
7215
  });
7140
7216
  }
7141
7217
  } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-x",
3
- "version": "3.0.0-beta.60",
3
+ "version": "3.0.0-beta.61",
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
  "string-ts": "^2.3.1",
46
46
  "ts-api-utils": "^2.4.0",
47
47
  "ts-pattern": "^5.9.0",
48
- "@eslint-react/ast": "3.0.0-beta.60",
49
- "@eslint-react/eff": "3.0.0-beta.60",
50
- "@eslint-react/core": "3.0.0-beta.60",
51
- "@eslint-react/shared": "3.0.0-beta.60",
52
- "@eslint-react/var": "3.0.0-beta.60"
48
+ "@eslint-react/ast": "3.0.0-beta.61",
49
+ "@eslint-react/eff": "3.0.0-beta.61",
50
+ "@eslint-react/shared": "3.0.0-beta.61",
51
+ "@eslint-react/var": "3.0.0-beta.61",
52
+ "@eslint-react/core": "3.0.0-beta.61"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@types/react": "^19.2.14",