]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/generator-star.md
f344cc469e193e4cf1927829187d19df8842e2ff
[pve-eslint.git] / eslint / docs / rules / generator-star.md
1 # generator-star: enforce consistent spacing around the asterisk in generator functions
2
3 (removed) This rule was **removed** in ESLint v1.0 and **replaced** by the [generator-star-spacing](generator-star-spacing.md) rule.
4
5 Generators are a new type of function in ECMAScript 6 that can return multiple values over time.
6 These special functions are indicated by placing an `*` after the `function` keyword.
7
8 Here is an example of a generator function:
9
10 ```js
11 /*eslint-env es6*/
12
13 function* generator() {
14 yield "44";
15 yield "55";
16 }
17 ```
18
19 This is also valid:
20
21 ```js
22 /*eslint-env es6*/
23
24 function *generator() {
25 yield "44";
26 yield "55";
27 }
28 ```
29
30 This is valid as well:
31
32 ```js
33 /*eslint-env es6*/
34
35 function * generator() {
36 yield "44";
37 yield "55";
38 }
39 ```
40
41 To keep a sense of consistency when using generators this rule enforces a single position for the `*`.
42
43 ## Rule Details
44
45 This rule enforces that the `*` is either placed next to the `function` keyword or the name of the function. The single
46 option for this rule is a string specifying the placement of the asterisk. For this option you may pass
47 `"start"`, `"middle"` or `"end"`. The default is `"end"`.
48
49 You can set the style in configuration like this:
50
51 ```json
52 "generator-star": ["error", "start"]
53 ```
54
55 When using `"start"` this placement will be enforced:
56
57 ```js
58 /*eslint-env es6*/
59
60 function* generator() {
61 }
62 ```
63
64 When using `"middle"` this placement will be enforced:
65
66 ```js
67 /*eslint-env es6*/
68
69 function * generator() {
70 }
71 ```
72
73 When using `"end"` this placement will be enforced:
74
75 ```js
76 /*eslint-env es6*/
77
78 function *generator() {
79 }
80 ```
81
82 When using the expression syntax `"start"` will be enforced here:
83
84 ```js
85 /*eslint-env es6*/
86
87 var generator = function* () {
88 }
89 ```
90
91 When using the expression syntax `"middle"` will be enforced here:
92
93 ```js
94 /*eslint-env es6*/
95
96 var generator = function * () {
97 }
98 ```
99
100 When using the expression syntax `"end"` will be enforced here:
101
102 ```js
103 /*eslint-env es6*/
104
105 var generator = function *() {
106 }
107 ```
108
109 When using the expression syntax this is valid for both `"start"` and `"end"`:
110
111 ```js
112 /*eslint-env es6*/
113
114 var generator = function*() {
115 }
116 ```
117
118 The shortened object literal syntax for generators is not affected by this rule.
119
120 ## When Not To Use It
121
122 If your project will not be using generators you do not need this rule.
123
124 ## Further Reading
125
126 * [Understanding ES6: Generators](https://leanpub.com/understandinges6/read/#leanpub-auto-generators)