Skip to content

Commit dc0b425

Browse files
committed
fix(linter/exhaustive-deps): use codegen in fixer rather than manual string manipulation
1 parent d7cca12 commit dc0b425

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
@@ -1463,9 +1463,9 @@ fn is_inside_effect_cleanup(stack: &[AstType]) -> bool {
14631463

14641464
mod fix {
14651465
use super::Name;
1466-
use itertools::Itertools;
1467-
use oxc_ast::ast::ArrayExpression;
1468-
use oxc_span::GetSpan;
1466+
use oxc_allocator::{Allocator, CloneIn};
1467+
use oxc_ast::{AstBuilder, ast::ArrayExpression};
1468+
use oxc_span::{Atom, SPAN};
14691469

14701470
use crate::fixer::{RuleFix, RuleFixer};
14711471

@@ -1474,27 +1474,23 @@ mod fix {
14741474
names: &[Name<'a>],
14751475
deps: &ArrayExpression<'a>,
14761476
) -> RuleFix<'a> {
1477-
let names_as_deps = names.iter().map(|n| n.name.as_ref()).join(", ");
1478-
let Some(last) = deps.elements.last() else {
1479-
return fixer.replace(deps.span, format!("[{names_as_deps}]"));
1480-
};
1481-
// look for a trailing comma. we'll need to add one if its not there already
1482-
let mut needs_comma = true;
1483-
let last_span = last.span();
1484-
for c in fixer.source_text()[(last_span.end as usize)..].chars() {
1485-
match c {
1486-
',' => {
1487-
needs_comma = false;
1488-
break;
1489-
}
1490-
']' => break,
1491-
_ => {} // continue
1492-
}
1477+
let mut codegen = fixer.codegen();
1478+
1479+
let alloc = Allocator::default();
1480+
let ast_builder = AstBuilder::new(&alloc);
1481+
1482+
let mut vec = deps.elements.clone_in(&alloc);
1483+
1484+
for name in names {
1485+
vec.push(
1486+
ast_builder
1487+
.expression_identifier(SPAN, Atom::from_cow_in(&name.name, &alloc))
1488+
.into(),
1489+
);
14931490
}
1494-
fixer.insert_text_after_range(
1495-
last_span,
1496-
if needs_comma { format!(", {names_as_deps}") } else { format!(" {names_as_deps}") },
1497-
)
1491+
1492+
codegen.print_expression(&ast_builder.expression_array(SPAN, vec));
1493+
fixer.replace(deps.span, codegen.into_source_text())
14981494
}
14991495
}
15001496

@@ -4089,17 +4085,33 @@ fn test() {
40894085
// FixKind::DangerousSuggestion,
40904086
),
40914087
(
4092-
r"const useHook = () => {
4088+
"const useHook = () => {
40934089
const [x] = useState(0);
40944090
const [y] = useState(0);
40954091
const [z] = useState(0);
40964092
const foo = useCallback(() => x + y + z, [x]);
40974093
}",
4094+
"const useHook = () => {
4095+
const [x] = useState(0);
4096+
const [y] = useState(0);
4097+
const [z] = useState(0);
4098+
const foo = useCallback(() => x + y + z, [\n\tx,\n\ty,\n\tz\n]);
4099+
}",
4100+
// None,
4101+
// FixKind::DangerousSuggestion,
4102+
),
4103+
(
40984104
r"const useHook = () => {
40994105
const [x] = useState(0);
41004106
const [y] = useState(0);
41014107
const [z] = useState(0);
4102-
const foo = useCallback(() => x + y + z, [x, y, z]);
4108+
const foo = useCallback(() => x + y + z, [x, y]);
4109+
}",
4110+
"const useHook = () => {
4111+
const [x] = useState(0);
4112+
const [y] = useState(0);
4113+
const [z] = useState(0);
4114+
const foo = useCallback(() => x + y + z, [\n\tx,\n\ty,\n\tz\n]);
41034115
}",
41044116
// None,
41054117
// FixKind::DangerousSuggestion,

0 commit comments

Comments
 (0)