2 title: rest-spread-spacing
6 - https://github.com/tc39/proposal-object-rest-spread
11 ES2015 introduced the rest and spread operators, which expand an iterable structure into its individual parts. Some examples of their usage are as follows:
14 let numArr = [1, 2, 3];
15 function add(a, b, c) {
18 add(...numArr); // -> 6
22 arr1.push(...arr2); // -> [1, 2, 3, 4, 5, 6]
24 let [a, b, ...arr] = [1, 2, 3, 4, 5];
29 function numArgs(...args) {
32 numArgs(a, b, c); // -> 3
35 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:
39 let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
42 z; // -> { a: 3, b: 4 }
44 let n = { x, y, ...z };
45 n; // -> { x: 1, y: 2, a: 3, b: 4 }
48 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.
52 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:
62 Please read the user guide's section on [configuring parser options](/docs/user-guide/configuring#specifying-parser-options) to learn more.
66 This rule takes one option: a string with the value of `"never"` or `"always"`. The default value is `"never"`.
70 When using the default `"never"` option, whitespace is not allowed between spread operators and their expressions.
73 rest-spread-spacing: ["error"]
79 rest-spread-spacing: ["error", "never"]
82 Examples of **incorrect** code for this rule with `"never"`:
87 /*eslint rest-spread-spacing: ["error", "never"]*/
91 let [a, b, ... arr] = [1, 2, 3, 4, 5];
92 function fn(... args) { console.log(args); }
93 let { x, y, ... z } = { x: 1, y: 2, a: 3, b: 4 };
94 let n = { x, y, ... z };
99 Examples of **correct** code for this rule with `"never"`:
104 /*eslint rest-spread-spacing: ["error", "never"]*/
108 let [a, b, ...arr] = [1, 2, 3, 4, 5];
109 function fn(...args) { console.log(args); }
110 let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
111 let n = { x, y, ...z };
118 When using the `"always"` option, whitespace is required between spread operators and their expressions.
121 rest-spread-spacing: ["error", "always"]
124 Examples of **incorrect** code for this rule with `"always"`:
129 /*eslint rest-spread-spacing:["error", "always"]*/
133 let [a, b, ...arr] = [1, 2, 3, 4, 5];
134 function fn(...args) { console.log(args); }
135 let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
136 let n = { x, y, ...z };
141 Examples of **correct** code for this rule with `"always"`:
146 /*eslint rest-spread-spacing: ["error", "always"]*/
150 let [a, b, ... arr] = [1, 2, 3, 4, 5];
151 function fn(... args) { console.log(args); }
152 let { x, y, ... z } = { x: 1, y: 2, a: 3, b: 4 };
153 let n = { x, y, ... z };
158 ## When Not To Use It
160 You can safely disable this rule if you do not care about enforcing consistent spacing between spread operators and their expressions.