eslint-plugin-react-hooks-extra 1.23.1 → 1.23.2-beta.2
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 +50 -50
- package/dist/index.mjs +51 -51
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -31,7 +31,7 @@ var VAR5__namespace = /*#__PURE__*/_interopNamespace(VAR5);
|
|
|
31
31
|
|
|
32
32
|
// package.json
|
|
33
33
|
var name = "eslint-plugin-react-hooks-extra";
|
|
34
|
-
var version = "1.23.
|
|
34
|
+
var version = "1.23.2-beta.2";
|
|
35
35
|
var createRule = shared.createRuleForPlugin("hooks-extra");
|
|
36
36
|
function isFromHookCall(name2, context, settings, predicate = eff.F.constTrue) {
|
|
37
37
|
const hookAlias = settings.additionalHooks?.[name2] ?? [];
|
|
@@ -128,7 +128,7 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
128
128
|
name: RULE_NAME,
|
|
129
129
|
create(context) {
|
|
130
130
|
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
|
|
131
|
-
const settings = shared.
|
|
131
|
+
const settings = shared.getSettingsFromContext(context);
|
|
132
132
|
const additionalHooks = settings.additionalHooks ?? {};
|
|
133
133
|
const isUseEffectLikeCall = core.isReactHookCallWithNameAlias("useEffect", context, additionalHooks.useEffect ?? []);
|
|
134
134
|
const isUseStateCall2 = core.isReactHookCallWithNameAlias("useState", context, additionalHooks.useState ?? []);
|
|
@@ -139,11 +139,11 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
139
139
|
const functionStack = [];
|
|
140
140
|
const setupFunctionRef = { current: eff.O.none() };
|
|
141
141
|
const setupFunctionIdentifiers = [];
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
const
|
|
146
|
-
const
|
|
142
|
+
const indFunctionCalls = [];
|
|
143
|
+
const indSetStateCalls = /* @__PURE__ */ new WeakMap();
|
|
144
|
+
const indSetStateCallsInUseEffectArg0 = /* @__PURE__ */ new WeakMap();
|
|
145
|
+
const indSetStateCallsInUseEffectSetup = /* @__PURE__ */ new Map();
|
|
146
|
+
const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
|
|
147
147
|
const onSetupFunctionEnter = (node) => {
|
|
148
148
|
setupFunctionRef.current = eff.O.some(node);
|
|
149
149
|
};
|
|
@@ -180,12 +180,12 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
180
180
|
const maybeVd = AST2__namespace.traverseUpGuard(node, isVariableDeclaratorFromHookCall);
|
|
181
181
|
if (eff.O.isSome(maybeVd)) {
|
|
182
182
|
const vd = maybeVd.value;
|
|
183
|
-
const calls2 =
|
|
184
|
-
|
|
183
|
+
const calls2 = indSetStateCallsInUseMemoOrCallback.get(vd.init) ?? [];
|
|
184
|
+
indSetStateCallsInUseMemoOrCallback.set(vd.init, [...calls2, node]);
|
|
185
185
|
return;
|
|
186
186
|
}
|
|
187
|
-
const calls =
|
|
188
|
-
|
|
187
|
+
const calls = indSetStateCalls.get(parentFn) ?? [];
|
|
188
|
+
indSetStateCalls.set(parentFn, [...calls, node]);
|
|
189
189
|
return;
|
|
190
190
|
}
|
|
191
191
|
context.report({
|
|
@@ -201,7 +201,7 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
201
201
|
}).with("other", () => {
|
|
202
202
|
const isInSetupFunction = effectFn === parentFn;
|
|
203
203
|
if (!isInSetupFunction) return;
|
|
204
|
-
|
|
204
|
+
indFunctionCalls.push(node);
|
|
205
205
|
}).otherwise(eff.F.constVoid);
|
|
206
206
|
},
|
|
207
207
|
Identifier(node) {
|
|
@@ -215,8 +215,8 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
215
215
|
const maybeVd = AST2__namespace.traverseUpGuard(parent, isVariableDeclaratorFromHookCall);
|
|
216
216
|
if (eff.O.isNone(maybeVd)) break;
|
|
217
217
|
const vd = maybeVd.value;
|
|
218
|
-
const calls =
|
|
219
|
-
|
|
218
|
+
const calls = indSetStateCallsInUseEffectArg0.get(vd.init) ?? [];
|
|
219
|
+
indSetStateCallsInUseEffectArg0.set(vd.init, [...calls, node]);
|
|
220
220
|
break;
|
|
221
221
|
}
|
|
222
222
|
case types.AST_NODE_TYPES.CallExpression: {
|
|
@@ -226,12 +226,12 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
226
226
|
const maybeVd = AST2__namespace.traverseUpGuard(node.parent, isVariableDeclaratorFromHookCall);
|
|
227
227
|
if (eff.O.isNone(maybeVd)) break;
|
|
228
228
|
const vd = maybeVd.value;
|
|
229
|
-
const calls =
|
|
230
|
-
|
|
229
|
+
const calls = indSetStateCallsInUseEffectArg0.get(vd.init) ?? [];
|
|
230
|
+
indSetStateCallsInUseEffectArg0.set(vd.init, [...calls, node]);
|
|
231
231
|
}
|
|
232
232
|
if (isUseEffectLikeCall(node.parent)) {
|
|
233
|
-
const calls =
|
|
234
|
-
|
|
233
|
+
const calls = indSetStateCallsInUseEffectArg0.get(node.parent) ?? [];
|
|
234
|
+
indSetStateCallsInUseEffectSetup.set(node.parent, [...calls, node]);
|
|
235
235
|
}
|
|
236
236
|
break;
|
|
237
237
|
}
|
|
@@ -244,13 +244,13 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
244
244
|
case types.AST_NODE_TYPES.ArrowFunctionExpression:
|
|
245
245
|
case types.AST_NODE_TYPES.FunctionDeclaration:
|
|
246
246
|
case types.AST_NODE_TYPES.FunctionExpression:
|
|
247
|
-
return
|
|
247
|
+
return indSetStateCalls.get(node) ?? [];
|
|
248
248
|
case types.AST_NODE_TYPES.CallExpression:
|
|
249
|
-
return
|
|
249
|
+
return indSetStateCallsInUseMemoOrCallback.get(node) ?? indSetStateCallsInUseEffectArg0.get(node) ?? [];
|
|
250
250
|
}
|
|
251
251
|
return [];
|
|
252
252
|
};
|
|
253
|
-
for (const [_, calls] of
|
|
253
|
+
for (const [_, calls] of indSetStateCallsInUseEffectSetup) {
|
|
254
254
|
for (const call of calls) {
|
|
255
255
|
context.report({
|
|
256
256
|
messageId: "noDirectSetStateInUseEffect",
|
|
@@ -259,7 +259,7 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
259
259
|
});
|
|
260
260
|
}
|
|
261
261
|
}
|
|
262
|
-
for (const { callee } of
|
|
262
|
+
for (const { callee } of indFunctionCalls) {
|
|
263
263
|
if (!("name" in callee)) continue;
|
|
264
264
|
const { name: name2 } = callee;
|
|
265
265
|
const setStateCalls = getSetStateCalls(name2, context.sourceCode.getScope(callee));
|
|
@@ -309,9 +309,9 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
309
309
|
name: RULE_NAME2,
|
|
310
310
|
create(context) {
|
|
311
311
|
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
|
|
312
|
-
const settings = shared.
|
|
312
|
+
const settings = shared.getSettingsFromContext(context);
|
|
313
313
|
const additionalHooks = settings.additionalHooks ?? {};
|
|
314
|
-
const
|
|
314
|
+
const isUseLayoutEffectLikeCall = core.isReactHookCallWithNameAlias(
|
|
315
315
|
"useLayoutEffect",
|
|
316
316
|
context,
|
|
317
317
|
additionalHooks.useLayoutEffect ?? []
|
|
@@ -324,11 +324,11 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
324
324
|
const functionStack = [];
|
|
325
325
|
const setupFunctionRef = { current: eff.O.none() };
|
|
326
326
|
const setupFunctionIdentifiers = [];
|
|
327
|
-
const
|
|
328
|
-
const
|
|
329
|
-
const
|
|
330
|
-
const
|
|
331
|
-
const
|
|
327
|
+
const indFunctionCalls = [];
|
|
328
|
+
const indSetStateCalls = /* @__PURE__ */ new WeakMap();
|
|
329
|
+
const indSetStateCallsInUseLayoutEffectArg0 = /* @__PURE__ */ new WeakMap();
|
|
330
|
+
const indSetStateCallsInUseLayoutEffectSetup = /* @__PURE__ */ new Map();
|
|
331
|
+
const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
|
|
332
332
|
const onSetupFunctionEnter = (node) => {
|
|
333
333
|
setupFunctionRef.current = eff.O.some(node);
|
|
334
334
|
};
|
|
@@ -336,10 +336,10 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
336
336
|
setupFunctionRef.current = eff.O.filter(setupFunctionRef.current, (current) => current !== node);
|
|
337
337
|
};
|
|
338
338
|
function isSetupFunction(node) {
|
|
339
|
-
return node.parent?.type === types.AST_NODE_TYPES.CallExpression && node.parent.callee !== node &&
|
|
339
|
+
return node.parent?.type === types.AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseLayoutEffectLikeCall(node.parent);
|
|
340
340
|
}
|
|
341
341
|
function getCallKind(node) {
|
|
342
|
-
return tsPattern.match(node).when(isUseStateCall2, () => "useState").when(
|
|
342
|
+
return tsPattern.match(node).when(isUseStateCall2, () => "useState").when(isUseLayoutEffectLikeCall, () => "useLayoutEffect").when(isSetStateCall, () => "setState").when(isThenCall, () => "then").otherwise(() => "other");
|
|
343
343
|
}
|
|
344
344
|
function getFunctionKind(node) {
|
|
345
345
|
return tsPattern.match(node).when(isSetupFunction, () => "setup").when(AST2__namespace.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
|
|
@@ -365,12 +365,12 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
365
365
|
const maybeVd = AST2__namespace.traverseUpGuard(node, isVariableDeclaratorFromHookCall);
|
|
366
366
|
if (eff.O.isSome(maybeVd)) {
|
|
367
367
|
const vd = maybeVd.value;
|
|
368
|
-
const calls2 =
|
|
369
|
-
|
|
368
|
+
const calls2 = indSetStateCallsInUseMemoOrCallback.get(vd.init) ?? [];
|
|
369
|
+
indSetStateCallsInUseMemoOrCallback.set(vd.init, [...calls2, node]);
|
|
370
370
|
return;
|
|
371
371
|
}
|
|
372
|
-
const calls =
|
|
373
|
-
|
|
372
|
+
const calls = indSetStateCalls.get(parentFn) ?? [];
|
|
373
|
+
indSetStateCalls.set(parentFn, [...calls, node]);
|
|
374
374
|
return;
|
|
375
375
|
}
|
|
376
376
|
context.report({
|
|
@@ -386,7 +386,7 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
386
386
|
}).with("other", () => {
|
|
387
387
|
const isInSetupFunction = effectFn === parentFn;
|
|
388
388
|
if (!isInSetupFunction) return;
|
|
389
|
-
|
|
389
|
+
indFunctionCalls.push(node);
|
|
390
390
|
}).otherwise(eff.F.constVoid);
|
|
391
391
|
},
|
|
392
392
|
Identifier(node) {
|
|
@@ -400,8 +400,8 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
400
400
|
const maybeVd = AST2__namespace.traverseUpGuard(parent, isVariableDeclaratorFromHookCall);
|
|
401
401
|
if (eff.O.isNone(maybeVd)) break;
|
|
402
402
|
const vd = maybeVd.value;
|
|
403
|
-
const calls =
|
|
404
|
-
|
|
403
|
+
const calls = indSetStateCallsInUseLayoutEffectArg0.get(vd.init) ?? [];
|
|
404
|
+
indSetStateCallsInUseLayoutEffectArg0.set(vd.init, [...calls, node]);
|
|
405
405
|
break;
|
|
406
406
|
}
|
|
407
407
|
case types.AST_NODE_TYPES.CallExpression: {
|
|
@@ -411,12 +411,12 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
411
411
|
const maybeVd = AST2__namespace.traverseUpGuard(node.parent, isVariableDeclaratorFromHookCall);
|
|
412
412
|
if (eff.O.isNone(maybeVd)) break;
|
|
413
413
|
const vd = maybeVd.value;
|
|
414
|
-
const calls =
|
|
415
|
-
|
|
414
|
+
const calls = indSetStateCallsInUseLayoutEffectArg0.get(vd.init) ?? [];
|
|
415
|
+
indSetStateCallsInUseLayoutEffectArg0.set(vd.init, [...calls, node]);
|
|
416
416
|
}
|
|
417
|
-
if (
|
|
418
|
-
const calls =
|
|
419
|
-
|
|
417
|
+
if (isUseLayoutEffectLikeCall(node.parent)) {
|
|
418
|
+
const calls = indSetStateCallsInUseLayoutEffectArg0.get(node.parent) ?? [];
|
|
419
|
+
indSetStateCallsInUseLayoutEffectSetup.set(node.parent, [...calls, node]);
|
|
420
420
|
}
|
|
421
421
|
break;
|
|
422
422
|
}
|
|
@@ -429,13 +429,13 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
429
429
|
case types.AST_NODE_TYPES.ArrowFunctionExpression:
|
|
430
430
|
case types.AST_NODE_TYPES.FunctionDeclaration:
|
|
431
431
|
case types.AST_NODE_TYPES.FunctionExpression:
|
|
432
|
-
return
|
|
432
|
+
return indSetStateCalls.get(node) ?? [];
|
|
433
433
|
case types.AST_NODE_TYPES.CallExpression:
|
|
434
|
-
return
|
|
434
|
+
return indSetStateCallsInUseMemoOrCallback.get(node) ?? indSetStateCallsInUseLayoutEffectArg0.get(node) ?? [];
|
|
435
435
|
}
|
|
436
436
|
return [];
|
|
437
437
|
};
|
|
438
|
-
for (const [_, calls] of
|
|
438
|
+
for (const [_, calls] of indSetStateCallsInUseLayoutEffectSetup) {
|
|
439
439
|
for (const call of calls) {
|
|
440
440
|
context.report({
|
|
441
441
|
messageId: "noDirectSetStateInUseLayoutEffect",
|
|
@@ -444,7 +444,7 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
444
444
|
});
|
|
445
445
|
}
|
|
446
446
|
}
|
|
447
|
-
for (const { callee } of
|
|
447
|
+
for (const { callee } of indFunctionCalls) {
|
|
448
448
|
if (!("name" in callee)) continue;
|
|
449
449
|
const { name: name2 } = callee;
|
|
450
450
|
const setStateCalls = getSetStateCalls(name2, context.sourceCode.getScope(callee));
|
|
@@ -494,7 +494,7 @@ var no_unnecessary_use_callback_default = createRule({
|
|
|
494
494
|
name: RULE_NAME3,
|
|
495
495
|
create(context) {
|
|
496
496
|
if (!context.sourceCode.text.includes("use")) return {};
|
|
497
|
-
const alias = shared.
|
|
497
|
+
const alias = shared.getSettingsFromContext(context).additionalHooks?.useCallback ?? [];
|
|
498
498
|
return {
|
|
499
499
|
CallExpression(node) {
|
|
500
500
|
if (!core.isReactHookCall(node)) return;
|
|
@@ -563,7 +563,7 @@ var no_unnecessary_use_memo_default = createRule({
|
|
|
563
563
|
name: RULE_NAME4,
|
|
564
564
|
create(context) {
|
|
565
565
|
if (!context.sourceCode.text.includes("use")) return {};
|
|
566
|
-
const alias = shared.
|
|
566
|
+
const alias = shared.getSettingsFromContext(context).additionalHooks?.useMemo ?? [];
|
|
567
567
|
return {
|
|
568
568
|
CallExpression(node) {
|
|
569
569
|
if (!core.isReactHookCall(node)) return;
|
|
@@ -682,7 +682,7 @@ var prefer_use_state_lazy_initialization_default = createRule({
|
|
|
682
682
|
name: RULE_NAME6,
|
|
683
683
|
create(context) {
|
|
684
684
|
if (!context.sourceCode.text.includes("use")) return {};
|
|
685
|
-
const alias = shared.
|
|
685
|
+
const alias = shared.getSettingsFromContext(context).additionalHooks?.useState ?? [];
|
|
686
686
|
return {
|
|
687
687
|
CallExpression(node) {
|
|
688
688
|
if (!core.isReactHookCall(node)) return;
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import * as AST2 from '@eslint-react/ast';
|
|
2
2
|
import { isReactHookCallWithNameAlias, isReactHookCall, isUseCallbackCall, isReactHookCallWithNameLoose, isUseMemoCall, useHookCollector, isUseStateCall, isReactHookName } from '@eslint-react/core';
|
|
3
3
|
import { O, F } from '@eslint-react/eff';
|
|
4
|
-
import { createRuleForPlugin,
|
|
4
|
+
import { createRuleForPlugin, getSettingsFromContext } from '@eslint-react/shared';
|
|
5
5
|
import * as VAR5 from '@eslint-react/var';
|
|
6
6
|
import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
7
7
|
import { match, isMatching } from 'ts-pattern';
|
|
8
8
|
|
|
9
9
|
// package.json
|
|
10
10
|
var name = "eslint-plugin-react-hooks-extra";
|
|
11
|
-
var version = "1.23.
|
|
11
|
+
var version = "1.23.2-beta.2";
|
|
12
12
|
var createRule = createRuleForPlugin("hooks-extra");
|
|
13
13
|
function isFromHookCall(name2, context, settings, predicate = F.constTrue) {
|
|
14
14
|
const hookAlias = settings.additionalHooks?.[name2] ?? [];
|
|
@@ -105,7 +105,7 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
105
105
|
name: RULE_NAME,
|
|
106
106
|
create(context) {
|
|
107
107
|
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
|
|
108
|
-
const settings =
|
|
108
|
+
const settings = getSettingsFromContext(context);
|
|
109
109
|
const additionalHooks = settings.additionalHooks ?? {};
|
|
110
110
|
const isUseEffectLikeCall = isReactHookCallWithNameAlias("useEffect", context, additionalHooks.useEffect ?? []);
|
|
111
111
|
const isUseStateCall2 = isReactHookCallWithNameAlias("useState", context, additionalHooks.useState ?? []);
|
|
@@ -116,11 +116,11 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
116
116
|
const functionStack = [];
|
|
117
117
|
const setupFunctionRef = { current: O.none() };
|
|
118
118
|
const setupFunctionIdentifiers = [];
|
|
119
|
-
const
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
const
|
|
119
|
+
const indFunctionCalls = [];
|
|
120
|
+
const indSetStateCalls = /* @__PURE__ */ new WeakMap();
|
|
121
|
+
const indSetStateCallsInUseEffectArg0 = /* @__PURE__ */ new WeakMap();
|
|
122
|
+
const indSetStateCallsInUseEffectSetup = /* @__PURE__ */ new Map();
|
|
123
|
+
const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
|
|
124
124
|
const onSetupFunctionEnter = (node) => {
|
|
125
125
|
setupFunctionRef.current = O.some(node);
|
|
126
126
|
};
|
|
@@ -157,12 +157,12 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
157
157
|
const maybeVd = AST2.traverseUpGuard(node, isVariableDeclaratorFromHookCall);
|
|
158
158
|
if (O.isSome(maybeVd)) {
|
|
159
159
|
const vd = maybeVd.value;
|
|
160
|
-
const calls2 =
|
|
161
|
-
|
|
160
|
+
const calls2 = indSetStateCallsInUseMemoOrCallback.get(vd.init) ?? [];
|
|
161
|
+
indSetStateCallsInUseMemoOrCallback.set(vd.init, [...calls2, node]);
|
|
162
162
|
return;
|
|
163
163
|
}
|
|
164
|
-
const calls =
|
|
165
|
-
|
|
164
|
+
const calls = indSetStateCalls.get(parentFn) ?? [];
|
|
165
|
+
indSetStateCalls.set(parentFn, [...calls, node]);
|
|
166
166
|
return;
|
|
167
167
|
}
|
|
168
168
|
context.report({
|
|
@@ -178,7 +178,7 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
178
178
|
}).with("other", () => {
|
|
179
179
|
const isInSetupFunction = effectFn === parentFn;
|
|
180
180
|
if (!isInSetupFunction) return;
|
|
181
|
-
|
|
181
|
+
indFunctionCalls.push(node);
|
|
182
182
|
}).otherwise(F.constVoid);
|
|
183
183
|
},
|
|
184
184
|
Identifier(node) {
|
|
@@ -192,8 +192,8 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
192
192
|
const maybeVd = AST2.traverseUpGuard(parent, isVariableDeclaratorFromHookCall);
|
|
193
193
|
if (O.isNone(maybeVd)) break;
|
|
194
194
|
const vd = maybeVd.value;
|
|
195
|
-
const calls =
|
|
196
|
-
|
|
195
|
+
const calls = indSetStateCallsInUseEffectArg0.get(vd.init) ?? [];
|
|
196
|
+
indSetStateCallsInUseEffectArg0.set(vd.init, [...calls, node]);
|
|
197
197
|
break;
|
|
198
198
|
}
|
|
199
199
|
case AST_NODE_TYPES.CallExpression: {
|
|
@@ -203,12 +203,12 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
203
203
|
const maybeVd = AST2.traverseUpGuard(node.parent, isVariableDeclaratorFromHookCall);
|
|
204
204
|
if (O.isNone(maybeVd)) break;
|
|
205
205
|
const vd = maybeVd.value;
|
|
206
|
-
const calls =
|
|
207
|
-
|
|
206
|
+
const calls = indSetStateCallsInUseEffectArg0.get(vd.init) ?? [];
|
|
207
|
+
indSetStateCallsInUseEffectArg0.set(vd.init, [...calls, node]);
|
|
208
208
|
}
|
|
209
209
|
if (isUseEffectLikeCall(node.parent)) {
|
|
210
|
-
const calls =
|
|
211
|
-
|
|
210
|
+
const calls = indSetStateCallsInUseEffectArg0.get(node.parent) ?? [];
|
|
211
|
+
indSetStateCallsInUseEffectSetup.set(node.parent, [...calls, node]);
|
|
212
212
|
}
|
|
213
213
|
break;
|
|
214
214
|
}
|
|
@@ -221,13 +221,13 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
221
221
|
case AST_NODE_TYPES.ArrowFunctionExpression:
|
|
222
222
|
case AST_NODE_TYPES.FunctionDeclaration:
|
|
223
223
|
case AST_NODE_TYPES.FunctionExpression:
|
|
224
|
-
return
|
|
224
|
+
return indSetStateCalls.get(node) ?? [];
|
|
225
225
|
case AST_NODE_TYPES.CallExpression:
|
|
226
|
-
return
|
|
226
|
+
return indSetStateCallsInUseMemoOrCallback.get(node) ?? indSetStateCallsInUseEffectArg0.get(node) ?? [];
|
|
227
227
|
}
|
|
228
228
|
return [];
|
|
229
229
|
};
|
|
230
|
-
for (const [_, calls] of
|
|
230
|
+
for (const [_, calls] of indSetStateCallsInUseEffectSetup) {
|
|
231
231
|
for (const call of calls) {
|
|
232
232
|
context.report({
|
|
233
233
|
messageId: "noDirectSetStateInUseEffect",
|
|
@@ -236,7 +236,7 @@ var no_direct_set_state_in_use_effect_default = createRule({
|
|
|
236
236
|
});
|
|
237
237
|
}
|
|
238
238
|
}
|
|
239
|
-
for (const { callee } of
|
|
239
|
+
for (const { callee } of indFunctionCalls) {
|
|
240
240
|
if (!("name" in callee)) continue;
|
|
241
241
|
const { name: name2 } = callee;
|
|
242
242
|
const setStateCalls = getSetStateCalls(name2, context.sourceCode.getScope(callee));
|
|
@@ -286,9 +286,9 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
286
286
|
name: RULE_NAME2,
|
|
287
287
|
create(context) {
|
|
288
288
|
if (!/use\w*Effect/u.test(context.sourceCode.text)) return {};
|
|
289
|
-
const settings =
|
|
289
|
+
const settings = getSettingsFromContext(context);
|
|
290
290
|
const additionalHooks = settings.additionalHooks ?? {};
|
|
291
|
-
const
|
|
291
|
+
const isUseLayoutEffectLikeCall = isReactHookCallWithNameAlias(
|
|
292
292
|
"useLayoutEffect",
|
|
293
293
|
context,
|
|
294
294
|
additionalHooks.useLayoutEffect ?? []
|
|
@@ -301,11 +301,11 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
301
301
|
const functionStack = [];
|
|
302
302
|
const setupFunctionRef = { current: O.none() };
|
|
303
303
|
const setupFunctionIdentifiers = [];
|
|
304
|
-
const
|
|
305
|
-
const
|
|
306
|
-
const
|
|
307
|
-
const
|
|
308
|
-
const
|
|
304
|
+
const indFunctionCalls = [];
|
|
305
|
+
const indSetStateCalls = /* @__PURE__ */ new WeakMap();
|
|
306
|
+
const indSetStateCallsInUseLayoutEffectArg0 = /* @__PURE__ */ new WeakMap();
|
|
307
|
+
const indSetStateCallsInUseLayoutEffectSetup = /* @__PURE__ */ new Map();
|
|
308
|
+
const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
|
|
309
309
|
const onSetupFunctionEnter = (node) => {
|
|
310
310
|
setupFunctionRef.current = O.some(node);
|
|
311
311
|
};
|
|
@@ -313,10 +313,10 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
313
313
|
setupFunctionRef.current = O.filter(setupFunctionRef.current, (current) => current !== node);
|
|
314
314
|
};
|
|
315
315
|
function isSetupFunction(node) {
|
|
316
|
-
return node.parent?.type === AST_NODE_TYPES.CallExpression && node.parent.callee !== node &&
|
|
316
|
+
return node.parent?.type === AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseLayoutEffectLikeCall(node.parent);
|
|
317
317
|
}
|
|
318
318
|
function getCallKind(node) {
|
|
319
|
-
return match(node).when(isUseStateCall2, () => "useState").when(
|
|
319
|
+
return match(node).when(isUseStateCall2, () => "useState").when(isUseLayoutEffectLikeCall, () => "useLayoutEffect").when(isSetStateCall, () => "setState").when(isThenCall, () => "then").otherwise(() => "other");
|
|
320
320
|
}
|
|
321
321
|
function getFunctionKind(node) {
|
|
322
322
|
return match(node).when(isSetupFunction, () => "setup").when(AST2.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
|
|
@@ -342,12 +342,12 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
342
342
|
const maybeVd = AST2.traverseUpGuard(node, isVariableDeclaratorFromHookCall);
|
|
343
343
|
if (O.isSome(maybeVd)) {
|
|
344
344
|
const vd = maybeVd.value;
|
|
345
|
-
const calls2 =
|
|
346
|
-
|
|
345
|
+
const calls2 = indSetStateCallsInUseMemoOrCallback.get(vd.init) ?? [];
|
|
346
|
+
indSetStateCallsInUseMemoOrCallback.set(vd.init, [...calls2, node]);
|
|
347
347
|
return;
|
|
348
348
|
}
|
|
349
|
-
const calls =
|
|
350
|
-
|
|
349
|
+
const calls = indSetStateCalls.get(parentFn) ?? [];
|
|
350
|
+
indSetStateCalls.set(parentFn, [...calls, node]);
|
|
351
351
|
return;
|
|
352
352
|
}
|
|
353
353
|
context.report({
|
|
@@ -363,7 +363,7 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
363
363
|
}).with("other", () => {
|
|
364
364
|
const isInSetupFunction = effectFn === parentFn;
|
|
365
365
|
if (!isInSetupFunction) return;
|
|
366
|
-
|
|
366
|
+
indFunctionCalls.push(node);
|
|
367
367
|
}).otherwise(F.constVoid);
|
|
368
368
|
},
|
|
369
369
|
Identifier(node) {
|
|
@@ -377,8 +377,8 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
377
377
|
const maybeVd = AST2.traverseUpGuard(parent, isVariableDeclaratorFromHookCall);
|
|
378
378
|
if (O.isNone(maybeVd)) break;
|
|
379
379
|
const vd = maybeVd.value;
|
|
380
|
-
const calls =
|
|
381
|
-
|
|
380
|
+
const calls = indSetStateCallsInUseLayoutEffectArg0.get(vd.init) ?? [];
|
|
381
|
+
indSetStateCallsInUseLayoutEffectArg0.set(vd.init, [...calls, node]);
|
|
382
382
|
break;
|
|
383
383
|
}
|
|
384
384
|
case AST_NODE_TYPES.CallExpression: {
|
|
@@ -388,12 +388,12 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
388
388
|
const maybeVd = AST2.traverseUpGuard(node.parent, isVariableDeclaratorFromHookCall);
|
|
389
389
|
if (O.isNone(maybeVd)) break;
|
|
390
390
|
const vd = maybeVd.value;
|
|
391
|
-
const calls =
|
|
392
|
-
|
|
391
|
+
const calls = indSetStateCallsInUseLayoutEffectArg0.get(vd.init) ?? [];
|
|
392
|
+
indSetStateCallsInUseLayoutEffectArg0.set(vd.init, [...calls, node]);
|
|
393
393
|
}
|
|
394
|
-
if (
|
|
395
|
-
const calls =
|
|
396
|
-
|
|
394
|
+
if (isUseLayoutEffectLikeCall(node.parent)) {
|
|
395
|
+
const calls = indSetStateCallsInUseLayoutEffectArg0.get(node.parent) ?? [];
|
|
396
|
+
indSetStateCallsInUseLayoutEffectSetup.set(node.parent, [...calls, node]);
|
|
397
397
|
}
|
|
398
398
|
break;
|
|
399
399
|
}
|
|
@@ -406,13 +406,13 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
406
406
|
case AST_NODE_TYPES.ArrowFunctionExpression:
|
|
407
407
|
case AST_NODE_TYPES.FunctionDeclaration:
|
|
408
408
|
case AST_NODE_TYPES.FunctionExpression:
|
|
409
|
-
return
|
|
409
|
+
return indSetStateCalls.get(node) ?? [];
|
|
410
410
|
case AST_NODE_TYPES.CallExpression:
|
|
411
|
-
return
|
|
411
|
+
return indSetStateCallsInUseMemoOrCallback.get(node) ?? indSetStateCallsInUseLayoutEffectArg0.get(node) ?? [];
|
|
412
412
|
}
|
|
413
413
|
return [];
|
|
414
414
|
};
|
|
415
|
-
for (const [_, calls] of
|
|
415
|
+
for (const [_, calls] of indSetStateCallsInUseLayoutEffectSetup) {
|
|
416
416
|
for (const call of calls) {
|
|
417
417
|
context.report({
|
|
418
418
|
messageId: "noDirectSetStateInUseLayoutEffect",
|
|
@@ -421,7 +421,7 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
|
|
|
421
421
|
});
|
|
422
422
|
}
|
|
423
423
|
}
|
|
424
|
-
for (const { callee } of
|
|
424
|
+
for (const { callee } of indFunctionCalls) {
|
|
425
425
|
if (!("name" in callee)) continue;
|
|
426
426
|
const { name: name2 } = callee;
|
|
427
427
|
const setStateCalls = getSetStateCalls(name2, context.sourceCode.getScope(callee));
|
|
@@ -471,7 +471,7 @@ var no_unnecessary_use_callback_default = createRule({
|
|
|
471
471
|
name: RULE_NAME3,
|
|
472
472
|
create(context) {
|
|
473
473
|
if (!context.sourceCode.text.includes("use")) return {};
|
|
474
|
-
const alias =
|
|
474
|
+
const alias = getSettingsFromContext(context).additionalHooks?.useCallback ?? [];
|
|
475
475
|
return {
|
|
476
476
|
CallExpression(node) {
|
|
477
477
|
if (!isReactHookCall(node)) return;
|
|
@@ -540,7 +540,7 @@ var no_unnecessary_use_memo_default = createRule({
|
|
|
540
540
|
name: RULE_NAME4,
|
|
541
541
|
create(context) {
|
|
542
542
|
if (!context.sourceCode.text.includes("use")) return {};
|
|
543
|
-
const alias =
|
|
543
|
+
const alias = getSettingsFromContext(context).additionalHooks?.useMemo ?? [];
|
|
544
544
|
return {
|
|
545
545
|
CallExpression(node) {
|
|
546
546
|
if (!isReactHookCall(node)) return;
|
|
@@ -659,7 +659,7 @@ var prefer_use_state_lazy_initialization_default = createRule({
|
|
|
659
659
|
name: RULE_NAME6,
|
|
660
660
|
create(context) {
|
|
661
661
|
if (!context.sourceCode.text.includes("use")) return {};
|
|
662
|
-
const alias =
|
|
662
|
+
const alias = getSettingsFromContext(context).additionalHooks?.useState ?? [];
|
|
663
663
|
return {
|
|
664
664
|
CallExpression(node) {
|
|
665
665
|
if (!isReactHookCall(node)) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-react-hooks-extra",
|
|
3
|
-
"version": "1.23.
|
|
3
|
+
"version": "1.23.2-beta.2",
|
|
4
4
|
"description": "ESLint React's ESLint plugin for React Hooks related rules.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -48,13 +48,13 @@
|
|
|
48
48
|
"@typescript-eslint/utils": "^8.19.0",
|
|
49
49
|
"string-ts": "^2.2.0",
|
|
50
50
|
"ts-pattern": "^5.6.0",
|
|
51
|
-
"@eslint-react/ast": "1.23.
|
|
52
|
-
"@eslint-react/
|
|
53
|
-
"@eslint-react/
|
|
54
|
-
"@eslint-react/jsx": "1.23.
|
|
55
|
-
"@eslint-react/
|
|
56
|
-
"@eslint-react/
|
|
57
|
-
"@eslint-react/
|
|
51
|
+
"@eslint-react/ast": "1.23.2-beta.2",
|
|
52
|
+
"@eslint-react/core": "1.23.2-beta.2",
|
|
53
|
+
"@eslint-react/eff": "1.23.2-beta.2",
|
|
54
|
+
"@eslint-react/jsx": "1.23.2-beta.2",
|
|
55
|
+
"@eslint-react/var": "1.23.2-beta.2",
|
|
56
|
+
"@eslint-react/types": "1.23.2-beta.2",
|
|
57
|
+
"@eslint-react/shared": "1.23.2-beta.2"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"@types/react": "^19.0.2",
|