Skip to content

Latest commit

 

History

History
76 lines (51 loc) · 1.54 KB

File metadata and controls

76 lines (51 loc) · 1.54 KB

@putout/plugin-remove-useless-spread NPM version

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.

Install

npm i @putout/plugin-remove-useless-spread

Rule

{
    "rules": {
        "remove-useless-spread/array": "on",
        "remove-useless-spread/object": "on"
    }
}

array

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 in and use slice for copying and Array() for converting to decrease cognitive load. Also sometimes there is no need on any of this operations, and we can drop spread.

❌ Example of incorrect code

for (const a of [...b]) {}

const places = [...getPlaces()];

✅ Example of correct code

for (const a of b) {}

const places = getPlaces();

// Array constructor creates sparse array
[...Array(5)].map(Number);

object

❌ Example of incorrect code

const a = {
    ...fn(),
};

✅ Example of correct code

const a = fn();

License

MIT