]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/rest-spread-spacing.md
8850bead00f59d6d44131ee2c1098c612338ed47
[pve-eslint.git] / eslint / docs / src / rules / rest-spread-spacing.md
1 ---
2 title: rest-spread-spacing
3 layout: doc
4 rule_type: layout
5 further_reading:
6 - https://github.com/tc39/proposal-object-rest-spread
7 ---
8
9
10
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:
12
13 ```js
14 let numArr = [1, 2, 3];
15 function add(a, b, c) {
16 return a + b + c;
17 }
18 add(...numArr); // -> 6
19
20 let arr1 = [1, 2, 3];
21 let arr2 = [4, 5, 6];
22 arr1.push(...arr2); // -> [1, 2, 3, 4, 5, 6]
23
24 let [a, b, ...arr] = [1, 2, 3, 4, 5];
25 a; // -> 1
26 b // -> 2
27 arr; // -> [3, 4, 5]
28
29 function numArgs(...args) {
30 return args.length;
31 }
32 numArgs(a, b, c); // -> 3
33 ```
34
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:
36
37 ```js
38
39 let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
40 x; // -> 1
41 y; // -> 2
42 z; // -> { a: 3, b: 4 }
43
44 let n = { x, y, ...z };
45 n; // -> { x: 1, y: 2, a: 3, b: 4 }
46 ```
47
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.
49
50 ## Rule Details
51
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:
53
54 ```json
55 {
56 "parserOptions": {
57 "ecmaVersion": 2018
58 }
59 }
60 ```
61
62 Please read the user guide's section on [configuring parser options](/docs/user-guide/configuring#specifying-parser-options) to learn more.
63
64 ## Options
65
66 This rule takes one option: a string with the value of `"never"` or `"always"`. The default value is `"never"`.
67
68 ### "never"
69
70 When using the default `"never"` option, whitespace is not allowed between spread operators and their expressions.
71
72 ```json
73 rest-spread-spacing: ["error"]
74 ```
75
76 or
77
78 ```json
79 rest-spread-spacing: ["error", "never"]
80 ```
81
82 Examples of **incorrect** code for this rule with `"never"`:
83
84 ::: incorrect
85
86 ```js
87 /*eslint rest-spread-spacing: ["error", "never"]*/
88
89 fn(... args)
90 [... arr, 4, 5, 6]
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 };
95 ```
96
97 :::
98
99 Examples of **correct** code for this rule with `"never"`:
100
101 ::: correct
102
103 ```js
104 /*eslint rest-spread-spacing: ["error", "never"]*/
105
106 fn(...args)
107 [...arr, 4, 5, 6]
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 };
112 ```
113
114 :::
115
116 ### "always"
117
118 When using the `"always"` option, whitespace is required between spread operators and their expressions.
119
120 ```json
121 rest-spread-spacing: ["error", "always"]
122 ```
123
124 Examples of **incorrect** code for this rule with `"always"`:
125
126 ::: incorrect
127
128 ```js
129 /*eslint rest-spread-spacing:["error", "always"]*/
130
131 fn(...args)
132 [...arr, 4, 5, 6]
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 };
137 ```
138
139 :::
140
141 Examples of **correct** code for this rule with `"always"`:
142
143 ::: correct
144
145 ```js
146 /*eslint rest-spread-spacing: ["error", "always"]*/
147
148 fn(... args)
149 [... arr, 4, 5, 6]
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 };
154 ```
155
156 :::
157
158 ## When Not To Use It
159
160 You can safely disable this rule if you do not care about enforcing consistent spacing between spread operators and their expressions.