question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`prefer-spread`: Add prefer spread over `.slice()/.concat()`

See original GitHub issue

Currently, 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sindresorhuscommented, Feb 16, 2021

@coreyfarrell You can just use [value].flat() for that (we will eventually enforce that https://github.com/sindresorhus/eslint-plugin-unicorn/issues/975).

2reactions
sindresorhuscommented, Jan 26, 2021

@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 catch Array#slice(). // eslint-disable are good for edge-cases like this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found