Skip to content

Commit 7f14359

Browse files
camc314taearls
authored andcommitted
fix(linter/exhaustive-deps): use codegen in fixer rather than manual string manipulation (oxc-project#12987)
fixes oxc-project#12979
1 parent 2a37ea5 commit 7f14359

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

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

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,9 +2109,9 @@ impl<'a> ExhaustiveDepsVisitor<'a, '_> {
21092109

21102110
mod fix {
21112111
use super::Name;
2112-
use itertools::Itertools;
2113-
use oxc_ast::ast::ArrayExpression;
2114-
use oxc_span::GetSpan;
2112+
use oxc_allocator::{Allocator, CloneIn};
2113+
use oxc_ast::{AstBuilder, ast::ArrayExpression};
2114+
use oxc_span::{Atom, SPAN};
21152115

21162116
use crate::fixer::{RuleFix, RuleFixer};
21172117

@@ -2120,27 +2120,23 @@ mod fix {
21202120
names: &[Name<'a>],
21212121
deps: &ArrayExpression<'a>,
21222122
) -> RuleFix<'a> {
2123-
let names_as_deps = names.iter().map(|n| n.name.as_ref()).join(", ");
2124-
let Some(last) = deps.elements.last() else {
2125-
return fixer.replace(deps.span, format!("[{names_as_deps}]"));
2126-
};
2127-
// look for a trailing comma. we'll need to add one if its not there already
2128-
let mut needs_comma = true;
2129-
let last_span = last.span();
2130-
for c in fixer.source_text()[(last_span.end as usize)..].chars() {
2131-
match c {
2132-
',' => {
2133-
needs_comma = false;
2134-
break;
2135-
}
2136-
']' => break,
2137-
_ => {} // continue
2138-
}
2123+
let mut codegen = fixer.codegen();
2124+
2125+
let alloc = Allocator::default();
2126+
let ast_builder = AstBuilder::new(&alloc);
2127+
2128+
let mut vec = deps.elements.clone_in(&alloc);
2129+
2130+
for name in names {
2131+
vec.push(
2132+
ast_builder
2133+
.expression_identifier(SPAN, Atom::from_cow_in(&name.name, &alloc))
2134+
.into(),
2135+
);
21392136
}
2140-
fixer.insert_text_after_range(
2141-
last_span,
2142-
if needs_comma { format!(", {names_as_deps}") } else { format!(" {names_as_deps}") },
2143-
)
2137+
2138+
codegen.print_expression(&ast_builder.expression_array(SPAN, vec));
2139+
fixer.replace(deps.span, codegen.into_source_text())
21442140
}
21452141
}
21462142

@@ -4747,17 +4743,33 @@ fn test() {
47474743
// FixKind::DangerousSuggestion,
47484744
),
47494745
(
4750-
r"const useHook = () => {
4746+
"const useHook = () => {
47514747
const [x] = useState(0);
47524748
const [y] = useState(0);
47534749
const [z] = useState(0);
47544750
const foo = useCallback(() => x + y + z, [x]);
47554751
}",
4752+
"const useHook = () => {
4753+
const [x] = useState(0);
4754+
const [y] = useState(0);
4755+
const [z] = useState(0);
4756+
const foo = useCallback(() => x + y + z, [\n\tx,\n\ty,\n\tz\n]);
4757+
}",
4758+
// None,
4759+
// FixKind::DangerousSuggestion,
4760+
),
4761+
(
47564762
r"const useHook = () => {
47574763
const [x] = useState(0);
47584764
const [y] = useState(0);
47594765
const [z] = useState(0);
4760-
const foo = useCallback(() => x + y + z, [x, y, z]);
4766+
const foo = useCallback(() => x + y + z, [x, y]);
4767+
}",
4768+
"const useHook = () => {
4769+
const [x] = useState(0);
4770+
const [y] = useState(0);
4771+
const [z] = useState(0);
4772+
const foo = useCallback(() => x + y + z, [\n\tx,\n\ty,\n\tz\n]);
47614773
}",
47624774
// None,
47634775
// FixKind::DangerousSuggestion,

0 commit comments

Comments
 (0)