Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions crates/oxc_linter/src/rules/unicorn/no_useless_spread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ fn spread_in_list(span: Span, arr_or_obj: &str) -> OxcDiagnostic {
}

fn spread_in_arguments(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Using a spread operator here creates a new array unnecessarily.").with_help("This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.\nFor example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.").with_label(span)
OxcDiagnostic::warn("Using a spread operator here creates a new array unnecessarily.")
.with_help("Pass arguments directly instead of spreading an array.")
.with_label(span)
}

fn iterable_to_array(span: Span, ctor_name: &str) -> OxcDiagnostic {
Expand Down Expand Up @@ -215,7 +217,18 @@ fn check_useless_spread_in_list<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) -
// foo(...[ ])
AstKind::Argument(_) => {
ctx.diagnostic_with_fix(spread_in_arguments(span), |fixer| {
fix_by_removing_array_spread(fixer, array_expr, spread_elem)
let replacer = if let Some(first) = array_expr.elements.first() {
let mut span = first.span();
if array_expr.elements.len() != 1 {
let last = array_expr.elements.last().unwrap();
span = Span::new(first.span().start, last.span().end);
}
ctx.source_range(span)
} else {
""
};

fixer.replace(spread_elem.span(), replacer)
});
true
}
Expand Down Expand Up @@ -744,6 +757,9 @@ fn test() {
("[...((0, []))]", "((0, []))"),
("[...arr.reduce((a, b) => a.push(b), [])]", "arr.reduce((a, b) => a.push(b), [])"),
("[...arr.reduce((a, b) => a.push(b), [])]", "arr.reduce((a, b) => a.push(b), [])"),
// Issue: <https://github.com/oxc-project/oxc/issues/8115>
("setupServer(...[...importHandlers])", "setupServer(...importHandlers)"),
("setupServer(...[1, 2, 3])", "setupServer(1, 2, 3)"),
];
Tester::new(NoUselessSpread::NAME, NoUselessSpread::PLUGIN, pass, fail)
.expect_fix(fix)
Expand Down
84 changes: 28 additions & 56 deletions crates/oxc_linter/src/snapshots/unicorn_no_useless_spread.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[a])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[a])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -50,16 +48,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[a,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[a,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -80,16 +76,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[a,],)
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[a,],)
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -110,16 +104,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...(( [a] )))
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...(( [a] )))
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -140,16 +132,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -163,16 +153,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -186,16 +174,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[,,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[,,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -209,16 +195,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[a, , b,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[a, , b,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -232,24 +216,21 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[a, , b,],)
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[a, , b,],)
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:5]
1 │ foo(...[,, ,(( a )), ,,(0, b), ,,])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:19]
Expand All @@ -270,16 +251,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(a, ...[a, b])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:12]
1 │ new Foo(a, ...[a, b])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:16]
Expand All @@ -300,16 +279,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(...[a, b], b,)
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Foo(...[a, b], b,)
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:19]
Expand All @@ -330,16 +307,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ foo(a, ...[a, b], b,)
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:12]
1 │ new Foo(a, ...[a, b], b,)
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new object unnecessarily.
╭─[no_useless_spread.tsx:1:8]
Expand Down Expand Up @@ -374,16 +349,14 @@ source: crates/oxc_linter/src/tester.rs
1 │ Promise.all(...[...iterable])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): Using a spread operator here creates a new array unnecessarily.
╭─[no_useless_spread.tsx:1:9]
1 │ new Map(...[...iterable])
· ───
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

⚠ eslint-plugin-unicorn(no-useless-spread): `Map` accepts an iterable, so it's unnecessary to convert the iterable to an array.
╭─[no_useless_spread.tsx:1:22]
Expand Down Expand Up @@ -707,8 +680,7 @@ source: crates/oxc_linter/src/tester.rs
· ───
4 │ }
╰────
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
help: Pass arguments directly instead of spreading an array.

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