Skip to content

Commit 8c19b18

Browse files
committed
feat(linter/exhaustive-deps): implement fixer for dep in global scope (#13783)
In the below example, `x` is a useless dependency, as changes to `x` will not trigger a re-rended as it is not in the component scope. This commit implements a fixer that removes the dependency from the dependency array. ``` const x = {} function MyComponent() { useEffect(() => {}, [x]) } ```
1 parent 06bce8f commit 8c19b18

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,11 +556,14 @@ impl Rule for ExhaustiveDeps {
556556
}
557557
}
558558

559-
ctx.diagnostic(unnecessary_outer_scope_dependency_diagnostic(
560-
hook_name,
561-
&dependency.name,
562-
dependency.span,
563-
));
559+
ctx.diagnostic_with_fix(
560+
unnecessary_outer_scope_dependency_diagnostic(
561+
hook_name,
562+
&dependency.name,
563+
dependency.span,
564+
),
565+
|fixer| fix::remove_dependency(fixer, dependency, dependencies_node),
566+
);
564567
}
565568

566569
let undeclared_deps = found_dependencies.difference(&declared_dependencies).filter(|dep| {
@@ -4197,6 +4200,10 @@ fn test() {
41974200
"function MyComponent() { const local = {}; useEffect(() => { console.log(local); }, [local, local]); }",
41984201
"function MyComponent() { const local = {}; useEffect(() => { console.log(local); }, [local]); }",
41994202
),
4203+
(
4204+
"const x = {}; function Comp() { useEffect(() => {}, [x]) }",
4205+
"const x = {}; function Comp() { useEffect(() => {}, []) }",
4206+
),
42004207
];
42014208

42024209
Tester::new(

0 commit comments

Comments
 (0)