2 title: generator-star-spacing
5 - https://leanpub.com/understandinges6/read/#leanpub-auto-generators
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.
13 Here is an example of a generator function:
18 function* generator() {
29 function *generator() {
35 This is valid as well:
40 function * generator() {
46 To keep a sense of consistency when using generators this rule enforces a single position for the `*`.
50 This rule aims to enforce spacing around the `*` of generator functions.
54 The rule takes one option, an object, which has two keys `"before"` and `"after"` having boolean values `true` or `false`.
56 * `"before"` enforces spacing between the `*` and the `function` keyword.
57 If it is `true`, a space is required, otherwise spaces are disallowed.
59 In object literal shorthand methods, spacing before the `*` is not checked, as they lack a `function` keyword.
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.
64 The default is `{"before": true, "after": false}`.
66 An example configuration:
69 "generator-star-spacing": ["error", {"before": true, "after": false}]
72 And the option has shorthand as a string keyword:
74 * `{"before": true, "after": false}` → `"before"`
75 * `{"before": false, "after": true}` → `"after"`
76 * `{"before": true, "after": true}` → `"both"`
77 * `{"before": false, "after": false}` → `"neither"`
79 An example of shorthand configuration:
82 "generator-star-spacing": ["error", "after"]
85 Additionally, this rule allows further configurability via overrides per function type.
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
91 An example of a configuration with overrides:
94 "generator-star-spacing": ["error", {
97 "anonymous": "neither",
98 "method": {"before": true, "after": true}
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.
110 Examples of **correct** code for this rule with the `"before"` option:
115 /*eslint generator-star-spacing: ["error", {"before": true, "after": false}]*/
118 function *generator() {}
120 var anonymous = function *() {};
122 var shorthand = { *generator() {} };
129 Examples of **correct** code for this rule with the `"after"` option:
134 /*eslint generator-star-spacing: ["error", {"before": false, "after": true}]*/
137 function* generator() {}
139 var anonymous = function* () {};
141 var shorthand = { * generator() {} };
148 Examples of **correct** code for this rule with the `"both"` option:
153 /*eslint generator-star-spacing: ["error", {"before": true, "after": true}]*/
156 function * generator() {}
158 var anonymous = function * () {};
160 var shorthand = { * generator() {} };
167 Examples of **correct** code for this rule with the `"neither"` option:
172 /*eslint generator-star-spacing: ["error", {"before": false, "after": false}]*/
175 function*generator() {}
177 var anonymous = function*() {};
179 var shorthand = { *generator() {} };
184 Examples of **incorrect** code for this rule with overrides present:
189 /*eslint generator-star-spacing: ["error", {
192 "anonymous": "neither",
193 "method": {"before": true, "after": true}
197 function * generator() {}
199 var anonymous = function* () {};
201 var shorthand = { *generator() {} };
203 class Class { static* method() {} }
208 Examples of **correct** code for this rule with overrides present:
213 /*eslint generator-star-spacing: ["error", {
216 "anonymous": "neither",
217 "method": {"before": true, "after": true}
221 function* generator() {}
223 var anonymous = function*() {};
225 var shorthand = { * generator() {} };
227 class Class { static * method() {} }
232 ## When Not To Use It
234 If your project will not be using generators or you are not concerned with spacing consistency, you do not need this rule.