Skip to content

eslint/prefer-spread Style

🛠️ An auto-fix is available for this rule for some violations.

This rule is combined 2 rules from eslint:prefer-spread and unicorn:prefer-spread.

original eslint:prefer-spread

What it does

Require spread operators instead of .apply()

Why is this bad?

Before ES2015, one must use Function.prototype.apply() to call variadic functions.

javascript
var args = [1, 2, 3, 4];
Math.max.apply(Math, args);

In ES2015, one can use spread syntax to call variadic functions.

javascript
var args = [1, 2, 3, 4];
Math.max(...args);

Examples

Examples of incorrect code for this rule:

javascript
foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);

Examples of correct code for this rule:

javascript
// Using spread syntax
foo(...args);
obj.foo(...args);

// The `this` binding is different.
foo.apply(obj, args);
obj.foo.apply(null, args);
obj.foo.apply(otherObj, args);

// The argument list is not variadic.
// Those are warned by the `no-useless-call` rule.
foo.apply(undefined, [1, 2, 3]);
foo.apply(null, [1, 2, 3]);
obj.foo.apply(obj, [1, 2, 3]);

unicorn:prefer-spread

What it does

Enforces the use of the spread operator (...) over outdated patterns.

Why is this bad?

Using the spread operator is more concise and readable.

Examples

Examples of incorrect code for this rule:

javascript
const foo = Array.from(set);
const foo = Array.from(new Set([1, 2]));

Examples of correct code for this rule:

javascript
[...set].map(() => {});
Array.from(...argumentsArray);

References

Released under the MIT License.