Skip to content

Commit 540d0d9

Browse files
committed
refactor(eslint-plugin-react-hooks): improve conditionals
This change addresses several feedback items from #32240
1 parent 4c0fbe7 commit 540d0d9

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ const rule = {
184184
// Get the current scope.
185185
const scope = scopeManager.acquire(node);
186186
if (!scope) {
187-
return;
187+
throw new Error('Unable to acquire scope for the current node.');
188188
}
189189

190190
// Find all our "pure scopes". On every re-render of a component these
@@ -255,7 +255,7 @@ const rule = {
255255
// Detect primitive constants
256256
// const foo = 42
257257
let declaration = defNode.parent;
258-
if (declaration == null && componentScope) {
258+
if (declaration == null && componentScope != null) {
259259
// This might happen if variable is declared after the callback.
260260
// In that case ESLint won't set up .parent refs.
261261
// So we'll set them up manually.
@@ -266,7 +266,7 @@ const rule = {
266266
}
267267
}
268268
if (
269-
declaration &&
269+
declaration != null &&
270270
'kind' in declaration &&
271271
declaration.kind === 'const' &&
272272
init.type === 'Literal' &&
@@ -454,7 +454,7 @@ const rule = {
454454
function isInsideEffectCleanup(reference: Scope.Reference): boolean {
455455
let curScope: Scope.Scope | null = reference.from;
456456
let isInReturnedFunction = false;
457-
while (curScope && curScope.block !== node) {
457+
while (curScope != null && curScope.block !== node) {
458458
if (curScope.type === 'function') {
459459
isInReturnedFunction =
460460
curScope.block.parent != null &&
@@ -529,7 +529,7 @@ const rule = {
529529
continue;
530530
}
531531
// Ignore references to the function itself as it's not defined yet.
532-
if (def.node && def.node.init === node.parent) {
532+
if (def.node != null && def.node.init === node.parent) {
533533
continue;
534534
}
535535
// Ignore Flow type parameters
@@ -660,7 +660,7 @@ const rule = {
660660
}
661661

662662
let fnScope: Scope.Scope | null = reference.from;
663-
while (fnScope && fnScope.type !== 'function') {
663+
while (fnScope != null && fnScope.type !== 'function') {
664664
fnScope = fnScope.upper;
665665
}
666666
const isDirectlyInsideEffect = fnScope?.block === node;

packages/eslint-plugin-react-hooks/src/RulesOfHooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ const rule = {
563563
context.report({node: hook, message});
564564
}
565565
} else if (
566-
codePathNode.parent &&
566+
codePathNode.parent != null &&
567567
(codePathNode.parent.type === 'MethodDefinition' ||
568568
// @ts-expect-error `ClassProperty` was removed from typescript-estree in https://github.com/typescript-eslint/typescript-eslint/pull/3806
569569
codePathNode.parent.type === 'ClassProperty' ||

0 commit comments

Comments
 (0)