🐊Putout plugin adds ability to remove useless spread
npm install @putout/plugin-remove-useless-spread[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-remove-useless-spread.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-remove-useless-spread "npm"
> Spread syntax can be used when all elements from an object or array need to be included in a list of some kind.
>
> (c) MDN
🐊Putout plugin adds ability to remove useless spread syntax. Merged with @putout/plugin-spread.
```
npm i @putout/plugin-remove-useless-spread
`json`
{
"rules": {
"remove-useless-spread/array": "on",
"remove-useless-spread/object": "on",
"remove-useless-spread/nested": "on"
}
}
The thing is [...b] can be used for:
- copying an array;
- converting different value type like string to an array.
So better to be more concrete and use slice for copying and Array()/Array.from() for converting to decrease cognitive load.spread
Also sometimes there is no need on any of this operations, and we can drop .
`js
for (const a of [...b]) {}
const places = [...getPlaces()];
`
`js
for (const a of b) {}
const places = getPlaces();
// Array constructor creates sparse array
[...Array(5)].map(Number);
`
`js`
const a = {
...fn(),
};
`js`
const a = fn();
Checkout in 🐊Putout Editor.
`js`
[
...[
...a,
...b,
],
...x,
];
`js``
[
...a,
...b,
...x,
];
MIT