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