Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 61aba28

Browse files
overlookmotelBoshen
authored andcommittedMar 19, 2025·
fix(linter/react): exhaustive-deps report longest dependency
1 parent 38ad787 commit 61aba28

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
 

‎crates/oxc_linter/src/rules/react/exhaustive_deps.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,13 @@ impl Rule for ExhaustiveDeps {
525525
// for example if `props.foo`, AND `props.foo.bar.baz` was declared in the deps array
526526
// `props.foo.bar.baz` is unnecessary (already covered by `props.foo`)
527527
declared_dependencies.iter().tuple_combinations().for_each(|(a, b)| {
528-
if a.contains(b) || b.contains(a) {
528+
if a.contains(b) {
529+
ctx.diagnostic(unnecessary_dependency_diagnostic(
530+
hook_name,
531+
&a.to_string(),
532+
dependencies_node.span,
533+
));
534+
} else if b.contains(a) {
529535
ctx.diagnostic(unnecessary_dependency_diagnostic(
530536
hook_name,
531537
&b.to_string(),
@@ -2362,6 +2368,13 @@ fn test() {
23622368
console.log(props.bar);
23632369
}, [props, props.foo]);
23642370
}",
2371+
r"function MyComponent(props) {
2372+
const local = {};
2373+
useCallback(() => {
2374+
console.log(props.foo);
2375+
console.log(props.bar);
2376+
}, [props.foo, props]);
2377+
}",
23652378
r"function MyComponent(props) {
23662379
const local = {};
23672380
useCallback(() => {

‎crates/oxc_linter/src/snapshots/react_exhaustive_deps.snap

+11-2
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ source: crates/oxc_linter/src/tester.rs
514514
╰────
515515
help: Extract the expression to a separate variable so it can be statically checked.
516516

517-
eslint-plugin-react(exhaustive-deps): React Hook useCallback has unnecessary dependency: props
517+
eslint-plugin-react(exhaustive-deps): React Hook useCallback has unnecessary dependency: props.foo
518518
╭─[exhaustive_deps.tsx:6:14]
519519
5console.log(props.bar);
520520
6 │ }, [props, props.foo]);
@@ -523,6 +523,15 @@ source: crates/oxc_linter/src/tester.rs
523523
╰────
524524
help: Either include it or remove the dependency array.
525525

526+
eslint-plugin-react(exhaustive-deps): React Hook useCallback has unnecessary dependency: props.foo
527+
╭─[exhaustive_deps.tsx:6:14]
528+
5console.log(props.bar);
529+
6 │ }, [props.foo, props]);
530+
· ──────────────────
531+
7 │ }
532+
╰────
533+
help: Either include it or remove the dependency array.
534+
526535
eslint-plugin-react-hooks(exhaustive-deps): React Hook useCallback has missing dependencies: 'props.foo', and 'props.bar'
527536
╭─[exhaustive_deps.tsx:6:14]
528537
5console.log(props.bar);
@@ -559,7 +568,7 @@ source: crates/oxc_linter/src/tester.rs
559568
╰────
560569
help: Either include it or remove the dependency array.
561570

562-
eslint-plugin-react(exhaustive-deps): React Hook useCallback has unnecessary dependency: local
571+
eslint-plugin-react(exhaustive-deps): React Hook useCallback has unnecessary dependency: local.id
563572
╭─[exhaustive_deps.tsx:5:14]
564573
4console.log(local);
565574
5 │ }, [local.id, local]);

0 commit comments

Comments
 (0)
Please sign in to comment.