2 title: generator-star-spacing
6 - https://leanpub.com/understandinges6/read/#leanpub-auto-generators
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.
14 Here is an example of a generator function:
19 function* generator() {
30 function *generator() {
36 This is valid as well:
41 function * generator() {
47 To keep a sense of consistency when using generators this rule enforces a single position for the `*`.
51 This rule aims to enforce spacing around the `*` of generator functions.
55 The rule takes one option, an object, which has two keys `before` and `after` having boolean values `true` or `false`.
57 * `before` enforces spacing between the `*` and the `function` keyword.
58 If it is `true`, a space is required, otherwise spaces are disallowed.
60 In object literal shorthand methods, spacing before the `*` is not checked, as they lack a `function` keyword.
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.
65 The default is `{"before": true, "after": false}`.
67 An example configuration:
70 "generator-star-spacing": ["error", {"before": true, "after": false}]
73 And the option has shorthand as a string keyword:
75 * `{"before": true, "after": false}` → `"before"`
76 * `{"before": false, "after": true}` → `"after"`
77 * `{"before": true, "after": true}` → `"both"`
78 * `{"before": false, "after": false}` → `"neither"`
80 An example of shorthand configuration:
83 "generator-star-spacing": ["error", "after"]
86 Additionally, this rule allows further configurability via overrides per function type.
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
92 An example of a configuration with overrides:
95 "generator-star-spacing": ["error", {
98 "anonymous": "neither",
99 "method": {"before": true, "after": true}
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.
111 Examples of **correct** code for this rule with the `"before"` option:
116 /*eslint generator-star-spacing: ["error", {"before": true, "after": false}]*/
119 function *generator() {}
121 var anonymous = function *() {};
123 var shorthand = { *generator() {} };
130 Examples of **correct** code for this rule with the `"after"` option:
135 /*eslint generator-star-spacing: ["error", {"before": false, "after": true}]*/
138 function* generator() {}
140 var anonymous = function* () {};
142 var shorthand = { * generator() {} };
149 Examples of **correct** code for this rule with the `"both"` option:
154 /*eslint generator-star-spacing: ["error", {"before": true, "after": true}]*/
157 function * generator() {}
159 var anonymous = function * () {};
161 var shorthand = { * generator() {} };
168 Examples of **correct** code for this rule with the `"neither"` option:
173 /*eslint generator-star-spacing: ["error", {"before": false, "after": false}]*/
176 function*generator() {}
178 var anonymous = function*() {};
180 var shorthand = { *generator() {} };
185 Examples of **incorrect** code for this rule with overrides present:
190 /*eslint generator-star-spacing: ["error", {
193 "anonymous": "neither",
194 "method": {"before": true, "after": true}
198 function * generator() {}
200 var anonymous = function* () {};
202 var shorthand = { *generator() {} };
204 class Class { static* method() {} }
209 Examples of **correct** code for this rule with overrides present:
214 /*eslint generator-star-spacing: ["error", {
217 "anonymous": "neither",
218 "method": {"before": true, "after": true}
222 function* generator() {}
224 var anonymous = function*() {};
226 var shorthand = { * generator() {} };
228 class Class { static * method() {} }
233 ## When Not To Use It
235 If your project will not be using generators or you are not concerned with spacing consistency, you do not need this rule.