Skip to content

Commit

Permalink
refactor: improve fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Oct 19, 2024
1 parent 4dfefe1 commit 9871a55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
35 changes: 21 additions & 14 deletions crates/oxc_linter/src/rules/unicorn/no_useless_spread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,24 +389,30 @@ fn check_useless_clone<'a>(
return;
}

// unsafe fixer of `[...new Array(1)]`
if let Expression::NewExpression(new_expr) = target {
let is_array_constructor = new_expr
.callee
.without_parentheses()
.get_identifier_reference()
.is_some_and(|id| id.name == "Array");

if is_array_constructor && new_expr.arguments.len() == 1 {
return;
}
}

let hint = target.const_eval();
let hint_matches_expr = if is_array { hint.is_array() } else { hint.is_object() };
if hint_matches_expr {
let name = diagnostic_name(ctx, target);

// `[...new Array(1)]` -> `new Array(1).fill()`
if let Expression::NewExpression(new_expr) = target {
let is_array_constructor = new_expr
.callee
.without_parentheses()
.get_identifier_reference()
.is_some_and(|id| id.name == "Array");

if is_array_constructor && new_expr.arguments.len() == 1 {
ctx.diagnostic_with_fix(clone(span, is_array, name), |fixer| {
fixer.replace(
array_or_obj_span,
format!("{}.fill()", fixer.source_range(spread_elem.argument.span())),
)
});
return;
}
}

ctx.diagnostic_with_fix(clone(span, is_array, name), |fixer| {
fix_by_removing_array_spread(fixer, &array_or_obj_span, spread_elem)
});
Expand Down Expand Up @@ -588,7 +594,6 @@ fn test() {
"[...arr.reduce((set, b) => set.add(b), new Set(iter))]",
// NOTE: we may want to consider this a violation in the future
"[...(foo ? new Set() : [])]",
"[...new Array(3)]",
];

let fail = vec![
Expand Down Expand Up @@ -678,6 +683,7 @@ fn test() {
r"[...foo.toSpliced(0, 1)]",
r"[...foo.with(0, bar)]",
r#"[...foo.split("|")]"#,
r"[...new Array(3)]",
r"[...Object.keys(foo)]",
r"[...Object.values(foo)]",
r"[...Array.from(foo)]",
Expand Down Expand Up @@ -736,6 +742,7 @@ fn test() {
// useless clones - simple arrays
("[...foo.map(x => !!x)]", "foo.map(x => !!x)"),
("[...new Array()]", "new Array()"),
("[...new Array(3)]", "new Array(3).fill()"),
("[...new Array(1, 2, 3)]", "new Array(1, 2, 3)"),
// useless clones - complex
(r"[...await Promise.all(foo)]", r"await Promise.all(foo)"),
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_linter/src/snapshots/no_useless_spread.snap
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,13 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: `foo.split` returns a new array. Spreading it into an array expression to create a new array is redundant.

eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:2]
1 │ [...new Array(3)]
· ───
╰────
help: `new Array(3)` returns a new array. Spreading it into an array expression to create a new array is redundant.

eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:2]
1 │ [...Object.keys(foo)]
Expand Down

0 comments on commit 9871a55

Please sign in to comment.