eslint-plugin-react-x 5.28.8 → 5.30.0-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.
package/dist/index.js CHANGED
@@ -8,13 +8,13 @@ import { P, isMatching, match } from "ts-pattern";
8
8
  import { isAssignmentTargetEqual, isInitializedFromReact, resolveEnclosingAssignmentTarget, resolveObjectType, resolveOrigin } from "@eslint-react/var";
9
9
  import { DefinitionType, ScopeType } from "@typescript-eslint/scope-manager";
10
10
  import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
11
- import { findParentAttribute, getElementFullType, hasAttribute, isAttribute } from "@eslint-react/jsx";
11
+ import { findParentAttribute, getCreateElementPropsObject, getElementFullType, hasAttribute, isAttribute, isInsideCreateElementProps } from "@eslint-react/jsx";
12
12
  import { compare } from "compare-versions";
13
13
  import { unionConstituents } from "ts-api-utils";
14
14
  import "typescript";
15
15
  import { simpleTraverse } from "@typescript-eslint/typescript-estree";
16
16
  import { delimiterCase, snakeCase, toLowerCase } from "string-ts";
17
- //#region \0rolldown/runtime.js
17
+ // \0rolldown/runtime.js
18
18
  var __defProp = Object.defineProperty;
19
19
  var __exportAll = (all, no_symbols) => {
20
20
  let target = {};
@@ -25,8 +25,7 @@ var __exportAll = (all, no_symbols) => {
25
25
  if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
26
26
  return target;
27
27
  };
28
- //#endregion
29
- //#region src/configs/disable-conflict-eslint-plugin-react.ts
28
+ // src/configs/disable-conflict-eslint-plugin-react.ts
30
29
  var disable_conflict_eslint_plugin_react_exports = /* @__PURE__ */ __exportAll({
31
30
  name: () => name$10,
32
31
  rules: () => rules$9
@@ -75,8 +74,7 @@ const conflictingRules$1 = [
75
74
  ];
76
75
  const name$10 = "react-x/disable-conflict-eslint-plugin-react";
77
76
  const rules$9 = Object.fromEntries(conflictingRules$1.map((key) => [key, "off"]));
78
- //#endregion
79
- //#region src/configs/disable-conflict-eslint-plugin-react-hooks.ts
77
+ // src/configs/disable-conflict-eslint-plugin-react-hooks.ts
80
78
  var disable_conflict_eslint_plugin_react_hooks_exports = /* @__PURE__ */ __exportAll({
81
79
  name: () => name$9,
82
80
  rules: () => rules$8
@@ -97,8 +95,7 @@ const conflictingRules = [
97
95
  ];
98
96
  const name$9 = "react-x/disable-conflict-eslint-plugin-react-hooks";
99
97
  const rules$8 = Object.fromEntries(conflictingRules.map((key) => [key, "off"]));
100
- //#endregion
101
- //#region src/configs/disable-experimental.ts
98
+ // src/configs/disable-experimental.ts
102
99
  var disable_experimental_exports = /* @__PURE__ */ __exportAll({
103
100
  name: () => name$8,
104
101
  rules: () => rules$7
@@ -118,8 +115,7 @@ const rules$7 = {
118
115
  "react-x/set-state-in-render": "off",
119
116
  "react-x/static-components": "off"
120
117
  };
121
- //#endregion
122
- //#region src/configs/disable-type-checked.ts
118
+ // src/configs/disable-type-checked.ts
123
119
  var disable_type_checked_exports = /* @__PURE__ */ __exportAll({
124
120
  name: () => name$7,
125
121
  rules: () => rules$6
@@ -132,12 +128,10 @@ const rules$6 = {
132
128
  "react-x/no-leaked-conditional-rendering": "off",
133
129
  "react-x/no-unused-props": "off"
134
130
  };
135
- //#endregion
136
- //#region package.json
131
+ // package.json
137
132
  var name$6 = "eslint-plugin-react-x";
138
- var version = "5.28.8";
139
- //#endregion
140
- //#region src/utils/create-rule.ts
133
+ var version = "5.30.0-beta.1";
134
+ // src/utils/create-rule.ts
141
135
  function getDocsUrl(ruleName) {
142
136
  return `https://eslint-react.xyz/docs/rules/${ruleName}`;
143
137
  }
@@ -197,8 +191,7 @@ function create$50(context) {
197
191
  }
198
192
  });
199
193
  }
200
- //#endregion
201
- //#region src/rules/exhaustive-deps/exhaustive-deps.ts
194
+ // src/rules/exhaustive-deps/exhaustive-deps.ts
202
195
  const rule$1 = {
203
196
  meta: {
204
197
  type: "suggestion",
@@ -1068,8 +1061,7 @@ function isUseEffectEventIdentifier$1(node) {
1068
1061
  function getUnknownDependenciesMessage(reactiveHookName) {
1069
1062
  return `React Hook ${reactiveHookName} received a function whose dependencies are unknown. Pass an inline function instead.`;
1070
1063
  }
1071
- //#endregion
1072
- //#region src/rules/globals/helpers.ts
1064
+ // src/rules/globals/helpers.ts
1073
1065
  const MUTATING_ARRAY_METHODS = /* @__PURE__ */ new Set([
1074
1066
  "copyWithin",
1075
1067
  "fill",
@@ -1081,8 +1073,7 @@ const MUTATING_ARRAY_METHODS = /* @__PURE__ */ new Set([
1081
1073
  "splice",
1082
1074
  "unshift"
1083
1075
  ]);
1084
- //#endregion
1085
- //#region src/rules/globals/collect.ts
1076
+ // src/rules/globals/collect.ts
1086
1077
  function createFactCollector$6() {
1087
1078
  const facts = {
1088
1079
  callEdges: [],
@@ -1147,8 +1138,7 @@ function createFactCollector$6() {
1147
1138
  }
1148
1139
  };
1149
1140
  }
1150
- //#endregion
1151
- //#region src/rules/globals/origins.ts
1141
+ // src/rules/globals/origins.ts
1152
1142
  function isGlobalVariable(context, node) {
1153
1143
  const variable = findVariable(context.sourceCode.getScope(node), node);
1154
1144
  if (variable == null || variable.defs.length === 0) return true;
@@ -1179,8 +1169,7 @@ function resolveToFunction(context, node, seen = /* @__PURE__ */ new Set()) {
1179
1169
  if (resolved == null) return null;
1180
1170
  return resolveToFunction(context, resolved, seen);
1181
1171
  }
1182
- //#endregion
1183
- //#region src/rules/globals/effects.ts
1172
+ // src/rules/globals/effects.ts
1184
1173
  function inferGlobalMutations(context, facts) {
1185
1174
  const directEffects = /* @__PURE__ */ new Map();
1186
1175
  function pushEffect(enclosingFunction, effect) {
@@ -1282,8 +1271,7 @@ function create$49(context) {
1282
1271
  }
1283
1272
  } });
1284
1273
  }
1285
- //#endregion
1286
- //#region src/rules/immutability/helpers.ts
1274
+ // src/rules/immutability/helpers.ts
1287
1275
  const KNOWN_MUTATING_METHODS = /* @__PURE__ */ new Set([
1288
1276
  "add",
1289
1277
  "clear",
@@ -1313,16 +1301,15 @@ function isNodeWithin(node, ancestor) {
1313
1301
  }
1314
1302
  return false;
1315
1303
  }
1316
- function isRefLikeName$1(name) {
1304
+ function isRefLikeName$2(name) {
1317
1305
  return name === "ref" || name.endsWith("Ref");
1318
1306
  }
1319
1307
  function hasRefLikeNameInChain(node) {
1320
1308
  const expr = Extract.unwrap(node);
1321
1309
  if (!Check.isExpression(expr)) return false;
1322
- return Extract.getMemberChain(expr).some((member) => Check.isIdentifier(member) && isRefLikeName$1(member.name));
1310
+ return Extract.getMemberChain(expr).some((member) => Check.isIdentifier(member) && isRefLikeName$2(member.name));
1323
1311
  }
1324
- //#endregion
1325
- //#region src/rules/immutability/collect.ts
1312
+ // src/rules/immutability/collect.ts
1326
1313
  function createFactCollector$5() {
1327
1314
  const facts = {
1328
1315
  mutations: [],
@@ -1409,8 +1396,7 @@ function createFactCollector$5() {
1409
1396
  }
1410
1397
  };
1411
1398
  }
1412
- //#endregion
1413
- //#region src/rules/immutability/origins.ts
1399
+ // src/rules/immutability/origins.ts
1414
1400
  function resolveVariableOrigin(context, variable, mode = "alias", seen = /* @__PURE__ */ new Set()) {
1415
1401
  if (seen.has(variable)) return variable;
1416
1402
  seen.add(variable);
@@ -1522,13 +1508,12 @@ function classifyFrozenOrigin(context, variable, components, mode = "alias", see
1522
1508
  default: return null;
1523
1509
  }
1524
1510
  }
1525
- //#endregion
1526
- //#region src/rules/immutability/effects.ts
1511
+ // src/rules/immutability/effects.ts
1527
1512
  function isGlobalOrModuleVariable(variable) {
1528
1513
  return variable.defs.length === 0 || variable.scope.type === ScopeType.global || variable.scope.type === ScopeType.module;
1529
1514
  }
1530
1515
  function isRefMutation(context, mutation) {
1531
- if (Check.isIdentifier(mutation.target)) return isRefLikeName$1(mutation.target.name);
1516
+ if (Check.isIdentifier(mutation.target)) return isRefLikeName$2(mutation.target.name);
1532
1517
  return isRefLikeChain(context, mutation.target);
1533
1518
  }
1534
1519
  function isKnownNonMutatingMethodCall(context, node) {
@@ -1708,8 +1693,7 @@ function create$47(context) {
1708
1693
  });
1709
1694
  } };
1710
1695
  }
1711
- //#endregion
1712
- //#region src/rules/no-array-index-key/lib.ts
1696
+ // src/rules/no-array-index-key/lib.ts
1713
1697
  const KNOWN_COERCION_FUNCTIONS = /* @__PURE__ */ new Set(["Number", "String"]);
1714
1698
  const KNOWN_INDEX_PARAM_POSITIONS = /* @__PURE__ */ new Map([
1715
1699
  ["every", 1],
@@ -1766,7 +1750,7 @@ function create$46(context) {
1766
1750
  }
1767
1751
  function getPropsObject(node) {
1768
1752
  const props = node.arguments[1];
1769
- if (core.isCreateElementCall(context, node)) return core.getCreateElementPropsObject(context, node);
1753
+ if (core.isCreateElementCall(context, node)) return getCreateElementPropsObject(context, node);
1770
1754
  if (!core.isCloneElementCall(context, node)) return null;
1771
1755
  return props?.type === AST_NODE_TYPES.ObjectExpression ? props : null;
1772
1756
  }
@@ -1910,8 +1894,7 @@ function create$41(context) {
1910
1894
  });
1911
1895
  } };
1912
1896
  }
1913
- //#endregion
1914
- //#region ../../.pkgs/eff/dist/index.js
1897
+ // ../../.pkgs/eff/dist/index.js
1915
1898
  const pipeArguments = (self, args) => {
1916
1899
  switch (args.length) {
1917
1900
  case 0: return self;
@@ -2003,6 +1986,14 @@ function flow(ab, bc, cd, de, ef, fg, gh, hi, ij) {
2003
1986
  }
2004
1987
  }
2005
1988
  cast(absurd);
1989
+ function memoize(f) {
1990
+ const objects = /* @__PURE__ */ new WeakMap();
1991
+ const primitives = /* @__PURE__ */ new Map();
1992
+ return (a) => {
1993
+ if (isObjectKeyword(a)) return getOrInsertComputed(objects, a, () => f(a));
1994
+ return getOrInsertComputed(primitives, a, () => f(a));
1995
+ };
1996
+ }
2006
1997
  function not(predicate) {
2007
1998
  return (data) => !predicate(data);
2008
1999
  }
@@ -2443,8 +2434,7 @@ function buildFixForComponentProps(context, fixer, node, typeArguments) {
2443
2434
  "}"
2444
2435
  ].join(" ")), ...arg1 == null ? [] : [fixer.remove(arg1), fixer.removeRange([arg0.range[1], arg1.range[0]])]];
2445
2436
  }
2446
- //#endregion
2447
- //#region src/utils/create-implicit-prop-listener.ts
2437
+ // src/utils/create-implicit-prop-listener.ts
2448
2438
  function createImplicitPropListener(context, options) {
2449
2439
  const { name, isAllowedProp, isAllowedType, onImplicitProp } = options;
2450
2440
  const services = ESLintUtils.getParserServices(context, false);
@@ -2462,8 +2452,7 @@ function createImplicitPropListener(context, options) {
2462
2452
  }
2463
2453
  } };
2464
2454
  }
2465
- //#endregion
2466
- //#region src/rules/no-implicit-children/no-implicit-children.ts
2455
+ // src/rules/no-implicit-children/no-implicit-children.ts
2467
2456
  const RULE_NAME$30 = "no-implicit-children";
2468
2457
  const RE_REACT_CHILDREN_TYPE = /react\.(reactnode|reactelement|reactportal)$/i;
2469
2458
  var no_implicit_children_default = createRule({
@@ -2512,8 +2501,7 @@ function create$29(context) {
2512
2501
  }
2513
2502
  });
2514
2503
  }
2515
- //#endregion
2516
- //#region src/rules/no-implicit-ref/no-implicit-ref.ts
2504
+ // src/rules/no-implicit-ref/no-implicit-ref.ts
2517
2505
  const RULE_NAME$28 = "no-implicit-ref";
2518
2506
  const RE_REACT_REF_TYPE = /react\.(ref|legacyref|refcallback|refobject)$/i;
2519
2507
  var no_implicit_ref_default = createRule({
@@ -2690,22 +2678,21 @@ function create$25(context) {
2690
2678
  }
2691
2679
  };
2692
2680
  }
2693
- //#endregion
2694
- //#region src/rules/no-missing-key/lib.ts
2681
+ // src/rules/no-missing-key/lib.ts
2695
2682
  const KNOWN_CALLBACK_POSITIONS = /* @__PURE__ */ new Map([
2696
2683
  ["flatMap", 0],
2697
2684
  ["from", 1],
2698
2685
  ["map", 0]
2699
2686
  ]);
2700
- function getNestedReturnStatements$1(node) {
2701
- const statements = [];
2687
+ function getReturnStatements$1(node) {
2688
+ const stmts = [];
2702
2689
  const boundary = Check.isFunction(node) ? node : Traverse.findParent(node, Check.isFunction);
2703
2690
  simpleTraverse(node, { enter(node) {
2704
2691
  if (node.type !== AST_NODE_TYPES.ReturnStatement) return;
2705
2692
  if (Traverse.hasParent(node, Check.isFunction, (n) => n === boundary)) return;
2706
- statements.push(node);
2693
+ stmts.push(node);
2707
2694
  } });
2708
- return statements;
2695
+ return stmts;
2709
2696
  }
2710
2697
  var no_missing_key_default = createRule({
2711
2698
  meta: {
@@ -2755,7 +2742,7 @@ function create$24(context) {
2755
2742
  }
2756
2743
  function visitIteratorCallback(node) {
2757
2744
  if (node.body.type !== AST_NODE_TYPES.BlockStatement) return visitItemExpression(node.body);
2758
- return getNestedReturnStatements$1(node.body).flatMap((stmt) => stmt.argument == null ? [] : visitItemExpression(stmt.argument));
2745
+ return getReturnStatements$1(node.body).flatMap((stmt) => stmt.argument == null ? [] : visitItemExpression(stmt.argument));
2759
2746
  }
2760
2747
  return {
2761
2748
  ArrayExpression(node) {
@@ -2780,8 +2767,7 @@ function create$24(context) {
2780
2767
  }
2781
2768
  };
2782
2769
  }
2783
- //#endregion
2784
- //#region src/rules/no-misused-capture-owner-stack/lib.ts
2770
+ // src/rules/no-misused-capture-owner-stack/lib.ts
2785
2771
  function isProcessEnvNodeEnv(context, node) {
2786
2772
  return node != null && core.isAPI("process.env.NODE_ENV")(context, node);
2787
2773
  }
@@ -2838,8 +2824,7 @@ function create$23(context) {
2838
2824
  }
2839
2825
  };
2840
2826
  }
2841
- //#endregion
2842
- //#region src/rules/no-nested-component-definitions/lib.ts
2827
+ // src/rules/no-nested-component-definitions/lib.ts
2843
2828
  const isConnectCall = core.isAPICall("connect");
2844
2829
  const isCreateFragmentContainerCall = core.isAPICall("createFragmentContainer");
2845
2830
  const isCreatePaginationContainerCall = core.isAPICall("createPaginationContainer");
@@ -2945,7 +2930,7 @@ function create$22(context) {
2945
2930
  });
2946
2931
  continue;
2947
2932
  }
2948
- if (core.isInsideCreateElementProps(context, component)) {
2933
+ if (isInsideCreateElementProps(context, component)) {
2949
2934
  context.report({
2950
2935
  data: { suggestion: "Move it to the top level or pass it as a prop." },
2951
2936
  messageId: "default",
@@ -3085,8 +3070,7 @@ function create$18(context) {
3085
3070
  });
3086
3071
  } };
3087
3072
  }
3088
- //#endregion
3089
- //#region src/rules/no-unnecessary-use-prefix/lib.ts
3073
+ // src/rules/no-unnecessary-use-prefix/lib.ts
3090
3074
  const WELL_KNOWN_HOOKS = ["use", "useMDXComponents"];
3091
3075
  function containsUseComments(context, node) {
3092
3076
  return context.sourceCode.getCommentsInside(node).some(({ value }) => /use\([\s\S]*?\)/u.test(value) || /use[A-Z0-9]\w*\([\s\S]*?\)/u.test(value));
@@ -3200,8 +3184,7 @@ function create$14(context) {
3200
3184
  }
3201
3185
  } });
3202
3186
  }
3203
- //#endregion
3204
- //#region src/rules/no-unstable-context-value/lib.ts
3187
+ // src/rules/no-unstable-context-value/lib.ts
3205
3188
  function isContextName(name, isReact18OrBelow) {
3206
3189
  if (name === "Provider") return true;
3207
3190
  if (!isReact18OrBelow) return name.endsWith("Context") || name.endsWith("CONTEXT");
@@ -3245,7 +3228,7 @@ function create$13(context) {
3245
3228
  const valueExpression = attribute.value.expression;
3246
3229
  const construction = resolveObjectType(context, valueExpression);
3247
3230
  if (construction == null) return;
3248
- if (core.isHookCall(construction.node)) return;
3231
+ if (construction.node.type === AST_NODE_TYPES.CallExpression && core.isHookCall(construction.node)) return;
3249
3232
  getOrInsertComputed(constructions, enclosingFunction, () => []).push(construction);
3250
3233
  },
3251
3234
  "Program:exit"(program) {
@@ -3267,8 +3250,7 @@ function create$13(context) {
3267
3250
  }
3268
3251
  });
3269
3252
  }
3270
- //#endregion
3271
- //#region src/rules/no-unstable-default-props/lib.ts
3253
+ // src/rules/no-unstable-default-props/lib.ts
3272
3254
  const SEL_OBJECT_DESTRUCTURING_VARIABLE_DECLARATOR = [
3273
3255
  "VariableDeclarator",
3274
3256
  "[id.type='ObjectPattern']",
@@ -3282,8 +3264,7 @@ function getHumanReadableKind(node) {
3282
3264
  }
3283
3265
  return toLowerCase(delimiterCase(node.type, " "));
3284
3266
  }
3285
- //#endregion
3286
- //#region src/rules/no-unstable-default-props/no-unstable-default-props.ts
3267
+ // src/rules/no-unstable-default-props/no-unstable-default-props.ts
3287
3268
  const RULE_NAME$12 = "no-unstable-default-props";
3288
3269
  const defaultOptions$1 = [{ safeDefaultProps: [] }];
3289
3270
  const schema$1 = [{
@@ -3333,7 +3314,7 @@ function create$12(context, [options]) {
3333
3314
  const { right } = value;
3334
3315
  const construction = resolveObjectType(context, value);
3335
3316
  if (construction == null) continue;
3336
- if (core.isHookCall(construction.node)) continue;
3317
+ if (construction.node.type === AST_NODE_TYPES.CallExpression && core.isHookCall(construction.node)) continue;
3337
3318
  if (safePatterns.length > 0) {
3338
3319
  const getName = (n) => Check.isIdentifier(n) ? n.name : null;
3339
3320
  const name = match(right).with({ type: AST_NODE_TYPES.NewExpression }, (n) => getName(n.callee)).with({ type: AST_NODE_TYPES.CallExpression }, (n) => {
@@ -3351,8 +3332,7 @@ function create$12(context, [options]) {
3351
3332
  }
3352
3333
  });
3353
3334
  }
3354
- //#endregion
3355
- //#region src/rules/no-unused-class-component-members/lib.ts
3335
+ // src/rules/no-unused-class-component-members/lib.ts
3356
3336
  const LIFECYCLE_METHODS = /* @__PURE__ */ new Set([
3357
3337
  "componentDidCatch",
3358
3338
  "componentDidMount",
@@ -3466,8 +3446,7 @@ function create$11(context) {
3466
3446
  }
3467
3447
  };
3468
3448
  }
3469
- //#endregion
3470
- //#region src/rules/no-unused-props/lib.ts
3449
+ // src/rules/no-unused-props/lib.ts
3471
3450
  function collectUsedPropKeysOfParameter(context, usedPropKeys, parameter) {
3472
3451
  switch (parameter.type) {
3473
3452
  case AST_NODE_TYPES.Identifier: return collectUsedPropKeysOfIdentifier(context, usedPropKeys, parameter);
@@ -3662,8 +3641,7 @@ function create$8(context) {
3662
3641
  });
3663
3642
  } };
3664
3643
  }
3665
- //#endregion
3666
- //#region src/rules/purity/lib.ts
3644
+ // src/rules/purity/lib.ts
3667
3645
  const IMPURE_FUNCS = /* @__PURE__ */ new Map([
3668
3646
  ["Atomics", /* @__PURE__ */ new Set([
3669
3647
  "add",
@@ -4191,8 +4169,7 @@ function create$7(context) {
4191
4169
  }
4192
4170
  });
4193
4171
  }
4194
- //#endregion
4195
- //#region src/rules/refs/helpers.ts
4172
+ // src/rules/refs/helpers.ts
4196
4173
  const SYNC_ARRAY_CALLBACKS = /* @__PURE__ */ new Set([
4197
4174
  "every",
4198
4175
  "filter",
@@ -4211,7 +4188,7 @@ const SYNC_ARRAY_CALLBACKS = /* @__PURE__ */ new Set([
4211
4188
  function isFunctionExpressionLike(node) {
4212
4189
  return node.type === AST_NODE_TYPES.FunctionExpression || node.type === AST_NODE_TYPES.ArrowFunctionExpression;
4213
4190
  }
4214
- function isRefLikeName(name) {
4191
+ function isRefLikeName$1(name) {
4215
4192
  return name === "ref" || name.endsWith("Ref");
4216
4193
  }
4217
4194
  function getSynchronousCallbackIndexes(node) {
@@ -4356,8 +4333,7 @@ function isAfterTerminatingNonNullGuard(node, getNullBranch) {
4356
4333
  }
4357
4334
  return false;
4358
4335
  }
4359
- //#endregion
4360
- //#region src/rules/refs/collect.ts
4336
+ // src/rules/refs/collect.ts
4361
4337
  function createFactCollector$4() {
4362
4338
  const facts = {
4363
4339
  bindings: [],
@@ -4454,8 +4430,7 @@ function createFactCollector$4() {
4454
4430
  }
4455
4431
  };
4456
4432
  }
4457
- //#endregion
4458
- //#region src/rules/refs/effects.ts
4433
+ // src/rules/refs/effects.ts
4459
4434
  function findBoundary(node, boundaries) {
4460
4435
  return Traverse.findParent(node, (candidate) => Check.isFunction(candidate) && boundaries.has(candidate));
4461
4436
  }
@@ -4578,8 +4553,7 @@ function inferRefPassViolations(passSites, boundaries, reachability, resolver) {
4578
4553
  }
4579
4554
  return violations;
4580
4555
  }
4581
- //#endregion
4582
- //#region src/rules/refs/origins.ts
4556
+ // src/rules/refs/origins.ts
4583
4557
  function createBindingResolver(context, facts) {
4584
4558
  const { additionalRefHooks } = getSettingsFromContext(context);
4585
4559
  const bindings = /* @__PURE__ */ new Map();
@@ -4603,7 +4577,7 @@ function createBindingResolver(context, facts) {
4603
4577
  };
4604
4578
  if (value.type === AST_NODE_TYPES.CallExpression && (core.isUseRefLikeCall(value, additionalRefHooks) || core.isCreateRefCall(context, value))) return { kind: "ref" };
4605
4579
  if (value.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(value.property)) {
4606
- if (isRefLikeName(value.property.name)) return { kind: "ref" };
4580
+ if (isRefLikeName$1(value.property.name)) return { kind: "ref" };
4607
4581
  }
4608
4582
  return { kind: "unknown" };
4609
4583
  }
@@ -4650,7 +4624,7 @@ function createBindingResolver(context, facts) {
4650
4624
  function resolveRef(variable, position, seen = /* @__PURE__ */ new Set()) {
4651
4625
  if (seen.has(variable)) return null;
4652
4626
  seen.add(variable);
4653
- if (jsxRefs.has(variable) || isRefLikeName(variable.name)) return variable;
4627
+ if (jsxRefs.has(variable) || isRefLikeName$1(variable.name)) return variable;
4654
4628
  const event = getLatestValue(bindings.get(variable), position);
4655
4629
  if (event != null) switch (event.value.kind) {
4656
4630
  case "ref": return variable;
@@ -4707,7 +4681,7 @@ function createBindingResolver(context, facts) {
4707
4681
  return identity == null ? null : { identity };
4708
4682
  }
4709
4683
  if (object.type === AST_NODE_TYPES.MemberExpression && Check.isIdentifier(object.property)) {
4710
- if (isRefLikeName(object.property.name)) return { identity: null };
4684
+ if (isRefLikeName$1(object.property.name)) return { identity: null };
4711
4685
  }
4712
4686
  return null;
4713
4687
  }
@@ -4771,8 +4745,7 @@ function create$6(context) {
4771
4745
  });
4772
4746
  } });
4773
4747
  }
4774
- //#endregion
4775
- //#region src/rules/rules-of-hooks/lib.ts
4748
+ // src/rules/rules-of-hooks/lib.ts
4776
4749
  function assert(cond, message) {
4777
4750
  if (!cond) throw new Error(message ?? "Assertion violated.");
4778
4751
  }
@@ -6045,8 +6018,7 @@ var CodePathAnalyzer = class {
6045
6018
  if (fromSegment.reachable && toSegment.reachable) this.emitter.emit("onCodePathSegmentLoop", fromSegment, toSegment, this.currentNode);
6046
6019
  }
6047
6020
  };
6048
- //#endregion
6049
- //#region src/rules/rules-of-hooks/rules-of-hooks.ts
6021
+ // src/rules/rules-of-hooks/rules-of-hooks.ts
6050
6022
  function isHookName(s) {
6051
6023
  return s === "use" || /^use[A-Z0-9]/.test(s);
6052
6024
  }
@@ -6368,8 +6340,51 @@ function getFunctionName(node) {
6368
6340
  function last(array) {
6369
6341
  return array[array.length - 1];
6370
6342
  }
6371
- //#endregion
6372
- //#region src/rules/set-state-in-effect/helpers.ts
6343
+ // src/rules/set-state-in-effect/collect.ts
6344
+ function createFactCollector$3(additionalStateHooks, additionalEffectHooks) {
6345
+ const facts = {
6346
+ calls: [],
6347
+ effectRoots: [],
6348
+ jsxRefExprs: []
6349
+ };
6350
+ const functionEntries = [];
6351
+ const isUseStateCall = (node) => node.type === AST_NODE_TYPES.CallExpression && core.isUseStateLikeCall(node, additionalStateHooks);
6352
+ const isUseEffectCall = (node) => node.type === AST_NODE_TYPES.CallExpression && core.isUseEffectLikeCall(node, additionalEffectHooks);
6353
+ return {
6354
+ facts,
6355
+ visitor: {
6356
+ ":function"(node) {
6357
+ functionEntries.push(node);
6358
+ },
6359
+ ":function:exit"() {
6360
+ functionEntries.pop();
6361
+ },
6362
+ JSXAttribute(node) {
6363
+ if (node.name.type !== AST_NODE_TYPES.JSXIdentifier || node.name.name !== "ref") return;
6364
+ if (node.value?.type !== AST_NODE_TYPES.JSXExpressionContainer) return;
6365
+ facts.jsxRefExprs.push(node.value.expression);
6366
+ },
6367
+ CallExpression(node) {
6368
+ if (isUseStateCall(node)) return;
6369
+ if (isUseEffectCall(node)) {
6370
+ const setup = node.arguments.at(0);
6371
+ if (setup != null) facts.effectRoots.push({
6372
+ node,
6373
+ setup
6374
+ });
6375
+ return;
6376
+ }
6377
+ const enclosingFunction = functionEntries.at(-1);
6378
+ if (enclosingFunction == null) return;
6379
+ facts.calls.push({
6380
+ enclosingFunction,
6381
+ node
6382
+ });
6383
+ }
6384
+ }
6385
+ };
6386
+ }
6387
+ // src/rules/set-state-in-effect/helpers.ts
6373
6388
  const IDENTIFIER_VISITOR_KEYS = {
6374
6389
  [AST_NODE_TYPES.ArrayExpression]: ["elements"],
6375
6390
  [AST_NODE_TYPES.ArrayPattern]: ["elements"],
@@ -6406,7 +6421,7 @@ const IDENTIFIER_VISITOR_KEYS = {
6406
6421
  [AST_NODE_TYPES.UpdateExpression]: ["argument"],
6407
6422
  [AST_NODE_TYPES.YieldExpression]: ["argument"]
6408
6423
  };
6409
- function getNestedIdentifiers(node) {
6424
+ function getIdentifiers(node) {
6410
6425
  const identifiers = [];
6411
6426
  simpleTraverse(node, {
6412
6427
  enter(node, parent) {
@@ -6418,263 +6433,85 @@ function getNestedIdentifiers(node) {
6418
6433
  });
6419
6434
  return identifiers;
6420
6435
  }
6421
- function isHookDecl(node) {
6422
- if (node.type !== AST_NODE_TYPES.VariableDeclarator) return false;
6423
- if (!Check.isIdentifier(node.id)) return false;
6424
- const init = node.init;
6425
- if (init == null) return false;
6426
- if (init.type !== AST_NODE_TYPES.CallExpression) return false;
6427
- const name = Extract.getCalleeName(init);
6428
- return name != null && core.isHookName(name);
6429
- }
6430
- function isThenCall(node) {
6431
- return Extract.unwrap(node.callee).type === AST_NODE_TYPES.MemberExpression && Extract.getCalleeName(node) === "then";
6432
- }
6433
6436
  function isTerminatingStatement(node) {
6434
6437
  if (node.type === AST_NODE_TYPES.ReturnStatement || node.type === AST_NODE_TYPES.ThrowStatement) return true;
6435
6438
  if (node.type === AST_NODE_TYPES.BlockStatement) return node.body.some(isTerminatingStatement);
6436
6439
  return false;
6437
6440
  }
6438
- function getCallName(node, getText) {
6439
- if (node.type === AST_NODE_TYPES.CallExpression) return Extract.getFullyQualifiedName(node.callee, getText);
6440
- return Extract.getFullyQualifiedName(node, getText);
6441
- }
6442
- function getSetStateCallExpression(node) {
6443
- return Check.isIdentifier(node) && node.parent.type === AST_NODE_TYPES.CallExpression && node.parent.callee === node ? node.parent : node;
6444
- }
6445
- //#endregion
6446
- //#region src/rules/set-state-in-effect/collect.ts
6447
- function createFactCollector$3(additionalStateHooks, additionalEffectHooks) {
6448
- const facts = {
6449
- calls: [],
6450
- setStateReferences: [],
6451
- setupIdentifiers: []
6452
- };
6453
- const functionEntries = [];
6454
- const setupFnRef = { current: null };
6455
- function isUseStateCall(node) {
6456
- return core.isUseStateLikeCall(node, additionalStateHooks);
6457
- }
6458
- function isUseEffectCall(node) {
6459
- return core.isUseEffectLikeCall(node, additionalEffectHooks);
6460
- }
6461
- function isUseEffectSetupCallback(node) {
6462
- return node.parent?.type === AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseEffectCall(node.parent);
6463
- }
6464
- function getFunctionKind(node) {
6465
- const parent = Traverse.findParent(node, not(Check.isTypeExpression)) ?? node.parent;
6466
- if (node.async) return "deferred";
6467
- if (parent.type === AST_NODE_TYPES.CallExpression && isThenCall(parent)) return "deferred";
6468
- if (node.type !== AST_NODE_TYPES.FunctionDeclaration && parent.type === AST_NODE_TYPES.CallExpression && parent.callee === node) return "immediate";
6469
- if (isUseEffectSetupCallback(node)) return "setup";
6470
- return "other";
6471
- }
6472
- return {
6473
- facts,
6474
- visitor: {
6475
- ":function"(node) {
6476
- const kind = getFunctionKind(node);
6477
- functionEntries.push({
6478
- kind,
6479
- node
6480
- });
6481
- if (kind === "setup") setupFnRef.current = node;
6482
- },
6483
- ":function:exit"(node) {
6484
- const { kind } = functionEntries.at(-1) ?? {};
6485
- if (kind === "setup" && setupFnRef.current === node) setupFnRef.current = null;
6486
- functionEntries.pop();
6487
- },
6488
- CallExpression(node) {
6489
- const entry = functionEntries.at(-1);
6490
- if (entry == null) return;
6491
- if (entry.node.async) return;
6492
- if (isUseStateCall(node)) return;
6493
- if (isUseEffectCall(node)) {
6494
- const setup = node.arguments.at(0);
6495
- if (setup != null && !Check.isFunction(setup)) facts.setupIdentifiers.push(...getNestedIdentifiers(setup));
6496
- return;
6497
- }
6498
- facts.calls.push({
6499
- enclosingFunction: entry.node,
6500
- enclosingFunctionKind: entry.kind,
6501
- node,
6502
- setupFunction: setupFnRef.current
6503
- });
6504
- },
6505
- Identifier(node) {
6506
- if (node.parent.type === AST_NODE_TYPES.CallExpression && node.parent.callee === node) return;
6507
- if (node.parent.type === AST_NODE_TYPES.ArrowFunctionExpression && node === node.parent.body) {
6508
- facts.setStateReferences.push({
6509
- kind: "callback-body",
6510
- node
6511
- });
6512
- return;
6513
- }
6514
- if (node.parent.type === AST_NODE_TYPES.CallExpression && node === node.parent.arguments.at(0)) facts.setStateReferences.push({
6515
- kind: "call-argument",
6516
- node
6517
- });
6518
- }
6519
- }
6520
- };
6521
- }
6522
- //#endregion
6523
- //#region src/rules/set-state-in-effect/origins.ts
6524
- function createSetStateResolver$1(context) {
6525
- const { additionalStateHooks } = getSettingsFromContext(context);
6526
- function isUseStateCall(node) {
6527
- return core.isUseStateLikeCall(node, additionalStateHooks);
6528
- }
6529
- function isSetStateId(id, at) {
6530
- const initNode = resolveOrigin(context, id);
6531
- if (initNode == null) return false;
6532
- if (initNode.type !== AST_NODE_TYPES.CallExpression) return false;
6533
- if (!isUseStateCall(initNode)) return false;
6534
- const variableNodeParent = initNode.parent;
6535
- if (!hasProperty(variableNodeParent, "id")) return true;
6536
- 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));
6537
- if (variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return true;
6538
- return variableNodeParent.id.elements.findIndex((e) => e != null && Check.isIdentifier(e, id.name)) === at;
6539
- }
6540
- function isSetStateCall(node) {
6541
- const callee = Extract.unwrap(node.callee);
6542
- switch (callee.type) {
6543
- case AST_NODE_TYPES.CallExpression: {
6544
- const innerCallee = Extract.unwrap(callee.callee);
6545
- if (innerCallee.type !== AST_NODE_TYPES.MemberExpression) return false;
6546
- if (!hasProperty(innerCallee.object, "name")) return false;
6547
- if (!(Extract.getCalleeName(callee) === "at")) return false;
6548
- const [index] = callee.arguments;
6549
- if (index == null) return false;
6550
- const indexScope = context.sourceCode.getScope(node);
6551
- return getStaticValue(index, indexScope)?.value === 1 && isSetStateId(innerCallee.object);
6552
- }
6553
- case AST_NODE_TYPES.Identifier: return isSetStateId(callee, 1);
6554
- case AST_NODE_TYPES.MemberExpression: {
6555
- if (!hasProperty(callee.object, "name")) return false;
6556
- const property = callee.property;
6557
- const propertyScope = context.sourceCode.getScope(node);
6558
- return getStaticValue(property, propertyScope)?.value === 1 && isSetStateId(callee.object, 1);
6559
- }
6560
- default: return false;
6561
- }
6562
- }
6563
- return {
6564
- isSetStateCall,
6565
- isSetStateId,
6566
- isUseStateCall
6567
- };
6568
- }
6569
- function isInitializedFromRef(context, id, refParams, seen = /* @__PURE__ */ new Set()) {
6570
- const name = id.name;
6571
- if (seen.has(name)) return false;
6572
- seen.add(name);
6573
- const variable = findVariable(context.sourceCode.getScope(id), name);
6574
- for (const def of variable?.defs ?? []) {
6575
- if (def.type === DefinitionType.Parameter) {
6576
- if (refParams?.has(def.name) === true) return true;
6577
- if (def.name.type === AST_NODE_TYPES.Identifier && (def.name.name === "ref" || def.name.name.endsWith("Ref"))) return true;
6578
- continue;
6579
- }
6580
- const { node } = def;
6581
- if (node.type !== AST_NODE_TYPES.VariableDeclarator) continue;
6582
- const init = node.init == null ? null : Extract.unwrap(node.init);
6583
- if (init == null) continue;
6584
- if (isExpressionFromRef(context, init, refParams, seen)) return true;
6585
- }
6586
- for (const reference of variable?.references ?? []) {
6587
- if (!reference.isWrite()) continue;
6588
- const writeExpr = reference.writeExpr == null ? null : Extract.unwrap(reference.writeExpr);
6589
- if (writeExpr == null) continue;
6590
- if (writeExpr.range[1] > id.range[0]) continue;
6591
- if (Traverse.hasParent(writeExpr, and(Check.isFunction, (n) => !Traverse.hasParent(id, (m) => m === n)))) continue;
6592
- if (isExpressionFromRef(context, writeExpr, refParams, seen)) return true;
6593
- }
6594
- return false;
6595
- }
6596
- function isExpressionFromRef(context, init, refParams, seen) {
6597
- const { additionalRefHooks } = getSettingsFromContext(context);
6598
- if (init.type === AST_NODE_TYPES.CallExpression && core.isUseRefLikeCall(init, additionalRefHooks)) return true;
6599
- if (init.type === AST_NODE_TYPES.MemberExpression) {
6600
- let current = init;
6601
- while (current.type === AST_NODE_TYPES.MemberExpression) {
6602
- const property = Extract.unwrap(current.property);
6603
- const object = Extract.unwrap(current.object);
6604
- if (!current.computed && Check.isIdentifier(property, "current")) {
6605
- const sourceName = getRefSourceName(object);
6606
- if (sourceName === "ref" || sourceName?.endsWith("Ref") === true) return true;
6607
- if (object.type === AST_NODE_TYPES.Identifier && isParameterId(context, object)) return true;
6608
- }
6609
- current = object;
6610
- }
6611
- if (Check.isIdentifier(current) && (current.name === "ref" || current.name.endsWith("Ref"))) return true;
6612
- return getNestedIdentifiers(init).some((id) => isInitializedFromRef(context, id, refParams, seen));
6613
- }
6614
- return getNestedIdentifiers(init).some((id) => isInitializedFromRef(context, id, refParams, seen));
6615
- }
6616
- function isArgumentUsingRefValue(context, node, refParams) {
6617
- const isUsingRefValue = (n) => {
6618
- switch (n.type) {
6619
- case AST_NODE_TYPES.Identifier: return isInitializedFromRef(context, n, refParams);
6620
- case AST_NODE_TYPES.MemberExpression: {
6621
- const property = Extract.unwrap(n.property);
6622
- if (!n.computed && Check.isIdentifier(property, "current") && n.object.type === AST_NODE_TYPES.Identifier && isParameterId(context, n.object)) return true;
6623
- return isUsingRefValue(n.object);
6624
- }
6625
- case AST_NODE_TYPES.CallExpression: return isUsingRefValue(n.callee) || getNestedIdentifiers(n).some(isUsingRefValue);
6626
- case AST_NODE_TYPES.ChainExpression: return isUsingRefValue(n.expression);
6627
- case AST_NODE_TYPES.BinaryExpression:
6628
- case AST_NODE_TYPES.LogicalExpression: return isUsingRefValue(n.left) || isUsingRefValue(n.right);
6629
- case AST_NODE_TYPES.UnaryExpression:
6630
- case AST_NODE_TYPES.UpdateExpression: return isUsingRefValue(n.argument);
6631
- case AST_NODE_TYPES.ConditionalExpression: return isUsingRefValue(n.consequent) || isUsingRefValue(n.alternate);
6632
- case AST_NODE_TYPES.SequenceExpression: return n.expressions.some(isUsingRefValue);
6633
- case AST_NODE_TYPES.TemplateLiteral: return n.expressions.some(isUsingRefValue);
6634
- case AST_NODE_TYPES.TaggedTemplateExpression: return isUsingRefValue(n.tag) || isUsingRefValue(n.quasi);
6635
- case AST_NODE_TYPES.ObjectExpression: return n.properties.some(isUsingRefValue);
6636
- case AST_NODE_TYPES.Property: return isUsingRefValue(n.value);
6637
- case AST_NODE_TYPES.ArrayExpression: return n.elements.some((element) => element != null && isUsingRefValue(element));
6638
- case AST_NODE_TYPES.SpreadElement: return isUsingRefValue(n.argument);
6639
- case AST_NODE_TYPES.AssignmentExpression: return isUsingRefValue(n.right);
6640
- default: return false;
6441
+ function getReturnExpressions(fn) {
6442
+ if (fn.type === AST_NODE_TYPES.ArrowFunctionExpression && fn.body.type !== AST_NODE_TYPES.BlockStatement) return [fn.body];
6443
+ const returns = [];
6444
+ const visitStatements = (statements) => {
6445
+ for (const statement of statements) switch (statement.type) {
6446
+ case AST_NODE_TYPES.ReturnStatement:
6447
+ if (statement.argument != null) returns.push(statement.argument);
6448
+ break;
6449
+ case AST_NODE_TYPES.BlockStatement:
6450
+ visitStatements(statement.body);
6451
+ break;
6452
+ case AST_NODE_TYPES.IfStatement:
6453
+ visitStatements([statement.consequent]);
6454
+ if (statement.alternate != null) visitStatements([statement.alternate]);
6455
+ break;
6456
+ case AST_NODE_TYPES.SwitchStatement:
6457
+ for (const switchCase of statement.cases) visitStatements(switchCase.consequent);
6458
+ break;
6459
+ case AST_NODE_TYPES.TryStatement:
6460
+ visitStatements([statement.block]);
6461
+ if (statement.handler != null) visitStatements([statement.handler.body]);
6462
+ if (statement.finalizer != null) visitStatements([statement.finalizer]);
6463
+ break;
6464
+ case AST_NODE_TYPES.DoWhileStatement:
6465
+ case AST_NODE_TYPES.ForInStatement:
6466
+ case AST_NODE_TYPES.ForOfStatement:
6467
+ case AST_NODE_TYPES.ForStatement:
6468
+ case AST_NODE_TYPES.LabeledStatement:
6469
+ case AST_NODE_TYPES.WhileStatement:
6470
+ case AST_NODE_TYPES.WithStatement: visitStatements([statement.body]);
6641
6471
  }
6642
6472
  };
6643
- if (isUsingRefValue(node)) return true;
6644
- return Check.isFunction(node) && context.sourceCode.getScope(node.body).references.some((r) => isUsingRefValue(r.identifier));
6473
+ if (fn.body.type !== AST_NODE_TYPES.BlockStatement) return [];
6474
+ visitStatements(fn.body.body);
6475
+ return returns;
6645
6476
  }
6646
- function isRefGatedContext(context, node, refParams) {
6477
+ function getConditionalBranch(node) {
6647
6478
  let child = node;
6648
6479
  let current = node.parent;
6649
- while (current != null) {
6650
- if (Check.isFunction(current)) break;
6651
- if (current.type === AST_NODE_TYPES.IfStatement) {
6652
- if (isRefInExpression(context, current.test, refParams)) return true;
6653
- }
6654
- if (current.type === AST_NODE_TYPES.ConditionalExpression) {
6655
- if (isRefInExpression(context, current.test, refParams)) return true;
6656
- }
6657
- if (current.type === AST_NODE_TYPES.LogicalExpression && child === current.right) {
6658
- if (isRefInExpression(context, current.left, refParams)) return true;
6659
- }
6660
- if (current.type === AST_NODE_TYPES.BlockStatement) {
6661
- const index = current.body.findIndex((statement) => statement === child);
6662
- for (const statement of current.body.slice(0, Math.max(0, index))) {
6663
- if (statement.type !== AST_NODE_TYPES.IfStatement) continue;
6664
- if (!isRefInExpression(context, statement.test, refParams)) continue;
6665
- if (isTerminatingStatement(statement.consequent)) return true;
6666
- }
6480
+ while (current != null && !Check.isFunction(current)) {
6481
+ if (Check.isLoopStatement(current) && child === current.body) return child;
6482
+ switch (current.type) {
6483
+ case AST_NODE_TYPES.IfStatement:
6484
+ case AST_NODE_TYPES.ConditionalExpression:
6485
+ if (child === current.consequent || child === current.alternate) return child;
6486
+ break;
6487
+ case AST_NODE_TYPES.LogicalExpression:
6488
+ if (child === current.right) return child;
6489
+ break;
6490
+ case AST_NODE_TYPES.SwitchCase: return current;
6667
6491
  }
6668
6492
  child = current;
6669
6493
  current = current.parent;
6670
6494
  }
6671
- return false;
6495
+ return null;
6672
6496
  }
6673
- function isRefInExpression(context, node, refParams) {
6674
- return getNestedIdentifiers(node).some((id) => isInitializedFromRef(context, id, refParams));
6497
+ function uniqueTargets(targets) {
6498
+ const seen = /* @__PURE__ */ new Set();
6499
+ return targets.filter((target) => {
6500
+ const key = `${target.kind}:${String(target.node.range[0])}`;
6501
+ if (seen.has(key)) return false;
6502
+ seen.add(key);
6503
+ return true;
6504
+ });
6675
6505
  }
6676
- function isParameterId(context, id) {
6677
- return findVariable(context.sourceCode.getScope(id), id)?.defs.some((def) => def.type === DefinitionType.Parameter) ?? false;
6506
+ function isRefLikeName(name) {
6507
+ return name === "ref" || name.endsWith("Ref");
6508
+ }
6509
+ function getParameterId(param) {
6510
+ switch (param.type) {
6511
+ case AST_NODE_TYPES.Identifier: return param;
6512
+ case AST_NODE_TYPES.AssignmentPattern: return param.left.type === AST_NODE_TYPES.Identifier ? param.left : null;
6513
+ default: return null;
6514
+ }
6678
6515
  }
6679
6516
  function getRefSourceName(node) {
6680
6517
  switch (node.type) {
@@ -6686,132 +6523,329 @@ function getRefSourceName(node) {
6686
6523
  default: return null;
6687
6524
  }
6688
6525
  }
6689
- //#endregion
6690
- //#region src/rules/set-state-in-effect/effects.ts
6691
- function inferViolations$2(context, facts) {
6692
- const { additionalEffectHooks } = getSettingsFromContext(context);
6693
- const resolver = createSetStateResolver$1(context);
6694
- const isUseEffectCall = (node) => core.isUseEffectLikeCall(node, additionalEffectHooks);
6695
- const isUseStateOrThenCall = or(resolver.isUseStateCall, isThenCall);
6696
- const setStateCallsByFn = /* @__PURE__ */ new Map();
6697
- const setStateInHookCallbacks = /* @__PURE__ */ new Map();
6698
- const setStateInEffectArg = /* @__PURE__ */ new Map();
6699
- const setStateInEffectSetup = /* @__PURE__ */ new Map();
6700
- const trackedFnCalls = [];
6701
- const violations = [];
6702
- for (const { enclosingFunction, enclosingFunctionKind, node, setupFunction } of facts.calls) {
6703
- if (isUseStateOrThenCall(node)) continue;
6704
- if (!resolver.isSetStateCall(node)) {
6705
- if (setupFunction == null) continue;
6706
- if (enclosingFunction !== setupFunction) continue;
6707
- trackedFnCalls.push(node);
6708
- continue;
6526
+ // src/rules/set-state-in-effect/origins.ts
6527
+ const EMPTY_FACTS = {
6528
+ calls: [],
6529
+ effectRoots: [],
6530
+ jsxRefExprs: []
6531
+ };
6532
+ function createSetStateResolver$1(context, facts = EMPTY_FACTS) {
6533
+ const { additionalRefHooks, additionalStateHooks } = getSettingsFromContext(context);
6534
+ const getReturnExpressions$1 = memoize(getReturnExpressions);
6535
+ function isUseRefCall(node) {
6536
+ return node.type === AST_NODE_TYPES.CallExpression && core.isUseRefLikeCall(node, additionalRefHooks);
6537
+ }
6538
+ function isUseStateCall(node) {
6539
+ return node.type === AST_NODE_TYPES.CallExpression && core.isUseStateLikeCall(node, additionalStateHooks);
6540
+ }
6541
+ function getStateSetterVariable(definition) {
6542
+ if (definition.type !== DefinitionType.Variable || definition.node.init == null) return null;
6543
+ const pattern = definition.node.id;
6544
+ if (pattern.type !== AST_NODE_TYPES.ArrayPattern || pattern.elements[0] !== definition.name) return null;
6545
+ const init = Extract.unwrap(definition.node.init);
6546
+ if (init.type !== AST_NODE_TYPES.CallExpression || !isUseStateCall(init)) return null;
6547
+ const setter = pattern.elements[1];
6548
+ if (setter?.type !== AST_NODE_TYPES.Identifier) return null;
6549
+ return findVariable(context.sourceCode.getScope(setter), setter) ?? null;
6550
+ }
6551
+ function isJsxRefTargetForSetter(target, setterVariable) {
6552
+ if (target.kind === "setter") {
6553
+ if (!Check.isIdentifier(target.node)) return false;
6554
+ return findVariable(context.sourceCode.getScope(target.node), target.node) === setterVariable;
6555
+ }
6556
+ if (target.node.async) return false;
6557
+ return setterVariable.references.some((r) => Traverse.hasParent(r.identifier, (n) => n === target.node));
6558
+ }
6559
+ const isStateHeldDomNode = memoize((variable) => variable.defs.some((definition) => {
6560
+ const setterVariable = getStateSetterVariable(definition);
6561
+ if (setterVariable == null) return false;
6562
+ return facts.jsxRefExprs.some((e) => resolveValues(e).some((t) => isJsxRefTargetForSetter(t, setterVariable)));
6563
+ }));
6564
+ function isSetStateId(id) {
6565
+ const initNode = resolveOrigin(context, id);
6566
+ if (initNode?.type !== AST_NODE_TYPES.CallExpression || !isUseStateCall(initNode)) return false;
6567
+ const initNodeParent = initNode.parent;
6568
+ if (!hasProperty(initNodeParent, "id")) return false;
6569
+ if (initNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return false;
6570
+ return initNodeParent.id.elements.findIndex((e) => e != null && Check.isIdentifier(e, id.name)) === 1;
6571
+ }
6572
+ function isSetStateExpression(node) {
6573
+ const expression = Extract.unwrap(node);
6574
+ return Check.isIdentifier(expression) && isSetStateId(expression);
6575
+ }
6576
+ function isSetStateCall(node) {
6577
+ return isSetStateExpression(node.callee);
6578
+ }
6579
+ function resolveValues(node, seen = /* @__PURE__ */ new Set()) {
6580
+ const expression = Extract.unwrap(node);
6581
+ if (isSetStateExpression(expression)) return [{
6582
+ kind: "setter",
6583
+ node: expression
6584
+ }];
6585
+ if (Check.isFunction(expression)) return [{
6586
+ kind: "function",
6587
+ node: expression
6588
+ }];
6589
+ switch (expression.type) {
6590
+ case AST_NODE_TYPES.Identifier: return resolveIdentifierValues(expression, seen);
6591
+ case AST_NODE_TYPES.ConditionalExpression: return uniqueTargets([...resolveValues(expression.consequent, new Set(seen)), ...resolveValues(expression.alternate, new Set(seen))]);
6592
+ case AST_NODE_TYPES.CallExpression: return resolveCallValues(expression, seen);
6593
+ default: return [];
6709
6594
  }
6710
- if (enclosingFunctionKind === "deferred") continue;
6711
- const syncEnclosingFunction = enclosingFunctionKind === "immediate" ? Traverse.findParent(enclosingFunction, Check.isFunction) : enclosingFunction;
6712
- if (setupFunction != null && syncEnclosingFunction === setupFunction) {
6713
- const args0 = node.arguments.at(0);
6714
- if (args0 == null) continue;
6715
- if (isArgumentUsingRefValue(context, args0)) continue;
6716
- if (isRefGatedContext(context, node)) continue;
6717
- violations.push({
6718
- name: getText(context, Extract.unwrap(node.callee)),
6719
- node
6595
+ }
6596
+ function resolveIdentifierValues(id, seen) {
6597
+ const variable = findVariable(context.sourceCode.getScope(id), id);
6598
+ if (variable == null || seen.has(variable)) return [];
6599
+ const definition = variable.defs.length === 1 ? variable.defs[0] : null;
6600
+ if (definition == null || definition.type === DefinitionType.Parameter || definition.type === DefinitionType.ImportBinding) return [];
6601
+ if (definition.type === DefinitionType.FunctionName) return Check.isFunction(definition.node) ? [{
6602
+ kind: "function",
6603
+ node: definition.node
6604
+ }] : [];
6605
+ if (definition.type !== DefinitionType.Variable) return [];
6606
+ if (definition.node.id !== definition.name || definition.node.init == null) return [];
6607
+ if (definition.node.parent.kind !== "const") return [];
6608
+ seen.add(variable);
6609
+ return resolveValues(definition.node.init, seen);
6610
+ }
6611
+ function resolveCallValues(call, seen) {
6612
+ const [argument] = call.arguments;
6613
+ if (argument == null || argument.type === AST_NODE_TYPES.SpreadElement) return [];
6614
+ if (core.isUseCallbackCall(context, call) || Extract.getCalleeName(call) === "useEffectEvent") return resolveValues(argument, seen);
6615
+ if (!core.isUseMemoCall(context, call)) return [];
6616
+ const factories = resolveValues(argument, seen);
6617
+ const targets = [];
6618
+ for (const factory of factories) {
6619
+ if (factory.kind !== "function" || factory.node.async) continue;
6620
+ for (const returnExpression of getReturnExpressions$1(factory.node)) targets.push(...resolveValues(returnExpression, new Set(seen)));
6621
+ }
6622
+ return uniqueTargets(targets);
6623
+ }
6624
+ function getSetterName(node) {
6625
+ return Extract.getFullyQualifiedName(node, getText(context));
6626
+ }
6627
+ function getRefDerivedParams(call, fn, currentRefParams = /* @__PURE__ */ new Set()) {
6628
+ const refParams = new Set(currentRefParams);
6629
+ const parameterIds = fn.params.map(getParameterId);
6630
+ for (const id of parameterIds) if (id != null) refParams.delete(id);
6631
+ for (const [index, id] of parameterIds.entries()) {
6632
+ if (id == null) continue;
6633
+ const argument = call.arguments.at(index);
6634
+ if (argument == null || argument.type === AST_NODE_TYPES.SpreadElement) continue;
6635
+ if (isArgumentUsingRefValue(argument, currentRefParams)) refParams.add(id);
6636
+ }
6637
+ return refParams;
6638
+ }
6639
+ function isInitializedFromRef(id, refParams, seen = /* @__PURE__ */ new Set()) {
6640
+ const variable = findVariable(context.sourceCode.getScope(id), id);
6641
+ if (variable == null) return false;
6642
+ const key = `${String(variable.identifiers.at(0)?.range[0] ?? variable.name)}:${String(id.range[0])}`;
6643
+ if (seen.has(key)) return false;
6644
+ seen.add(key);
6645
+ if (isStateHeldDomNode(variable)) return true;
6646
+ const candidates = [];
6647
+ const addCandidate = (value, previous) => {
6648
+ if (value.range[1] > id.range[0] || candidates.some((c) => c.value === value)) return;
6649
+ candidates.push({
6650
+ branch: getConditionalBranch(value),
6651
+ position: value.range[1],
6652
+ previous,
6653
+ value
6720
6654
  });
6721
- continue;
6655
+ };
6656
+ for (const definition of variable.defs) {
6657
+ if (definition.type !== DefinitionType.Variable || definition.node.init == null) continue;
6658
+ addCandidate(definition.node.init);
6659
+ }
6660
+ for (const reference of variable.references) {
6661
+ if (!reference.isWrite() || reference.writeExpr == null) continue;
6662
+ if (reference.init === true && variable.defs.some((d) => d.type === DefinitionType.Parameter && refParams?.has(d.name) === true)) continue;
6663
+ const writeExpr = reference.writeExpr;
6664
+ if (Traverse.hasParent(writeExpr, and(Check.isFunction, (fn) => !Traverse.hasParent(id, (n) => n === fn)))) continue;
6665
+ const parent = reference.identifier.parent;
6666
+ addCandidate(writeExpr, parent.type === AST_NODE_TYPES.AssignmentExpression && parent.operator !== "=" && Extract.unwrap(parent.left) === reference.identifier ? reference.identifier : void 0);
6667
+ }
6668
+ const survivingCandidates = [];
6669
+ for (const candidate of [...candidates].sort((a, b) => a.position - b.position)) {
6670
+ if (candidate.branch == null) survivingCandidates.length = 0;
6671
+ else {
6672
+ const previousIndex = survivingCandidates.findIndex((o) => o.branch === candidate.branch);
6673
+ if (previousIndex >= 0) survivingCandidates.splice(previousIndex, 1);
6674
+ }
6675
+ survivingCandidates.push(candidate);
6722
6676
  }
6723
- const init = Traverse.findParent(node, isHookDecl)?.init;
6724
- if (init == null) getOrInsertComputed(setStateCallsByFn, enclosingFunction, () => []).push(node);
6725
- else getOrInsertComputed(setStateInHookCallbacks, init, () => []).push(node);
6726
- }
6727
- for (const { kind, node } of facts.setStateReferences) {
6728
- if (!resolver.isSetStateId(node, 1)) continue;
6729
- if (kind === "callback-body") {
6730
- const parent = node.parent.parent;
6731
- if (parent?.type !== AST_NODE_TYPES.CallExpression) continue;
6732
- if (!core.isUseMemoCall(context, parent)) continue;
6733
- const init = Traverse.findParent(parent, isHookDecl)?.init;
6734
- if (init != null) getOrInsertComputed(setStateInEffectArg, init, () => []).push(node);
6735
- continue;
6677
+ if (survivingCandidates.length > 0) return survivingCandidates.some((c) => c.previous != null && isInitializedFromRef(c.previous, refParams, seen) || isRefValue(c.value, refParams, seen, "initializer"));
6678
+ return variable.defs.some((d) => d.type === DefinitionType.Parameter && (refParams?.has(d.name) === true || d.name.type === AST_NODE_TYPES.Identifier && isRefLikeName(d.name.name)));
6679
+ }
6680
+ function isRefValue(node, refParams, seen, mode) {
6681
+ const expression = Extract.unwrap(node);
6682
+ if (isUseRefCall(expression)) return true;
6683
+ switch (expression.type) {
6684
+ case AST_NODE_TYPES.Identifier: return isInitializedFromRef(expression, refParams, seen);
6685
+ case AST_NODE_TYPES.MemberExpression: {
6686
+ const property = Extract.unwrap(expression.property);
6687
+ const object = Extract.unwrap(expression.object);
6688
+ if (!expression.computed && Check.isIdentifier(property, "current") && object.type === AST_NODE_TYPES.Identifier && isParameterId(object)) return true;
6689
+ if (mode === "initializer") {
6690
+ let current = expression;
6691
+ while (current.type === AST_NODE_TYPES.MemberExpression) {
6692
+ const currentProperty = Extract.unwrap(current.property);
6693
+ const currentObject = Extract.unwrap(current.object);
6694
+ if (!current.computed && Check.isIdentifier(currentProperty, "current")) {
6695
+ const sourceName = getRefSourceName(currentObject);
6696
+ if (sourceName != null && isRefLikeName(sourceName)) return true;
6697
+ if (currentObject.type === AST_NODE_TYPES.Identifier && isParameterId(currentObject)) return true;
6698
+ }
6699
+ current = currentObject;
6700
+ }
6701
+ if (Check.isIdentifier(current) && isRefLikeName(current.name)) return true;
6702
+ }
6703
+ return isRefValue(expression.object, refParams, seen, mode) || expression.computed && isRefValue(expression.property, refParams, seen, mode);
6704
+ }
6705
+ case AST_NODE_TYPES.CallExpression:
6706
+ for (const target of resolveValues(expression.callee)) {
6707
+ if (target.kind !== "function" || target.node.async) continue;
6708
+ const key = `getter:${String(target.node.range[0])}`;
6709
+ if (seen.has(key)) continue;
6710
+ seen.add(key);
6711
+ const childRefParams = getRefDerivedParams(expression, target.node, refParams);
6712
+ if (getReturnExpressions$1(target.node).some((r) => isRefValue(r, childRefParams, seen, mode))) return true;
6713
+ }
6714
+ return isRefValue(expression.callee, refParams, seen, mode) || expression.arguments.some((a) => isRefValue(a, refParams, seen, mode));
6715
+ case AST_NODE_TYPES.BinaryExpression:
6716
+ case AST_NODE_TYPES.LogicalExpression: return isRefValue(expression.left, refParams, seen, mode) || isRefValue(expression.right, refParams, seen, mode);
6717
+ case AST_NODE_TYPES.UnaryExpression:
6718
+ case AST_NODE_TYPES.UpdateExpression: return isRefValue(expression.argument, refParams, seen, mode);
6719
+ case AST_NODE_TYPES.ConditionalExpression: return mode === "initializer" && isRefValue(expression.test, refParams, seen, mode) || isRefValue(expression.consequent, refParams, seen, mode) || isRefValue(expression.alternate, refParams, seen, mode);
6720
+ case AST_NODE_TYPES.SequenceExpression: return expression.expressions.some((p) => isRefValue(p, refParams, seen, mode));
6721
+ case AST_NODE_TYPES.TemplateLiteral: return expression.expressions.some((p) => isRefValue(p, refParams, seen, mode));
6722
+ case AST_NODE_TYPES.TaggedTemplateExpression: return isRefValue(expression.tag, refParams, seen, mode) || isRefValue(expression.quasi, refParams, seen, mode);
6723
+ case AST_NODE_TYPES.ObjectExpression: return expression.properties.some((p) => isRefValue(p, refParams, seen, mode));
6724
+ case AST_NODE_TYPES.Property: return isRefValue(expression.value, refParams, seen, mode);
6725
+ case AST_NODE_TYPES.ArrayExpression: return expression.elements.some((e) => e != null && isRefValue(e, refParams, seen, mode));
6726
+ case AST_NODE_TYPES.SpreadElement:
6727
+ case AST_NODE_TYPES.AssignmentExpression:
6728
+ case AST_NODE_TYPES.AwaitExpression: return isRefValue(expression.type === AST_NODE_TYPES.AssignmentExpression ? expression.right : expression.argument, refParams, seen, mode);
6729
+ default: return false;
6736
6730
  }
6737
- const parent = node.parent;
6738
- if (parent.type !== AST_NODE_TYPES.CallExpression) continue;
6739
- if (core.isUseCallbackCall(context, parent)) {
6740
- const init = Traverse.findParent(parent, isHookDecl)?.init;
6741
- if (init != null) getOrInsertComputed(setStateInEffectArg, init, () => []).push(node);
6742
- continue;
6731
+ }
6732
+ function isArgumentUsingRefValue(node, refParams) {
6733
+ const seen = /* @__PURE__ */ new Set();
6734
+ if (isRefValue(node, refParams, seen, "argument")) return true;
6735
+ const expression = Extract.unwrap(node);
6736
+ if (!Check.isFunction(expression)) return false;
6737
+ return context.sourceCode.getScope(expression.body).references.some((r) => isRefValue(r.identifier, refParams, seen, "argument"));
6738
+ }
6739
+ function isRefInExpression(node, refParams) {
6740
+ return getIdentifiers(node).some((id) => isInitializedFromRef(id, refParams));
6741
+ }
6742
+ function isRefGatedContext(node, refParams) {
6743
+ let child = node;
6744
+ let current = node.parent;
6745
+ while (current != null) {
6746
+ if (Check.isFunction(current)) break;
6747
+ switch (current.type) {
6748
+ case AST_NODE_TYPES.IfStatement:
6749
+ case AST_NODE_TYPES.ConditionalExpression:
6750
+ if ((child === current.consequent || child === current.alternate) && isRefInExpression(current.test, refParams)) return true;
6751
+ break;
6752
+ case AST_NODE_TYPES.LogicalExpression:
6753
+ if (child === current.right && isRefInExpression(current.left, refParams)) return true;
6754
+ break;
6755
+ case AST_NODE_TYPES.BlockStatement: {
6756
+ const index = current.body.findIndex((s) => s === child);
6757
+ for (const statement of current.body.slice(0, Math.max(0, index))) {
6758
+ if (statement.type !== AST_NODE_TYPES.IfStatement) continue;
6759
+ if (!isRefInExpression(statement.test, refParams)) continue;
6760
+ if (isTerminatingStatement(statement.consequent)) return true;
6761
+ }
6762
+ break;
6763
+ }
6764
+ }
6765
+ child = current;
6766
+ current = current.parent;
6743
6767
  }
6744
- if (isUseEffectCall(parent)) getOrInsertComputed(setStateInEffectSetup, parent, () => []).push(node);
6768
+ return false;
6745
6769
  }
6746
- const isExemptSetStateCall = (setStateCall, refParams) => {
6747
- const callNode = getSetStateCallExpression(setStateCall);
6748
- if (isRefGatedContext(context, callNode, refParams)) return true;
6749
- if (callNode.type !== AST_NODE_TYPES.CallExpression) return false;
6750
- const args0 = callNode.arguments.at(0);
6751
- return args0 != null && isArgumentUsingRefValue(context, args0, refParams);
6770
+ function isParameterId(id) {
6771
+ return findVariable(context.sourceCode.getScope(id), id)?.defs.some((d) => d.type === DefinitionType.Parameter) ?? false;
6772
+ }
6773
+ return {
6774
+ getRefDerivedParams,
6775
+ getSetterName,
6776
+ isArgumentUsingRefValue,
6777
+ isInitializedFromRef,
6778
+ isRefGatedContext,
6779
+ isSetStateCall,
6780
+ isSetStateId,
6781
+ isUseStateCall,
6782
+ resolveValues
6752
6783
  };
6753
- const getHelperFunction = (callee) => {
6754
- const origin = resolveOrigin(context, callee);
6755
- if (Check.isFunction(origin)) return origin;
6756
- if (!core.isUseCallbackCall(context, origin)) return null;
6757
- const callback = origin.arguments.at(0);
6758
- if (callback == null || callback.type === AST_NODE_TYPES.SpreadElement) return null;
6759
- const unwrapped = Extract.unwrap(callback);
6760
- return Check.isFunction(unwrapped) ? unwrapped : null;
6784
+ }
6785
+ // src/rules/set-state-in-effect/effects.ts
6786
+ function inferViolations$2(context, facts) {
6787
+ if (facts.effectRoots.length === 0) return [];
6788
+ const resolver = createSetStateResolver$1(context, facts);
6789
+ const callsByFunction = /* @__PURE__ */ new Map();
6790
+ for (const fact of facts.calls) getOrInsertComputed(callsByFunction, fact.enclosingFunction, () => []).push(fact.node);
6791
+ const queue = [];
6792
+ const visited = /* @__PURE__ */ new Set();
6793
+ const violations = /* @__PURE__ */ new Map();
6794
+ const enqueue = (state) => {
6795
+ if (state.fn.async) return;
6796
+ const refParamsKey = [...state.refParams].map((n) => n.range[0]).sort((a, b) => a - b).join(",");
6797
+ const key = `${String(state.fn.range[0])}:${state.inheritedRefGate ? "gated" : "plain"}:${refParamsKey}`;
6798
+ if (visited.has(key)) return;
6799
+ visited.add(key);
6800
+ queue.push(state);
6761
6801
  };
6762
- const getRefDerivedParams = (call, callee) => {
6763
- const origin = getHelperFunction(callee);
6764
- if (origin == null) return void 0;
6765
- const refParams = /* @__PURE__ */ new Set();
6766
- origin.params.forEach((param, index) => {
6767
- const paramId = param.type === AST_NODE_TYPES.Identifier ? param : param.type === AST_NODE_TYPES.AssignmentPattern && param.left.type === AST_NODE_TYPES.Identifier ? param.left : null;
6768
- if (paramId == null) return;
6769
- const arg = call.arguments.at(index);
6770
- if (arg == null || arg.type === AST_NODE_TYPES.SpreadElement) return;
6771
- if (isArgumentUsingRefValue(context, arg)) refParams.add(paramId);
6802
+ const report = (sink, setter, reportNode) => {
6803
+ if (violations.has(sink)) return;
6804
+ violations.set(sink, {
6805
+ name: resolver.getSetterName(setter.node),
6806
+ node: reportNode
6772
6807
  });
6773
- return refParams.size > 0 ? refParams : void 0;
6774
6808
  };
6775
- const getSetStateCalls = (id) => {
6776
- const variable = findVariable(context.sourceCode.getScope(id), id);
6777
- if (variable != null && variable.defs.some((def) => def.type === DefinitionType.Parameter)) return [];
6778
- const node = resolveOrigin(context, id);
6779
- switch (node?.type) {
6780
- case AST_NODE_TYPES.ArrowFunctionExpression:
6781
- case AST_NODE_TYPES.FunctionDeclaration:
6782
- case AST_NODE_TYPES.FunctionExpression: return setStateCallsByFn.get(node) ?? [];
6783
- case AST_NODE_TYPES.CallExpression: return setStateInHookCallbacks.get(node) ?? setStateInEffectArg.get(node) ?? [];
6784
- }
6785
- return [];
6809
+ const isExemptCall = (call, state) => {
6810
+ if (state.inheritedRefGate || resolver.isRefGatedContext(call, state.refParams)) return true;
6811
+ const argument = call.arguments.at(0);
6812
+ return argument != null && resolver.isArgumentUsingRefValue(argument, state.refParams);
6786
6813
  };
6787
- for (const [, calls] of setStateInEffectSetup) for (const call of calls) {
6788
- if (isExemptSetStateCall(call)) continue;
6789
- violations.push({
6790
- name: call.name,
6791
- node: call
6792
- });
6793
- }
6794
- for (const trackedCall of trackedFnCalls) {
6795
- const unwrappedCallee = Extract.unwrap(trackedCall.callee);
6796
- if (!Check.isIdentifier(unwrappedCallee)) continue;
6797
- if (isRefGatedContext(context, trackedCall)) continue;
6798
- const refParams = getRefDerivedParams(trackedCall, unwrappedCallee);
6799
- for (const setStateCall of getSetStateCalls(unwrappedCallee)) {
6800
- if (isExemptSetStateCall(setStateCall, refParams)) continue;
6801
- violations.push({
6802
- name: getCallName(setStateCall, getText(context)),
6803
- node: setStateCall
6814
+ for (const root of [...facts.effectRoots].sort((a, b) => a.node.range[0] - b.node.range[0])) {
6815
+ const setup = root.setup;
6816
+ const unwrappedSetup = Extract.unwrap(setup);
6817
+ for (const target of resolver.resolveValues(setup)) {
6818
+ if (target.kind === "setter") {
6819
+ if (!resolver.isRefGatedContext(setup)) report(target.node, target, setup);
6820
+ continue;
6821
+ }
6822
+ const reportNode = unwrappedSetup === target.node ? null : setup;
6823
+ enqueue({
6824
+ fn: target.node,
6825
+ inheritedRefGate: false,
6826
+ refParams: /* @__PURE__ */ new Set(),
6827
+ reportNode
6804
6828
  });
6805
6829
  }
6806
6830
  }
6807
- for (const id of facts.setupIdentifiers) for (const setStateCall of getSetStateCalls(id)) {
6808
- if (isExemptSetStateCall(setStateCall)) continue;
6809
- violations.push({
6810
- name: getCallName(setStateCall, getText(context)),
6811
- node: setStateCall
6812
- });
6831
+ for (let index = 0; index < queue.length; index++) {
6832
+ const state = queue[index];
6833
+ if (state == null) continue;
6834
+ const calls = callsByFunction.get(state.fn) ?? [];
6835
+ for (const call of calls) for (const target of resolver.resolveValues(call.callee)) {
6836
+ if (target.kind === "setter") {
6837
+ if (!isExemptCall(call, state)) report(call, target, state.reportNode ?? call);
6838
+ continue;
6839
+ }
6840
+ enqueue({
6841
+ fn: target.node,
6842
+ inheritedRefGate: state.inheritedRefGate || resolver.isRefGatedContext(call, state.refParams),
6843
+ refParams: resolver.getRefDerivedParams(call, target.node, state.refParams),
6844
+ reportNode: state.reportNode ?? call
6845
+ });
6846
+ }
6813
6847
  }
6814
- return violations;
6848
+ return [...violations.values()].sort((a, b) => a.node.range[0] - b.node.range[0] || a.node.range[1] - b.node.range[1]);
6815
6849
  }
6816
6850
  var set_state_in_effect_default = createRule({
6817
6851
  meta: {
@@ -6835,8 +6869,7 @@ function create$5(context) {
6835
6869
  });
6836
6870
  } });
6837
6871
  }
6838
- //#endregion
6839
- //#region src/rules/set-state-in-render/helpers.ts
6872
+ // src/rules/set-state-in-render/helpers.ts
6840
6873
  const isComponentOrHookName = or(core.isFunctionComponentName, core.isHookName);
6841
6874
  function isComponentOrHookLikeFunction(context, node) {
6842
6875
  const id = core.getFunctionId(node);
@@ -6859,8 +6892,7 @@ function isInsideConditional(node, stopAt) {
6859
6892
  }
6860
6893
  return false;
6861
6894
  }
6862
- //#endregion
6863
- //#region src/rules/set-state-in-render/collect.ts
6895
+ // src/rules/set-state-in-render/collect.ts
6864
6896
  function createFactCollector$2(context) {
6865
6897
  const { additionalStateHooks } = getSettingsFromContext(context);
6866
6898
  const facts = {
@@ -6870,7 +6902,7 @@ function createFactCollector$2(context) {
6870
6902
  const functionEntries = [];
6871
6903
  const componentFnStack = [];
6872
6904
  function isUseStateCall(node) {
6873
- return core.isUseStateLikeCall(node, additionalStateHooks);
6905
+ return node.type === AST_NODE_TYPES.CallExpression && core.isUseStateLikeCall(node, additionalStateHooks);
6874
6906
  }
6875
6907
  function getFunctionKind(node) {
6876
6908
  if (isComponentOrHookLikeFunction(context, node)) return "component";
@@ -6926,46 +6958,22 @@ function createFactCollector$2(context) {
6926
6958
  }
6927
6959
  };
6928
6960
  }
6929
- //#endregion
6930
- //#region src/rules/set-state-in-render/origins.ts
6961
+ // src/rules/set-state-in-render/origins.ts
6931
6962
  function createSetStateResolver(context) {
6932
6963
  const { additionalStateHooks } = getSettingsFromContext(context);
6933
6964
  function isUseStateCall(node) {
6934
- return core.isUseStateLikeCall(node, additionalStateHooks);
6965
+ return node.type === AST_NODE_TYPES.CallExpression && core.isUseStateLikeCall(node, additionalStateHooks);
6935
6966
  }
6936
- function isSetStateId(id, at) {
6967
+ function isSetStateId(id) {
6937
6968
  const initNode = resolveOrigin(context, id);
6938
- if (initNode == null) return false;
6939
- if (initNode.type !== AST_NODE_TYPES.CallExpression) return false;
6940
- if (!isUseStateCall(initNode)) return false;
6969
+ if (initNode?.type !== AST_NODE_TYPES.CallExpression || !isUseStateCall(initNode)) return false;
6941
6970
  const variableNodeParent = initNode.parent;
6942
- if (!hasProperty(variableNodeParent, "id")) return true;
6943
- 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));
6944
- if (variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return true;
6945
- return variableNodeParent.id.elements.findIndex((e) => e != null && Check.isIdentifier(e, id.name)) === at;
6971
+ if (!hasProperty(variableNodeParent, "id") || variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return false;
6972
+ return variableNodeParent.id.elements.findIndex((e) => e != null && Check.isIdentifier(e, id.name)) === 1;
6946
6973
  }
6947
6974
  function isSetStateCall(node) {
6948
6975
  const callee = Extract.unwrap(node.callee);
6949
- switch (callee.type) {
6950
- case AST_NODE_TYPES.CallExpression: {
6951
- const innerCallee = Extract.unwrap(callee.callee);
6952
- if (innerCallee.type !== AST_NODE_TYPES.MemberExpression) return false;
6953
- if (!hasProperty(innerCallee.object, "name")) return false;
6954
- if (!(Extract.getCalleeName(callee) === "at")) return false;
6955
- const [index] = callee.arguments;
6956
- if (index == null) return false;
6957
- const indexScope = context.sourceCode.getScope(node);
6958
- return getStaticValue(index, indexScope)?.value === 1 && isSetStateId(innerCallee.object);
6959
- }
6960
- case AST_NODE_TYPES.Identifier: return isSetStateId(callee, 1);
6961
- case AST_NODE_TYPES.MemberExpression: {
6962
- if (!hasProperty(callee.object, "name")) return false;
6963
- const property = callee.property;
6964
- const propertyScope = context.sourceCode.getScope(node);
6965
- return getStaticValue(property, propertyScope)?.value === 1 && isSetStateId(callee.object, 1);
6966
- }
6967
- default: return false;
6968
- }
6976
+ return Check.isIdentifier(callee) && isSetStateId(callee);
6969
6977
  }
6970
6978
  return {
6971
6979
  isSetStateCall,
@@ -6973,8 +6981,7 @@ function createSetStateResolver(context) {
6973
6981
  isUseStateCall
6974
6982
  };
6975
6983
  }
6976
- //#endregion
6977
- //#region src/rules/set-state-in-render/effects.ts
6984
+ // src/rules/set-state-in-render/effects.ts
6978
6985
  function inferViolations$1(context, facts) {
6979
6986
  const resolver = createSetStateResolver(context);
6980
6987
  const earlyReturnPositions = /* @__PURE__ */ new Map();
@@ -7013,8 +7020,7 @@ function create$4(context) {
7013
7020
  });
7014
7021
  } });
7015
7022
  }
7016
- //#endregion
7017
- //#region src/rules/static-components/collect.ts
7023
+ // src/rules/static-components/collect.ts
7018
7024
  function createFactCollector$1() {
7019
7025
  const facts = { componentUsages: [] };
7020
7026
  return {
@@ -7030,8 +7036,7 @@ function createFactCollector$1() {
7030
7036
  } }
7031
7037
  };
7032
7038
  }
7033
- //#endregion
7034
- //#region src/rules/static-components/helpers.ts
7039
+ // src/rules/static-components/helpers.ts
7035
7040
  const KNOWN_DYNAMIC_EXPRESSION_TYPES = /* @__PURE__ */ new Set([
7036
7041
  AST_NODE_TYPES.ArrowFunctionExpression,
7037
7042
  AST_NODE_TYPES.CallExpression,
@@ -7043,8 +7048,7 @@ function createRenderBoundaryChecker(componentNodes) {
7043
7048
  const components = new Set(componentNodes);
7044
7049
  return (node) => Traverse.hasParent(node, (n) => Check.isFunctionOrClass(n) && components.has(n));
7045
7050
  }
7046
- //#endregion
7047
- //#region src/rules/static-components/origins.ts
7051
+ // src/rules/static-components/origins.ts
7048
7052
  function findDynamicCreationSite(context, node, isInsideRender, seen) {
7049
7053
  const expr = Extract.unwrap(node);
7050
7054
  if (KNOWN_DYNAMIC_EXPRESSION_TYPES.has(expr.type)) return expr;
@@ -7107,8 +7111,7 @@ function resolveDynamicComponentOrigin(context, variable, isInsideRender, seen =
7107
7111
  isDynamic: false
7108
7112
  };
7109
7113
  }
7110
- //#endregion
7111
- //#region src/rules/static-components/effects.ts
7114
+ // src/rules/static-components/effects.ts
7112
7115
  function inferCreatedComponents(context, componentUsages, isInsideRender) {
7113
7116
  const effects = [];
7114
7117
  for (const usage of componentUsages) {
@@ -7163,8 +7166,7 @@ function create$3(context) {
7163
7166
  }
7164
7167
  } });
7165
7168
  }
7166
- //#endregion
7167
- //#region src/rules/unsupported-syntax/lib.ts
7169
+ // src/rules/unsupported-syntax/lib.ts
7168
7170
  function isEvalCall(node) {
7169
7171
  const callee = Extract.unwrap(node.callee);
7170
7172
  switch (callee.type) {
@@ -7232,14 +7234,13 @@ function create$2(context) {
7232
7234
  }
7233
7235
  });
7234
7236
  }
7235
- //#endregion
7236
- //#region src/rules/use-memo/helpers.ts
7237
+ // src/rules/use-memo/helpers.ts
7237
7238
  function isResultUsed(node) {
7238
7239
  let parent = node.parent;
7239
7240
  while (Check.isTypeExpression(parent)) parent = parent.parent;
7240
7241
  return parent.type === AST_NODE_TYPES.VariableDeclarator || parent.type === AST_NODE_TYPES.AssignmentExpression || parent.type === AST_NODE_TYPES.AssignmentPattern || parent.type === AST_NODE_TYPES.Property || parent.type === AST_NODE_TYPES.ReturnStatement || parent.type === AST_NODE_TYPES.JSXExpressionContainer || parent.type === AST_NODE_TYPES.CallExpression || parent.type === AST_NODE_TYPES.NewExpression || parent.type === AST_NODE_TYPES.ArrayExpression || parent.type === AST_NODE_TYPES.ConditionalExpression || parent.type === AST_NODE_TYPES.LogicalExpression || parent.type === AST_NODE_TYPES.SequenceExpression || parent.type === AST_NODE_TYPES.SpreadElement || parent.type === AST_NODE_TYPES.TemplateLiteral || parent.type === AST_NODE_TYPES.BinaryExpression || parent.type === AST_NODE_TYPES.UnaryExpression || parent.type === AST_NODE_TYPES.MemberExpression || parent.type === AST_NODE_TYPES.TaggedTemplateExpression || parent.type === AST_NODE_TYPES.ChainExpression || parent.type === AST_NODE_TYPES.ArrowFunctionExpression || parent.type === AST_NODE_TYPES.ForOfStatement || parent.type === AST_NODE_TYPES.ForInStatement;
7241
7242
  }
7242
- function getNestedReturnStatements(node) {
7243
+ function getReturnStatements(node) {
7243
7244
  const statements = [];
7244
7245
  const boundary = Check.isFunction(node) ? node : Traverse.findParent(node, Check.isFunction);
7245
7246
  simpleTraverse(node, { enter(node) {
@@ -7249,8 +7250,7 @@ function getNestedReturnStatements(node) {
7249
7250
  } });
7250
7251
  return statements;
7251
7252
  }
7252
- //#endregion
7253
- //#region src/rules/use-memo/collect.ts
7253
+ // src/rules/use-memo/collect.ts
7254
7254
  function createFactCollector(isUseMemoCall) {
7255
7255
  const facts = { calls: [] };
7256
7256
  function collectWrites(callback) {
@@ -7289,14 +7289,13 @@ function createFactCollector(isUseMemoCall) {
7289
7289
  callbackArg,
7290
7290
  isResultUsed: isResultUsed(node),
7291
7291
  node,
7292
- returnStatements: callbackFunction != null ? getNestedReturnStatements(callbackFunction) : [],
7292
+ returnStatements: callbackFunction != null ? getReturnStatements(callbackFunction) : [],
7293
7293
  writes: callbackFunction != null ? collectWrites(callbackFunction) : []
7294
7294
  });
7295
7295
  } }
7296
7296
  };
7297
7297
  }
7298
- //#endregion
7299
- //#region src/rules/use-memo/origins.ts
7298
+ // src/rules/use-memo/origins.ts
7300
7299
  function isDeclaredInsideCallback(variable, callback) {
7301
7300
  let scope = variable.scope;
7302
7301
  while (scope != null) {
@@ -7311,8 +7310,7 @@ function isOuterVariable(context, target, callback) {
7311
7310
  if (variable == null || variable.defs.length === 0) return true;
7312
7311
  return !isDeclaredInsideCallback(variable, callback);
7313
7312
  }
7314
- //#endregion
7315
- //#region src/rules/use-memo/effects.ts
7313
+ // src/rules/use-memo/effects.ts
7316
7314
  function inferViolations(context, facts) {
7317
7315
  const violations = [];
7318
7316
  for (const fact of facts.calls) {
@@ -7379,8 +7377,7 @@ function create$1(context) {
7379
7377
  });
7380
7378
  } });
7381
7379
  }
7382
- //#endregion
7383
- //#region src/rules/use-state/lib.ts
7380
+ // src/rules/use-state/lib.ts
7384
7381
  const LAZY_INIT_ALLOW_LIST = [
7385
7382
  "Boolean",
7386
7383
  "String",
@@ -7439,7 +7436,7 @@ const EXPRESSION_VISITOR_KEYS = {
7439
7436
  [AST_NODE_TYPES.WhileStatement]: ["test"],
7440
7437
  [AST_NODE_TYPES.YieldExpression]: ["argument"]
7441
7438
  };
7442
- function getNestedExpressionsOfType(type) {
7439
+ function getExpressionsOfType(type) {
7443
7440
  const isNodeOfType = Check.is(type);
7444
7441
  const recurse = (node) => {
7445
7442
  const expressions = [];
@@ -7454,10 +7451,9 @@ function getNestedExpressionsOfType(type) {
7454
7451
  };
7455
7452
  return recurse;
7456
7453
  }
7457
- const getNestedNewExpressions = getNestedExpressionsOfType(AST_NODE_TYPES.NewExpression);
7458
- const getNestedCallExpressions = getNestedExpressionsOfType(AST_NODE_TYPES.CallExpression);
7459
- //#endregion
7460
- //#region src/rules/use-state/use-state.ts
7454
+ const getNewExpressions = getExpressionsOfType(AST_NODE_TYPES.NewExpression);
7455
+ const getCallExpressions = getExpressionsOfType(AST_NODE_TYPES.CallExpression);
7456
+ // src/rules/use-state/use-state.ts
7461
7457
  const RULE_NAME = "use-state";
7462
7458
  const defaultOptions = [{
7463
7459
  enforceAssignment: true,
@@ -7505,7 +7501,7 @@ function create(context) {
7505
7501
  if (enforceLazyInitialization) {
7506
7502
  const [useStateInput] = node.arguments;
7507
7503
  if (useStateInput != null) {
7508
- for (const expr of getNestedNewExpressions(useStateInput)) {
7504
+ for (const expr of getNewExpressions(useStateInput)) {
7509
7505
  if (!hasProperty(expr.callee, "name")) continue;
7510
7506
  if (LAZY_INIT_ALLOW_LIST.includes(expr.callee.name)) continue;
7511
7507
  if (Traverse.hasParent(expr, core.isUseCall(context))) continue;
@@ -7514,7 +7510,7 @@ function create(context) {
7514
7510
  node: expr
7515
7511
  });
7516
7512
  }
7517
- for (const expr of getNestedCallExpressions(useStateInput)) {
7513
+ for (const expr of getCallExpressions(useStateInput)) {
7518
7514
  if (!hasProperty(expr.callee, "name")) continue;
7519
7515
  if (core.isHookName(expr.callee.name)) continue;
7520
7516
  if (LAZY_INIT_ALLOW_LIST.includes(expr.callee.name)) continue;
@@ -7577,8 +7573,7 @@ function create(context) {
7577
7573
  });
7578
7574
  } };
7579
7575
  }
7580
- //#endregion
7581
- //#region src/plugin.ts
7576
+ // src/plugin.ts
7582
7577
  const plugin = {
7583
7578
  meta: {
7584
7579
  name: name$6,
@@ -7640,8 +7635,7 @@ const plugin = {
7640
7635
  "use-state": use_state_default
7641
7636
  }
7642
7637
  };
7643
- //#endregion
7644
- //#region src/configs/recommended.ts
7638
+ // src/configs/recommended.ts
7645
7639
  var recommended_exports = /* @__PURE__ */ __exportAll({
7646
7640
  name: () => name$5,
7647
7641
  plugins: () => plugins$5,
@@ -7690,8 +7684,7 @@ const rules$5 = {
7690
7684
  };
7691
7685
  const plugins$5 = { "react-x": plugin };
7692
7686
  const settings$5 = { "react-x": DEFAULT_ESLINT_REACT_SETTINGS };
7693
- //#endregion
7694
- //#region src/configs/recommended-typescript.ts
7687
+ // src/configs/recommended-typescript.ts
7695
7688
  var recommended_typescript_exports = /* @__PURE__ */ __exportAll({
7696
7689
  name: () => name$4,
7697
7690
  plugins: () => plugins$4,
@@ -7702,8 +7695,7 @@ const name$4 = "react-x/recommended-typescript";
7702
7695
  const rules$4 = { ...rules$5 };
7703
7696
  const plugins$4 = { ...plugins$5 };
7704
7697
  const settings$4 = { ...settings$5 };
7705
- //#endregion
7706
- //#region src/configs/recommended-type-checked.ts
7698
+ // src/configs/recommended-type-checked.ts
7707
7699
  var recommended_type_checked_exports = /* @__PURE__ */ __exportAll({
7708
7700
  name: () => name$3,
7709
7701
  plugins: () => plugins$3,
@@ -7717,8 +7709,7 @@ const rules$3 = {
7717
7709
  };
7718
7710
  const plugins$3 = { ...plugins$4 };
7719
7711
  const settings$3 = { ...settings$4 };
7720
- //#endregion
7721
- //#region src/configs/strict.ts
7712
+ // src/configs/strict.ts
7722
7713
  var strict_exports = /* @__PURE__ */ __exportAll({
7723
7714
  name: () => name$2,
7724
7715
  plugins: () => plugins$2,
@@ -7735,8 +7726,7 @@ const rules$2 = {
7735
7726
  };
7736
7727
  const plugins$2 = { ...plugins$5 };
7737
7728
  const settings$2 = { ...settings$5 };
7738
- //#endregion
7739
- //#region src/configs/strict-typescript.ts
7729
+ // src/configs/strict-typescript.ts
7740
7730
  var strict_typescript_exports = /* @__PURE__ */ __exportAll({
7741
7731
  name: () => name$1,
7742
7732
  plugins: () => plugins$1,
@@ -7747,8 +7737,7 @@ const name$1 = "react-x/strict-typescript";
7747
7737
  const rules$1 = { ...rules$2 };
7748
7738
  const plugins$1 = { ...plugins$2 };
7749
7739
  const settings$1 = { ...settings$2 };
7750
- //#endregion
7751
- //#region src/configs/strict-type-checked.ts
7740
+ // src/configs/strict-type-checked.ts
7752
7741
  var strict_type_checked_exports = /* @__PURE__ */ __exportAll({
7753
7742
  name: () => name,
7754
7743
  plugins: () => plugins,
@@ -7763,8 +7752,7 @@ const rules = {
7763
7752
  };
7764
7753
  const plugins = { ...plugins$1 };
7765
7754
  const settings = { ...settings$1 };
7766
- //#endregion
7767
- //#region src/index.ts
7755
+ // src/index.ts
7768
7756
  function createConfig(base) {
7769
7757
  return {
7770
7758
  ...base,
@@ -7791,5 +7779,4 @@ const finalPlugin = {
7791
7779
  ["strict-typescript"]: createConfig(strict_typescript_exports)
7792
7780
  }
7793
7781
  };
7794
- //#endregion
7795
7782
  export { finalPlugin as default };