Skip to content
Merged
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
58 changes: 35 additions & 23 deletions crates/oxc_linter/src/rules/react/exhaustive_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ fn is_stable_value<'a, 'b>(
return false;
};

if init_name == "useRef" || init_name == "useCallback" {
if init_name == "useRef" {
return true;
}

Expand Down Expand Up @@ -2009,18 +2009,20 @@ fn test() {
</>
);
}",
r"function Example() {
const foo = useCallback(() => {
foo();
}, []);
}",
r"function Example({ prop }) {
const foo = useCallback(() => {
if (prop) {
foo();
}
}, [prop]);
}",
// we don't support the following two cases as they would both cause an infinite loop at runtime
// r"function Example() {
// const foo = useCallback(() => {
// foo();
// }, []);
// }",
//
// r"function Example({ prop }) {
// const foo = useCallback(() => {
// if (prop) {
// foo();
// }
// }, [prop]);
// }",
r"function Hello() {
const [state, setState] = useState(0);
useEffect(() => {
Expand Down Expand Up @@ -3324,21 +3326,20 @@ fn test() {
r"function Thing() {
useEffect(async () => {});
}",
// TODO: not supported yet
// NOTE: intentionally not supported, as `foo` would be referenced before it's declaration
// r"function Example() {
// const foo = useCallback(() => {
// foo();
// }, [foo]);
// }",
// TODO: not supported yet
// r"function Example({ prop }) {
// const foo = useCallback(() => {
// prop.hello(foo);
// }, [foo]);
// const bar = useCallback(() => {
// foo();
// }, [foo]);
// }",
r"function Example({ prop }) {
const foo = useCallback(() => {
prop.hello(foo);
}, [foo]);
const bar = useCallback(() => {
foo();
}, [foo]);
}",
r"function MyComponent() {
const local = {};
function myEffect() {
Expand Down Expand Up @@ -3563,6 +3564,17 @@ fn test() {
<></>
)
}",
// https://github.com/oxc-project/oxc/issues/9788
r#"import { useCallback, useEffect } from "react";

function Component({ foo }) {
const log = useCallback(() => {
console.log(foo);
}, [foo]);
useEffect(() => {
log();
}, []);
}"#,
];

Tester::new(ExhaustiveDeps::NAME, ExhaustiveDeps::PLUGIN, pass, fail).test_and_snapshot();
Expand Down
18 changes: 18 additions & 0 deletions crates/oxc_linter/src/snapshots/react_exhaustive_deps.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,15 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Consider putting the asynchronous code inside a function and calling it from the effect.

⚠ eslint-plugin-react-hooks(exhaustive-deps): React Hook useCallback has a missing dependency: 'prop'
╭─[exhaustive_deps.tsx:4:14]
3 │ prop.hello(foo);
4 │ }, [foo]);
· ─────
5 │ const bar = useCallback(() => {
╰────
help: Either include it or remove the dependency array.

⚠ eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'local'
╭─[exhaustive_deps.tsx:6:31]
5 │ }
Expand Down Expand Up @@ -2248,3 +2257,12 @@ source: crates/oxc_linter/src/tester.rs
11 │
╰────
help: Either include it or remove the dependency array.

⚠ eslint-plugin-react-hooks(exhaustive-deps): React Hook useEffect has a missing dependency: 'log'
╭─[exhaustive_deps.tsx:9:12]
8 │ log();
9 │ }, []);
· ──
10 │ }
╰────
help: Either include it or remove the dependency array.
Loading