Skip to content

Latest commit

 

History

History
133 lines (100 loc) · 2.58 KB

no-useless-spread.md

File metadata and controls

133 lines (100 loc) · 2.58 KB

Disallow unnecessary spread

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

  • Using spread syntax in the following cases is unnecessary:

    • Spread an array literal as elements of an array literal
    • Spread an array literal as arguments of a call or a new call
    • Spread an object literal as properties of an object literal
    • Use spread syntax to clone an array created inline
  • The following builtins accept an iterable, so it's unnecessary to convert the iterable to an array:

    • Map constructor
    • WeakMap constructor
    • Set constructor
    • WeakSet constructor
    • TypedArray constructor
    • Array.from(…)
    • TypedArray.from(…)
    • Promise.{all,allSettled,any,race}(…)
    • Object.fromEntries(…)
  • for…of loop can iterate over any iterable object not just array, so it's unnecessary to convert the iterable to an array.

  • yield* can delegate to another iterable, so it's unnecessary to convert the iterable to an array.

Fail

const array = [firstElement, ...[secondElement], thirdElement];
const object = {firstProperty, ...{secondProperty}, thirdProperty};
foo(firstArgument, ...[secondArgument], thirdArgument);
const object = new Foo(firstArgument, ...[secondArgument], thirdArgument);
const set = new Set([...iterable]);
const results = await Promise.all([...iterable]);
for (const foo of [...set]);
function * foo() {
	yield * [...anotherGenerator()];
}
function foo(bar) {
	return [
		...bar.map(x => x * 2),
	];
}

Pass

const array = [firstElement, secondElement, thirdElement];
const object = {firstProperty, secondProperty, thirdProperty};
foo(firstArgument, secondArgument, thirdArgument);
const object = new Foo(firstArgument, secondArgument, thirdArgument);
const array = [...foo, bar];
const object = {...foo, bar};
foo(foo, ...bar);
const object = new Foo(...foo, bar);
const set = new Set(iterable);
const results = await Promise.all(iterable);
for (const foo of set);
function * foo() {
	yield * anotherGenerator();
}
function foo(bar) {
	return bar.map(x => x * 2);
}