]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/generator-star-spacing.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / generator-star-spacing.md
1 ---
2 title: generator-star-spacing
3 layout: doc
4 rule_type: layout
5 further_reading:
6 - https://leanpub.com/understandinges6/read/#leanpub-auto-generators
7 ---
8
9
10
11 Generators are a new type of function in ECMAScript 6 that can return multiple values over time.
12 These special functions are indicated by placing an `*` after the `function` keyword.
13
14 Here is an example of a generator function:
15
16 ```js
17 /*eslint-env es6*/
18
19 function* generator() {
20 yield "44";
21 yield "55";
22 }
23 ```
24
25 This is also valid:
26
27 ```js
28 /*eslint-env es6*/
29
30 function *generator() {
31 yield "44";
32 yield "55";
33 }
34 ```
35
36 This is valid as well:
37
38 ```js
39 /*eslint-env es6*/
40
41 function * generator() {
42 yield "44";
43 yield "55";
44 }
45 ```
46
47 To keep a sense of consistency when using generators this rule enforces a single position for the `*`.
48
49 ## Rule Details
50
51 This rule aims to enforce spacing around the `*` of generator functions.
52
53 ## Options
54
55 The rule takes one option, an object, which has two keys `before` and `after` having boolean values `true` or `false`.
56
57 * `before` enforces spacing between the `*` and the `function` keyword.
58 If it is `true`, a space is required, otherwise spaces are disallowed.
59
60 In object literal shorthand methods, spacing before the `*` is not checked, as they lack a `function` keyword.
61
62 * `after` enforces spacing between the `*` and the function name (or the opening parenthesis for anonymous generator functions).
63 If it is `true`, a space is required, otherwise spaces are disallowed.
64
65 The default is `{"before": true, "after": false}`.
66
67 An example configuration:
68
69 ```json
70 "generator-star-spacing": ["error", {"before": true, "after": false}]
71 ```
72
73 And the option has shorthand as a string keyword:
74
75 * `{"before": true, "after": false}` → `"before"`
76 * `{"before": false, "after": true}` → `"after"`
77 * `{"before": true, "after": true}` → `"both"`
78 * `{"before": false, "after": false}` → `"neither"`
79
80 An example of shorthand configuration:
81
82 ```json
83 "generator-star-spacing": ["error", "after"]
84 ```
85
86 Additionally, this rule allows further configurability via overrides per function type.
87
88 * `named` provides overrides for named functions
89 * `anonymous` provides overrides for anonymous functions
90 * `method` provides overrides for class methods or property function shorthand
91
92 An example of a configuration with overrides:
93
94 ```json
95 "generator-star-spacing": ["error", {
96 "before": false,
97 "after": true,
98 "anonymous": "neither",
99 "method": {"before": true, "after": true}
100 }]
101 ```
102
103 In the example configuration above, the top level "before" and "after" options define the default behavior of
104 the rule, while the "anonymous" and "method" options override the default behavior.
105 Overrides can be either an object with "before" and "after", or a shorthand string as above.
106
107 ## Examples
108
109 ### before
110
111 Examples of **correct** code for this rule with the `"before"` option:
112
113 ::: correct
114
115 ```js
116 /*eslint generator-star-spacing: ["error", {"before": true, "after": false}]*/
117 /*eslint-env es6*/
118
119 function *generator() {}
120
121 var anonymous = function *() {};
122
123 var shorthand = { *generator() {} };
124 ```
125
126 :::
127
128 ### after
129
130 Examples of **correct** code for this rule with the `"after"` option:
131
132 ::: correct
133
134 ```js
135 /*eslint generator-star-spacing: ["error", {"before": false, "after": true}]*/
136 /*eslint-env es6*/
137
138 function* generator() {}
139
140 var anonymous = function* () {};
141
142 var shorthand = { * generator() {} };
143 ```
144
145 :::
146
147 ### both
148
149 Examples of **correct** code for this rule with the `"both"` option:
150
151 ::: correct
152
153 ```js
154 /*eslint generator-star-spacing: ["error", {"before": true, "after": true}]*/
155 /*eslint-env es6*/
156
157 function * generator() {}
158
159 var anonymous = function * () {};
160
161 var shorthand = { * generator() {} };
162 ```
163
164 :::
165
166 ### neither
167
168 Examples of **correct** code for this rule with the `"neither"` option:
169
170 ::: correct
171
172 ```js
173 /*eslint generator-star-spacing: ["error", {"before": false, "after": false}]*/
174 /*eslint-env es6*/
175
176 function*generator() {}
177
178 var anonymous = function*() {};
179
180 var shorthand = { *generator() {} };
181 ```
182
183 :::
184
185 Examples of **incorrect** code for this rule with overrides present:
186
187 ::: incorrect
188
189 ```js
190 /*eslint generator-star-spacing: ["error", {
191 "before": false,
192 "after": true,
193 "anonymous": "neither",
194 "method": {"before": true, "after": true}
195 }]*/
196 /*eslint-env es6*/
197
198 function * generator() {}
199
200 var anonymous = function* () {};
201
202 var shorthand = { *generator() {} };
203
204 class Class { static* method() {} }
205 ```
206
207 :::
208
209 Examples of **correct** code for this rule with overrides present:
210
211 ::: correct
212
213 ```js
214 /*eslint generator-star-spacing: ["error", {
215 "before": false,
216 "after": true,
217 "anonymous": "neither",
218 "method": {"before": true, "after": true}
219 }]*/
220 /*eslint-env es6*/
221
222 function* generator() {}
223
224 var anonymous = function*() {};
225
226 var shorthand = { * generator() {} };
227
228 class Class { static * method() {} }
229 ```
230
231 :::
232
233 ## When Not To Use It
234
235 If your project will not be using generators or you are not concerned with spacing consistency, you do not need this rule.