@putout/plugin-spread
🐊Putout plugin adds ability to remove useless spread
Last updated 3 months ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @putout/plugin-spread 
SYNC missed versions from official npm registry.

@putout/plugin-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 transform spread syntax.

Install

npm i @putout/plugin-spread

Rules

Rule

{
    "rules": {
        "spread/convert-apply-to-spread": "on",
        "spread/convert-object-assign-to-merge-spread": "on",
        "spread/remove-useless-array": "on",
        "spread/remove-useless-object": "on",
        "spread/simplify-nested": "on",
        "spread/remove-useless-logical": "on"
    }
}

convert-object-assign-to-merge-spread

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object and returns the modified target object.

Spread syntax (...) allows an object expression to be expanded in places where zero or more key-value pairs are expected.

(c) MDN

Convert Object.assign() to merge spread since it shorter but does (mostly) the same.

❌ Example of incorrect code

function merge(a) {
    return Object.assign({}, a, {
        hello: 'world',
    });
}

βœ… Example of correct code

function merge(a) {
    return {
        ...a,
        hello: 'world',
    };
}

Comparison

Linter Rule Fix
🐊 Putout spread/convert-object-assign-to-merge-spread βœ…
⏣ ESLint prefer-object-spread βœ…

remove-useless-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 and use slice for copying and Array()/Array.from() 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);

remove-useless-object

❌ Example of incorrect code

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

βœ… Example of correct code

const a = fn();

nested

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

[
    ...[
        ...a,
        ...b,
    ],
    ...x,
];

βœ… Example of correct code

[
    ...a,
    ...b,
    ...x,
];

convert-apply-to-spread

Spread syntax (...) allows an array expression to be expanded in places where zero or more arguments are expected.

(c) MDN

❌ Example of incorrect code

console.apply(null, arguments);

βœ… Example of correct code

console.log(...arguments);

remove-useless-logical

Checkout in 🐊Putout Editor.

❌ Example of incorrect code

const state = {
    workbench: {
        ...base.workbench,
        ...overrides.workbench || {},
    },
};

βœ… Example of correct code

const state = {
    workbench: {
        ...base.workbench,
        ...overrides.workbench,
    },
};

License

MIT

Current Tags

  • 3.2.0                                ...           latest (3 months ago)

7 Versions

  • 3.2.0                                ...           3 months ago
  • 3.1.0                                ...           8 months ago
  • 3.0.0                                ...           9 months ago
  • 2.0.0                                ...           10 months ago
  • 1.2.0                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.0                                ...           a year ago
Maintainers (1)
Downloads
Total 1
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (5)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE