Skip to content

Commit bf11006

Browse files
committed
fix(linter/react/exhaustive-deps): suggest dependency removals (#28925)
Removing `ref.current` or outer-scope dependencies can change when an effect runs. Mark these removals as dangerous suggestions so plain `--fix` preserves the dependency array and applying them requires `--fix-dangerously`. Addresses the plain `--fix` behavior in #28884. Regression tests cover both reported cases and verify that only dangerous suggestions apply the removals.
1 parent 6bbb92a commit bf11006

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ impl Rule for ExhaustiveDeps {
592592
}
593593
}
594594

595-
ctx.diagnostic_with_fix(
595+
ctx.diagnostic_with_dangerous_suggestion(
596596
unnecessary_outer_scope_dependency_diagnostic(
597597
hook_name,
598598
&dependency.name,
@@ -1554,6 +1554,36 @@ fn invalid_configs_error_in_from_configuration() {
15541554
assert!(ExhaustiveDeps::from_configuration(valid_regex).is_ok());
15551555
}
15561556

1557+
#[test]
1558+
fn test_unnecessary_dependency_suggestions() {
1559+
use crate::{fixer::FixKind, tester::Tester};
1560+
1561+
let cases = [
1562+
(
1563+
"function A({ section }) { const ref = useRef(null); useEffect(() => { if (section === 'a') ref.current?.scrollIntoView(); }, [section, ref.current]); }",
1564+
"function A({ section }) { const ref = useRef(null); useEffect(() => { if (section === 'a') ref.current?.scrollIntoView(); }, [section]); }",
1565+
),
1566+
(
1567+
"const OUTER = 1; function B({ section }) { useEffect(() => { console.log(section, OUTER); }, [section, OUTER]); }",
1568+
"const OUTER = 1; function B({ section }) { useEffect(() => { console.log(section, OUTER); }, [section]); }",
1569+
),
1570+
];
1571+
1572+
let fail = cases.iter().map(|(source, _)| *source).collect();
1573+
let fix = cases
1574+
.into_iter()
1575+
.flat_map(|(source, expected)| {
1576+
[
1577+
(source, source, None, FixKind::SafeFix),
1578+
(source, source, None, FixKind::SafeFixOrSuggestion),
1579+
(source, expected, None, FixKind::DangerousSuggestion),
1580+
]
1581+
})
1582+
.collect();
1583+
1584+
Tester::new(ExhaustiveDeps::NAME, ExhaustiveDeps::PLUGIN, vec![], fail).expect_fix(fix).test();
1585+
}
1586+
15571587
#[test]
15581588
fn test() {
15591589
use crate::tester::Tester;

0 commit comments

Comments
 (0)