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