oxlint-plugin-react-doctor 0.7.4-dev.4a5d9bb → 0.7.4-dev.5113067

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.d.ts CHANGED
@@ -4445,6 +4445,29 @@ declare const REACT_DOCTOR_RULES: readonly [{
4445
4445
  readonly recommendationFor?: (hasCapability: CapabilityQuery) => string | undefined;
4446
4446
  readonly create: (context: RuleContext) => RuleVisitors;
4447
4447
  };
4448
+ }, {
4449
+ readonly key: "react-doctor/no-impure-state-updater";
4450
+ readonly id: "no-impure-state-updater";
4451
+ readonly source: "react-doctor";
4452
+ readonly originallyExternal: false;
4453
+ readonly rule: {
4454
+ readonly framework: "global";
4455
+ readonly category: "Bugs";
4456
+ readonly requires: readonly Capability[];
4457
+ readonly id: string;
4458
+ readonly title?: string;
4459
+ readonly severity: RuleSeverity;
4460
+ readonly disabledWhen?: ReadonlyArray<Capability>;
4461
+ readonly tags?: ReadonlyArray<string>;
4462
+ readonly matchByOccurrence?: boolean;
4463
+ readonly defaultEnabled?: boolean;
4464
+ readonly lifecycle?: "retired";
4465
+ readonly scan?: FileScan;
4466
+ readonly committedFilesOnly?: boolean;
4467
+ readonly recommendation?: string;
4468
+ readonly recommendationFor?: (hasCapability: CapabilityQuery) => string | undefined;
4469
+ readonly create: (context: RuleContext) => RuleVisitors;
4470
+ };
4448
4471
  }, {
4449
4472
  readonly key: "react-doctor/no-indeterminate-attribute";
4450
4473
  readonly id: "no-indeterminate-attribute";
@@ -13642,6 +13665,29 @@ declare const RULES: readonly [{
13642
13665
  readonly recommendationFor?: (hasCapability: CapabilityQuery) => string | undefined;
13643
13666
  readonly create: (context: RuleContext) => RuleVisitors;
13644
13667
  };
13668
+ }, {
13669
+ readonly key: "react-doctor/no-impure-state-updater";
13670
+ readonly id: "no-impure-state-updater";
13671
+ readonly source: "react-doctor";
13672
+ readonly originallyExternal: false;
13673
+ readonly rule: {
13674
+ readonly framework: "global";
13675
+ readonly category: "Bugs";
13676
+ readonly requires: readonly Capability[];
13677
+ readonly id: string;
13678
+ readonly title?: string;
13679
+ readonly severity: RuleSeverity;
13680
+ readonly disabledWhen?: ReadonlyArray<Capability>;
13681
+ readonly tags?: ReadonlyArray<string>;
13682
+ readonly matchByOccurrence?: boolean;
13683
+ readonly defaultEnabled?: boolean;
13684
+ readonly lifecycle?: "retired";
13685
+ readonly scan?: FileScan;
13686
+ readonly committedFilesOnly?: boolean;
13687
+ readonly recommendation?: string;
13688
+ readonly recommendationFor?: (hasCapability: CapabilityQuery) => string | undefined;
13689
+ readonly create: (context: RuleContext) => RuleVisitors;
13690
+ };
13645
13691
  }, {
13646
13692
  readonly key: "react-doctor/no-indeterminate-attribute";
13647
13693
  readonly id: "no-indeterminate-attribute";
package/dist/index.js CHANGED
@@ -9968,13 +9968,18 @@ const unwrapExpression$3 = (node) => {
9968
9968
  return current;
9969
9969
  };
9970
9970
  /**
9971
- * Get the hook name from a call expression's callee, regardless of
9972
- * whether the hook is called as `useFoo()` (Identifier) or
9973
- * `React.useFoo()` (MemberExpression).
9971
+ * Get the hook name from a direct, wrapped, namespaced, or immutable
9972
+ * React import alias call.
9974
9973
  */
9975
- const getHookName = (callee) => {
9976
- if (isNodeOfType(callee, "Identifier")) return callee.name;
9977
- if (isNodeOfType(callee, "MemberExpression") && !callee.computed && isNodeOfType(callee.property, "Identifier")) return callee.property.name;
9974
+ const getHookName = (callee, scopes) => {
9975
+ const strippedCallee = unwrapExpression$3(callee);
9976
+ if (isNodeOfType(strippedCallee, "Identifier")) {
9977
+ const resolvedSymbol = scopes ? resolveConstIdentifierAlias(strippedCallee, scopes) : null;
9978
+ const importDeclaration = resolvedSymbol?.declarationNode.parent;
9979
+ if (resolvedSymbol?.kind === "import" && importDeclaration && isNodeOfType(importDeclaration, "ImportDeclaration") && importDeclaration.source.value === "react") return getImportedName(resolvedSymbol.declarationNode) ?? strippedCallee.name;
9980
+ return strippedCallee.name;
9981
+ }
9982
+ if (isNodeOfType(strippedCallee, "MemberExpression") && !strippedCallee.computed && isNodeOfType(strippedCallee.property, "Identifier")) return strippedCallee.property.name;
9978
9983
  return null;
9979
9984
  };
9980
9985
  const FUNCTION_SCOPE_KINDS = new Set([
@@ -10159,7 +10164,7 @@ const STABLE_IDENTITY_WRAPPER_HOOK_NAMES = new Set([
10159
10164
  * - primitive-literal local consts (the value never changes
10160
10165
  * between renders unless the literal does)
10161
10166
  */
10162
- const symbolHasStableHookOrigin = (symbol) => {
10167
+ const symbolHasStableHookOrigin = (symbol, scopes) => {
10163
10168
  if (symbol.references.some((reference) => reference.flag !== "read")) return false;
10164
10169
  let declarator = symbol.declarationNode;
10165
10170
  while (declarator && declarator.type !== "VariableDeclarator") declarator = declarator.parent ?? null;
@@ -10172,7 +10177,7 @@ const symbolHasStableHookOrigin = (symbol) => {
10172
10177
  if (isNodeOfType(initializer, "TemplateLiteral") && getStaticTemplateLiteralValue(initializer) !== null) return true;
10173
10178
  }
10174
10179
  if (!isNodeOfType(initializer, "CallExpression")) return false;
10175
- const initializerHookName = getHookName(initializer.callee);
10180
+ const initializerHookName = getHookName(initializer.callee, scopes);
10176
10181
  if (!initializerHookName) return false;
10177
10182
  if (initializerHookName === "useRef") return true;
10178
10183
  if (initializerHookName === "useEffectEvent") return true;
@@ -10207,7 +10212,7 @@ const symbolHasStableMemoizedOrigin = (symbol, scopes, visitedSymbolIds) => {
10207
10212
  if (!declarator.init) return false;
10208
10213
  const initializer = unwrapExpression$3(declarator.init);
10209
10214
  if (!isNodeOfType(initializer, "CallExpression")) return false;
10210
- const initializerHookName = getHookName(initializer.callee);
10215
+ const initializerHookName = getHookName(initializer.callee, scopes);
10211
10216
  if (!initializerHookName || !MEMOIZING_HOOK_NAMES.has(initializerHookName)) return false;
10212
10217
  const depsArgument = initializer.arguments[1];
10213
10218
  if (!depsArgument || !isAstNode(depsArgument)) return false;
@@ -10237,7 +10242,7 @@ const getObjectPropertyValue = (objectExpression, propertyName) => {
10237
10242
  }
10238
10243
  return null;
10239
10244
  };
10240
- const isStableRefContainerCapture = (symbol, depKey) => {
10245
+ const isStableRefContainerCapture = (symbol, depKey, scopes) => {
10241
10246
  if (symbol.kind !== "const") return false;
10242
10247
  if (!depKey.startsWith(`${symbol.name}.`)) return false;
10243
10248
  if (symbol.references.some((reference) => reference.flag !== "read")) return false;
@@ -10251,7 +10256,7 @@ const isStableRefContainerCapture = (symbol, depKey) => {
10251
10256
  if (!propertyValue) return false;
10252
10257
  currentValue = propertyValue;
10253
10258
  }
10254
- return isNodeOfType(currentValue, "CallExpression") && getHookName(currentValue.callee) === "useRef";
10259
+ return isNodeOfType(currentValue, "CallExpression") && getHookName(currentValue.callee, scopes) === "useRef";
10255
10260
  };
10256
10261
  const symbolHasStableFunctionOrigin = (symbol, scopes, visitedSymbolIds) => {
10257
10262
  if (visitedSymbolIds.has(symbol.id)) return true;
@@ -10269,7 +10274,13 @@ const symbolHasStableFunctionOrigin = (symbol, scopes, visitedSymbolIds) => {
10269
10274
  }
10270
10275
  return true;
10271
10276
  };
10272
- const symbolHasStableValue = (symbol, scopes, visitedSymbolIds = /* @__PURE__ */ new Set()) => symbolHasStableHookOrigin(symbol) || symbolHasStableFunctionOrigin(symbol, scopes, visitedSymbolIds) || symbolHasStableMemoizedOrigin(symbol, scopes, visitedSymbolIds);
10277
+ const symbolHasStableImportedAlias = (symbol, scopes) => {
10278
+ if (symbol.kind !== "const") return false;
10279
+ if (symbol.references.some((reference) => reference.flag !== "read")) return false;
10280
+ const resolvedSymbol = resolveConstIdentifierAlias(symbol.bindingIdentifier, scopes);
10281
+ return resolvedSymbol !== null && resolvedSymbol !== symbol && resolvedSymbol.kind === "import";
10282
+ };
10283
+ const symbolHasStableValue = (symbol, scopes, visitedSymbolIds = /* @__PURE__ */ new Set()) => symbolHasStableHookOrigin(symbol, scopes) || symbolHasStableImportedAlias(symbol, scopes) || symbolHasStableFunctionOrigin(symbol, scopes, visitedSymbolIds) || symbolHasStableMemoizedOrigin(symbol, scopes, visitedSymbolIds);
10273
10284
  //#endregion
10274
10285
  //#region src/plugin/utils/symbol-has-react-use-effect-event-origin.ts
10275
10286
  const symbolHasReactUseEffectEventOrigin = (symbol, scopes) => {
@@ -10457,10 +10468,18 @@ const collectCaptureDepKeys = (callback, scopes) => {
10457
10468
  }
10458
10469
  const depKey = computeDepKey(reference);
10459
10470
  if (!depKey) continue;
10460
- if (isStableRefContainerCapture(symbol, depKey)) {
10471
+ if (isStableRefContainerCapture(symbol, depKey, scopes)) {
10461
10472
  stableCapturedNames.add(depKey);
10462
10473
  continue;
10463
10474
  }
10475
+ if (depKey === symbol.name) {
10476
+ const identitySourceKeys = resolveReactiveIdentitySourceKeys(symbol, scopes);
10477
+ if (identitySourceKeys) {
10478
+ if (identitySourceKeys.size === 0) stableCapturedNames.add(depKey);
10479
+ for (const identitySourceKey of identitySourceKeys) keys.add(identitySourceKey);
10480
+ continue;
10481
+ }
10482
+ }
10464
10483
  keys.add(depKey);
10465
10484
  }
10466
10485
  return {
@@ -10489,12 +10508,60 @@ const hasComputedMemberExpression = (node) => {
10489
10508
  if (stripped.computed) return true;
10490
10509
  return hasComputedMemberExpression(stripped.object);
10491
10510
  };
10511
+ const mergeIdentitySourceKeys = (expressions, scopes, visitedSymbolIds) => {
10512
+ const identitySourceKeys = /* @__PURE__ */ new Set();
10513
+ for (const expression of expressions) {
10514
+ const expressionSourceKeys = resolveIdentitySourceKeysFromExpression(expression, scopes, visitedSymbolIds);
10515
+ if (!expressionSourceKeys) return null;
10516
+ for (const expressionSourceKey of expressionSourceKeys) identitySourceKeys.add(expressionSourceKey);
10517
+ }
10518
+ return identitySourceKeys;
10519
+ };
10520
+ const resolveIdentitySourceKeysFromExpression = (expression, scopes, visitedSymbolIds) => {
10521
+ const stripped = unwrapExpression$3(expression);
10522
+ if (isNodeOfType(stripped, "Literal") && (stripped.value === null || typeof stripped.value === "string" || typeof stripped.value === "number" || typeof stripped.value === "boolean") || isNodeOfType(stripped, "TemplateLiteral") && getStaticTemplateLiteralValue(stripped) !== null) return /* @__PURE__ */ new Set();
10523
+ if (isNodeOfType(stripped, "Identifier")) {
10524
+ const sourceSymbol = scopes.symbolFor(stripped);
10525
+ if (!sourceSymbol) return null;
10526
+ if (isOutsideAllFunctions(sourceSymbol) || symbolHasStableValue(sourceSymbol, scopes)) return /* @__PURE__ */ new Set();
10527
+ if (sourceSymbol.kind === "const" && sourceSymbol.initializer && isNodeOfType(sourceSymbol.declarationNode, "VariableDeclarator") && sourceSymbol.declarationNode.id === sourceSymbol.bindingIdentifier && sourceSymbol.references.every((reference) => reference.flag === "read")) {
10528
+ if (visitedSymbolIds.has(sourceSymbol.id)) return null;
10529
+ visitedSymbolIds.add(sourceSymbol.id);
10530
+ const sourceKeys = resolveIdentitySourceKeysFromExpression(sourceSymbol.initializer, scopes, visitedSymbolIds);
10531
+ visitedSymbolIds.delete(sourceSymbol.id);
10532
+ if (sourceKeys) return sourceKeys;
10533
+ }
10534
+ return new Set([sourceSymbol.name]);
10535
+ }
10536
+ if (isNodeOfType(stripped, "MemberExpression")) {
10537
+ if (hasComputedMemberExpression(stripped)) return null;
10538
+ const sourceKey = stringifyMemberChain(stripped);
10539
+ const rootIdentifier = getMemberRootIdentifier(stripped);
10540
+ const rootSymbol = rootIdentifier ? scopes.symbolFor(rootIdentifier) : null;
10541
+ if (!sourceKey || !rootSymbol) return null;
10542
+ if (isOutsideAllFunctions(rootSymbol)) return /* @__PURE__ */ new Set();
10543
+ if (isStableRefContainerCapture(rootSymbol, sourceKey, scopes)) return /* @__PURE__ */ new Set();
10544
+ if (symbolHasStableValue(rootSymbol, scopes)) return /* @__PURE__ */ new Set();
10545
+ return new Set([sourceKey]);
10546
+ }
10547
+ if (isNodeOfType(stripped, "LogicalExpression")) return mergeIdentitySourceKeys([stripped.left, stripped.right], scopes, visitedSymbolIds);
10548
+ if (isNodeOfType(stripped, "ConditionalExpression")) return mergeIdentitySourceKeys([
10549
+ stripped.test,
10550
+ stripped.consequent,
10551
+ stripped.alternate
10552
+ ], scopes, visitedSymbolIds);
10553
+ return null;
10554
+ };
10555
+ const resolveReactiveIdentitySourceKeys = (symbol, scopes) => {
10556
+ if (symbol.kind !== "const" || !symbol.initializer || !isNodeOfType(symbol.declarationNode, "VariableDeclarator") || symbol.declarationNode.id !== symbol.bindingIdentifier || symbol.references.some((reference) => reference.flag !== "read")) return null;
10557
+ return resolveIdentitySourceKeysFromExpression(symbol.initializer, scopes, new Set([symbol.id]));
10558
+ };
10492
10559
  const isUseCallbackResultDep = (node, scopes) => {
10493
10560
  const rootSymbol = getRootSymbol(node, scopes);
10494
10561
  const initializer = rootSymbol?.initializer ? unwrapExpression$3(rootSymbol.initializer) : null;
10495
- return Boolean(initializer && isNodeOfType(initializer, "CallExpression") && getHookName(initializer.callee) === "useCallback");
10562
+ return Boolean(initializer && isNodeOfType(initializer, "CallExpression") && getHookName(initializer.callee, scopes) === "useCallback");
10496
10563
  };
10497
- const isExtraEffectDepAllowed = (node, scopes) => {
10564
+ const isExtraReactiveDepAllowed = (node, scopes) => {
10498
10565
  const rootIdentifier = getMemberRootIdentifier(node);
10499
10566
  if (!rootIdentifier) return false;
10500
10567
  const symbol = scopes.symbolFor(rootIdentifier);
@@ -10522,10 +10589,17 @@ const isUnstableInitializer = (node) => {
10522
10589
  if (isNodeOfType(stripped, "LogicalExpression")) return isUnstableInitializer(stripped.left) || isUnstableInitializer(stripped.right);
10523
10590
  return isNodeOfType(stripped, "ObjectExpression") || isNodeOfType(stripped, "ArrayExpression") || isNodeOfType(stripped, "ClassExpression") || isNodeOfType(stripped, "ClassDeclaration") || isNodeOfType(stripped, "JSXElement") || isNodeOfType(stripped, "JSXFragment") || isNodeOfType(stripped, "AssignmentExpression") || isNodeOfType(stripped, "NewExpression");
10524
10591
  };
10592
+ const isExtraDepAllowedForHook = (hookName, node, scopes) => {
10593
+ if (!isExtraReactiveDepAllowed(node, scopes)) return false;
10594
+ if (EFFECT_HOOKS_ALLOWING_EXTRA_REACTIVE_DEPS.has(hookName)) return true;
10595
+ if (hookName !== "useMemo") return false;
10596
+ const rootSymbol = getRootSymbol(node, scopes);
10597
+ return Boolean(rootSymbol && !symbolHasStableValue(rootSymbol, scopes) && !isUnstableInitializer(rootSymbol.initializer));
10598
+ };
10525
10599
  const hasDirectIdentifierDeclarator = (symbol) => isNodeOfType(symbol.declarationNode, "VariableDeclarator") && isNodeOfType(symbol.declarationNode.id, "Identifier") || isNodeOfType(symbol.declarationNode, "ClassDeclaration");
10526
10600
  const isFunctionValueSymbol = (symbol) => getFunctionValueNode(symbol) !== null;
10527
- const isStableSetterLikeSymbol = (symbol) => {
10528
- if (!symbolHasStableHookOrigin(symbol)) return false;
10601
+ const isStableSetterLikeSymbol = (symbol, scopes) => {
10602
+ if (!symbolHasStableHookOrigin(symbol, scopes)) return false;
10529
10603
  return symbol.name.startsWith("set") || symbol.name.startsWith("dispatch") || symbol.name.startsWith("startTransition");
10530
10604
  };
10531
10605
  const findStableSetterReference = (node, scopes) => {
@@ -10535,7 +10609,7 @@ const findStableSetterReference = (node, scopes) => {
10535
10609
  if (current !== node && (isNodeOfType(current, "FunctionDeclaration") || isNodeOfType(current, "FunctionExpression") || isNodeOfType(current, "ArrowFunctionExpression"))) return;
10536
10610
  if (isNodeOfType(current, "Identifier")) {
10537
10611
  const symbol = scopes.referenceFor(current)?.resolvedSymbol;
10538
- if (symbol && isStableSetterLikeSymbol(symbol)) {
10612
+ if (symbol && isStableSetterLikeSymbol(symbol, scopes)) {
10539
10613
  setterName = symbol.name;
10540
10614
  return;
10541
10615
  }
@@ -10651,11 +10725,11 @@ const hasRefCurrentAssignmentInComponent = (refSymbol) => {
10651
10725
  }
10652
10726
  return false;
10653
10727
  };
10654
- const isSeededDataRefSymbol = (refSymbol) => {
10728
+ const isSeededDataRefSymbol = (refSymbol, scopes) => {
10655
10729
  if (!refSymbol) return false;
10656
10730
  const initializer = refSymbol.initializer ? unwrapExpression$3(refSymbol.initializer) : null;
10657
10731
  if (!initializer || !isNodeOfType(initializer, "CallExpression")) return false;
10658
- if (getHookName(initializer.callee) !== "useRef") return false;
10732
+ if (getHookName(initializer.callee, scopes) !== "useRef") return false;
10659
10733
  const firstArgument = initializer.arguments[0];
10660
10734
  if (!firstArgument || !isAstNode(firstArgument)) return false;
10661
10735
  const strippedArgument = unwrapExpression$3(firstArgument);
@@ -10695,7 +10769,9 @@ const isOuterFunctionScopeDep = (node, callback, scopes) => {
10695
10769
  const componentOrHookScope = componentOrHookFunction ? scopes.ownScopeFor(componentOrHookFunction) : null;
10696
10770
  return Boolean(componentOrHookScope && !isDescendantScope(symbol.scope, componentOrHookScope));
10697
10771
  };
10698
- const hasMemberCallForRoot = (node, rootName) => {
10772
+ const hasMemberCallForRoot = (node, rootName, scopes) => {
10773
+ const rootSymbol = closureCaptures(node, scopes).find((reference) => reference.resolvedSymbol?.name === rootName)?.resolvedSymbol ?? null;
10774
+ if (!rootSymbol) return false;
10699
10775
  let didFindMemberCall = false;
10700
10776
  const visit = (current) => {
10701
10777
  if (didFindMemberCall) return;
@@ -10711,7 +10787,7 @@ const hasMemberCallForRoot = (node, rootName) => {
10711
10787
  }
10712
10788
  chainObject = unwrapExpression$3(chainObject.object);
10713
10789
  }
10714
- if (!doesChainPassThroughCurrent && chainObject && isNodeOfType(chainObject, "Identifier") && chainObject.name === rootName) {
10790
+ if (!doesChainPassThroughCurrent && chainObject && isNodeOfType(chainObject, "Identifier") && chainObject.name === rootName && scopes.symbolFor(chainObject) === rootSymbol) {
10715
10791
  didFindMemberCall = true;
10716
10792
  return;
10717
10793
  }
@@ -10729,9 +10805,11 @@ const hasMemberCallForRoot = (node, rootName) => {
10729
10805
  visit(node);
10730
10806
  return didFindMemberCall;
10731
10807
  };
10732
- const addAggregatePropsDependency = (captureKeys, declaredKeys, callback) => {
10733
- if ([...captureKeys].filter((captureKey) => captureKey.startsWith("props.")).length < 2 || declaredKeys.has("props")) return;
10734
- if (hasMemberCallForRoot(callback, "props")) captureKeys.add("props");
10808
+ const addAggregatePropsDependency = (captureKeys, declaredKeys, callback, scopes) => {
10809
+ const propsCaptureKeys = [...captureKeys].filter((captureKey) => captureKey.startsWith("props."));
10810
+ if (propsCaptureKeys.length < 2 || declaredKeys.has("props")) return;
10811
+ if (propsCaptureKeys.every((captureKey) => [...declaredKeys].some((declaredKey) => isMatchingDepOrPrefix(declaredKey, captureKey)))) return;
10812
+ if (hasMemberCallForRoot(callback, "props", scopes)) captureKeys.add("props");
10735
10813
  };
10736
10814
  const exhaustiveDeps = defineRule({
10737
10815
  id: "exhaustive-deps",
@@ -10786,7 +10864,7 @@ If the missing value is recreated every render, move it inside the hook or stabi
10786
10864
  };
10787
10865
  const isAutoDependenciesHook = (hookName) => settings.experimental_autoDependenciesHooks.includes(hookName);
10788
10866
  return { CallExpression(node) {
10789
- const hookName = getHookName(node.callee);
10867
+ const hookName = getHookName(node.callee, context.scopes);
10790
10868
  if (!hookName || !isHookOfInterest(hookName, node.callee)) return;
10791
10869
  const callbackArgumentIndex = getCallbackArgumentIndex(hookName);
10792
10870
  const depsArgumentIndex = getDepsArgumentIndex(hookName);
@@ -10843,7 +10921,7 @@ If the missing value is recreated every render, move it inside the hook or stabi
10843
10921
  if (outerAssignments.length > 0) return;
10844
10922
  const refCurrentInCleanup = findRefCurrentInCleanup(callbackToAnalyze, context.scopes);
10845
10923
  const shouldCheckRefCleanup = EFFECT_HOOKS_ALLOWING_EXTRA_REACTIVE_DEPS.has(hookName) || Boolean(additionalHooksRegex && additionalHooksRegex.test(hookName));
10846
- if (refCurrentInCleanup && shouldCheckRefCleanup && !hasRefCurrentAssignment(callbackToAnalyze, refCurrentInCleanup.refCurrentName) && !hasRefCurrentAssignmentInComponent(refCurrentInCleanup.refSymbol) && !isSeededDataRefSymbol(refCurrentInCleanup.refSymbol)) context.report({
10924
+ if (refCurrentInCleanup && shouldCheckRefCleanup && !hasRefCurrentAssignment(callbackToAnalyze, refCurrentInCleanup.refCurrentName) && !hasRefCurrentAssignmentInComponent(refCurrentInCleanup.refSymbol) && !isSeededDataRefSymbol(refCurrentInCleanup.refSymbol, context.scopes)) context.report({
10847
10925
  node: callbackToAnalyze,
10848
10926
  message: buildRefCleanupMessage(refCurrentInCleanup.refCurrentName)
10849
10927
  });
@@ -10942,7 +11020,7 @@ If the missing value is recreated every render, move it inside the hook or stabi
10942
11020
  const fullChain = stringifyMemberChain(stripped);
10943
11021
  if (fullChain && fullChain.endsWith(".current") && isNodeOfType(stripped, "MemberExpression") && isNodeOfType(stripped.object, "Identifier")) {
10944
11022
  const refSymbol = context.scopes.symbolFor(stripped.object);
10945
- if (refSymbol && symbolHasStableHookOrigin(refSymbol)) {
11023
+ if (refSymbol && symbolHasStableHookOrigin(refSymbol, context.scopes)) {
10946
11024
  if (!didReportRefCurrentDep) {
10947
11025
  context.report({
10948
11026
  node: elementNode,
@@ -10980,7 +11058,7 @@ If the missing value is recreated every render, move it inside the hook or stabi
10980
11058
  declaredKeys.add(key);
10981
11059
  declaredKeyToReportNode.set(key, elementNode);
10982
11060
  }
10983
- addAggregatePropsDependency(captureKeys, declaredKeys, callbackToAnalyze ?? callbackArgument);
11061
+ addAggregatePropsDependency(captureKeys, declaredKeys, callbackToAnalyze ?? callbackArgument, context.scopes);
10984
11062
  const missingCaptureKeys = [];
10985
11063
  for (const captureKey of captureKeys) {
10986
11064
  let isCoveredByDeclared = false;
@@ -11066,7 +11144,7 @@ If the missing value is recreated every render, move it inside the hook or stabi
11066
11144
  if (stableCapturedNames.has(rootName) || stableCapturedNames.has(declaredKey)) continue;
11067
11145
  if (outerFunctionCapturedNames.has(rootName)) continue;
11068
11146
  const reportNode = declaredKeyToReportNode.get(declaredKey) ?? depsArgument;
11069
- if (EFFECT_HOOKS_ALLOWING_EXTRA_REACTIVE_DEPS.has(hookName) && isExtraEffectDepAllowed(reportNode, context.scopes)) continue;
11147
+ if (isExtraDepAllowedForHook(hookName, reportNode, context.scopes)) continue;
11070
11148
  if (missingCaptureKeys.length > 0 && isOuterFunctionScopeDep(reportNode, callbackToAnalyze ?? callbackArgument, context.scopes)) continue;
11071
11149
  const rootSymbol = getRootSymbol(reportNode, context.scopes);
11072
11150
  if (rootSymbol && missingCaptureKeys.length > 0 && isRecursiveInitializerCapture(rootSymbol, callbackToAnalyze ?? callbackArgument)) continue;
@@ -30603,6 +30681,131 @@ const noImgLazyWithHighFetchpriority = defineRule({
30603
30681
  } })
30604
30682
  });
30605
30683
  //#endregion
30684
+ //#region src/plugin/rules/state-and-effects/no-impure-state-updater.ts
30685
+ const TIMER_FUNCTION_NAMES = new Set([
30686
+ "cancelAnimationFrame",
30687
+ "clearInterval",
30688
+ "clearTimeout",
30689
+ "queueMicrotask",
30690
+ "requestAnimationFrame",
30691
+ "setInterval",
30692
+ "setTimeout"
30693
+ ]);
30694
+ const STORAGE_MUTATION_METHOD_NAMES = new Set([
30695
+ "clear",
30696
+ "removeItem",
30697
+ "setItem"
30698
+ ]);
30699
+ const STORAGE_RECEIVER_NAMES = new Set(["localStorage", "sessionStorage"]);
30700
+ const EXTERNAL_READ_METHOD_NAMES = new Set(["getBoundingClientRect", "getClientRects"]);
30701
+ const NOTIFICATION_RECEIVER_NAMES = new Set([
30702
+ "message",
30703
+ "notification",
30704
+ "toast"
30705
+ ]);
30706
+ const NOTIFICATION_METHOD_NAMES = new Set([
30707
+ "error",
30708
+ "info",
30709
+ "loading",
30710
+ "open",
30711
+ "show",
30712
+ "success",
30713
+ "warning"
30714
+ ]);
30715
+ const getMemberCall = (node) => {
30716
+ if (!isNodeOfType(node, "CallExpression")) return null;
30717
+ if (!isNodeOfType(node.callee, "MemberExpression") || node.callee.computed) return null;
30718
+ if (!isNodeOfType(node.callee.property, "Identifier")) return null;
30719
+ return {
30720
+ methodName: node.callee.property.name,
30721
+ receiver: stripParenExpression(node.callee.object)
30722
+ };
30723
+ };
30724
+ const isNotificationReceiver = (receiver, scopes) => {
30725
+ if (!isNodeOfType(receiver, "Identifier")) return false;
30726
+ if (!NOTIFICATION_RECEIVER_NAMES.has(receiver.name)) return false;
30727
+ const symbol = scopes.symbolFor(receiver);
30728
+ if (symbol?.kind === "import") return true;
30729
+ if (!isNodeOfType(symbol?.initializer, "CallExpression")) return false;
30730
+ const callee = symbol.initializer.callee;
30731
+ return isNodeOfType(callee, "Identifier") && /^use(?:Message|Notification|Toast)$/.test(callee.name);
30732
+ };
30733
+ const getKnownImpureCall = (callExpression, scopes) => {
30734
+ if (isNodeOfType(callExpression.callee, "Identifier") && TIMER_FUNCTION_NAMES.has(callExpression.callee.name) && scopes.isGlobalReference(callExpression.callee)) return `${callExpression.callee.name}()`;
30735
+ const memberCall = getMemberCall(callExpression);
30736
+ if (!memberCall) return null;
30737
+ const { methodName, receiver } = memberCall;
30738
+ if (STORAGE_MUTATION_METHOD_NAMES.has(methodName) && isNodeOfType(receiver, "Identifier") && STORAGE_RECEIVER_NAMES.has(receiver.name) && scopes.isGlobalReference(receiver)) return `${receiver.name}.${methodName}()`;
30739
+ if (EXTERNAL_READ_METHOD_NAMES.has(methodName)) return `.${methodName}()`;
30740
+ if (NOTIFICATION_METHOD_NAMES.has(methodName) && isNodeOfType(receiver, "Identifier") && isNotificationReceiver(receiver, scopes)) return `${receiver.name}.${methodName}()`;
30741
+ return null;
30742
+ };
30743
+ const getExternalAssignmentDescription = (assignmentTarget, updater, scopes) => {
30744
+ let rootIdentifier = null;
30745
+ if (isNodeOfType(assignmentTarget, "Identifier")) rootIdentifier = assignmentTarget;
30746
+ else if (isNodeOfType(assignmentTarget, "MemberExpression") && isNodeOfType(assignmentTarget.object, "Identifier")) rootIdentifier = assignmentTarget.object;
30747
+ if (!rootIdentifier) return null;
30748
+ const updaterScope = scopes.ownScopeFor(updater);
30749
+ if (!updaterScope) return null;
30750
+ const symbol = scopes.symbolFor(rootIdentifier);
30751
+ if (!symbol) return `the external value "${rootIdentifier.name}"`;
30752
+ if (symbol.kind === "parameter" && symbol.scope === updaterScope) return `the updater argument "${rootIdentifier.name}"`;
30753
+ return isDescendantScope(symbol.scope, updaterScope) ? null : `the captured value "${rootIdentifier.name}"`;
30754
+ };
30755
+ const findImpureUpdaterOperation = (updater, scopes) => {
30756
+ const analysis = getProgramAnalysis(updater);
30757
+ let operation = null;
30758
+ walkAst(updater, (child) => {
30759
+ if (operation) return false;
30760
+ if (child !== updater && isFunctionLike$1(child) && !executesDuringRender(child, scopes)) return false;
30761
+ if (isNodeOfType(child, "CallExpression")) {
30762
+ if (isNodeOfType(child.callee, "Identifier") && analysis) {
30763
+ const calleeReference = getRef(analysis, child.callee);
30764
+ if (calleeReference && isStateSetterCall(analysis, calleeReference)) {
30765
+ operation = `the nested state update "${child.callee.name}()"`;
30766
+ return false;
30767
+ }
30768
+ }
30769
+ const impureCall = getKnownImpureCall(child, scopes);
30770
+ if (impureCall) {
30771
+ operation = impureCall;
30772
+ return false;
30773
+ }
30774
+ }
30775
+ if (isNodeOfType(child, "AssignmentExpression")) {
30776
+ operation = getExternalAssignmentDescription(child.left, updater, scopes);
30777
+ if (operation) return false;
30778
+ }
30779
+ if (isNodeOfType(child, "UpdateExpression")) {
30780
+ operation = getExternalAssignmentDescription(child.argument, updater, scopes);
30781
+ if (operation) return false;
30782
+ }
30783
+ });
30784
+ return operation;
30785
+ };
30786
+ const noImpureStateUpdater = defineRule({
30787
+ id: "no-impure-state-updater",
30788
+ title: "State updater has side effects",
30789
+ severity: "error",
30790
+ recommendation: "Keep state updater callbacks pure and return only the next state. Move notifications, storage, timers, ref writes, and other external work into the event or effect that queues the update.",
30791
+ create: (context) => ({ CallExpression(node) {
30792
+ const updater = node.arguments?.[0];
30793
+ if (!updater || !isFunctionLike$1(updater)) return;
30794
+ const analysis = getProgramAnalysis(node);
30795
+ if (!analysis || !isNodeOfType(node.callee, "Identifier")) return;
30796
+ const calleeReference = getRef(analysis, node.callee);
30797
+ if (!calleeReference || !isStateSetterCall(analysis, calleeReference)) return;
30798
+ const stateDeclarator = getUseStateDecl(analysis, calleeReference);
30799
+ if (!isNodeOfType(stateDeclarator, "VariableDeclarator") || !isNodeOfType(stateDeclarator.init, "CallExpression") || !isReactApiCall(stateDeclarator.init, "useState", context.scopes, { allowGlobalReactNamespace: true })) return;
30800
+ const operation = findImpureUpdaterOperation(updater, context.scopes);
30801
+ if (!operation) return;
30802
+ context.report({
30803
+ node: updater,
30804
+ message: `This state updater performs ${operation}. React may run updater functions more than once, so side effects here can repeat or observe inconsistent external state.`
30805
+ });
30806
+ } })
30807
+ });
30808
+ //#endregion
30606
30809
  //#region src/plugin/rules/correctness/no-indeterminate-attribute.ts
30607
30810
  const MESSAGE$21 = "The `indeterminate` HTML attribute does not set a checkbox's visual state. Assign the `HTMLInputElement.indeterminate` DOM property instead.";
30608
30811
  const REACT_USE_REF_OPTIONS = {
@@ -33771,6 +33974,166 @@ const isDirectParentCallbackRef = (analysis, ref) => {
33771
33974
  return getDownstreamRefs(analysis, initializer).some((initializerRef) => getUpstreamRefs(analysis, initializerRef).some((upstreamRef) => isProp(analysis, upstreamRef)));
33772
33975
  }));
33773
33976
  };
33977
+ const getDeclarationKind = (declarator) => {
33978
+ const declaration = declarator.parent;
33979
+ return declaration && isNodeOfType(declaration, "VariableDeclaration") ? declaration.kind : null;
33980
+ };
33981
+ const hasMutableBindingWrite = (reference) => Boolean(reference.resolved?.references.some((candidateReference) => candidateReference.isWrite() && !candidateReference.init));
33982
+ const getDestructuredPropertyName = (bindingIdentifier) => {
33983
+ const property = bindingIdentifier.parent;
33984
+ if (!property || !isNodeOfType(property, "Property")) return null;
33985
+ if (property.computed) return isNodeOfType(property.key, "Literal") && typeof property.key.value === "string" ? String(property.key.value) : null;
33986
+ return isNodeOfType(property.key, "Identifier") ? property.key.name : null;
33987
+ };
33988
+ const getParentCallbackPropName = (analysis, expression, visitedVariables = /* @__PURE__ */ new Set()) => {
33989
+ const unwrappedExpression = stripParenExpression(expression);
33990
+ if (isNodeOfType(unwrappedExpression, "Identifier")) {
33991
+ const callbackReference = getRef(analysis, unwrappedExpression);
33992
+ const callbackVariable = callbackReference?.resolved;
33993
+ if (!callbackReference || !callbackVariable || visitedVariables.has(callbackVariable)) return null;
33994
+ if (isProp(analysis, callbackReference)) {
33995
+ if (isWholePropsObjectReference(analysis, callbackReference)) return null;
33996
+ const bindingIdentifier = callbackVariable.defs.find((definition) => definition.type === "Parameter")?.name;
33997
+ return (bindingIdentifier && getDestructuredPropertyName(bindingIdentifier)) ?? unwrappedExpression.name;
33998
+ }
33999
+ if (hasMutableBindingWrite(callbackReference)) return null;
34000
+ const definitions = callbackVariable.defs.map((definition) => definition.node).filter((definitionNode) => isNodeOfType(definitionNode, "VariableDeclarator"));
34001
+ if (definitions.length !== 1) return null;
34002
+ const declarator = definitions[0];
34003
+ if (!declarator || !isNodeOfType(declarator, "VariableDeclarator") || getDeclarationKind(declarator) !== "const" || !declarator.init) return null;
34004
+ visitedVariables.add(callbackVariable);
34005
+ if (isNodeOfType(declarator.id, "ObjectPattern")) {
34006
+ const bindingIdentifier = callbackVariable.defs[0]?.name;
34007
+ const propertyName = bindingIdentifier ? getDestructuredPropertyName(bindingIdentifier) : null;
34008
+ const propsReference = getDownstreamRefs(analysis, declarator.init).find((candidateReference) => isWholePropsObjectReference(analysis, candidateReference));
34009
+ return propertyName && propsReference ? propertyName : null;
34010
+ }
34011
+ if (!isNodeOfType(declarator.id, "Identifier")) return null;
34012
+ return getParentCallbackPropName(analysis, declarator.init, visitedVariables);
34013
+ }
34014
+ if (!isNodeOfType(unwrappedExpression, "MemberExpression")) return null;
34015
+ const callbackName = getStaticMemberPropertyName(unwrappedExpression);
34016
+ if (!callbackName) return null;
34017
+ const receiver = stripParenExpression(unwrappedExpression.object);
34018
+ if (!isNodeOfType(receiver, "Identifier")) return null;
34019
+ const receiverReference = getRef(analysis, receiver);
34020
+ if (!receiverReference || !isWholePropsObjectReference(analysis, receiverReference)) return null;
34021
+ return callbackName;
34022
+ };
34023
+ const isNullishRefInitializer = (initializer) => {
34024
+ if (!initializer) return true;
34025
+ const unwrappedInitializer = stripParenExpression(initializer);
34026
+ if (isNodeOfType(unwrappedInitializer, "Literal")) return unwrappedInitializer.value === null;
34027
+ if (!isNodeOfType(unwrappedInitializer, "Identifier")) return false;
34028
+ return unwrappedInitializer.name === "undefined";
34029
+ };
34030
+ const getDirectComponentBodyStatement = (node, componentBody) => {
34031
+ let current = node;
34032
+ while (current?.parent && current.parent !== componentBody) current = current.parent;
34033
+ return current?.parent === componentBody ? current : null;
34034
+ };
34035
+ const getVariableForDeclarator = (analysis, declarator) => {
34036
+ for (const scope of analysis.scopeManager.scopes) {
34037
+ const variable = scope.variables.find((candidateVariable) => candidateVariable.defs.some((definition) => definition.node === declarator));
34038
+ if (variable) return variable;
34039
+ }
34040
+ return null;
34041
+ };
34042
+ const getRefMember = (identifier) => {
34043
+ const receiver = findTransparentExpressionRoot(identifier);
34044
+ const member = receiver.parent;
34045
+ if (!member || !isNodeOfType(member, "MemberExpression") || member.object !== receiver) return null;
34046
+ return member;
34047
+ };
34048
+ const getRefMemberAssignment = (identifier) => {
34049
+ const member = getRefMember(identifier);
34050
+ if (!member) return null;
34051
+ const memberRoot = findTransparentExpressionRoot(member);
34052
+ const assignment = memberRoot.parent;
34053
+ if (!assignment || !isNodeOfType(assignment, "AssignmentExpression") || assignment.left !== memberRoot) return null;
34054
+ return assignment;
34055
+ };
34056
+ const getRefAliasDeclarator = (identifier) => {
34057
+ const initializer = findTransparentExpressionRoot(identifier);
34058
+ const declarator = initializer.parent;
34059
+ if (!declarator || !isNodeOfType(declarator, "VariableDeclarator") || declarator.init !== initializer || !isNodeOfType(declarator.id, "Identifier") || getDeclarationKind(declarator) !== "const") return null;
34060
+ return declarator;
34061
+ };
34062
+ const getRefBindingProvenance = (analysis, receiver, isReactUseRefCall) => {
34063
+ if (!isNodeOfType(receiver, "Identifier")) return null;
34064
+ const receiverReference = getRef(analysis, receiver);
34065
+ if (!receiverReference?.resolved || hasMutableBindingWrite(receiverReference)) return null;
34066
+ const variables = /* @__PURE__ */ new Set();
34067
+ let currentVariable = receiverReference.resolved;
34068
+ let refCall = null;
34069
+ while (currentVariable && !variables.has(currentVariable)) {
34070
+ variables.add(currentVariable);
34071
+ const definitions = currentVariable.defs.map((definition) => definition.node).filter((definitionNode) => isNodeOfType(definitionNode, "VariableDeclarator"));
34072
+ if (definitions.length !== 1) return null;
34073
+ const declarator = definitions[0];
34074
+ if (!declarator || !isNodeOfType(declarator, "VariableDeclarator") || !isNodeOfType(declarator.id, "Identifier") || !declarator.init) return null;
34075
+ if (isNodeOfType(declarator.init, "CallExpression") && isReactUseRefCall(declarator.init)) {
34076
+ refCall = declarator.init;
34077
+ break;
34078
+ }
34079
+ if (getDeclarationKind(declarator) !== "const" || !isNodeOfType(stripParenExpression(declarator.init), "Identifier")) return null;
34080
+ const upstreamReference = getRef(analysis, stripParenExpression(declarator.init));
34081
+ if (!upstreamReference?.resolved || hasMutableBindingWrite(upstreamReference)) return null;
34082
+ currentVariable = upstreamReference.resolved;
34083
+ }
34084
+ if (!refCall) return null;
34085
+ const pendingVariables = [...variables];
34086
+ while (pendingVariables.length > 0) {
34087
+ const variable = pendingVariables.pop();
34088
+ if (!variable) continue;
34089
+ for (const candidateReference of variable.references) {
34090
+ const aliasDeclarator = getRefAliasDeclarator(candidateReference.identifier);
34091
+ if (!aliasDeclarator) continue;
34092
+ const aliasVariable = getVariableForDeclarator(analysis, aliasDeclarator);
34093
+ if (!aliasVariable || variables.has(aliasVariable)) continue;
34094
+ if (aliasVariable.references.some((aliasReference) => aliasReference.isWrite() && !aliasReference.init)) return null;
34095
+ variables.add(aliasVariable);
34096
+ pendingVariables.push(aliasVariable);
34097
+ }
34098
+ }
34099
+ return {
34100
+ refCall,
34101
+ variables
34102
+ };
34103
+ };
34104
+ const getCallbackRefProvenance = (analysis, effectCall, callExpression, isReactUseRefCall) => {
34105
+ const callee = stripParenExpression(callExpression.callee);
34106
+ if (!isNodeOfType(callee, "MemberExpression") || getStaticMemberPropertyName(callee) !== "current") return null;
34107
+ const bindingProvenance = getRefBindingProvenance(analysis, stripParenExpression(callee.object), isReactUseRefCall);
34108
+ if (!bindingProvenance) return null;
34109
+ const { refCall, variables } = bindingProvenance;
34110
+ const componentFunction = findEnclosingFunction(effectCall);
34111
+ if (!componentFunction || !isFunctionLike$1(componentFunction) || !isComponentFunction$1(componentFunction) || !isNodeOfType(componentFunction.body, "BlockStatement")) return null;
34112
+ if (!getDirectComponentBodyStatement(effectCall, componentFunction.body)) return null;
34113
+ const callbackPropNames = /* @__PURE__ */ new Set();
34114
+ const initializer = refCall.arguments?.[0];
34115
+ const initializerCallbackName = initializer ? getParentCallbackPropName(analysis, initializer) : null;
34116
+ if (initializerCallbackName) callbackPropNames.add(initializerCallbackName);
34117
+ else if (!isNullishRefInitializer(initializer)) return null;
34118
+ for (const variable of variables) for (const candidateReference of variable.references) {
34119
+ if (candidateReference.init) continue;
34120
+ if (candidateReference.isWrite()) return null;
34121
+ const identifier = candidateReference.identifier;
34122
+ if (getRefAliasDeclarator(identifier)) continue;
34123
+ const member = getRefMember(identifier);
34124
+ if (!member || getStaticMemberPropertyName(member) !== "current") return null;
34125
+ const memberParent = findTransparentExpressionRoot(member).parent;
34126
+ if (memberParent && (isNodeOfType(memberParent, "UpdateExpression") || isNodeOfType(memberParent, "UnaryExpression") && memberParent.operator === "delete")) return null;
34127
+ const assignment = getRefMemberAssignment(identifier);
34128
+ if (!assignment) continue;
34129
+ const assignmentStatement = findTransparentExpressionRoot(assignment).parent;
34130
+ if (assignment.operator !== "=" || !assignmentStatement || !isNodeOfType(assignmentStatement, "ExpressionStatement") || assignmentStatement.parent !== componentFunction.body) return null;
34131
+ const assignedCallbackName = getParentCallbackPropName(analysis, assignment.right);
34132
+ if (!assignedCallbackName) return null;
34133
+ callbackPropNames.add(assignedCallbackName);
34134
+ }
34135
+ return callbackPropNames.size > 0 ? { callbackPropNames } : null;
34136
+ };
33774
34137
  const isWrapperHookCallbackRef = (analysis, ref) => Boolean(ref.resolved?.defs.some((def) => {
33775
34138
  const node = def.node;
33776
34139
  if (!isNodeOfType(node, "VariableDeclarator") || !node.init) return false;
@@ -33826,71 +34189,79 @@ const noPassDataToParent = defineRule({
33826
34189
  severity: "warn",
33827
34190
  tags: ["test-noise"],
33828
34191
  recommendation: "Fetch the data in the parent and pass it down as a prop (or return it from the hook), instead of handing it back up through a prop callback in a useEffect. See https://react.dev/learn/you-might-not-need-an-effect#passing-data-to-the-parent",
33829
- create: (context) => ({ CallExpression(node) {
33830
- if (!isUseEffect(node)) return;
33831
- const analysis = getProgramAnalysis(node);
33832
- if (!analysis) return;
33833
- if (hasCleanup(analysis, node)) return;
33834
- const effectFnRefs = getEffectFnRefs(analysis, node);
33835
- if (!effectFnRefs) return;
33836
- const effectFn = getEffectFn(analysis, node);
33837
- if (!effectFn) return;
33838
- for (const ref of effectFnRefs) {
33839
- const callExpr = getCallExpr(ref);
33840
- if (!callExpr || !isNodeOfType(callExpr, "CallExpression")) continue;
33841
- if (isRefCall(analysis, ref)) continue;
33842
- if (!isSynchronous(ref.identifier, effectFn)) continue;
33843
- const calleeNode = unwrapChainExpression$1(callExpr.callee);
33844
- const identifier = ref.identifier;
33845
- if (calleeNode === identifier) {
33846
- if (!isDirectParentCallbackRef(analysis, ref)) continue;
33847
- if (isNodeOfType(identifier, "Identifier") && COMMAND_PROP_NAME_PATTERN.test(identifier.name)) continue;
33848
- } else if (isNodeOfType(calleeNode, "MemberExpression") && stripParenExpression(calleeNode.object) === identifier) {
33849
- if (!isWholePropsObjectReference(analysis, ref)) continue;
33850
- if (isCustomHookParameter(ref)) continue;
33851
- } else continue;
33852
- const methodName = getCallMethodName(calleeNode);
33853
- const isPropCallbackNamedLikeStringRead = Boolean(methodName && STRING_READ_METHOD_NAMES.has(methodName) && isNodeOfType(calleeNode, "MemberExpression") && stripParenExpression(calleeNode.object) === ref.identifier && isWholePropsObjectReference(analysis, ref));
33854
- if (methodName && DATA_SINK_METHOD_NAMES.has(methodName) && !isPropCallbackNamedLikeStringRead) continue;
33855
- if (methodName && COMMAND_PROP_NAME_PATTERN.test(methodName)) continue;
33856
- if (isNamespacedApiCallee(calleeNode)) continue;
33857
- const calleeName = isNodeOfType(identifier, "Identifier") ? identifier.name : methodName;
33858
- const isSetterNamedCallee = Boolean(calleeName && SETTER_NAMED_PROP_PATTERN.test(calleeName));
33859
- const isLeafRef = (argRef) => getUpstreamRefs(analysis, argRef).length === 1;
33860
- const argsUpstreamRefs = (callExpr.arguments ?? []).flatMap((argument) => {
33861
- if (isFunctionLike$1(argument)) {
33862
- if (!isSetterNamedCallee) return [];
33863
- return getFunctionalUpdaterDataRefs(analysis, argument);
33864
- }
33865
- if (isHandlerBagArgument(analysis, argument)) return [];
33866
- if (isParentWiredHookResultArgument(analysis, argument)) return [];
33867
- if (isNodeOfType(argument, "Identifier")) {
33868
- const argumentRef = getRef(analysis, argument);
33869
- if (argumentRef && resolveToFunction(argumentRef)) return [];
33870
- }
33871
- return getDownstreamRefs(analysis, argument);
33872
- }).flatMap((argumentRef) => getUpstreamRefs(analysis, argumentRef)).filter(isLeafRef);
33873
- if (calleeNode === identifier && isWrapperHookCallbackRef(analysis, ref)) argsUpstreamRefs.push(...getArgsUpstreamRefs(analysis, ref).filter(isLeafRef));
33874
- if (!argsUpstreamRefs.some((argRef) => {
33875
- if (isUseStateIdentifier(argRef.identifier)) return false;
33876
- if (isProp(analysis, argRef)) return false;
33877
- if (isUseRefIdentifier(argRef.identifier)) return false;
33878
- if (isRefCurrent(argRef)) return false;
33879
- if (isConstant(argRef)) return false;
33880
- if (isParentWiredHookResultRef(analysis, argRef)) return false;
33881
- if (isParentWiredHookCalleeRef(analysis, argRef)) return false;
33882
- if (resolvesToFunctionBinding(argRef)) return false;
33883
- const argIdentifier = argRef.identifier;
33884
- if (isImportBindingRef(argRef) && !isCalleePosition(argIdentifier)) return false;
33885
- if (isNodeOfType(argIdentifier, "Identifier") && argIdentifier.name === "undefined") return false;
33886
- return true;
33887
- })) continue;
33888
- context.report({
33889
- node: callExpr,
33890
- message: "Handing data back to a parent from a useEffect costs your users an extra render."
33891
- });
33892
- }
33893
- } })
34192
+ create: (context) => {
34193
+ const isReactUseRefCall = (node) => isReactApiCall(node, "useRef", context.scopes, {
34194
+ allowGlobalReactNamespace: true,
34195
+ allowUnboundBareCalls: true
34196
+ });
34197
+ return { CallExpression(node) {
34198
+ if (!isUseEffect(node)) return;
34199
+ const analysis = getProgramAnalysis(node);
34200
+ if (!analysis) return;
34201
+ if (hasCleanup(analysis, node)) return;
34202
+ const effectFnRefs = getEffectFnRefs(analysis, node);
34203
+ if (!effectFnRefs) return;
34204
+ const effectFn = getEffectFn(analysis, node);
34205
+ if (!effectFn) return;
34206
+ for (const ref of effectFnRefs) {
34207
+ const callExpr = getCallExpr(ref);
34208
+ if (!callExpr || !isNodeOfType(callExpr, "CallExpression")) continue;
34209
+ const callbackRefProvenance = getCallbackRefProvenance(analysis, node, callExpr, isReactUseRefCall);
34210
+ if (isRefCall(analysis, ref) && !callbackRefProvenance) continue;
34211
+ if (!isSynchronous(ref.identifier, effectFn)) continue;
34212
+ const calleeNode = unwrapChainExpression$1(callExpr.callee);
34213
+ const identifier = ref.identifier;
34214
+ if (callbackRefProvenance) {
34215
+ if ([...callbackRefProvenance.callbackPropNames].some((callbackPropName) => COMMAND_PROP_NAME_PATTERN.test(callbackPropName))) continue;
34216
+ } else if (calleeNode === identifier) {
34217
+ if (!isDirectParentCallbackRef(analysis, ref)) continue;
34218
+ if (isNodeOfType(identifier, "Identifier") && COMMAND_PROP_NAME_PATTERN.test(identifier.name)) continue;
34219
+ } else if (isNodeOfType(calleeNode, "MemberExpression") && stripParenExpression(calleeNode.object) === identifier) {
34220
+ if (!isWholePropsObjectReference(analysis, ref)) continue;
34221
+ if (isCustomHookParameter(ref)) continue;
34222
+ } else continue;
34223
+ const methodName = getCallMethodName(calleeNode);
34224
+ const isPropCallbackNamedLikeStringRead = Boolean(methodName && STRING_READ_METHOD_NAMES.has(methodName) && isNodeOfType(calleeNode, "MemberExpression") && stripParenExpression(calleeNode.object) === ref.identifier && isWholePropsObjectReference(analysis, ref));
34225
+ if (methodName && DATA_SINK_METHOD_NAMES.has(methodName) && !isPropCallbackNamedLikeStringRead) continue;
34226
+ if (methodName && COMMAND_PROP_NAME_PATTERN.test(methodName)) continue;
34227
+ if (!callbackRefProvenance && isNamespacedApiCallee(calleeNode)) continue;
34228
+ const isSetterNamedCallee = callbackRefProvenance ? [...callbackRefProvenance.callbackPropNames].every((callbackPropName) => SETTER_NAMED_PROP_PATTERN.test(callbackPropName)) : Boolean((isNodeOfType(identifier, "Identifier") ? identifier.name : methodName) && SETTER_NAMED_PROP_PATTERN.test((isNodeOfType(identifier, "Identifier") ? identifier.name : methodName) ?? ""));
34229
+ const isLeafRef = (argRef) => getUpstreamRefs(analysis, argRef).length === 1;
34230
+ const argsUpstreamRefs = (callExpr.arguments ?? []).flatMap((argument) => {
34231
+ if (isFunctionLike$1(argument)) {
34232
+ if (!isSetterNamedCallee) return [];
34233
+ return getFunctionalUpdaterDataRefs(analysis, argument);
34234
+ }
34235
+ if (isHandlerBagArgument(analysis, argument)) return [];
34236
+ if (isParentWiredHookResultArgument(analysis, argument)) return [];
34237
+ if (isNodeOfType(argument, "Identifier")) {
34238
+ const argumentRef = getRef(analysis, argument);
34239
+ if (argumentRef && resolveToFunction(argumentRef)) return [];
34240
+ }
34241
+ return getDownstreamRefs(analysis, argument);
34242
+ }).flatMap((argumentRef) => getUpstreamRefs(analysis, argumentRef)).filter(isLeafRef);
34243
+ if (calleeNode === identifier && isWrapperHookCallbackRef(analysis, ref)) argsUpstreamRefs.push(...getArgsUpstreamRefs(analysis, ref).filter(isLeafRef));
34244
+ if (!argsUpstreamRefs.some((argRef) => {
34245
+ if (isUseStateIdentifier(argRef.identifier)) return false;
34246
+ if (isProp(analysis, argRef)) return false;
34247
+ if (isUseRefIdentifier(argRef.identifier)) return false;
34248
+ if (isRefCurrent(argRef)) return false;
34249
+ if (isConstant(argRef)) return false;
34250
+ if (isParentWiredHookResultRef(analysis, argRef)) return false;
34251
+ if (isParentWiredHookCalleeRef(analysis, argRef)) return false;
34252
+ if (resolvesToFunctionBinding(argRef)) return false;
34253
+ const argIdentifier = argRef.identifier;
34254
+ if (isImportBindingRef(argRef) && !isCalleePosition(argIdentifier)) return false;
34255
+ if (isNodeOfType(argIdentifier, "Identifier") && argIdentifier.name === "undefined") return false;
34256
+ return true;
34257
+ })) continue;
34258
+ context.report({
34259
+ node: callExpr,
34260
+ message: "Handing data back to a parent from a useEffect costs your users an extra render."
34261
+ });
34262
+ }
34263
+ } };
34264
+ }
33894
34265
  });
33895
34266
  //#endregion
33896
34267
  //#region src/plugin/utils/is-call-result-consumed-as-argument.ts
@@ -56100,6 +56471,18 @@ const reactDoctorRules = [
56100
56471
  requires: [...new Set(["react", ...noImgLazyWithHighFetchpriority.requires ?? []])]
56101
56472
  }
56102
56473
  },
56474
+ {
56475
+ key: "react-doctor/no-impure-state-updater",
56476
+ id: "no-impure-state-updater",
56477
+ source: "react-doctor",
56478
+ originallyExternal: false,
56479
+ rule: {
56480
+ ...noImpureStateUpdater,
56481
+ framework: "global",
56482
+ category: "Bugs",
56483
+ requires: [...new Set(["react", ...noImpureStateUpdater.requires ?? []])]
56484
+ }
56485
+ },
56103
56486
  {
56104
56487
  key: "react-doctor/no-indeterminate-attribute",
56105
56488
  id: "no-indeterminate-attribute",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oxlint-plugin-react-doctor",
3
- "version": "0.7.4-dev.4a5d9bb",
3
+ "version": "0.7.4-dev.5113067",
4
4
  "description": "React Doctor rules for oxlint.",
5
5
  "keywords": [
6
6
  "accessibility",