generator-star

在生成器函数中强制星号周围的间距一致。

¥Enforces consistent spacing around the asterisk in generator functions.

生成器是 ECMAScript 6 中的一种新型函数,它可以随着时间的推移返回多个值。这些特殊功能通过在 function 关键字后放置 * 来表示。

¥Generators are a new type of function in ECMAScript 6 that can return multiple values over time. These special functions are indicated by placing an * after the function keyword.

以下是生成器函数的示例:

¥Here is an example of a generator function:

function* generator() {
    yield "44";
    yield "55";
}

这也是有效的:

¥This is also valid:

function *generator() {
    yield "44";
    yield "55";
}

这也是有效的:

¥This is valid as well:

function * generator() {
    yield "44";
    yield "55";
}

为了在使用生成器时保持一致性,此规则对 * 强制执行单个位置。

¥To keep a sense of consistency when using generators this rule enforces a single position for the *.

规则详情

¥Rule Details

此规则强制将 * 放置在 function 关键字或函数名称旁边。此规则的唯一选项是指定星号位置的字符串。对于此选项,你可以通过 "start""middle""end"。默认值为 "end"

¥This rule enforces that the * is either placed next to the function keyword or the name of the function. The single option for this rule is a string specifying the placement of the asterisk. For this option you may pass "start", "middle" or "end". The default is "end".

你可以像这样在配置中设置样式:

¥You can set the style in configuration like this:

"generator-star": ["error", "start"]

使用 "start" 时,将强制执行此放置:

¥When using "start" this placement will be enforced:

function* generator() {
}

使用 "middle" 时,将强制执行此放置:

¥When using "middle" this placement will be enforced:

function * generator() {
}

使用 "end" 时,将强制执行此放置:

¥When using "end" this placement will be enforced:

function *generator() {
}

使用表达式语法时,"start" 将在此处强制执行:

¥When using the expression syntax "start" will be enforced here:

var generator = function* () {
}

使用表达式语法时,"middle" 将在此处强制执行:

¥When using the expression syntax "middle" will be enforced here:

var generator = function * () {
}

使用表达式语法时,"end" 将在此处强制执行:

¥When using the expression syntax "end" will be enforced here:

var generator = function *() {
}

使用表达式语法时,这对 "start""end" 都有效:

¥When using the expression syntax this is valid for both "start" and "end":

var generator = function*() {
}

生成器的缩短的对象字面语法不受此规则的影响。

¥The shortened object literal syntax for generators is not affected by this rule.

何时不使用

¥When Not To Use It

如果你的项目不使用生成器,则不需要此规则。

¥If your project will not be using generators you do not need this rule.

版本

此规则是在 ESLint v0.12.0 中引入,并在 v1.0.0-rc-1 中删除。

进阶读物