Enforce spacing around the * in generator functions (generator-star-spacing)

强制 generator 函数中 * 号周围有空格 (generator-star-spacing)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

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.

在 ECMAScript 6 中,Generators 是一个新的函数类型,随着时间的推移可以返回多个值。这些特殊的函数是在 function 关键字后放置一个 *

Here is an example of a generator function:

以下是个 generator 函数的示例:

/*eslint-env es6*/

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

This is also valid:

这个同样有效:

/*eslint-env es6*/

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

This is valid as well:

这个也是有效的:

/*eslint-env es6*/

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

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

为了保持使用 generators 函数的一致性,该规则对 * 强制设置一个单独的位置。

Rule Details

This rule aims to enforce spacing around the * of generator functions.

该规则旨在约束 generator 函数的 * 周围的空格。

Options

The rule takes one option, an object, which has two keys before and after having boolean values true or false.

该规则只有一个可选项,是个对象,有两个键beforeafter对应的值可以为truefalse

The default is {"before": true, "after": false}.

默认为 {"before": true, "after": false}

"generator-star-spacing": ["error", {"before": false, "after": true}]

And the option has shorthand as a string keyword:

这些选项可缩写为一个字符串关键字:

"generator-star-spacing": ["error", "after"]

Examples

before

Examples of correct code for this rule with the "before" option:

选项 "before"正确 代码示例:

/*eslint generator-star-spacing: ["error", {"before": true, "after": false}]*/
/*eslint-env es6*/

function *generator() {}

var anonymous = function *() {};

var shorthand = { *generator() {} };

after

Examples of correct code for this rule with the "after" option:

选项 "after"正确 代码示例:

/*eslint generator-star-spacing: ["error", {"before": false, "after": true}]*/
/*eslint-env es6*/

function* generator() {}

var anonymous = function* () {};

var shorthand = { * generator() {} };

both

Examples of correct code for this rule with the "both" option:

选项 "both"正确 代码示例:

/*eslint generator-star-spacing: ["error", {"before": true, "after": true}]*/
/*eslint-env es6*/

function * generator() {}

var anonymous = function * () {};

var shorthand = { * generator() {} };

neither

Examples of correct code for this rule with the "neither" option:

选项 "neither"正确 代码示例:

/*eslint generator-star-spacing: ["error", {"before": false, "after": false}]*/
/*eslint-env es6*/

function*generator() {}

var anonymous = function*() {};

var shorthand = { *generator() {} };

When Not To Use It

If your project will not be using generators or you are not concerned with spacing consistency, you do not need this rule.

如果你的项目不使用 generators 或者你不关心空格的一致性,可以关闭此规则。

Further Reading

Version

This rule was introduced in ESLint 0.17.0.

该规则在 ESLint 0.17.0 中被引入。

Resources


+