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