rest-spread-spacing

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

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

ES2015 引入了 rest 和 spread 运算符,它们将可迭代的结构扩展到其各个部分。它们的一些用法示例如下:

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

除了上述之外,目前还有一个提议是在规范中添加 object rest 和 spread 属性。它们可以按如下方式使用:


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 运算符与它所操作的表达式之间允许有空格,这可能导致代码库中的间距不一致。

规则详情

Y8ONwgFf6PD46x1w8iXB/G/OD3UFBBe4/Z0RiAI/2NAesR5QZqW2OXPcMM5q/PZmJ1VeWu1BmLS//ew9CgJTZ8YtT+vXh6PtV8Zxa5O4ERJAiu0MsZIDBhTgu/MqhorKRkjEVBLZ+jMIuw6bgQ5K471mdJDqTT8V+JGNEJiHHGpETzh4moay1Z9rlPJyomc6LwYqWVh2qrMzkG7583873TVLYGw0vdJBHkTCK732dzI=

{
    "parserOptions": {
        "ecmaVersion": 2018
    }
}

Q1Addav4+Ao13gjBWzV+Zf51Nhfp5NxN7HHTgk3d6AgCiqGXU7ykw66P06OCycdkupkfJ45g0i4HdGmQ6HWBZRbclwMO0ZDnTKY32cRJkywHhRRpECY0AL+GvazWaH0/GpZDdFDsxnEvafHr5oE1JVGmALH28/zqPQJwaCb0okJ7zuqYkvI75l2q43QI4rrw

选项

HX4Qsndi6mjc8iPAb/kIn6eBdmMtGuXqZr9Kp18Mb6ILrClEVGJL6q3e4U5mFl2qSIa0S3BgS+ViIlJBHfZAFGw202r6lVmHHo1aERQmli1iNtguQHXcLNoCUaZ+kvEEOK7eqaRQ5QnnNqu6mZFQdHxz8ZkrIe/MsXIWGz4tgDPisPo0cYRdbZniHmfb89+V

"never"

/W27lLFYPvKLIkA88Qpac8WFfUnI7MAGGqMg9F6Ojsax6yAbGWCBEre7k2v7/hd48OiVjqSsOTgvNqnk7Wz11M/urW/ObubsvY3yP2/Aywgi6ASJGYgjq5ZUeLn9ds9ENMlXUy54IAoYbUraDj9VlQ==

rest-spread-spacing: ["error"]

5VtFLu83t88XyDaNPYMT9w==

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

JN6ecJM9dSbhMdVJpJelv+aHSdwbQ+scL4FrnoVe0UmXNrwY5wKCiiJf5uzVZP2tnb0vMhzksPJPTaqZ1IcAdA==

/*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 };

PwR5DrJrN80min96hKNCvTqBajX6zEp21bd6ZYasO8ylAnaFf0LFBKuEeMjESQ9caanRuXWhogTIBjW2pPAqemj/2OzGvLhg3Wtcii0lBE8=

/*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"

uHqINRr2VrgOn8iPsFEyhELi1KPc97AuEYH95ttteUWtQWs6fzlQDEWKXIT4b2JYALWcfwU1z0/v5cL42pIVk0duP1CmEG5QxtMO714M68Bhm92sIEKgJV3uVozVt3Oc

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

Qq6MGVeFSEqS/fkiMVe3TFumA9S81dJO8VTt8M0X+1s5jiG7vHtj5FbCOUrK3cifBPqFK4fPO7a3ivyaISjPDA==

/*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 };

PwR5DrJrN80min96hKNCvTqBajX6zEp21bd6ZYasO8z3RLUF29Mnsp1UJ9+6xRXTJ8cXU6TCK1coKMAmJVTfD0P57gEzZbccs9Ds0A+Z+Qg=

/*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 };

何时不使用

zXXq1lVIqQfAG97FXseTuVnuANv0GURrjAW88vzaH2npZxloJv1dy7hv+C2K7SzLcwdpmHgB6qnwh+xxV8CG06b3Pchd6WsXETOyG7vLy7dE77zif6TLoLMwaP3B6Ka0OeZ2kBUBvVjSfyhDa4ODFw/7QmzAVXHtlvlCqNcWZN4=