Index

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 中删除。

进阶读物