Index

rest-spread-spacing

强制 rest 和 spread 运算符及其表达式之间的间距

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

ES2015 引入了剩余参数和展开运算符,它们可以将可迭代结构扩展为其各个部分。它们的一些用法示例如下:

🌐 ES2015 introduced the rest and spread operators, which expand an iterable structure into its individual parts. Some examples of their usage are as follows:

let numArr = [1, 2, 3];
function add(a, b, c) {
    return a + b + c;
}
add(...numArr); // -> 6

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
arr1.push(...arr2); // -> [1, 2, 3, 4, 5, 6]

let [a, b, ...arr] = [1, 2, 3, 4, 5];
a; // -> 1
b // -> 2
arr; // ->  [3, 4, 5]

function numArgs(...args) {
  return args.length;
}
numArgs(a, b, c); // -> 3

除了上述内容,目前还有一个提议是将对象的剩余属性和扩展属性添加到规范中。它们可以如下使用:

🌐 In addition to the above, there is currently a proposal to add object rest and spread properties to the spec. They can be used as follows:


let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
x; // -> 1
y; // -> 2
z; // -> { a: 3, b: 4 }

let n = { x, y, ...z };
n; // -> { x: 1, y: 2, a: 3, b: 4 }

与其他运算符一样,rest 或 spread 运算符与它所操作的表达式之间允许有空格,这可能导致代码库中的间距不一致。

🌐 As with other operators, whitespace is allowed between the rest or spread operator and the expression it is operating on, which can lead to inconsistent spacing within a codebase.

规则详情

🌐 Rule Details

该规则旨在强制在 rest 和 spread 操作符及其表达式之间保持一致的空格。该规则还支持 ES2018 中的对象 rest 和 spread 属性:

🌐 This rule aims to enforce consistent spacing between rest and spread operators and their expressions. The rule also supports object rest and spread properties in ES2018:

{
    "languageOptions": {
        "ecmaVersion": 2018
    }
}

请阅读用户指南中关于配置解析器选项的部分以了解更多信息。

🌐 Please read the user guide’s section on configure parser options to learn more.

选项

🌐 Options

此规则接受一个选项:一个值为 "never""always" 的字符串。默认值是 "never"

🌐 This rule takes one option: a string with the value of "never" or "always". The default value is "never".

“never”

使用默认的 "never" 选项时,不允许在扩展运算符与其表达式之间有空白。

🌐 When using the default "never" option, whitespace is not allowed between spread operators and their expressions.

rest-spread-spacing: ["error"]

or

rest-spread-spacing: ["error", "never"]

此规则下使用 "never"错误代码示例:

🌐 Examples of incorrect code for this rule with "never":

在线运行
/*eslint rest-spread-spacing: ["error", "never"]*/

fn(... args);
[... arr, 4, 5, 6];
let [a, b, ... arr] = [1, 2, 3, 4, 5];
function fn(... args) { console.log(args); }
let { x, y, ... z } = { x: 1, y: 2, a: 3, b: 4 };
let n = { x, y, ... z };

使用 "never" 的此规则的正确代码示例:

🌐 Examples of correct code for this rule with "never":

在线运行
/*eslint rest-spread-spacing: ["error", "never"]*/

fn(...args);
[...arr, 4, 5, 6];
let [a, b, ...arr] = [1, 2, 3, 4, 5];
function fn(...args) { console.log(args); }
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
let n = { x, y, ...z };

“always”

在使用 "always" 选项时,展开运算符与其表达式之间需要空格。

🌐 When using the "always" option, whitespace is required between spread operators and their expressions.

rest-spread-spacing: ["error", "always"]

使用 "always" 的此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with "always":

在线运行
/*eslint rest-spread-spacing:["error", "always"]*/

fn(...args);
[...arr, 4, 5, 6];
let [a, b, ...arr] = [1, 2, 3, 4, 5];
function fn(...args) { console.log(args); }
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
let n = { x, y, ...z };

使用 "always" 的此规则的正确代码示例:

🌐 Examples of correct code for this rule with "always":

在线运行
/*eslint rest-spread-spacing: ["error", "always"]*/

fn(... args);
[... arr, 4, 5, 6];
let [a, b, ... arr] = [1, 2, 3, 4, 5];
function fn(... args) { console.log(args); }
let { x, y, ... z } = { x: 1, y: 2, a: 3, b: 4 };
let n = { x, y, ... z };

何时不使用

🌐 When Not To Use It

如果你不关心在展开运算符及其表达式之间强制保持一致的间距,则可以安全地禁用此规则。

🌐 You can safely disable this rule if you do not care about enforcing consistent spacing between spread operators and their expressions.

版本

此规则是在 ESLint v2.12.0 中引入。

进阶读物

资源