Skip to content
Closed
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 @@ -7936,6 +7936,26 @@ const testsTypescript = {
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useEffect(() => {
const { whatever } = props;
whatever();
}, [props.whatever]);
}
`,
},
{
code: normalizeIndent`
function MyComponent(props) {
useEffect(() => {
const { whatever: foo, something } = props;
console.log(foo, something);
}, [props.whatever, props.something]);
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -8361,6 +8381,36 @@ const testsTypescript = {
},
],
},
{
code: normalizeIndent`
function MyComponent(props) {
useEffect(() => {
const { whatever: foo, something } = props;
console.log(foo, something);
}, [props.something]);
}
`,
errors: [
{
message:
"React Hook useEffect has a missing dependency: 'props.whatever'. " +
'Either include it or remove the dependency array.',
suggestions: [
{
desc: 'Update the dependencies array to be: [props.something, props.whatever]',
output: normalizeIndent`
function MyComponent(props) {
useEffect(() => {
const { whatever: foo, something } = props;
console.log(foo, something);
}, [props.something, props.whatever]);
}
`,
},
],
},
],
},
],
};

Expand Down
72 changes: 54 additions & 18 deletions packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import type {Rule, Scope} from 'eslint';
import type {
ArrayExpression,
ArrowFunctionExpression,
AssignmentProperty,
CallExpression,
Expression,
FunctionDeclaration,
FunctionExpression,
Identifier,
Node,
ObjectPattern,
Pattern,
PrivateIdentifier,
Super,
Expand Down Expand Up @@ -564,19 +566,30 @@ const rule = {
continue;
}

// Add the dependency to a map so we can make sure it is referenced
// again in our dependencies array. Remember whether it's stable.
if (!dependencies.has(dependency)) {
const resolved = reference.resolved;
const isStable =
memoizedIsStableKnownHookValue(resolved) ||
memoizedIsFunctionWithoutCapturedValues(resolved);
dependencies.set(dependency, {
isStable,
references: [reference],
});
} else {
dependencies.get(dependency)?.references.push(reference);
const pattern = destructuredPattern(dependencyNode);
const deps = pattern == null ?
[dependency] :
pattern.properties
.filter((prop): prop is AssignmentProperty => prop.type === 'Property')
.map((prop) => prop.key)
.filter(key => key.type === 'Identifier')
.map(key => `${dependency}.${key.name}`);

for (const dep of deps) {
// Add the dependency to a map so we can make sure it is referenced
// again in our dependencies array. Remember whether it's stable.
if (!dependencies.has(dep)) {
const resolved = reference.resolved;
const isStable =
memoizedIsStableKnownHookValue(resolved) ||
memoizedIsFunctionWithoutCapturedValues(resolved);
dependencies.set(dep, {
isStable,
references: [reference],
});
} else {
dependencies.get(dep)?.references.push(reference);
}
}
}

Expand Down Expand Up @@ -1847,11 +1860,15 @@ function scanForConstructions({
}

/**
* Assuming () means the passed/returned node:
* (props) => (props)
* props.(foo) => (props.foo)
* props.foo.(bar) => (props).foo.bar
* props.foo.bar.(baz) => (props).foo.bar.baz
* Assuming {} means the passed/returned node and multiple "=>" means recursive calls:
* {props} => {props}
* {props}.foo => {props.foo}
* {props}.foo.bar.baz => {props.foo}.bar.baz => {props.foo.bar}.baz => {props.foo.bar.baz}
* props.{foo} => props.{foo}
* props.foo.{bar} => props.foo.{bar}
* {ref}.current => {ref}.current
* {props}.foo() => {props}.foo()
* {foo}.bar.baz=123 => {foo.bar}.baz=123
Comment on lines -1850 to +1871
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm suspicious of the original comment but not sure enough. Feel free to revert the comment.

*/
function getDependency(node: Node): Node {
if (
Expand Down Expand Up @@ -2129,4 +2146,23 @@ function getUnknownDependenciesMessage(reactiveHookName: string): string {
);
}

// Retruns ObjectPattern node if the node is destructured into a pattern.
// Otherwise returns null.
function destructuredPattern(
node: Node
): ObjectPattern | null {
const { parent } = node;
if (!parent || parent.type !== 'VariableDeclarator') {
return null;
}
if (parent.init !== node) {
return null;
}
const { id } = parent;
if (id.type !== 'ObjectPattern') {
return null;
}
return id;
}

export default rule;