Skip to content

Commit

Permalink
refactor(eslint-plugin-react-hooks): improve conditionals
Browse files Browse the repository at this point in the history
This change addresses several feedback items from facebook#32240
  • Loading branch information
michaelfaith committed Feb 16, 2025
1 parent d7df534 commit 077a4b0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const rule = {
// Get the current scope.
const scope = scopeManager.acquire(node);
if (!scope) {
return;
throw new Error('Unable to acquire scope for the current node.');
}

// Find all our "pure scopes". On every re-render of a component these
Expand Down Expand Up @@ -255,7 +255,7 @@ const rule = {
// Detect primitive constants
// const foo = 42
let declaration = defNode.parent;
if (declaration == null && componentScope) {
if (declaration == null && componentScope != null) {
// This might happen if variable is declared after the callback.
// In that case ESLint won't set up .parent refs.
// So we'll set them up manually.
Expand All @@ -266,7 +266,7 @@ const rule = {
}
}
if (
declaration &&
declaration != null &&
'kind' in declaration &&
declaration.kind === 'const' &&
init.type === 'Literal' &&
Expand Down Expand Up @@ -454,7 +454,7 @@ const rule = {
function isInsideEffectCleanup(reference: Scope.Reference): boolean {
let curScope: Scope.Scope | null = reference.from;
let isInReturnedFunction = false;
while (curScope && curScope.block !== node) {
while (curScope != null && curScope.block !== node) {
if (curScope.type === 'function') {
isInReturnedFunction =
curScope.block.parent != null &&
Expand Down Expand Up @@ -529,7 +529,7 @@ const rule = {
continue;
}
// Ignore references to the function itself as it's not defined yet.
if (def.node && def.node.init === node.parent) {
if (def.node != null && def.node.init === node.parent) {
continue;
}
// Ignore Flow type parameters
Expand Down Expand Up @@ -660,7 +660,7 @@ const rule = {
}

let fnScope: Scope.Scope | null = reference.from;
while (fnScope && fnScope.type !== 'function') {
while (fnScope != null && fnScope.type !== 'function') {
fnScope = fnScope.upper;
}
const isDirectlyInsideEffect = fnScope?.block === node;
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-react-hooks/src/RulesOfHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ const rule = {
context.report({node: hook, message});
}
} else if (
codePathNode.parent &&
codePathNode.parent != null &&
(codePathNode.parent.type === 'MethodDefinition' ||
// @ts-expect-error `ClassProperty` was removed from typescript-estree in https://github.com/typescript-eslint/typescript-eslint/pull/3806
codePathNode.parent.type === 'ClassProperty' ||
Expand Down

0 comments on commit 077a4b0

Please sign in to comment.