eslint-plugin-react-hooks-extra 1.24.0-beta.0 → 1.24.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.js +49 -85
  2. package/dist/index.mjs +50 -86
  3. package/package.json +13 -14
package/dist/index.js CHANGED
@@ -31,10 +31,10 @@ 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.24.0-beta.0";
34
+ var version = "1.24.0-beta.16";
35
35
  var createRule = shared.createRuleForPlugin("hooks-extra");
36
36
  function isFromHookCall(name2, context, settings, predicate = eff.returnTrue) {
37
- const hookAlias = settings.additionalHooks?.[name2] ?? [];
37
+ const hookAlias = settings.additionalHooks[name2] ?? [];
38
38
  return (topLevelId) => {
39
39
  const variable = VAR5__namespace.findVariable(topLevelId, context.sourceCode.getScope(topLevelId));
40
40
  const variableNode = VAR5__namespace.getVariableNode(variable, 0);
@@ -74,7 +74,7 @@ function isSetFunctionCall(context, settings) {
74
74
  return false;
75
75
  }
76
76
  const indexScope = context.sourceCode.getScope(node);
77
- const indexValue = VAR5__namespace.toResolved({
77
+ const indexValue = VAR5__namespace.toStaticValue({
78
78
  kind: "lazy",
79
79
  node: index,
80
80
  initialScope: indexScope
@@ -94,7 +94,7 @@ function isSetFunctionCall(context, settings) {
94
94
  }
95
95
  const property = node.callee.property;
96
96
  const propertyScope = context.sourceCode.getScope(node);
97
- const propertyValue = VAR5__namespace.toResolved({
97
+ const propertyValue = VAR5__namespace.toStaticValue({
98
98
  kind: "lazy",
99
99
  node: property,
100
100
  initialScope: propertyScope
@@ -153,7 +153,7 @@ var no_direct_set_state_in_use_effect_default = createRule({
153
153
  return {};
154
154
  }
155
155
  const settings = shared.getSettingsFromContext(context);
156
- const additionalHooks = settings.additionalHooks ?? {};
156
+ const additionalHooks = settings.additionalHooks;
157
157
  const isUseEffectLikeCall = core.isReactHookCallWithNameAlias("useEffect", context, additionalHooks.useEffect ?? []);
158
158
  const isUseStateCall2 = core.isReactHookCallWithNameAlias("useState", context, additionalHooks.useState ?? []);
159
159
  const isUseMemoCall2 = core.isReactHookCallWithNameAlias("useMemo", context, additionalHooks.useMemo ?? []);
@@ -164,10 +164,10 @@ var no_direct_set_state_in_use_effect_default = createRule({
164
164
  const setupFunctionRef = { current: null };
165
165
  const setupFunctionIdentifiers = [];
166
166
  const indFunctionCalls = [];
167
- const indSetStateCalls = /* @__PURE__ */ new WeakMap();
168
- const indSetStateCallsInUseEffectArg0 = /* @__PURE__ */ new WeakMap();
167
+ const indSetStateCalls = /* @__PURE__ */ new Map();
168
+ const indSetStateCallsInUseEffectArg0 = /* @__PURE__ */ new Map();
169
169
  const indSetStateCallsInUseEffectSetup = /* @__PURE__ */ new Map();
170
- const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
170
+ const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new Map();
171
171
  const onSetupFunctionEnter = (node) => {
172
172
  setupFunctionRef.current = node;
173
173
  };
@@ -176,14 +176,14 @@ var no_direct_set_state_in_use_effect_default = createRule({
176
176
  setupFunctionRef.current = null;
177
177
  }
178
178
  };
179
- function isSetupFunction(node) {
179
+ function isFunctionOfUseEffectSetup(node) {
180
180
  return node.parent?.type === types.AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseEffectLikeCall(node.parent);
181
181
  }
182
182
  function getCallKind(node) {
183
183
  return tsPattern.match(node).when(isUseStateCall2, () => "useState").when(isUseEffectLikeCall, () => "useEffect").when(isSetStateCall, () => "setState").when(isThenCall, () => "then").otherwise(() => "other");
184
184
  }
185
185
  function getFunctionKind(node) {
186
- return tsPattern.match(node).when(isSetupFunction, () => "setup").when(AST__namespace.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
186
+ return tsPattern.match(node).when(isFunctionOfUseEffectSetup, () => "setup").when(AST__namespace.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
187
187
  }
188
188
  return {
189
189
  ":function"(node) {
@@ -203,13 +203,10 @@ var no_direct_set_state_in_use_effect_default = createRule({
203
203
  CallExpression(node) {
204
204
  const setupFunction = setupFunctionRef.current;
205
205
  const pEntry = functionEntries.at(-1);
206
- if (pEntry?.node.async) {
206
+ if (pEntry == null || pEntry.node.async) {
207
207
  return;
208
208
  }
209
209
  tsPattern.match(getCallKind(node)).with("setState", () => {
210
- if (pEntry == null) {
211
- return;
212
- }
213
210
  switch (true) {
214
211
  case (pEntry.node === setupFunction || pEntry.kind === "immediate"): {
215
212
  context.report({
@@ -222,26 +219,16 @@ var no_direct_set_state_in_use_effect_default = createRule({
222
219
  return;
223
220
  }
224
221
  default: {
225
- const variableDeclarator = AST__namespace.findParentNodeGuard(node, isVariableDeclaratorFromHookCall);
226
- if (variableDeclarator === eff._) {
227
- const calls = indSetStateCalls.get(pEntry.node) ?? [];
228
- indSetStateCalls.set(pEntry.node, [...calls, node]);
229
- return;
230
- }
231
- const init = variableDeclarator.init;
232
- const prevs = indSetStateCallsInUseMemoOrCallback.get(init) ?? [];
233
- indSetStateCallsInUseMemoOrCallback.set(init, [...prevs, node]);
222
+ const vd = AST__namespace.findParentNode(node, isVariableDeclaratorFromHookCall);
223
+ if (vd == null) eff.getOrUpdate(indSetStateCalls, pEntry.node, () => []).push(node);
224
+ else eff.getOrUpdate(indSetStateCallsInUseMemoOrCallback, vd.init, () => []).push(node);
234
225
  }
235
226
  }
236
227
  }).with("useEffect", () => {
237
- if (AST__namespace.isFunction(node.arguments.at(0))) {
238
- return;
239
- }
228
+ if (AST__namespace.isFunction(node.arguments.at(0))) return;
240
229
  setupFunctionIdentifiers.push(...AST__namespace.getNestedIdentifiers(node));
241
230
  }).with("other", () => {
242
- if (pEntry?.node !== setupFunction) {
243
- return;
244
- }
231
+ if (pEntry.node !== setupFunction) return;
245
232
  indFunctionCalls.push(node);
246
233
  }).otherwise(() => eff._);
247
234
  },
@@ -261,12 +248,10 @@ var no_direct_set_state_in_use_effect_default = createRule({
261
248
  if (!isUseMemoCall2(parent)) {
262
249
  break;
263
250
  }
264
- const variableDeclarator = AST__namespace.findParentNodeGuard(parent, isVariableDeclaratorFromHookCall);
265
- if (variableDeclarator == null) {
266
- break;
251
+ const vd = AST__namespace.findParentNode(parent, isVariableDeclaratorFromHookCall);
252
+ if (vd != null) {
253
+ eff.getOrUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
267
254
  }
268
- const calls = indSetStateCallsInUseEffectArg0.get(variableDeclarator.init) ?? [];
269
- indSetStateCallsInUseEffectArg0.set(variableDeclarator.init, [...calls, node]);
270
255
  break;
271
256
  }
272
257
  case types.AST_NODE_TYPES.CallExpression: {
@@ -274,18 +259,15 @@ var no_direct_set_state_in_use_effect_default = createRule({
274
259
  break;
275
260
  }
276
261
  if (isUseCallbackCall2(node.parent)) {
277
- const variableDeclarator = AST__namespace.findParentNodeGuard(node.parent, isVariableDeclaratorFromHookCall);
278
- if (variableDeclarator == null) {
279
- break;
262
+ const vd = AST__namespace.findParentNode(node.parent, isVariableDeclaratorFromHookCall);
263
+ if (vd != null) {
264
+ eff.getOrUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
280
265
  }
281
- const prevs = indSetStateCallsInUseEffectArg0.get(variableDeclarator.init) ?? [];
282
- indSetStateCallsInUseEffectArg0.set(variableDeclarator.init, [...prevs, node]);
266
+ break;
283
267
  }
284
268
  if (isUseEffectLikeCall(node.parent)) {
285
- const prevs = indSetStateCallsInUseEffectArg0.get(node.parent) ?? [];
286
- indSetStateCallsInUseEffectSetup.set(node.parent, [...prevs, node]);
269
+ eff.getOrUpdate(indSetStateCallsInUseEffectSetup, node.parent, () => []).push(node);
287
270
  }
288
- break;
289
271
  }
290
272
  }
291
273
  },
@@ -366,7 +348,7 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
366
348
  return {};
367
349
  }
368
350
  const settings = shared.getSettingsFromContext(context);
369
- const additionalHooks = settings.additionalHooks ?? {};
351
+ const additionalHooks = settings.additionalHooks;
370
352
  const isUseLayoutEffectLikeCall = core.isReactHookCallWithNameAlias(
371
353
  "useLayoutEffect",
372
354
  context,
@@ -381,10 +363,10 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
381
363
  const setupFunctionRef = { current: null };
382
364
  const setupFunctionIdentifiers = [];
383
365
  const indFunctionCalls = [];
384
- const indSetStateCalls = /* @__PURE__ */ new WeakMap();
385
- const indSetStateCallsInUseLayoutEffectArg0 = /* @__PURE__ */ new WeakMap();
366
+ const indSetStateCalls = /* @__PURE__ */ new Map();
367
+ const indSetStateCallsInUseLayoutEffectArg0 = /* @__PURE__ */ new Map();
386
368
  const indSetStateCallsInUseLayoutEffectSetup = /* @__PURE__ */ new Map();
387
- const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
369
+ const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new Map();
388
370
  const onSetupFunctionEnter = (node) => {
389
371
  setupFunctionRef.current = node;
390
372
  };
@@ -393,14 +375,14 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
393
375
  setupFunctionRef.current = null;
394
376
  }
395
377
  };
396
- function isSetupFunction(node) {
378
+ function isFunctionOfUseEffectSetup(node) {
397
379
  return node.parent?.type === types.AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseLayoutEffectLikeCall(node.parent);
398
380
  }
399
381
  function getCallKind(node) {
400
382
  return tsPattern.match(node).when(isUseStateCall2, () => "useState").when(isUseLayoutEffectLikeCall, () => "useLayoutEffect").when(isSetStateCall, () => "setState").when(isThenCall, () => "then").otherwise(() => "other");
401
383
  }
402
384
  function getFunctionKind(node) {
403
- return tsPattern.match(node).when(isSetupFunction, () => "setup").when(AST__namespace.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
385
+ return tsPattern.match(node).when(isFunctionOfUseEffectSetup, () => "setup").when(AST__namespace.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
404
386
  }
405
387
  return {
406
388
  ":function"(node) {
@@ -420,13 +402,10 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
420
402
  CallExpression(node) {
421
403
  const setupFunction = setupFunctionRef.current;
422
404
  const pEntry = functionEntries.at(-1);
423
- if (pEntry?.node.async) {
405
+ if (pEntry == null || pEntry.node.async) {
424
406
  return;
425
407
  }
426
408
  tsPattern.match(getCallKind(node)).with("setState", () => {
427
- if (pEntry == null) {
428
- return;
429
- }
430
409
  switch (true) {
431
410
  case (pEntry.node === setupFunction || pEntry.kind === "immediate"): {
432
411
  context.report({
@@ -439,26 +418,16 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
439
418
  return;
440
419
  }
441
420
  default: {
442
- const variableDeclarator = AST__namespace.findParentNodeGuard(node, isVariableDeclaratorFromHookCall);
443
- if (variableDeclarator === eff._) {
444
- const calls = indSetStateCalls.get(pEntry.node) ?? [];
445
- indSetStateCalls.set(pEntry.node, [...calls, node]);
446
- return;
447
- }
448
- const init = variableDeclarator.init;
449
- const prevs = indSetStateCallsInUseMemoOrCallback.get(init) ?? [];
450
- indSetStateCallsInUseMemoOrCallback.set(init, [...prevs, node]);
421
+ const vd = AST__namespace.findParentNode(node, isVariableDeclaratorFromHookCall);
422
+ if (vd == null) eff.getOrUpdate(indSetStateCalls, pEntry.node, () => []).push(node);
423
+ else eff.getOrUpdate(indSetStateCallsInUseMemoOrCallback, vd.init, () => []).push(node);
451
424
  }
452
425
  }
453
426
  }).with("useLayoutEffect", () => {
454
- if (AST__namespace.isFunction(node.arguments.at(0))) {
455
- return;
456
- }
427
+ if (AST__namespace.isFunction(node.arguments.at(0))) return;
457
428
  setupFunctionIdentifiers.push(...AST__namespace.getNestedIdentifiers(node));
458
429
  }).with("other", () => {
459
- if (pEntry?.node !== setupFunction) {
460
- return;
461
- }
430
+ if (pEntry.node !== setupFunction) return;
462
431
  indFunctionCalls.push(node);
463
432
  }).otherwise(() => eff._);
464
433
  },
@@ -478,12 +447,10 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
478
447
  if (!isUseMemoCall2(parent)) {
479
448
  break;
480
449
  }
481
- const variableDeclarator = AST__namespace.findParentNodeGuard(parent, isVariableDeclaratorFromHookCall);
482
- if (variableDeclarator == null) {
483
- break;
450
+ const vd = AST__namespace.findParentNode(parent, isVariableDeclaratorFromHookCall);
451
+ if (vd != null) {
452
+ eff.getOrUpdate(indSetStateCallsInUseLayoutEffectArg0, vd.init, () => []).push(node);
484
453
  }
485
- const calls = indSetStateCallsInUseLayoutEffectArg0.get(variableDeclarator.init) ?? [];
486
- indSetStateCallsInUseLayoutEffectArg0.set(variableDeclarator.init, [...calls, node]);
487
454
  break;
488
455
  }
489
456
  case types.AST_NODE_TYPES.CallExpression: {
@@ -491,18 +458,15 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
491
458
  break;
492
459
  }
493
460
  if (isUseCallbackCall2(node.parent)) {
494
- const variableDeclarator = AST__namespace.findParentNodeGuard(node.parent, isVariableDeclaratorFromHookCall);
495
- if (variableDeclarator == null) {
496
- break;
461
+ const vd = AST__namespace.findParentNode(node.parent, isVariableDeclaratorFromHookCall);
462
+ if (vd != null) {
463
+ eff.getOrUpdate(indSetStateCallsInUseLayoutEffectArg0, vd.init, () => []).push(node);
497
464
  }
498
- const prevs = indSetStateCallsInUseLayoutEffectArg0.get(variableDeclarator.init) ?? [];
499
- indSetStateCallsInUseLayoutEffectArg0.set(variableDeclarator.init, [...prevs, node]);
465
+ break;
500
466
  }
501
467
  if (isUseLayoutEffectLikeCall(node.parent)) {
502
- const prevs = indSetStateCallsInUseLayoutEffectArg0.get(node.parent) ?? [];
503
- indSetStateCallsInUseLayoutEffectSetup.set(node.parent, [...prevs, node]);
468
+ eff.getOrUpdate(indSetStateCallsInUseLayoutEffectSetup, node.parent, () => []).push(node);
504
469
  }
505
- break;
506
470
  }
507
471
  }
508
472
  },
@@ -582,7 +546,7 @@ var no_unnecessary_use_callback_default = createRule({
582
546
  if (!context.sourceCode.text.includes("use")) {
583
547
  return {};
584
548
  }
585
- const alias = shared.getSettingsFromContext(context).additionalHooks?.useCallback ?? [];
549
+ const alias = shared.getSettingsFromContext(context).additionalHooks.useCallback ?? [];
586
550
  return {
587
551
  CallExpression(node) {
588
552
  if (!core.isReactHookCall(node)) {
@@ -625,7 +589,7 @@ var no_unnecessary_use_callback_default = createRule({
625
589
  }
626
590
  return variableNode;
627
591
  }).otherwise(() => eff._);
628
- if (arg0Node === eff._) return;
592
+ if (arg0Node == null) return;
629
593
  const arg0NodeScope = context.sourceCode.getScope(arg0Node);
630
594
  const arg0NodeReferences = VAR5__namespace.getChidScopes(arg0NodeScope).flatMap((x) => x.references);
631
595
  const isReferencedToComponentScope = arg0NodeReferences.some((x) => x.resolved?.scope.block === component);
@@ -661,7 +625,7 @@ var no_unnecessary_use_memo_default = createRule({
661
625
  if (!context.sourceCode.text.includes("use")) {
662
626
  return {};
663
627
  }
664
- const alias = shared.getSettingsFromContext(context).additionalHooks?.useMemo ?? [];
628
+ const alias = shared.getSettingsFromContext(context).additionalHooks.useMemo ?? [];
665
629
  return {
666
630
  CallExpression(node) {
667
631
  if (!core.isReactHookCall(node)) {
@@ -708,7 +672,7 @@ var no_unnecessary_use_memo_default = createRule({
708
672
  }
709
673
  return variableNode;
710
674
  }).otherwise(() => eff._);
711
- if (arg0Node === eff._) return;
675
+ if (arg0Node == null) return;
712
676
  const arg0NodeScope = context.sourceCode.getScope(arg0Node);
713
677
  const arg0NodeReferences = VAR5__namespace.getChidScopes(arg0NodeScope).flatMap((x) => x.references);
714
678
  const isReferencedToComponentScope = arg0NodeReferences.some((x) => x.resolved?.scope.block === component);
@@ -794,7 +758,7 @@ var prefer_use_state_lazy_initialization_default = createRule({
794
758
  if (!context.sourceCode.text.includes("use")) {
795
759
  return {};
796
760
  }
797
- const alias = shared.getSettingsFromContext(context).additionalHooks?.useState ?? [];
761
+ const alias = shared.getSettingsFromContext(context).additionalHooks.useState ?? [];
798
762
  return {
799
763
  CallExpression(node) {
800
764
  if (!core.isReactHookCall(node)) {
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as AST from '@eslint-react/ast';
2
2
  import { isReactHookCallWithNameAlias, isReactHookCall, isUseCallbackCall, isReactHookCallWithNameLoose, isUseMemoCall, useHookCollector, isUseStateCall, isReactHookName } from '@eslint-react/core';
3
- import { _, identity, returnTrue } from '@eslint-react/eff';
3
+ import { getOrUpdate, _, identity, returnTrue } from '@eslint-react/eff';
4
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';
@@ -8,10 +8,10 @@ import { match } from 'ts-pattern';
8
8
 
9
9
  // package.json
10
10
  var name = "eslint-plugin-react-hooks-extra";
11
- var version = "1.24.0-beta.0";
11
+ var version = "1.24.0-beta.16";
12
12
  var createRule = createRuleForPlugin("hooks-extra");
13
13
  function isFromHookCall(name2, context, settings, predicate = returnTrue) {
14
- const hookAlias = settings.additionalHooks?.[name2] ?? [];
14
+ const hookAlias = settings.additionalHooks[name2] ?? [];
15
15
  return (topLevelId) => {
16
16
  const variable = VAR5.findVariable(topLevelId, context.sourceCode.getScope(topLevelId));
17
17
  const variableNode = VAR5.getVariableNode(variable, 0);
@@ -51,7 +51,7 @@ function isSetFunctionCall(context, settings) {
51
51
  return false;
52
52
  }
53
53
  const indexScope = context.sourceCode.getScope(node);
54
- const indexValue = VAR5.toResolved({
54
+ const indexValue = VAR5.toStaticValue({
55
55
  kind: "lazy",
56
56
  node: index,
57
57
  initialScope: indexScope
@@ -71,7 +71,7 @@ function isSetFunctionCall(context, settings) {
71
71
  }
72
72
  const property = node.callee.property;
73
73
  const propertyScope = context.sourceCode.getScope(node);
74
- const propertyValue = VAR5.toResolved({
74
+ const propertyValue = VAR5.toStaticValue({
75
75
  kind: "lazy",
76
76
  node: property,
77
77
  initialScope: propertyScope
@@ -130,7 +130,7 @@ var no_direct_set_state_in_use_effect_default = createRule({
130
130
  return {};
131
131
  }
132
132
  const settings = getSettingsFromContext(context);
133
- const additionalHooks = settings.additionalHooks ?? {};
133
+ const additionalHooks = settings.additionalHooks;
134
134
  const isUseEffectLikeCall = isReactHookCallWithNameAlias("useEffect", context, additionalHooks.useEffect ?? []);
135
135
  const isUseStateCall2 = isReactHookCallWithNameAlias("useState", context, additionalHooks.useState ?? []);
136
136
  const isUseMemoCall2 = isReactHookCallWithNameAlias("useMemo", context, additionalHooks.useMemo ?? []);
@@ -141,10 +141,10 @@ var no_direct_set_state_in_use_effect_default = createRule({
141
141
  const setupFunctionRef = { current: null };
142
142
  const setupFunctionIdentifiers = [];
143
143
  const indFunctionCalls = [];
144
- const indSetStateCalls = /* @__PURE__ */ new WeakMap();
145
- const indSetStateCallsInUseEffectArg0 = /* @__PURE__ */ new WeakMap();
144
+ const indSetStateCalls = /* @__PURE__ */ new Map();
145
+ const indSetStateCallsInUseEffectArg0 = /* @__PURE__ */ new Map();
146
146
  const indSetStateCallsInUseEffectSetup = /* @__PURE__ */ new Map();
147
- const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
147
+ const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new Map();
148
148
  const onSetupFunctionEnter = (node) => {
149
149
  setupFunctionRef.current = node;
150
150
  };
@@ -153,14 +153,14 @@ var no_direct_set_state_in_use_effect_default = createRule({
153
153
  setupFunctionRef.current = null;
154
154
  }
155
155
  };
156
- function isSetupFunction(node) {
156
+ function isFunctionOfUseEffectSetup(node) {
157
157
  return node.parent?.type === AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseEffectLikeCall(node.parent);
158
158
  }
159
159
  function getCallKind(node) {
160
160
  return match(node).when(isUseStateCall2, () => "useState").when(isUseEffectLikeCall, () => "useEffect").when(isSetStateCall, () => "setState").when(isThenCall, () => "then").otherwise(() => "other");
161
161
  }
162
162
  function getFunctionKind(node) {
163
- return match(node).when(isSetupFunction, () => "setup").when(AST.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
163
+ return match(node).when(isFunctionOfUseEffectSetup, () => "setup").when(AST.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
164
164
  }
165
165
  return {
166
166
  ":function"(node) {
@@ -180,13 +180,10 @@ var no_direct_set_state_in_use_effect_default = createRule({
180
180
  CallExpression(node) {
181
181
  const setupFunction = setupFunctionRef.current;
182
182
  const pEntry = functionEntries.at(-1);
183
- if (pEntry?.node.async) {
183
+ if (pEntry == null || pEntry.node.async) {
184
184
  return;
185
185
  }
186
186
  match(getCallKind(node)).with("setState", () => {
187
- if (pEntry == null) {
188
- return;
189
- }
190
187
  switch (true) {
191
188
  case (pEntry.node === setupFunction || pEntry.kind === "immediate"): {
192
189
  context.report({
@@ -199,26 +196,16 @@ var no_direct_set_state_in_use_effect_default = createRule({
199
196
  return;
200
197
  }
201
198
  default: {
202
- const variableDeclarator = AST.findParentNodeGuard(node, isVariableDeclaratorFromHookCall);
203
- if (variableDeclarator === _) {
204
- const calls = indSetStateCalls.get(pEntry.node) ?? [];
205
- indSetStateCalls.set(pEntry.node, [...calls, node]);
206
- return;
207
- }
208
- const init = variableDeclarator.init;
209
- const prevs = indSetStateCallsInUseMemoOrCallback.get(init) ?? [];
210
- indSetStateCallsInUseMemoOrCallback.set(init, [...prevs, node]);
199
+ const vd = AST.findParentNode(node, isVariableDeclaratorFromHookCall);
200
+ if (vd == null) getOrUpdate(indSetStateCalls, pEntry.node, () => []).push(node);
201
+ else getOrUpdate(indSetStateCallsInUseMemoOrCallback, vd.init, () => []).push(node);
211
202
  }
212
203
  }
213
204
  }).with("useEffect", () => {
214
- if (AST.isFunction(node.arguments.at(0))) {
215
- return;
216
- }
205
+ if (AST.isFunction(node.arguments.at(0))) return;
217
206
  setupFunctionIdentifiers.push(...AST.getNestedIdentifiers(node));
218
207
  }).with("other", () => {
219
- if (pEntry?.node !== setupFunction) {
220
- return;
221
- }
208
+ if (pEntry.node !== setupFunction) return;
222
209
  indFunctionCalls.push(node);
223
210
  }).otherwise(() => _);
224
211
  },
@@ -238,12 +225,10 @@ var no_direct_set_state_in_use_effect_default = createRule({
238
225
  if (!isUseMemoCall2(parent)) {
239
226
  break;
240
227
  }
241
- const variableDeclarator = AST.findParentNodeGuard(parent, isVariableDeclaratorFromHookCall);
242
- if (variableDeclarator == null) {
243
- break;
228
+ const vd = AST.findParentNode(parent, isVariableDeclaratorFromHookCall);
229
+ if (vd != null) {
230
+ getOrUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
244
231
  }
245
- const calls = indSetStateCallsInUseEffectArg0.get(variableDeclarator.init) ?? [];
246
- indSetStateCallsInUseEffectArg0.set(variableDeclarator.init, [...calls, node]);
247
232
  break;
248
233
  }
249
234
  case AST_NODE_TYPES.CallExpression: {
@@ -251,18 +236,15 @@ var no_direct_set_state_in_use_effect_default = createRule({
251
236
  break;
252
237
  }
253
238
  if (isUseCallbackCall2(node.parent)) {
254
- const variableDeclarator = AST.findParentNodeGuard(node.parent, isVariableDeclaratorFromHookCall);
255
- if (variableDeclarator == null) {
256
- break;
239
+ const vd = AST.findParentNode(node.parent, isVariableDeclaratorFromHookCall);
240
+ if (vd != null) {
241
+ getOrUpdate(indSetStateCallsInUseEffectArg0, vd.init, () => []).push(node);
257
242
  }
258
- const prevs = indSetStateCallsInUseEffectArg0.get(variableDeclarator.init) ?? [];
259
- indSetStateCallsInUseEffectArg0.set(variableDeclarator.init, [...prevs, node]);
243
+ break;
260
244
  }
261
245
  if (isUseEffectLikeCall(node.parent)) {
262
- const prevs = indSetStateCallsInUseEffectArg0.get(node.parent) ?? [];
263
- indSetStateCallsInUseEffectSetup.set(node.parent, [...prevs, node]);
246
+ getOrUpdate(indSetStateCallsInUseEffectSetup, node.parent, () => []).push(node);
264
247
  }
265
- break;
266
248
  }
267
249
  }
268
250
  },
@@ -343,7 +325,7 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
343
325
  return {};
344
326
  }
345
327
  const settings = getSettingsFromContext(context);
346
- const additionalHooks = settings.additionalHooks ?? {};
328
+ const additionalHooks = settings.additionalHooks;
347
329
  const isUseLayoutEffectLikeCall = isReactHookCallWithNameAlias(
348
330
  "useLayoutEffect",
349
331
  context,
@@ -358,10 +340,10 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
358
340
  const setupFunctionRef = { current: null };
359
341
  const setupFunctionIdentifiers = [];
360
342
  const indFunctionCalls = [];
361
- const indSetStateCalls = /* @__PURE__ */ new WeakMap();
362
- const indSetStateCallsInUseLayoutEffectArg0 = /* @__PURE__ */ new WeakMap();
343
+ const indSetStateCalls = /* @__PURE__ */ new Map();
344
+ const indSetStateCallsInUseLayoutEffectArg0 = /* @__PURE__ */ new Map();
363
345
  const indSetStateCallsInUseLayoutEffectSetup = /* @__PURE__ */ new Map();
364
- const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new WeakMap();
346
+ const indSetStateCallsInUseMemoOrCallback = /* @__PURE__ */ new Map();
365
347
  const onSetupFunctionEnter = (node) => {
366
348
  setupFunctionRef.current = node;
367
349
  };
@@ -370,14 +352,14 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
370
352
  setupFunctionRef.current = null;
371
353
  }
372
354
  };
373
- function isSetupFunction(node) {
355
+ function isFunctionOfUseEffectSetup(node) {
374
356
  return node.parent?.type === AST_NODE_TYPES.CallExpression && node.parent.callee !== node && isUseLayoutEffectLikeCall(node.parent);
375
357
  }
376
358
  function getCallKind(node) {
377
359
  return match(node).when(isUseStateCall2, () => "useState").when(isUseLayoutEffectLikeCall, () => "useLayoutEffect").when(isSetStateCall, () => "setState").when(isThenCall, () => "then").otherwise(() => "other");
378
360
  }
379
361
  function getFunctionKind(node) {
380
- return match(node).when(isSetupFunction, () => "setup").when(AST.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
362
+ return match(node).when(isFunctionOfUseEffectSetup, () => "setup").when(AST.isFunctionOfImmediatelyInvoked, () => "immediate").otherwise(() => "other");
381
363
  }
382
364
  return {
383
365
  ":function"(node) {
@@ -397,13 +379,10 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
397
379
  CallExpression(node) {
398
380
  const setupFunction = setupFunctionRef.current;
399
381
  const pEntry = functionEntries.at(-1);
400
- if (pEntry?.node.async) {
382
+ if (pEntry == null || pEntry.node.async) {
401
383
  return;
402
384
  }
403
385
  match(getCallKind(node)).with("setState", () => {
404
- if (pEntry == null) {
405
- return;
406
- }
407
386
  switch (true) {
408
387
  case (pEntry.node === setupFunction || pEntry.kind === "immediate"): {
409
388
  context.report({
@@ -416,26 +395,16 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
416
395
  return;
417
396
  }
418
397
  default: {
419
- const variableDeclarator = AST.findParentNodeGuard(node, isVariableDeclaratorFromHookCall);
420
- if (variableDeclarator === _) {
421
- const calls = indSetStateCalls.get(pEntry.node) ?? [];
422
- indSetStateCalls.set(pEntry.node, [...calls, node]);
423
- return;
424
- }
425
- const init = variableDeclarator.init;
426
- const prevs = indSetStateCallsInUseMemoOrCallback.get(init) ?? [];
427
- indSetStateCallsInUseMemoOrCallback.set(init, [...prevs, node]);
398
+ const vd = AST.findParentNode(node, isVariableDeclaratorFromHookCall);
399
+ if (vd == null) getOrUpdate(indSetStateCalls, pEntry.node, () => []).push(node);
400
+ else getOrUpdate(indSetStateCallsInUseMemoOrCallback, vd.init, () => []).push(node);
428
401
  }
429
402
  }
430
403
  }).with("useLayoutEffect", () => {
431
- if (AST.isFunction(node.arguments.at(0))) {
432
- return;
433
- }
404
+ if (AST.isFunction(node.arguments.at(0))) return;
434
405
  setupFunctionIdentifiers.push(...AST.getNestedIdentifiers(node));
435
406
  }).with("other", () => {
436
- if (pEntry?.node !== setupFunction) {
437
- return;
438
- }
407
+ if (pEntry.node !== setupFunction) return;
439
408
  indFunctionCalls.push(node);
440
409
  }).otherwise(() => _);
441
410
  },
@@ -455,12 +424,10 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
455
424
  if (!isUseMemoCall2(parent)) {
456
425
  break;
457
426
  }
458
- const variableDeclarator = AST.findParentNodeGuard(parent, isVariableDeclaratorFromHookCall);
459
- if (variableDeclarator == null) {
460
- break;
427
+ const vd = AST.findParentNode(parent, isVariableDeclaratorFromHookCall);
428
+ if (vd != null) {
429
+ getOrUpdate(indSetStateCallsInUseLayoutEffectArg0, vd.init, () => []).push(node);
461
430
  }
462
- const calls = indSetStateCallsInUseLayoutEffectArg0.get(variableDeclarator.init) ?? [];
463
- indSetStateCallsInUseLayoutEffectArg0.set(variableDeclarator.init, [...calls, node]);
464
431
  break;
465
432
  }
466
433
  case AST_NODE_TYPES.CallExpression: {
@@ -468,18 +435,15 @@ var no_direct_set_state_in_use_layout_effect_default = createRule({
468
435
  break;
469
436
  }
470
437
  if (isUseCallbackCall2(node.parent)) {
471
- const variableDeclarator = AST.findParentNodeGuard(node.parent, isVariableDeclaratorFromHookCall);
472
- if (variableDeclarator == null) {
473
- break;
438
+ const vd = AST.findParentNode(node.parent, isVariableDeclaratorFromHookCall);
439
+ if (vd != null) {
440
+ getOrUpdate(indSetStateCallsInUseLayoutEffectArg0, vd.init, () => []).push(node);
474
441
  }
475
- const prevs = indSetStateCallsInUseLayoutEffectArg0.get(variableDeclarator.init) ?? [];
476
- indSetStateCallsInUseLayoutEffectArg0.set(variableDeclarator.init, [...prevs, node]);
442
+ break;
477
443
  }
478
444
  if (isUseLayoutEffectLikeCall(node.parent)) {
479
- const prevs = indSetStateCallsInUseLayoutEffectArg0.get(node.parent) ?? [];
480
- indSetStateCallsInUseLayoutEffectSetup.set(node.parent, [...prevs, node]);
445
+ getOrUpdate(indSetStateCallsInUseLayoutEffectSetup, node.parent, () => []).push(node);
481
446
  }
482
- break;
483
447
  }
484
448
  }
485
449
  },
@@ -559,7 +523,7 @@ var no_unnecessary_use_callback_default = createRule({
559
523
  if (!context.sourceCode.text.includes("use")) {
560
524
  return {};
561
525
  }
562
- const alias = getSettingsFromContext(context).additionalHooks?.useCallback ?? [];
526
+ const alias = getSettingsFromContext(context).additionalHooks.useCallback ?? [];
563
527
  return {
564
528
  CallExpression(node) {
565
529
  if (!isReactHookCall(node)) {
@@ -602,7 +566,7 @@ var no_unnecessary_use_callback_default = createRule({
602
566
  }
603
567
  return variableNode;
604
568
  }).otherwise(() => _);
605
- if (arg0Node === _) return;
569
+ if (arg0Node == null) return;
606
570
  const arg0NodeScope = context.sourceCode.getScope(arg0Node);
607
571
  const arg0NodeReferences = VAR5.getChidScopes(arg0NodeScope).flatMap((x) => x.references);
608
572
  const isReferencedToComponentScope = arg0NodeReferences.some((x) => x.resolved?.scope.block === component);
@@ -638,7 +602,7 @@ var no_unnecessary_use_memo_default = createRule({
638
602
  if (!context.sourceCode.text.includes("use")) {
639
603
  return {};
640
604
  }
641
- const alias = getSettingsFromContext(context).additionalHooks?.useMemo ?? [];
605
+ const alias = getSettingsFromContext(context).additionalHooks.useMemo ?? [];
642
606
  return {
643
607
  CallExpression(node) {
644
608
  if (!isReactHookCall(node)) {
@@ -685,7 +649,7 @@ var no_unnecessary_use_memo_default = createRule({
685
649
  }
686
650
  return variableNode;
687
651
  }).otherwise(() => _);
688
- if (arg0Node === _) return;
652
+ if (arg0Node == null) return;
689
653
  const arg0NodeScope = context.sourceCode.getScope(arg0Node);
690
654
  const arg0NodeReferences = VAR5.getChidScopes(arg0NodeScope).flatMap((x) => x.references);
691
655
  const isReferencedToComponentScope = arg0NodeReferences.some((x) => x.resolved?.scope.block === component);
@@ -771,7 +735,7 @@ var prefer_use_state_lazy_initialization_default = createRule({
771
735
  if (!context.sourceCode.text.includes("use")) {
772
736
  return {};
773
737
  }
774
- const alias = getSettingsFromContext(context).additionalHooks?.useState ?? [];
738
+ const alias = getSettingsFromContext(context).additionalHooks.useState ?? [];
775
739
  return {
776
740
  CallExpression(node) {
777
741
  if (!isReactHookCall(node)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-hooks-extra",
3
- "version": "1.24.0-beta.0",
3
+ "version": "1.24.0-beta.16",
4
4
  "description": "ESLint React's ESLint plugin for React Hooks related rules.",
5
5
  "keywords": [
6
6
  "react",
@@ -42,23 +42,22 @@
42
42
  "./package.json"
43
43
  ],
44
44
  "dependencies": {
45
- "@typescript-eslint/scope-manager": "^8.19.1",
46
- "@typescript-eslint/type-utils": "^8.19.1",
47
- "@typescript-eslint/types": "^8.19.1",
48
- "@typescript-eslint/utils": "^8.19.1",
45
+ "@typescript-eslint/scope-manager": "^8.20.0",
46
+ "@typescript-eslint/type-utils": "^8.20.0",
47
+ "@typescript-eslint/types": "^8.20.0",
48
+ "@typescript-eslint/utils": "^8.20.0",
49
49
  "string-ts": "^2.2.0",
50
50
  "ts-pattern": "^5.6.0",
51
- "@eslint-react/ast": "1.24.0-beta.0",
52
- "@eslint-react/core": "1.24.0-beta.0",
53
- "@eslint-react/jsx": "1.24.0-beta.0",
54
- "@eslint-react/shared": "1.24.0-beta.0",
55
- "@eslint-react/var": "1.24.0-beta.0",
56
- "@eslint-react/eff": "1.24.0-beta.0",
57
- "@eslint-react/types": "1.24.0-beta.0"
51
+ "@eslint-react/ast": "1.24.0-beta.16",
52
+ "@eslint-react/eff": "1.24.0-beta.16",
53
+ "@eslint-react/core": "1.24.0-beta.16",
54
+ "@eslint-react/shared": "1.24.0-beta.16",
55
+ "@eslint-react/jsx": "1.24.0-beta.16",
56
+ "@eslint-react/var": "1.24.0-beta.16"
58
57
  },
59
58
  "devDependencies": {
60
- "@types/react": "^19.0.4",
61
- "@types/react-dom": "^19.0.2",
59
+ "@types/react": "^19.0.7",
60
+ "@types/react-dom": "^19.0.3",
62
61
  "tsup": "^8.3.5",
63
62
  "@workspace/configs": "0.0.0"
64
63
  },