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

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

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:

/*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 *.

Rule Details

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

Options

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

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

An example configuration:

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

And the option has shorthand as a string keyword:

An example of shorthand configuration:

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

Examples

before

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

/*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:

/*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:

/*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:

/*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.

Further Reading

Version

This rule was introduced in ESLint 0.17.0.

Resources