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