Skip to content

Commit 6d2666b

Browse files
authored
Fix ESLint rule crash (#15044)
1 parent 9b7e1d1 commit 6d2666b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,17 @@ const tests = {
838838
}
839839
`,
840840
},
841+
{
842+
// Regression test for a crash
843+
code: `
844+
function Podcasts() {
845+
useEffect(() => {
846+
setPodcasts([]);
847+
}, []);
848+
let [podcasts, setPodcasts] = useState(null);
849+
}
850+
`,
851+
},
841852
],
842853
invalid: [
843854
{
@@ -3812,6 +3823,32 @@ const tests = {
38123823
'Either include it or remove the dependency array.',
38133824
],
38143825
},
3826+
{
3827+
// Regression test for a crash
3828+
code: `
3829+
function Podcasts() {
3830+
useEffect(() => {
3831+
alert(podcasts);
3832+
}, []);
3833+
let [podcasts, setPodcasts] = useState(null);
3834+
}
3835+
`,
3836+
// Note: this autofix is shady because
3837+
// the variable is used before declaration.
3838+
// TODO: Maybe we can catch those fixes and not autofix.
3839+
output: `
3840+
function Podcasts() {
3841+
useEffect(() => {
3842+
alert(podcasts);
3843+
}, [podcasts]);
3844+
let [podcasts, setPodcasts] = useState(null);
3845+
}
3846+
`,
3847+
errors: [
3848+
`React Hook useEffect has a missing dependency: 'podcasts'. ` +
3849+
`Either include it or remove the dependency array.`,
3850+
],
3851+
},
38153852
],
38163853
};
38173854

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,17 @@ export default {
164164
}
165165
// Detect primitive constants
166166
// const foo = 42
167-
const declaration = def.node.parent;
167+
let declaration = def.node.parent;
168+
if (declaration == null) {
169+
// This might happen if variable is declared after the callback.
170+
// In that case ESLint won't set up .parent refs.
171+
// So we'll set them up manually.
172+
fastFindReferenceWithParent(componentScope.block, def.node.id);
173+
declaration = def.node.parent;
174+
if (declaration == null) {
175+
return false;
176+
}
177+
}
168178
if (
169179
declaration.kind === 'const' &&
170180
init.type === 'Literal' &&

0 commit comments

Comments
 (0)