@@ -182,12 +182,17 @@ fn unnecessary_outer_scope_dependency_diagnostic(
182182 hook_name : & str ,
183183 dep_name : & str ,
184184 span : Span ,
185+ is_mutable : bool ,
185186) -> OxcDiagnostic {
186187 OxcDiagnostic :: warn ( format ! (
187188 "React Hook {hook_name} has an unnecessary dependency: {dep_name}."
188189 ) )
189190 . with_label ( span)
190- . with_help ( "Consider removing it from the dependency array. Outer scope values aren't valid dependencies because mutating them doesn't re-render the component." )
191+ . with_help ( if is_mutable {
192+ "Consider removing it from the dependency array. Mutable values aren't valid dependencies because mutating them doesn't re-render the component."
193+ } else {
194+ "Consider removing it from the dependency array. Outer scope values aren't valid dependencies because mutating them doesn't re-render the component."
195+ } )
191196 . with_error_code_scope ( SCOPE )
192197}
193198
@@ -570,6 +575,7 @@ impl Rule for ExhaustiveDeps {
570575 declared_dependencies
571576 } ;
572577
578+ let mut unnecessary_dependencies = Vec :: new ( ) ;
573579 for dependency in & declared_dependencies {
574580 if let Some ( symbol_id) = dependency. symbol_id {
575581 let dependency_scope_id = ctx. scoping ( ) . symbol_scope_id ( symbol_id) ;
@@ -592,13 +598,28 @@ impl Rule for ExhaustiveDeps {
592598 }
593599 }
594600
601+ if !ctx. disable_directives ( ) . contains ( ExhaustiveDeps :: NAME , dependency. span ) {
602+ unnecessary_dependencies. push ( dependency) ;
603+ }
604+ }
605+
606+ unnecessary_dependencies. sort_unstable_by_key ( |dep| dep. span . start ) ;
607+ for dependency in & unnecessary_dependencies {
595608 ctx. diagnostic_with_dangerous_suggestion (
596609 unnecessary_outer_scope_dependency_diagnostic (
597610 hook_name,
598- & dependency. name ,
611+ & dependency. to_string ( ) ,
599612 dependency. span ,
613+ dependency. chain . last ( ) . is_some_and ( |part| part == "current" ) ,
600614 ) ,
601- |fixer| fix:: remove_dependency ( fixer, dependency, dependencies_node) ,
615+ |fixer| {
616+ fix:: remove_dependencies (
617+ fixer,
618+ unnecessary_dependencies. iter ( ) . map ( |dep| dep. span ) ,
619+ dependencies_node,
620+ ctx,
621+ )
622+ } ,
602623 ) ;
603624 }
604625
@@ -736,7 +757,7 @@ impl Rule for ExhaustiveDeps {
736757 dependency_changes_on_every_render_diagnostic (
737758 hook_name, dep. span , name, decl_span,
738759 ) ,
739- |fixer| fix:: remove_dependency ( fixer, & dep, dependencies_node) ,
760+ |fixer| fix:: remove_dependencies ( fixer, [ dep. span ] , dependencies_node, ctx ) ,
740761 ) ;
741762 }
742763 }
@@ -1470,17 +1491,17 @@ fn is_inside_effect_cleanup(stack: &[AstType]) -> bool {
14701491
14711492mod fix {
14721493 use super :: Name ;
1473- use oxc_allocator:: { Allocator , ArenaVec , CloneIn } ;
1494+ use oxc_allocator:: { Allocator , CloneIn } ;
14741495 use oxc_ast:: {
14751496 ast:: { ArrayExpression , Expression } ,
14761497 builder:: AstBuilder ,
14771498 } ;
1478- use oxc_span:: { GetSpan , SPAN } ;
1499+ use oxc_span:: { GetSpan , SPAN , Span } ;
14791500 use oxc_str:: Str ;
14801501
14811502 use crate :: {
1503+ context:: LintContext ,
14821504 fixer:: { RuleFix , RuleFixer } ,
1483- rules:: react:: exhaustive_deps:: Dependency ,
14841505 } ;
14851506
14861507 pub fn append_dependencies < ' c , ' a : ' c > (
@@ -1511,28 +1532,51 @@ mod fix {
15111532 fixer. replace ( deps. span , codegen. into_source_text ( ) )
15121533 }
15131534
1514- pub fn remove_dependency < ' c , ' a : ' c > (
1535+ /// Dependencies must be ordered by their position in the source.
1536+ pub fn remove_dependencies < ' c , ' a : ' c > (
15151537 fixer : RuleFixer < ' c , ' a > ,
1516- dependency : & Dependency ,
1538+ dependencies : impl IntoIterator < Item = Span > ,
15171539 deps : & ArrayExpression < ' a > ,
1540+ ctx : & LintContext < ' a > ,
15181541 ) -> RuleFix {
1519- let mut codegen = fixer. codegen ( ) ;
1520-
1521- let alloc = Allocator :: default ( ) ;
1522- let ast_builder = AstBuilder :: new ( & alloc) ;
1523-
1524- let new_deps = deps
1525- . elements
1526- . iter ( )
1527- . filter ( |el| ( * el) . span ( ) != dependency. span )
1528- . map ( |el| el. clone_in ( & alloc) ) ;
1529-
1530- codegen. print_expression ( & Expression :: new_array_expression (
1531- deps. span ,
1532- ArenaVec :: from_iter_in ( new_deps, & ast_builder) ,
1533- & ast_builder,
1534- ) ) ;
1535- fixer. replace ( deps. span , codegen. into_source_text ( ) )
1542+ let fixer = fixer. for_multifix ( ) ;
1543+ let mut fix = fixer. new_fix_with_capacity ( 2 ) ;
1544+ let mut elements = deps. elements . iter ( ) . peekable ( ) ;
1545+ for dependency in dependencies {
1546+ let Some ( element) =
1547+ elements. by_ref ( ) . find ( |element| element. span ( ) . contains_inclusive ( dependency) )
1548+ else {
1549+ continue ;
1550+ } ;
1551+ let span = element. span ( ) ;
1552+ if ctx. has_comments_between ( span) {
1553+ let mut replacement = String :: new ( ) ;
1554+ let mut start = span. start ;
1555+ for comment in ctx. comments_range ( span. start ..span. end ) {
1556+ replacement. extend (
1557+ ctx. source_range ( Span :: new ( start, comment. span . start ) )
1558+ . chars ( )
1559+ . filter ( |ch| ch. is_whitespace ( ) ) ,
1560+ ) ;
1561+ replacement. push_str ( ctx. source_range ( comment. span ) ) ;
1562+ start = comment. span . end ;
1563+ }
1564+ replacement. extend (
1565+ ctx. source_range ( Span :: new ( start, span. end ) )
1566+ . chars ( )
1567+ . filter ( |ch| ch. is_whitespace ( ) ) ,
1568+ ) ;
1569+ fix. push ( fixer. replace ( span, replacement) ) ;
1570+ } else {
1571+ fix. push ( fixer. delete_range ( span) ) ;
1572+ }
1573+ let end = elements. peek ( ) . map_or ( deps. span . end - 1 , |el| el. span ( ) . start ) ;
1574+ if let Some ( offset) = ctx. find_next_token_within ( span. end , end, "," ) {
1575+ let start = span. end + offset;
1576+ fix. push ( fixer. delete_range ( Span :: new ( start, start + 1 ) ) ) ;
1577+ }
1578+ }
1579+ fix. with_message ( "Remove unnecessary dependencies." )
15361580 }
15371581}
15381582
@@ -1554,18 +1598,67 @@ fn invalid_configs_error_in_from_configuration() {
15541598 assert ! ( ExhaustiveDeps :: from_configuration( valid_regex) . is_ok( ) ) ;
15551599}
15561600
1601+ #[ test]
1602+ fn test_dependency_removals_preserve_source ( ) {
1603+ use crate :: { fixer:: FixKind , tester:: Tester } ;
1604+
1605+ let cases = [
1606+ ( "[section, OUTER]" , "[section, ]" ) ,
1607+ ( "[OUTER, section]" , "[ section]" ) ,
1608+ ( "[section, OUTER,]" , "[section, ]" ) ,
1609+ ( "[OUTER]" , "[]" ) ,
1610+ ( "[OUTER,]" , "[]" ) ,
1611+ ( "[OUTER,OTHER]" , "[]" ) ,
1612+ ( "[OUTER,OTHER,section]" , "[section]" ) ,
1613+ ( "[section,ref.current,OUTER,OTHER]" , "[section,]" ) ,
1614+ ( "[section, (OUTER)]" , "[section, ]" ) ,
1615+ ( "[section, (OUTER as number)]" , "[section, ]" ) ,
1616+ ( "[section, ref /* keep */ .current]" , "[section, /* keep */ ]" ) ,
1617+ (
1618+ "[\n OUTER, // eslint-disable-line react-hooks/exhaustive-deps\n OTHER, section]" ,
1619+ "[\n OUTER, // eslint-disable-line react-hooks/exhaustive-deps\n section]" ,
1620+ ) ,
1621+ ( "[section, (/* keep */ OUTER)]" , "[section, /* keep */ ]" ) ,
1622+ ( "[section, (// keep\n OUTER)]" , "[section, // keep\n ]" ) ,
1623+ ( "[OUTER,, section]" , "[, section]" ) ,
1624+ ( "[section, OUTER /* keep, this */ , OTHER]" , "[section, /* keep, this */ ]" ) ,
1625+ ( "[section, OUTER // keep, this\n , OTHER]" , "[section, // keep, this\n ]" ) ,
1626+ (
1627+ "[\n section, // re-run when the tab changes\n OUTER,\n OTHER,\n ]" ,
1628+ "[\n section, // re-run when the tab changes\n \n \n ]" ,
1629+ ) ,
1630+ ( "[section, OUTER /* 注释 , */, OTHER]" , "[section, /* 注释 , */ ]" ) ,
1631+ ( "[section, OUTER\u{2028} , OTHER]" , "[section, \u{2028} ]" ) ,
1632+ ] ;
1633+ let fix = cases
1634+ . into_iter ( )
1635+ . flat_map ( |( before, after) | {
1636+ let source = |deps| format ! ( "const OUTER = 1; const OTHER = 2; function Comp({{ section }}) {{ const ref = useRef(null); useEffect(() => {{}}, {deps}); }}" ) ;
1637+ [
1638+ ( source ( before) , source ( before) , None , FixKind :: SafeFix ) ,
1639+ ( source ( before) , source ( before) , None , FixKind :: SafeFixOrSuggestion ) ,
1640+ ( source ( before) , source ( after) , None , FixKind :: DangerousSuggestion ) ,
1641+ ]
1642+ } )
1643+ . collect ( ) ;
1644+
1645+ Tester :: new :: < & str > ( ExhaustiveDeps :: NAME , ExhaustiveDeps :: PLUGIN , vec ! [ ] , vec ! [ ] )
1646+ . expect_fix ( fix)
1647+ . test ( ) ;
1648+ }
1649+
15571650#[ test]
15581651fn test_unnecessary_dependency_suggestions ( ) {
15591652 use crate :: { fixer:: FixKind , tester:: Tester } ;
15601653
15611654 let cases = [
15621655 (
15631656 "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]); }" ,
1657+ "function A({ section }) { const ref = useRef(null); useEffect(() => { if (section === 'a') ref.current?.scrollIntoView(); }, [section, ]); }" ,
15651658 ) ,
15661659 (
15671660 "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]); }" ,
1661+ "const OUTER = 1; function B({ section }) { useEffect(() => { console.log(section, OUTER); }, [section, ]); }" ,
15691662 ) ,
15701663 ] ;
15711664
@@ -4471,7 +4564,7 @@ const Component = ({ filter }) => {
44714564 // Test duplicate dependency removal
44724565 (
44734566 "function MyComponent() { const local = {}; useEffect(() => { console.log(local); }, [local, local]); }" ,
4474- "function MyComponent() { const local = {}; useEffect(() => { console.log(local); }, [local]); }" ,
4567+ "function MyComponent() { const local = {}; useEffect(() => { console.log(local); }, [ local]); }" ,
44754568 ) ,
44764569 (
44774570 "const x = {}; function Comp() { useEffect(() => {}, [x]) }" ,
0 commit comments