`prefer-spread`: Add prefer spread over `.slice()/.concat()`
See original GitHub issueCurrently, prefer-spread
only check Array.from()
, how about add prefer spread over .slice()/.concat()
?
Fail
const foo = array.slice();
const foo = array.concat(item);
const foo = array.concat(anotherArray);
Pass
const foo = [...array];
const foo = array.slice(1);
The concat()
case, we can’t know item
is array or not.
Maybe with suggestions?
Suggestion 1: [...array, itemOrArray]
Suggestion 2: [...array, ...itemOrArray]
Another problem is TypedArray#slice()
and String#concat()
. String#slice
should not a problem. Nobody use string.slice()
(no arguments).
Any other method people use to clone an array?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:8 (6 by maintainers)
Top Results From Across the Web
eslint-plugin-unicorn/prefer-spread.md at main - GitHub
Prefer the spread operator over Array.from(…) , Array#concat(…) , Array#slice() and String#split(''). This rule is enabled in the ✓ recommended config.
Read more >prefer-spread - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >spread operator vs array.concat() - javascript - Stack Overflow
concat and spreads are very different when the argument is not an array. When the argument is not an array, concat adds it...
Read more >Array concat, slice and spread operator - YouTube
Learn how to combine and copy arrays using concat, slice and spread ...
Read more >Which Should You Use? push VS concat VS spread - YouTube
There are three different methods we use when we are adding items to an array or combining arrays. In this tutorial we are...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@coreyfarrell You can just use
[value].flat()
for that (we will eventually enforce that https://github.com/sindresorhus/eslint-plugin-unicorn/issues/975).@fisker Maybe we can make it opt-in? I have personally never used or seen
.slice()
used on TypedArray. For me, it’s more valuable to catchArray#slice()
.// eslint-disable
are good for edge-cases like this.