Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8009,6 +8009,20 @@ const testsTypescript = {
}
`,
},
{
// Regression test for facebook/react#27335: typeof with nested
// property access (TSQualifiedName) must not require the root
// identifier as a dependency.
code: normalizeIndent`
function MyComponent() {
const [foo, setFoo] = React.useState<{bar: number}>({bar: 42});
React.useEffect(() => {
const square = (x: typeof foo.bar) => x * x;
setFoo(previous => ({...previous, bar: square(previous.bar)}));
}, []);
}
`,
},
{
code: normalizeIndent`
function MyComponent() {
Expand Down
12 changes: 10 additions & 2 deletions packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,17 @@ const rule = {
});
}

// Skip references that appear inside a TypeScript type position.
// Walk up through TSQualifiedName chains so that nested accesses
// like `typeof foo.bar.baz` or `Foo.Bar.Baz` are also skipped, not
// just the direct `typeof foo` / `Foo` cases (fixes #27335).
let typeAncestor = dependencyNode.parent;
while (typeAncestor?.type === 'TSQualifiedName') {
typeAncestor = typeAncestor.parent;
}
if (
dependencyNode.parent?.type === 'TSTypeQuery' ||
dependencyNode.parent?.type === 'TSTypeReference'
typeAncestor?.type === 'TSTypeQuery' ||
typeAncestor?.type === 'TSTypeReference'
) {
continue;
}
Expand Down