eslint-plugin-react-hooks-extra 2.0.0-beta.20 → 2.0.0-beta.200

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/README.md CHANGED
@@ -17,26 +17,30 @@ npm install --save-dev eslint-plugin-react-hooks-extra
17
17
  // @ts-check
18
18
  import js from "@eslint/js";
19
19
  import reactHooksExtra from "eslint-plugin-react-hooks-extra";
20
+ import { defineConfig } from "eslint/config";
20
21
  import tseslint from "typescript-eslint";
21
22
 
22
- export default tseslint.config({
23
- files: ["**/*.ts", "**/*.tsx"],
24
- extends: [
25
- js.configs.recommended,
26
- tseslint.configs.recommended,
27
- reactHooksExtra.configs.recommended,
28
- ],
29
- languageOptions: {
30
- parser: tseslint.parser,
31
- parserOptions: {
32
- projectService: true,
33
- tsconfigRootDir: import.meta.dirname,
23
+ export default defineConfig([
24
+ {
25
+ files: ["**/*.ts", "**/*.tsx"],
26
+ extends: [
27
+ js.configs.recommended,
28
+ tseslint.configs.recommended,
29
+ reactHooksExtra.configs.recommended,
30
+ ],
31
+ languageOptions: {
32
+ parser: tseslint.parser,
33
+ parserOptions: {
34
+ projectService: true,
35
+ tsconfigRootDir: import.meta.dirname,
36
+ },
37
+ },
38
+ rules: {
39
+ // Put rules you want to override here
40
+ "react-hooks-extra/no-direct-set-state-in-use-effect": "warn",
34
41
  },
35
42
  },
36
- rules: {
37
- // Put rules you want to override here
38
- },
39
- });
43
+ ]);
40
44
  ```
41
45
 
42
46
  ## Rules
package/dist/index.d.ts CHANGED
@@ -1,45 +1,20 @@
1
- import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
2
- import { RulePreset } from '@eslint-react/kit';
1
+ import * as _eslint_react_kit0 from "@eslint-react/kit";
3
2
 
3
+ //#region src/index.d.ts
4
4
  declare const _default: {
5
- configs: {
6
- recommended: {
7
- plugins: {
8
- "react-hooks-extra": {
9
- readonly meta: {
10
- readonly name: string;
11
- readonly version: string;
12
- };
13
- readonly rules: {
14
- readonly "no-direct-set-state-in-use-effect": _typescript_eslint_utils_ts_eslint.RuleModule<"noDirectSetStateInUseEffect", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
15
- readonly "no-direct-set-state-in-use-layout-effect": _typescript_eslint_utils_ts_eslint.RuleModule<"noDirectSetStateInUseLayoutEffect", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
16
- readonly "no-unnecessary-use-callback": _typescript_eslint_utils_ts_eslint.RuleModule<"noUnnecessaryUseCallback", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
17
- readonly "no-unnecessary-use-memo": _typescript_eslint_utils_ts_eslint.RuleModule<"noUnnecessaryUseMemo", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
18
- readonly "no-unnecessary-use-prefix": _typescript_eslint_utils_ts_eslint.RuleModule<"noUnnecessaryUsePrefix", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
19
- readonly "prefer-use-state-lazy-initialization": _typescript_eslint_utils_ts_eslint.RuleModule<"preferUseStateLazyInitialization", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
20
- };
21
- };
22
- };
23
- name: string;
24
- rules: RulePreset;
25
- };
26
- "recommended-legacy": {
27
- plugins: string[];
28
- rules: RulePreset;
29
- };
30
- };
31
- meta: {
32
- readonly name: string;
33
- readonly version: string;
34
- };
35
- rules: {
36
- readonly "no-direct-set-state-in-use-effect": _typescript_eslint_utils_ts_eslint.RuleModule<"noDirectSetStateInUseEffect", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
37
- readonly "no-direct-set-state-in-use-layout-effect": _typescript_eslint_utils_ts_eslint.RuleModule<"noDirectSetStateInUseLayoutEffect", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
38
- readonly "no-unnecessary-use-callback": _typescript_eslint_utils_ts_eslint.RuleModule<"noUnnecessaryUseCallback", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
39
- readonly "no-unnecessary-use-memo": _typescript_eslint_utils_ts_eslint.RuleModule<"noUnnecessaryUseMemo", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
40
- readonly "no-unnecessary-use-prefix": _typescript_eslint_utils_ts_eslint.RuleModule<"noUnnecessaryUsePrefix", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
41
- readonly "prefer-use-state-lazy-initialization": _typescript_eslint_utils_ts_eslint.RuleModule<"preferUseStateLazyInitialization", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
5
+ configs: {
6
+ recommended: {
7
+ plugins: {};
8
+ name?: string;
9
+ rules?: Record<string, _eslint_react_kit0.RuleConfig>;
10
+ settings?: _eslint_react_kit0.SettingsConfig;
42
11
  };
12
+ };
13
+ meta: {
14
+ name: string;
15
+ version: string;
16
+ };
17
+ rules: Record<string, _eslint_react_kit0.CompatibleRule>;
43
18
  };
44
-
45
- export { _default as default };
19
+ //#endregion
20
+ export { _default as default };
package/dist/index.js CHANGED
@@ -1,815 +1,266 @@
1
- import * as AST from '@eslint-react/ast';
2
- import * as ER from '@eslint-react/core';
3
- import { identity, getOrElseUpdate, constVoid, not } from '@eslint-react/eff';
4
- import { getDocsUrl, getSettingsFromContext } from '@eslint-react/shared';
5
- import * as VAR from '@eslint-react/var';
6
- import { AST_NODE_TYPES } from '@typescript-eslint/types';
7
- import { match } from 'ts-pattern';
8
- import { ESLintUtils } from '@typescript-eslint/utils';
1
+ import { getConfigAdapters, getDocsUrl } from "@eslint-react/shared";
2
+ import * as AST from "@eslint-react/ast";
3
+ import * as ER from "@eslint-react/core";
4
+ import { constVoid, getOrElseUpdate, not } from "@eslint-react/eff";
5
+ import * as VAR from "@eslint-react/var";
6
+ import { AST_NODE_TYPES } from "@typescript-eslint/types";
7
+ import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
8
+ import { match } from "ts-pattern";
9
+ import { ESLintUtils } from "@typescript-eslint/utils";
9
10
 
11
+ //#region rolldown:runtime
10
12
  var __defProp = Object.defineProperty;
11
- var __export = (target, all) => {
12
- for (var name3 in all)
13
- __defProp(target, name3, { get: all[name3], enumerable: true });
13
+ var __export = (all) => {
14
+ let target = {};
15
+ for (var name$2 in all) __defProp(target, name$2, {
16
+ get: all[name$2],
17
+ enumerable: true
18
+ });
19
+ return target;
14
20
  };
15
21
 
16
- // src/configs/recommended.ts
17
- var recommended_exports = {};
18
- __export(recommended_exports, {
19
- name: () => name,
20
- rules: () => rules
22
+ //#endregion
23
+ //#region src/configs/recommended.ts
24
+ var recommended_exports = /* @__PURE__ */ __export({
25
+ name: () => name$1,
26
+ rules: () => rules
21
27
  });
22
- var name = "react-hooks-extra/recommended";
23
- var rules = {
24
- "react-hooks-extra/no-direct-set-state-in-use-effect": "warn"
25
- };
28
+ const name$1 = "react-hooks-extra/recommended";
29
+ const rules = { "react-hooks-extra/no-direct-set-state-in-use-effect": "warn" };
26
30
 
27
- // package.json
28
- var name2 = "eslint-plugin-react-hooks-extra";
29
- var version = "2.0.0-beta.20";
30
- function useNoDirectSetStateInUseEffect(context, options) {
31
- const { onViolation, useEffectKind } = options;
32
- const settings = getSettingsFromContext(context);
33
- const hooks = settings.additionalHooks;
34
- const getText = (n) => context.sourceCode.getText(n);
35
- const isUseEffectLikeCall = ER.isReactHookCallWithNameAlias(context, useEffectKind, hooks[useEffectKind]);
36
- const isUseStateCall = ER.isReactHookCallWithNameAlias(context, "useState", hooks.useState);
37
- const isUseMemoCall = ER.isReactHookCallWithNameAlias(context, "useMemo", hooks.useMemo);
38
- const isUseCallbackCall = ER.isReactHookCallWithNameAlias(context, "useCallback", hooks.useCallback);
39
- const functionEntries = [];
40
- const setupFunctionRef = { current: null };
41
- const setupFunctionIdentifiers = [];
42
- const indFunctionCalls = [];
43
- const indSetStateCalls = /* @__PURE__ */ new WeakMap();
44
- const indSetStateCallsInUseEffectArg0 = /* @__PURE__ */ new WeakMap();
45
- const indSetStateCallsInUseEffectSetup = /* @__PURE__ */ new Map();
46
- const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
47
- const onSetupFunctionEnter = (node) => {
48
- setupFunctionRef.current = node;
49
- };
50
- const onSetupFunctionExit = (node) => {
51
- if (setupFunctionRef.current === node) {
52
- setupFunctionRef.current = null;
53
- }
54
- };
55
- function isFunctionOfUseEffectSetup(node) {
56
- return node.parent?.type === AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseEffectLikeCall(node.parent);
57
- }
58
- function getCallName(node) {
59
- if (node.type === AST_NODE_TYPES.CallExpression) {
60
- return AST.toStringFormat(node.callee, getText);
61
- }
62
- return AST.toStringFormat(node, getText);
63
- }
64
- function getCallKind(node) {
65
- return match(node).when(isUseStateCall, () => "useState").when(isUseEffectLikeCall, () => useEffectKind).when(isSetStateCall, () => "setState").when(AST.isThenCall, () => "then").otherwise(() => "other");
66
- }
67
- function getFunctionKind(node) {
68
- const parent = AST.findParentNode(node, not(AST.isTypeExpression)) ?? node.parent;
69
- switch (true) {
70
- case node.async:
71
- case (parent.type === AST_NODE_TYPES.CallExpression && AST.isThenCall(parent)):
72
- return "deferred";
73
- case (node.type !== AST_NODE_TYPES.FunctionDeclaration && parent.type === AST_NODE_TYPES.CallExpression && parent.callee === node):
74
- return "immediate";
75
- case isFunctionOfUseEffectSetup(node):
76
- return "setup";
77
- default:
78
- return "other";
79
- }
80
- }
81
- function isIdFromUseStateCall(topLevelId, at) {
82
- const variable = VAR.findVariable(topLevelId, context.sourceCode.getScope(topLevelId));
83
- const variableNode = VAR.getVariableInitNode(variable, 0);
84
- if (variableNode == null) return false;
85
- if (variableNode.type !== AST_NODE_TYPES.CallExpression) return false;
86
- if (!ER.isReactHookCallWithNameAlias(context, "useState", hooks.useState)(variableNode)) return false;
87
- const variableNodeParent = variableNode.parent;
88
- if (!("id" in variableNodeParent) || variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) {
89
- return true;
90
- }
91
- return variableNodeParent.id.elements.findIndex((e) => e?.type === AST_NODE_TYPES.Identifier && e.name === topLevelId.name) === at;
92
- }
93
- function isSetStateCall(node) {
94
- switch (node.callee.type) {
95
- // const data = useState();
96
- // data.at(1)();
97
- case AST_NODE_TYPES.CallExpression: {
98
- const { callee } = node.callee;
99
- if (callee.type !== AST_NODE_TYPES.MemberExpression) {
100
- return false;
101
- }
102
- if (!("name" in callee.object)) {
103
- return false;
104
- }
105
- const isAt = callee.property.type === AST_NODE_TYPES.Identifier && callee.property.name === "at";
106
- const [index] = node.callee.arguments;
107
- if (!isAt || index == null) {
108
- return false;
109
- }
110
- const indexScope = context.sourceCode.getScope(node);
111
- const indexValue = VAR.toStaticValue({
112
- kind: "lazy",
113
- node: index,
114
- initialScope: indexScope
115
- }).value;
116
- return indexValue === 1 && isIdFromUseStateCall(callee.object);
117
- }
118
- // const [data, setData] = useState();
119
- // setData();
120
- case AST_NODE_TYPES.Identifier: {
121
- return isIdFromUseStateCall(node.callee, 1);
122
- }
123
- // const data = useState();
124
- // data[1]();
125
- case AST_NODE_TYPES.MemberExpression: {
126
- if (!("name" in node.callee.object)) {
127
- return false;
128
- }
129
- const property = node.callee.property;
130
- const propertyScope = context.sourceCode.getScope(node);
131
- const propertyValue = VAR.toStaticValue({
132
- kind: "lazy",
133
- node: property,
134
- initialScope: propertyScope
135
- }).value;
136
- return propertyValue === 1 && isIdFromUseStateCall(node.callee.object, 1);
137
- }
138
- default: {
139
- return false;
140
- }
141
- }
142
- }
143
- return {
144
- ":function"(node) {
145
- const kind = getFunctionKind(node);
146
- functionEntries.push({ kind, node });
147
- if (kind === "setup") {
148
- onSetupFunctionEnter(node);
149
- }
150
- },
151
- ":function:exit"(node) {
152
- const { kind } = functionEntries.at(-1) ?? {};
153
- if (kind === "setup") {
154
- onSetupFunctionExit(node);
155
- }
156
- functionEntries.pop();
157
- },
158
- CallExpression(node) {
159
- const setupFunction = setupFunctionRef.current;
160
- const pEntry = functionEntries.at(-1);
161
- if (pEntry == null || pEntry.node.async) {
162
- return;
163
- }
164
- match(getCallKind(node)).with("setState", () => {
165
- switch (true) {
166
- case pEntry.kind === "deferred":
167
- case pEntry.node.async:
168
- break;
169
- case pEntry.node === setupFunction:
170
- case (pEntry.kind === "immediate" && AST.findParentNode(pEntry.node, AST.isFunction) === setupFunction): {
171
- onViolation(context, node, {
172
- name: context.sourceCode.getText(node.callee)
173
- });
174
- return;
175
- }
176
- default: {
177
- const vd = AST.findParentNode(node, isVariableDeclaratorFromHookCall);
178
- if (vd == null) getOrElseUpdate(indSetStateCalls, pEntry.node, () => []).push(node);
179
- else getOrElseUpdate(indSetStateCallsInUseMemoOrCallback, vd.init, () => []).push(node);
180
- }
181
- }
182
- }).with(useEffectKind, () => {
183
- if (AST.isFunction(node.arguments.at(0))) return;
184
- setupFunctionIdentifiers.push(...AST.getNestedIdentifiers(node));
185
- }).with("other", () => {
186
- if (pEntry.node !== setupFunction) return;
187
- indFunctionCalls.push(node);
188
- }).otherwise(constVoid);
189
- },
190
- Identifier(node) {
191
- if (node.parent.type === AST_NODE_TYPES.CallExpression && node.parent.callee === node) {
192
- return;
193
- }
194
- if (!isIdFromUseStateCall(node, 1)) {
195
- return;
196
- }
197
- switch (node.parent.type) {
198
- case AST_NODE_TYPES.ArrowFunctionExpression: {
199
- const parent = node.parent.parent;
200
- if (parent.type !== AST_NODE_TYPES.CallExpression) {
201
- break;
202
- }
203
- if (!isUseMemoCall(parent)) {
204
- break;
205
- }
206
- const vd = AST.findParentNode(parent, isVariableDeclaratorFromHookCall);
207
- if (vd != null) {
208
- getOrElseUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
209
- }
210
- break;
211
- }
212
- case AST_NODE_TYPES.CallExpression: {
213
- if (node !== node.parent.arguments.at(0)) {
214
- break;
215
- }
216
- if (isUseCallbackCall(node.parent)) {
217
- const vd = AST.findParentNode(node.parent, isVariableDeclaratorFromHookCall);
218
- if (vd != null) {
219
- getOrElseUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
220
- }
221
- break;
222
- }
223
- if (isUseEffectLikeCall(node.parent)) {
224
- getOrElseUpdate(indSetStateCallsInUseEffectSetup, node.parent, () => []).push(node);
225
- }
226
- }
227
- }
228
- },
229
- "Program:exit"() {
230
- const getSetStateCalls = (id, initialScope) => {
231
- const node = VAR.getVariableInitNode(VAR.findVariable(id, initialScope), 0);
232
- switch (node?.type) {
233
- case AST_NODE_TYPES.ArrowFunctionExpression:
234
- case AST_NODE_TYPES.FunctionDeclaration:
235
- case AST_NODE_TYPES.FunctionExpression:
236
- return indSetStateCalls.get(node) ?? [];
237
- case AST_NODE_TYPES.CallExpression:
238
- return indSetStateCallsInUseMemoOrCallback.get(node) ?? indSetStateCallsInUseEffectArg0.get(node) ?? [];
239
- }
240
- return [];
241
- };
242
- for (const [, calls] of indSetStateCallsInUseEffectSetup) {
243
- for (const call of calls) {
244
- onViolation(context, call, { name: call.name });
245
- }
246
- }
247
- for (const { callee } of indFunctionCalls) {
248
- if (!("name" in callee)) {
249
- continue;
250
- }
251
- const { name: name3 } = callee;
252
- const setStateCalls = getSetStateCalls(name3, context.sourceCode.getScope(callee));
253
- for (const setStateCall of setStateCalls) {
254
- onViolation(context, setStateCall, {
255
- name: getCallName(setStateCall)
256
- });
257
- }
258
- }
259
- for (const id of setupFunctionIdentifiers) {
260
- const setStateCalls = getSetStateCalls(id.name, context.sourceCode.getScope(id));
261
- for (const setStateCall of setStateCalls) {
262
- onViolation(context, setStateCall, {
263
- name: getCallName(setStateCall)
264
- });
265
- }
266
- }
267
- }
268
- };
269
- }
31
+ //#endregion
32
+ //#region package.json
33
+ var name = "eslint-plugin-react-hooks-extra";
34
+ var version = "2.0.0-beta.200";
35
+
36
+ //#endregion
37
+ //#region src/utils/create-rule.ts
38
+ const createRule = ESLintUtils.RuleCreator(getDocsUrl("hooks-extra"));
39
+
40
+ //#endregion
41
+ //#region src/utils/is-variable-declarator-from-hook-call.ts
270
42
  function isInitFromHookCall(init) {
271
- if (init?.type !== AST_NODE_TYPES.CallExpression) return false;
272
- switch (init.callee.type) {
273
- case AST_NODE_TYPES.Identifier:
274
- return ER.isReactHookName(init.callee.name);
275
- case AST_NODE_TYPES.MemberExpression:
276
- return init.callee.property.type === AST_NODE_TYPES.Identifier && ER.isReactHookName(init.callee.property.name);
277
- default:
278
- return false;
279
- }
43
+ if (init?.type !== AST_NODE_TYPES.CallExpression) return false;
44
+ switch (init.callee.type) {
45
+ case AST_NODE_TYPES.Identifier: return ER.isReactHookName(init.callee.name);
46
+ case AST_NODE_TYPES.MemberExpression: return init.callee.property.type === AST_NODE_TYPES.Identifier && ER.isReactHookName(init.callee.property.name);
47
+ default: return false;
48
+ }
280
49
  }
281
50
  function isVariableDeclaratorFromHookCall(node) {
282
- if (node.type !== AST_NODE_TYPES.VariableDeclarator) return false;
283
- if (node.id.type !== AST_NODE_TYPES.Identifier) return false;
284
- return isInitFromHookCall(node.init);
51
+ if (node.type !== AST_NODE_TYPES.VariableDeclarator) return false;
52
+ if (node.id.type !== AST_NODE_TYPES.Identifier) return false;
53
+ return isInitFromHookCall(node.init);
285
54
  }
286
- var createRule = ESLintUtils.RuleCreator(getDocsUrl("hooks-extra"));
287
55
 
288
- // src/rules/no-direct-set-state-in-use-effect.ts
289
- var RULE_NAME = "no-direct-set-state-in-use-effect";
290
- var RULE_FEATURES = [
291
- "EXP"
292
- ];
56
+ //#endregion
57
+ //#region src/rules/no-direct-set-state-in-use-effect.ts
58
+ const RULE_NAME = "no-direct-set-state-in-use-effect";
59
+ const RULE_FEATURES = ["EXP"];
293
60
  var no_direct_set_state_in_use_effect_default = createRule({
294
- meta: {
295
- type: "problem",
296
- docs: {
297
- description: "Disallow direct calls to the `set` function of `useState` in `useEffect`.",
298
- [Symbol.for("rule_features")]: RULE_FEATURES
299
- },
300
- messages: {
301
- noDirectSetStateInUseEffect: "Do not call the 'set' function '{{name}}' of 'useState' directly in 'useEffect'."
302
- },
303
- schema: []
304
- },
305
- name: RULE_NAME,
306
- create,
307
- defaultOptions: []
61
+ meta: {
62
+ type: "problem",
63
+ docs: {
64
+ description: "Disallow direct calls to the `set` function of `useState` in `useEffect`.",
65
+ [Symbol.for("rule_features")]: RULE_FEATURES
66
+ },
67
+ messages: { noDirectSetStateInUseEffect: "Do not call the 'set' function '{{name}}' of 'useState' directly in 'useEffect'." },
68
+ schema: []
69
+ },
70
+ name: RULE_NAME,
71
+ create,
72
+ defaultOptions: []
308
73
  });
309
74
  function create(context) {
310
- if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
311
- return useNoDirectSetStateInUseEffect(context, {
312
- onViolation(ctx, node, data) {
313
- ctx.report({ messageId: "noDirectSetStateInUseEffect", node, data });
314
- },
315
- useEffectKind: "useEffect"
316
- });
75
+ if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
76
+ const functionEntries = [];
77
+ const setupFnRef = { current: null };
78
+ const setupFnIds = [];
79
+ const trackedFnCalls = [];
80
+ const setStateCallsByFn = /* @__PURE__ */ new WeakMap();
81
+ const setStateInEffectArg = /* @__PURE__ */ new WeakMap();
82
+ const setStateInEffectSetup = /* @__PURE__ */ new Map();
83
+ const setStateInHookCallbacks = /* @__PURE__ */ new WeakMap();
84
+ const getText = (n) => context.sourceCode.getText(n);
85
+ const onSetupFunctionEnter = (node) => {
86
+ setupFnRef.current = node;
87
+ };
88
+ const onSetupFunctionExit = (node) => {
89
+ if (setupFnRef.current === node) setupFnRef.current = null;
90
+ };
91
+ function isFunctionOfUseEffectSetup(node) {
92
+ return node.parent?.type === AST_NODE_TYPES.CallExpression && node.parent.callee !== node && ER.isUseEffectLikeCall(node.parent);
93
+ }
94
+ function getCallName(node) {
95
+ if (node.type === AST_NODE_TYPES.CallExpression) return AST.toStringFormat(node.callee, getText);
96
+ return AST.toStringFormat(node, getText);
97
+ }
98
+ function getCallKind(node) {
99
+ return match(node).when(ER.isUseStateCall, () => "useState").when(ER.isUseEffectLikeCall, () => "useEffect").when(isSetStateCall, () => "setState").when(AST.isThenCall, () => "then").otherwise(() => "other");
100
+ }
101
+ function getFunctionKind(node) {
102
+ const parent = AST.findParentNode(node, not(AST.isTypeExpression)) ?? node.parent;
103
+ switch (true) {
104
+ case node.async:
105
+ case parent.type === AST_NODE_TYPES.CallExpression && AST.isThenCall(parent): return "deferred";
106
+ case node.type !== AST_NODE_TYPES.FunctionDeclaration && parent.type === AST_NODE_TYPES.CallExpression && parent.callee === node: return "immediate";
107
+ case isFunctionOfUseEffectSetup(node): return "setup";
108
+ default: return "other";
109
+ }
110
+ }
111
+ function isIdFromUseStateCall(topLevelId, at) {
112
+ const variable = VAR.findVariable(topLevelId, context.sourceCode.getScope(topLevelId));
113
+ const variableNode = VAR.getVariableDefinitionNode(variable, 0);
114
+ if (variableNode == null) return false;
115
+ if (variableNode.type !== AST_NODE_TYPES.CallExpression) return false;
116
+ if (!ER.isUseStateCall(variableNode)) return false;
117
+ const variableNodeParent = variableNode.parent;
118
+ if (!("id" in variableNodeParent) || variableNodeParent.id?.type !== AST_NODE_TYPES.ArrayPattern) return true;
119
+ return variableNodeParent.id.elements.findIndex((e) => e?.type === AST_NODE_TYPES.Identifier && e.name === topLevelId.name) === at;
120
+ }
121
+ function isSetStateCall(node) {
122
+ switch (node.callee.type) {
123
+ case AST_NODE_TYPES.CallExpression: {
124
+ const { callee } = node.callee;
125
+ if (callee.type !== AST_NODE_TYPES.MemberExpression) return false;
126
+ if (!("name" in callee.object)) return false;
127
+ const isAt = callee.property.type === AST_NODE_TYPES.Identifier && callee.property.name === "at";
128
+ const [index] = node.callee.arguments;
129
+ if (!isAt || index == null) return false;
130
+ const indexScope = context.sourceCode.getScope(node);
131
+ return getStaticValue(index, indexScope)?.value === 1 && isIdFromUseStateCall(callee.object);
132
+ }
133
+ case AST_NODE_TYPES.Identifier: return isIdFromUseStateCall(node.callee, 1);
134
+ case AST_NODE_TYPES.MemberExpression: {
135
+ if (!("name" in node.callee.object)) return false;
136
+ const property = node.callee.property;
137
+ const propertyScope = context.sourceCode.getScope(node);
138
+ return getStaticValue(property, propertyScope)?.value === 1 && isIdFromUseStateCall(node.callee.object, 1);
139
+ }
140
+ default: return false;
141
+ }
142
+ }
143
+ return {
144
+ ":function"(node) {
145
+ const kind = getFunctionKind(node);
146
+ functionEntries.push({
147
+ kind,
148
+ node
149
+ });
150
+ if (kind === "setup") onSetupFunctionEnter(node);
151
+ },
152
+ ":function:exit"(node) {
153
+ const { kind } = functionEntries.at(-1) ?? {};
154
+ if (kind === "setup") onSetupFunctionExit(node);
155
+ functionEntries.pop();
156
+ },
157
+ CallExpression(node) {
158
+ const setupFunction = setupFnRef.current;
159
+ const pEntry = functionEntries.at(-1);
160
+ if (pEntry == null || pEntry.node.async) return;
161
+ match(getCallKind(node)).with("setState", () => {
162
+ switch (true) {
163
+ case pEntry.kind === "deferred":
164
+ case pEntry.node.async: break;
165
+ case pEntry.node === setupFunction:
166
+ case pEntry.kind === "immediate" && AST.findParentNode(pEntry.node, AST.isFunction) === setupFunction:
167
+ context.report({
168
+ messageId: "noDirectSetStateInUseEffect",
169
+ node,
170
+ data: { name: context.sourceCode.getText(node.callee) }
171
+ });
172
+ return;
173
+ default: {
174
+ const init = AST.findParentNode(node, isVariableDeclaratorFromHookCall)?.init;
175
+ if (init == null) getOrElseUpdate(setStateCallsByFn, pEntry.node, () => []).push(node);
176
+ else getOrElseUpdate(setStateInHookCallbacks, init, () => []).push(node);
177
+ }
178
+ }
179
+ }).with("useEffect", () => {
180
+ if (AST.isFunction(node.arguments.at(0))) return;
181
+ setupFnIds.push(...AST.getNestedIdentifiers(node));
182
+ }).with("other", () => {
183
+ if (pEntry.node !== setupFunction) return;
184
+ trackedFnCalls.push(node);
185
+ }).otherwise(constVoid);
186
+ },
187
+ Identifier(node) {
188
+ if (node.parent.type === AST_NODE_TYPES.CallExpression && node.parent.callee === node) return;
189
+ if (!isIdFromUseStateCall(node, 1)) return;
190
+ switch (node.parent.type) {
191
+ case AST_NODE_TYPES.ArrowFunctionExpression: {
192
+ const parent = node.parent.parent;
193
+ if (parent.type !== AST_NODE_TYPES.CallExpression) break;
194
+ if (!ER.isUseMemoCall(parent)) break;
195
+ const init = AST.findParentNode(parent, isVariableDeclaratorFromHookCall)?.init;
196
+ if (init != null) getOrElseUpdate(setStateInEffectArg, init, () => []).push(node);
197
+ break;
198
+ }
199
+ case AST_NODE_TYPES.CallExpression:
200
+ if (node !== node.parent.arguments.at(0)) break;
201
+ if (ER.isUseCallbackCall(node.parent)) {
202
+ const init = AST.findParentNode(node.parent, isVariableDeclaratorFromHookCall)?.init;
203
+ if (init != null) getOrElseUpdate(setStateInEffectArg, init, () => []).push(node);
204
+ break;
205
+ }
206
+ if (ER.isUseEffectLikeCall(node.parent)) getOrElseUpdate(setStateInEffectSetup, node.parent, () => []).push(node);
207
+ }
208
+ },
209
+ "Program:exit"() {
210
+ const getSetStateCalls = (id, initialScope) => {
211
+ const node = VAR.getVariableDefinitionNode(VAR.findVariable(id, initialScope), 0);
212
+ switch (node?.type) {
213
+ case AST_NODE_TYPES.ArrowFunctionExpression:
214
+ case AST_NODE_TYPES.FunctionDeclaration:
215
+ case AST_NODE_TYPES.FunctionExpression: return setStateCallsByFn.get(node) ?? [];
216
+ case AST_NODE_TYPES.CallExpression: return setStateInHookCallbacks.get(node) ?? setStateInEffectArg.get(node) ?? [];
217
+ }
218
+ return [];
219
+ };
220
+ for (const [, calls] of setStateInEffectSetup) for (const call of calls) context.report({
221
+ messageId: "noDirectSetStateInUseEffect",
222
+ node: call,
223
+ data: { name: call.name }
224
+ });
225
+ for (const { callee } of trackedFnCalls) {
226
+ if (!("name" in callee)) continue;
227
+ const { name: name$2 } = callee;
228
+ const setStateCalls = getSetStateCalls(name$2, context.sourceCode.getScope(callee));
229
+ for (const setStateCall of setStateCalls) context.report({
230
+ messageId: "noDirectSetStateInUseEffect",
231
+ node: setStateCall,
232
+ data: { name: getCallName(setStateCall) }
233
+ });
234
+ }
235
+ for (const id of setupFnIds) {
236
+ const setStateCalls = getSetStateCalls(id.name, context.sourceCode.getScope(id));
237
+ for (const setStateCall of setStateCalls) context.report({
238
+ messageId: "noDirectSetStateInUseEffect",
239
+ node: setStateCall,
240
+ data: { name: getCallName(setStateCall) }
241
+ });
242
+ }
243
+ }
244
+ };
317
245
  }
318
246
 
319
- // src/rules/no-direct-set-state-in-use-layout-effect.ts
320
- var RULE_NAME2 = "no-direct-set-state-in-use-layout-effect";
321
- var RULE_FEATURES2 = [
322
- "EXP"
323
- ];
324
- var no_direct_set_state_in_use_layout_effect_default = createRule({
325
- meta: {
326
- type: "problem",
327
- docs: {
328
- description: "Disallow direct calls to the `set` function of `useState` in `useLayoutEffect`.",
329
- [Symbol.for("rule_features")]: RULE_FEATURES2
330
- },
331
- messages: {
332
- noDirectSetStateInUseLayoutEffect: "Do not call the 'set' function '{{name}}' of 'useState' directly in 'useLayoutEffect'."
333
- },
334
- schema: []
335
- },
336
- name: RULE_NAME2,
337
- create: create2,
338
- defaultOptions: []
339
- });
340
- function create2(context) {
341
- if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
342
- return useNoDirectSetStateInUseEffect(context, {
343
- onViolation(ctx, node, data) {
344
- ctx.report({ messageId: "noDirectSetStateInUseLayoutEffect", node, data });
345
- },
346
- useEffectKind: "useLayoutEffect"
347
- });
348
- }
349
- var RULE_NAME3 = "no-unnecessary-use-callback";
350
- var RULE_FEATURES3 = [
351
- "EXP"
352
- ];
353
- var no_unnecessary_use_callback_default = createRule({
354
- meta: {
355
- type: "problem",
356
- deprecated: {
357
- deprecatedSince: "2.0.0",
358
- replacedBy: [
359
- {
360
- message: "Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.",
361
- plugin: {
362
- name: "eslint-plugin-react-x",
363
- url: "https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x"
364
- },
365
- rule: {
366
- name: "no-unnecessary-use-callback",
367
- url: "https://eslint-react.xyz/docs/rules/no-unnecessary-use-callback"
368
- }
369
- },
370
- {
371
- message: "Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.",
372
- plugin: {
373
- name: "@eslint-react/eslint-plugin",
374
- url: "https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin"
375
- },
376
- rule: {
377
- name: "no-unnecessary-use-callback",
378
- url: "https://eslint-react.xyz/docs/rules/no-unnecessary-use-callback"
379
- }
380
- }
381
- ]
382
- },
383
- docs: {
384
- description: "Disallow unnecessary usage of `useCallback`.",
385
- [Symbol.for("rule_features")]: RULE_FEATURES3
386
- },
387
- messages: {
388
- noUnnecessaryUseCallback: "An 'useCallback' with empty deps and no references to the component scope may be unnecessary."
389
- },
390
- schema: []
391
- },
392
- name: RULE_NAME3,
393
- create: create3,
394
- defaultOptions: []
395
- });
396
- function create3(context) {
397
- if (!context.sourceCode.text.includes("use")) return {};
398
- const alias = getSettingsFromContext(context).additionalHooks.useCallback ?? [];
399
- const isUseCallbackCall = ER.isReactHookCallWithNameAlias(context, "useCallback", alias);
400
- return {
401
- CallExpression(node) {
402
- if (!ER.isReactHookCall(node)) {
403
- return;
404
- }
405
- const initialScope = context.sourceCode.getScope(node);
406
- if (!isUseCallbackCall(node)) {
407
- return;
408
- }
409
- const scope = context.sourceCode.getScope(node);
410
- const component = scope.block;
411
- if (!AST.isFunction(component)) {
412
- return;
413
- }
414
- const [arg0, arg1] = node.arguments;
415
- if (arg0 == null || arg1 == null) {
416
- return;
417
- }
418
- const hasEmptyDeps = match(arg1).with({ type: AST_NODE_TYPES.ArrayExpression }, (n) => n.elements.length === 0).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
419
- const variable = VAR.findVariable(n.name, initialScope);
420
- const variableNode = VAR.getVariableInitNode(variable, 0);
421
- if (variableNode?.type !== AST_NODE_TYPES.ArrayExpression) {
422
- return false;
423
- }
424
- return variableNode.elements.length === 0;
425
- }).otherwise(() => false);
426
- if (!hasEmptyDeps) {
427
- return;
428
- }
429
- const arg0Node = match(arg0).with({ type: AST_NODE_TYPES.ArrowFunctionExpression }, (n) => {
430
- if (n.body.type === AST_NODE_TYPES.ArrowFunctionExpression) {
431
- return n.body;
432
- }
433
- return n;
434
- }).with({ type: AST_NODE_TYPES.FunctionExpression }, identity).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
435
- const variable = VAR.findVariable(n.name, initialScope);
436
- const variableNode = VAR.getVariableInitNode(variable, 0);
437
- if (variableNode?.type !== AST_NODE_TYPES.ArrowFunctionExpression && variableNode?.type !== AST_NODE_TYPES.FunctionExpression) {
438
- return null;
439
- }
440
- return variableNode;
441
- }).otherwise(() => null);
442
- if (arg0Node == null) return;
443
- const arg0NodeScope = context.sourceCode.getScope(arg0Node);
444
- const arg0NodeReferences = VAR.getChidScopes(arg0NodeScope).flatMap((x) => x.references);
445
- const isReferencedToComponentScope = arg0NodeReferences.some((x) => x.resolved?.scope.block === component);
446
- if (!isReferencedToComponentScope) {
447
- context.report({
448
- messageId: "noUnnecessaryUseCallback",
449
- node
450
- });
451
- }
452
- }
453
- };
454
- }
455
- var RULE_NAME4 = "no-unnecessary-use-memo";
456
- var RULE_FEATURES4 = [
457
- "EXP"
458
- ];
459
- var no_unnecessary_use_memo_default = createRule({
460
- meta: {
461
- type: "problem",
462
- deprecated: {
463
- deprecatedSince: "2.0.0",
464
- replacedBy: [
465
- {
466
- message: "Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.",
467
- plugin: {
468
- name: "eslint-plugin-react-x",
469
- url: "https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x"
470
- },
471
- rule: {
472
- name: "no-unnecessary-use-memo",
473
- url: "https://eslint-react.xyz/docs/rules/no-unnecessary-use-memo"
474
- }
475
- },
476
- {
477
- message: "Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.",
478
- plugin: {
479
- name: "@eslint-react/eslint-plugin",
480
- url: "https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin"
481
- },
482
- rule: {
483
- name: "no-unnecessary-use-memo",
484
- url: "https://eslint-react.xyz/docs/rules/no-unnecessary-use-memo"
485
- }
486
- }
487
- ]
488
- },
489
- docs: {
490
- description: "Disallow unnecessary usage of `useMemo`.",
491
- [Symbol.for("rule_features")]: RULE_FEATURES4
492
- },
493
- messages: {
494
- noUnnecessaryUseMemo: "An 'useMemo' with empty deps and no references to the component scope may be unnecessary."
495
- },
496
- schema: []
497
- },
498
- name: RULE_NAME4,
499
- create: create4,
500
- defaultOptions: []
501
- });
502
- function create4(context) {
503
- if (!context.sourceCode.text.includes("use")) return {};
504
- const alias = getSettingsFromContext(context).additionalHooks.useMemo ?? [];
505
- const isUseMemoCall = ER.isReactHookCallWithNameAlias(context, "useMemo", alias);
506
- return {
507
- CallExpression(node) {
508
- if (!ER.isReactHookCall(node)) {
509
- return;
510
- }
511
- const initialScope = context.sourceCode.getScope(node);
512
- if (!isUseMemoCall(node)) {
513
- return;
514
- }
515
- const scope = context.sourceCode.getScope(node);
516
- const component = scope.block;
517
- if (!AST.isFunction(component)) {
518
- return;
519
- }
520
- const [arg0, arg1] = node.arguments;
521
- if (arg0 == null || arg1 == null) {
522
- return;
523
- }
524
- const hasCallInArg0 = AST.isFunction(arg0) && [...AST.getNestedCallExpressions(arg0.body), ...AST.getNestedNewExpressions(arg0.body)].length > 0;
525
- if (hasCallInArg0) {
526
- return;
527
- }
528
- const hasEmptyDeps = match(arg1).with({ type: AST_NODE_TYPES.ArrayExpression }, (n) => n.elements.length === 0).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
529
- const variable = VAR.findVariable(n.name, initialScope);
530
- const variableNode = VAR.getVariableInitNode(variable, 0);
531
- if (variableNode?.type !== AST_NODE_TYPES.ArrayExpression) {
532
- return false;
533
- }
534
- return variableNode.elements.length === 0;
535
- }).otherwise(() => false);
536
- if (!hasEmptyDeps) {
537
- return;
538
- }
539
- const arg0Node = match(arg0).with({ type: AST_NODE_TYPES.ArrowFunctionExpression }, (n) => {
540
- if (n.body.type === AST_NODE_TYPES.ArrowFunctionExpression) {
541
- return n.body;
542
- }
543
- return n;
544
- }).with({ type: AST_NODE_TYPES.FunctionExpression }, identity).with({ type: AST_NODE_TYPES.Identifier }, (n) => {
545
- const variable = VAR.findVariable(n.name, initialScope);
546
- const variableNode = VAR.getVariableInitNode(variable, 0);
547
- if (variableNode?.type !== AST_NODE_TYPES.ArrowFunctionExpression && variableNode?.type !== AST_NODE_TYPES.FunctionExpression) {
548
- return null;
549
- }
550
- return variableNode;
551
- }).otherwise(() => null);
552
- if (arg0Node == null) return;
553
- const arg0NodeScope = context.sourceCode.getScope(arg0Node);
554
- const arg0NodeReferences = VAR.getChidScopes(arg0NodeScope).flatMap((x) => x.references);
555
- const isReferencedToComponentScope = arg0NodeReferences.some((x) => x.resolved?.scope.block === component);
556
- if (!isReferencedToComponentScope) {
557
- context.report({
558
- messageId: "noUnnecessaryUseMemo",
559
- node
560
- });
561
- }
562
- }
563
- };
564
- }
565
- var RULE_NAME5 = "no-unnecessary-use-prefix";
566
- var RULE_FEATURES5 = [];
567
- var WELL_KNOWN_HOOKS = [
568
- "useMDXComponents"
569
- ];
570
- function containsUseComments(context, node) {
571
- return context.sourceCode.getCommentsInside(node).some(({ value }) => /use\([\s\S]*?\)/u.test(value) || /use[A-Z0-9]\w*\([\s\S]*?\)/u.test(value));
572
- }
573
- var no_unnecessary_use_prefix_default = createRule({
574
- meta: {
575
- type: "problem",
576
- deprecated: {
577
- deprecatedSince: "2.0.0",
578
- replacedBy: [
579
- {
580
- message: "Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.",
581
- plugin: {
582
- name: "eslint-plugin-react-x",
583
- url: "https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x"
584
- },
585
- rule: {
586
- name: "no-unnecessary-use-prefix",
587
- url: "https://eslint-react.xyz/docs/rules/no-unnecessary-use-prefix"
588
- }
589
- },
590
- {
591
- message: "Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.",
592
- plugin: {
593
- name: "@eslint-react/eslint-plugin",
594
- url: "https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin"
595
- },
596
- rule: {
597
- name: "no-unnecessary-use-prefix",
598
- url: "https://eslint-react.xyz/docs/rules/no-unnecessary-use-prefix"
599
- }
600
- }
601
- ]
602
- },
603
- docs: {
604
- description: "Enforces that a function with the `use` prefix should use at least one Hook inside of it.",
605
- [Symbol.for("rule_features")]: RULE_FEATURES5
606
- },
607
- messages: {
608
- noUnnecessaryUsePrefix: "If your function doesn't call any Hooks, avoid the 'use' prefix. Instead, write it as a regular function without the 'use' prefix."
609
- },
610
- schema: []
611
- },
612
- name: RULE_NAME5,
613
- create: create5,
614
- defaultOptions: []
615
- });
616
- function create5(context) {
617
- const { ctx, listeners } = ER.useHookCollector();
618
- return {
619
- ...listeners,
620
- "Program:exit"(program) {
621
- const allHooks = ctx.getAllHooks(program);
622
- for (const { id, name: name3, node, hookCalls } of allHooks.values()) {
623
- if (WELL_KNOWN_HOOKS.includes(name3)) {
624
- continue;
625
- }
626
- if (AST.isEmptyFunction(node)) {
627
- continue;
628
- }
629
- if (hookCalls.length > 0) {
630
- continue;
631
- }
632
- if (containsUseComments(context, node)) {
633
- continue;
634
- }
635
- if (id != null) {
636
- context.report({
637
- messageId: "noUnnecessaryUsePrefix",
638
- data: {
639
- name: name3
640
- },
641
- loc: getPreferredLoc(context, id)
642
- });
643
- continue;
644
- }
645
- context.report({
646
- messageId: "noUnnecessaryUsePrefix",
647
- node,
648
- data: {
649
- name: name3
650
- }
651
- });
652
- }
653
- }
654
- };
655
- }
656
- function getPreferredLoc(context, id) {
657
- if (AST.isMultiLine(id)) return id.loc;
658
- if (!context.sourceCode.getText(id).startsWith("use")) return id.loc;
659
- return {
660
- end: {
661
- column: id.loc.start.column + 3,
662
- line: id.loc.start.line
663
- },
664
- start: {
665
- column: id.loc.start.column,
666
- line: id.loc.start.line
667
- }
668
- };
669
- }
670
- var RULE_NAME6 = "prefer-use-state-lazy-initialization";
671
- var RULE_FEATURES6 = [
672
- "EXP"
673
- ];
674
- var ALLOW_LIST = [
675
- "Boolean",
676
- "String",
677
- "Number"
678
- ];
679
- var prefer_use_state_lazy_initialization_default = createRule({
680
- meta: {
681
- type: "problem",
682
- deprecated: {
683
- deprecatedSince: "2.0.0",
684
- replacedBy: [
685
- {
686
- message: "Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.",
687
- plugin: {
688
- name: "eslint-plugin-react-x",
689
- url: "https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x"
690
- },
691
- rule: {
692
- name: "prefer-use-state-lazy-initialization",
693
- url: "https://eslint-react.xyz/docs/rules/prefer-use-state-lazy-initialization"
694
- }
695
- },
696
- {
697
- message: "Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.",
698
- plugin: {
699
- name: "@eslint-react/eslint-plugin",
700
- url: "https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin"
701
- },
702
- rule: {
703
- name: "prefer-use-state-lazy-initialization",
704
- url: "https://eslint-react.xyz/docs/rules/prefer-use-state-lazy-initialization"
705
- }
706
- }
707
- ]
708
- },
709
- docs: {
710
- description: "Enforces function calls made inside `useState` to be wrapped in an `initializer function`.",
711
- [Symbol.for("rule_features")]: RULE_FEATURES6
712
- },
713
- messages: {
714
- preferUseStateLazyInitialization: "To prevent re-computation, consider using lazy initial state for useState calls that involve function calls. Ex: 'useState(() => getValue())'."
715
- },
716
- schema: []
717
- },
718
- name: RULE_NAME6,
719
- create: create6,
720
- defaultOptions: []
721
- });
722
- function create6(context) {
723
- const alias = getSettingsFromContext(context).additionalHooks.useState ?? [];
724
- const isUseStateCall = ER.isReactHookCallWithNameAlias(context, "useState", alias);
725
- return {
726
- CallExpression(node) {
727
- if (!ER.isReactHookCall(node)) {
728
- return;
729
- }
730
- if (!isUseStateCall(node)) {
731
- return;
732
- }
733
- const [useStateInput] = node.arguments;
734
- if (useStateInput == null) {
735
- return;
736
- }
737
- for (const expr of AST.getNestedNewExpressions(useStateInput)) {
738
- if (!("name" in expr.callee)) continue;
739
- if (ALLOW_LIST.includes(expr.callee.name)) continue;
740
- if (AST.findParentNode(expr, (n) => ER.isUseCall(context, n)) != null) continue;
741
- context.report({
742
- messageId: "preferUseStateLazyInitialization",
743
- node: expr
744
- });
745
- }
746
- for (const expr of AST.getNestedCallExpressions(useStateInput)) {
747
- if (!("name" in expr.callee)) continue;
748
- if (ER.isReactHookName(expr.callee.name)) continue;
749
- if (ALLOW_LIST.includes(expr.callee.name)) continue;
750
- if (AST.findParentNode(expr, (n) => ER.isUseCall(context, n)) != null) continue;
751
- context.report({
752
- messageId: "preferUseStateLazyInitialization",
753
- node: expr
754
- });
755
- }
756
- }
757
- };
758
- }
759
-
760
- // src/plugin.ts
761
- var plugin = {
762
- meta: {
763
- name: name2,
764
- version
765
- },
766
- rules: {
767
- "no-direct-set-state-in-use-effect": no_direct_set_state_in_use_effect_default,
768
- "no-direct-set-state-in-use-layout-effect": no_direct_set_state_in_use_layout_effect_default,
769
- /**
770
- * @deprecated Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.
771
- * @see https://eslint-react.xyz/docs/rules/no-unnecessary-use-callback
772
- */
773
- "no-unnecessary-use-callback": no_unnecessary_use_callback_default,
774
- /**
775
- * @deprecated Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.
776
- * @see https://eslint-react.xyz/docs/rules/no-unnecessary-use-memo
777
- */
778
- "no-unnecessary-use-memo": no_unnecessary_use_memo_default,
779
- /**
780
- * @deprecated Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.
781
- * @see https://eslint-react.xyz/docs/rules/no-unnecessary-use-prefix
782
- */
783
- "no-unnecessary-use-prefix": no_unnecessary_use_prefix_default,
784
- /**
785
- * @deprecated Use the same rule from `eslint-plugin-react-x` or `@eslint-react/eslint-plugin` instead.
786
- * @see https://eslint-react.xyz/docs/rules/prefer-use-state-lazy-initialization
787
- */
788
- "prefer-use-state-lazy-initialization": prefer_use_state_lazy_initialization_default
789
- }
247
+ //#endregion
248
+ //#region src/plugin.ts
249
+ const plugin = {
250
+ meta: {
251
+ name,
252
+ version
253
+ },
254
+ rules: { "no-direct-set-state-in-use-effect": no_direct_set_state_in_use_effect_default }
790
255
  };
791
256
 
792
- // src/index.ts
793
- function makeConfig(config) {
794
- return {
795
- ...config,
796
- plugins: {
797
- "react-hooks-extra": plugin
798
- }
799
- };
800
- }
801
- function makeLegacyConfig({ rules: rules2 }) {
802
- return {
803
- plugins: ["react-hooks-extra"],
804
- rules: rules2
805
- };
806
- }
807
- var index_default = {
808
- ...plugin,
809
- configs: {
810
- ["recommended"]: makeConfig(recommended_exports),
811
- ["recommended-legacy"]: makeLegacyConfig(recommended_exports)
812
- }
257
+ //#endregion
258
+ //#region src/index.ts
259
+ const { toFlatConfig } = getConfigAdapters("react-hooks-extra", plugin);
260
+ var src_default = {
261
+ ...plugin,
262
+ configs: { ["recommended"]: toFlatConfig(recommended_exports) }
813
263
  };
814
264
 
815
- export { index_default as default };
265
+ //#endregion
266
+ export { src_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-hooks-extra",
3
- "version": "2.0.0-beta.20",
3
+ "version": "2.0.0-beta.200",
4
4
  "description": "ESLint React's ESLint plugin for React Hooks related rules.",
5
5
  "keywords": [
6
6
  "react",
@@ -36,45 +36,37 @@
36
36
  "./package.json"
37
37
  ],
38
38
  "dependencies": {
39
- "@typescript-eslint/scope-manager": "^8.38.0",
40
- "@typescript-eslint/type-utils": "^8.38.0",
41
- "@typescript-eslint/types": "^8.38.0",
42
- "@typescript-eslint/utils": "^8.38.0",
39
+ "@typescript-eslint/scope-manager": "^8.44.1",
40
+ "@typescript-eslint/type-utils": "^8.44.1",
41
+ "@typescript-eslint/types": "^8.44.1",
42
+ "@typescript-eslint/utils": "^8.44.1",
43
43
  "string-ts": "^2.2.1",
44
44
  "ts-pattern": "^5.8.0",
45
- "@eslint-react/ast": "2.0.0-beta.20",
46
- "@eslint-react/core": "2.0.0-beta.20",
47
- "@eslint-react/eff": "2.0.0-beta.20",
48
- "@eslint-react/kit": "2.0.0-beta.20",
49
- "@eslint-react/var": "2.0.0-beta.20",
50
- "@eslint-react/shared": "2.0.0-beta.20"
45
+ "@eslint-react/ast": "2.0.0-beta.200",
46
+ "@eslint-react/core": "2.0.0-beta.200",
47
+ "@eslint-react/eff": "2.0.0-beta.200",
48
+ "@eslint-react/shared": "2.0.0-beta.200",
49
+ "@eslint-react/kit": "2.0.0-beta.200",
50
+ "@eslint-react/var": "2.0.0-beta.200"
51
51
  },
52
52
  "devDependencies": {
53
- "@types/react": "^19.1.9",
54
- "@types/react-dom": "^19.1.7",
55
- "tsup": "^8.5.0",
53
+ "@types/react": "^19.1.13",
54
+ "@types/react-dom": "^19.1.9",
55
+ "tsdown": "^0.15.4",
56
56
  "@local/configs": "0.0.0"
57
57
  },
58
58
  "peerDependencies": {
59
- "eslint": "^8.57.0 || ^9.0.0",
60
- "typescript": "^4.9.5 || ^5.3.3"
61
- },
62
- "peerDependenciesMeta": {
63
- "eslint": {
64
- "optional": false
65
- },
66
- "typescript": {
67
- "optional": true
68
- }
59
+ "eslint": "^9.36.0",
60
+ "typescript": "^5.9.2"
69
61
  },
70
62
  "engines": {
71
- "node": ">=18.18.0"
63
+ "node": ">=20.0.0"
72
64
  },
73
65
  "publishConfig": {
74
66
  "access": "public"
75
67
  },
76
68
  "scripts": {
77
- "build": "tsup --dts-resolve",
69
+ "build": "tsdown",
78
70
  "lint:publish": "publint",
79
71
  "lint:ts": "tsc --noEmit"
80
72
  }