Skip to content

Commit

Permalink
fix(linter): fix false positives in exhaustive-deps
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Dec 3, 2024
1 parent b4f3812 commit 0eb64db
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 44 deletions.
69 changes: 38 additions & 31 deletions crates/oxc_linter/src/rules/react/exhaustive_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,22 +1032,30 @@ impl<'a> Visit<'a> for ExhaustiveDepsVisitor<'a, '_> {
return;
}

let is_parent_call_expr = self
.stack
.get(self.stack.len() - 2)
.is_some_and(|kind| matches!(kind, AstKind::CallExpression(_)));

match analyze_property_chain(&it.object, self.semantic) {
Ok(source) => {
if let Some(source) = source {
let new_chain = Vec::from([it.property.name.clone()]);

self.found_dependencies.insert(Dependency {
name: source.name.clone(),
reference_id: source.reference_id,
span: source.span,
chain: [source.chain, new_chain].concat(),
symbol_id: self
.semantic
.symbols()
.get_reference(source.reference_id)
.symbol_id(),
});
if is_parent_call_expr {
self.found_dependencies.insert(source);
} else {
let new_chain = Vec::from([it.property.name.clone()]);
self.found_dependencies.insert(Dependency {
name: source.name.clone(),
reference_id: source.reference_id,
span: source.span,
chain: [source.chain.clone(), new_chain].concat(),
symbol_id: self
.semantic
.symbols()
.get_reference(source.reference_id)
.symbol_id(),
});
}
}

let cur_skip_reporting_dependency = self.skip_reporting_dependency;
Expand Down Expand Up @@ -1612,16 +1620,15 @@ fn test() {
}, []);
return <div />;
}",
// TODO: fix, this shouldn't report because `myRef` comes from props
// r"function useMyThing(myRef) {
// useEffect(() => {
// const handleMove = () => {};
// myRef.current = {};
// return () => {
// console.log(myRef.current.toString())
// };
// }, [myRef]);
// }",
r"function useMyThing(myRef) {
useEffect(() => {
const handleMove = () => {};
myRef.current = {};
return () => {
console.log(myRef.current.toString())
};
}, [myRef]);
}",
r"function MyComponent() {
const myRef = useRef();
useEffect(() => {
Expand Down Expand Up @@ -2631,14 +2638,14 @@ fn test() {
}
}, []);
}",
// r"function MyComponent(props) {
// const [skillsCount] = useState();
// useEffect(() => {
// if (skillsCount === 0 && !props.isEditMode) {
// props.toggleEditMode();
// }
// }, [skillsCount, props.isEditMode, props.toggleEditMode]);
// }",
r"function MyComponent(props) {
const [skillsCount] = useState();
useEffect(() => {
if (skillsCount === 0 && !props.isEditMode) {
props.toggleEditMode();
}
}, [skillsCount, props.isEditMode, props.toggleEditMode]);
}",
r"function MyComponent(props) {
const [skillsCount] = useState();
useEffect(() => {
Expand Down
34 changes: 21 additions & 13 deletions crates/oxc_linter/src/snapshots/react_exhaustive_deps.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
source: crates/oxc_linter/src/tester.rs
snapshot_kind: text
---
eslint-plugin-react-hooks(exhaustive-deps): React Hook useCallback has a missing dependency: 'props.foo.toString'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useCallback has a missing dependency: 'props.foo'
╭─[exhaustive_deps.tsx:4:14]
3console.log(props.foo?.toString());
4 │ }, []);
Expand All @@ -29,7 +28,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.

eslint-plugin-react-hooks(exhaustive-deps): React Hook useCallback has a missing dependency: 'props.foo.bar.toString'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useCallback has a missing dependency: 'props.foo.bar'
╭─[exhaustive_deps.tsx:4:14]
3console.log(props.foo?.bar.toString());
4 │ }, []);
Expand Down Expand Up @@ -299,7 +298,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.

eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'history.listen'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'history'
╭─[exhaustive_deps.tsx:4:14]
3return history.listen();
4 │ }, []);
Expand All @@ -308,7 +307,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.

eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'history.foo.bar'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'history.foo', and 'history.foo.bar'
╭─[exhaustive_deps.tsx:7:14]
6 │ ];
7 │ }, []);
Expand Down Expand Up @@ -1035,7 +1034,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'props.onChange'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'props.onChange', and 'props'
╭─[exhaustive_deps.tsx:6:14]
5 │ }
6 │ }, []);
Expand All @@ -1044,7 +1043,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'props.onChange'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'props.onChange', and 'props'
╭─[exhaustive_deps.tsx:6:14]
5 │ }
6 │ }, []);
Expand All @@ -1053,7 +1052,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'props.onPause', and 'props.onPlay'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'props'
╭─[exhaustive_deps.tsx:9:14]
8 │ }
9 │ }, []);
Expand All @@ -1062,7 +1061,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'props.foo.onChange'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'props.foo.onChange', and 'props.foo'
╭─[exhaustive_deps.tsx:6:14]
5 │ }
6 │ }, []);
Expand All @@ -1071,7 +1070,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'props.onChange', and 'props.foo.onChange'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'props.foo', 'props', and 'props.foo.onChange'
╭─[exhaustive_deps.tsx:7:14]
6 │ }
7 │ }, []);
Expand All @@ -1080,7 +1079,16 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'skillsCount', 'props.toggleEditMode', and 'props.isEditMode'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'props'
╭─[exhaustive_deps.tsx:7:14]
6 │ }
7 │ }, [skillsCount, props.isEditMode, props.toggleEditMode]);
· ─────────────────────────────────────────────────────
8 │ }
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'skillsCount', 'props.isEditMode', and 'props'
╭─[exhaustive_deps.tsx:7:14]
6 │ }
7 │ }, []);
Expand All @@ -1089,7 +1097,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'props.onChange', and 'props'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'props'
╭─[exhaustive_deps.tsx:5:14]
4props.onChange();
5 │ }, []);
Expand All @@ -1098,7 +1106,7 @@ snapshot_kind: text
╰────
help: Either include it or remove the dependency array.
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has missing dependencies: 'props.onChange', and 'props'
eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'props'
╭─[exhaustive_deps.tsx:5:14]
4externalCall(props);
5 │ }, []);
Expand Down

0 comments on commit 0eb64db

Please sign in to comment.