Skip to content

Commit ff13be6

Browse files
committedMar 22, 2025·
fix(linter): correct fixer for spread in function arguments (#9972)
closes #8115
1 parent b9f80b9 commit ff13be6

File tree

2 files changed

+46
-58
lines changed

2 files changed

+46
-58
lines changed
 

‎crates/oxc_linter/src/rules/unicorn/no_useless_spread/mod.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ fn spread_in_list(span: Span, arr_or_obj: &str) -> OxcDiagnostic {
3131
}
3232

3333
fn spread_in_arguments(span: Span) -> OxcDiagnostic {
34-
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)
34+
OxcDiagnostic::warn("Using a spread operator here creates a new array unnecessarily.")
35+
.with_help("Pass arguments directly instead of spreading an array.")
36+
.with_label(span)
3537
}
3638

3739
fn iterable_to_array(span: Span, ctor_name: &str) -> OxcDiagnostic {
@@ -215,7 +217,18 @@ fn check_useless_spread_in_list<'a>(node: &AstNode<'a>, ctx: &LintContext<'a>) -
215217
// foo(...[ ])
216218
AstKind::Argument(_) => {
217219
ctx.diagnostic_with_fix(spread_in_arguments(span), |fixer| {
218-
fix_by_removing_array_spread(fixer, array_expr, spread_elem)
220+
let replacer = if let Some(first) = array_expr.elements.first() {
221+
let mut span = first.span();
222+
if array_expr.elements.len() != 1 {
223+
let last = array_expr.elements.last().unwrap();
224+
span = Span::new(first.span().start, last.span().end);
225+
}
226+
ctx.source_range(span)
227+
} else {
228+
""
229+
};
230+
231+
fixer.replace(spread_elem.span(), replacer)
219232
});
220233
true
221234
}
@@ -744,6 +757,9 @@ fn test() {
744757
("[...((0, []))]", "((0, []))"),
745758
("[...arr.reduce((a, b) => a.push(b), [])]", "arr.reduce((a, b) => a.push(b), [])"),
746759
("[...arr.reduce((a, b) => a.push(b), [])]", "arr.reduce((a, b) => a.push(b), [])"),
760+
// Issue: <https://github.com/oxc-project/oxc/issues/8115>
761+
("setupServer(...[...importHandlers])", "setupServer(...importHandlers)"),
762+
("setupServer(...[1, 2, 3])", "setupServer(1, 2, 3)"),
747763
];
748764
Tester::new(NoUselessSpread::NAME, NoUselessSpread::PLUGIN, pass, fail)
749765
.expect_fix(fix)

‎crates/oxc_linter/src/snapshots/unicorn_no_useless_spread.snap

+28-56
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ source: crates/oxc_linter/src/tester.rs
2020
1foo(...[a])
2121
· ───
2222
╰────
23-
help: This function accepts a rest parameter, it's unnecessary to create a new array and then spread it. Instead, supply the arguments directly.
24-
For example, replace `foo(...[1, 2, 3])` with `foo(1, 2, 3)`.
23+
help: Pass arguments directly instead of spreading an array.
2524

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.