Enforce the spacing around the * in generators (generator-star-spacing)
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
This rule aims to enforce spacing around * of the generator function.
The rule takes one option, an object, which has two keys before and after having boolean values true or false.
-
beforeaims to spacing between the*and thefunctionkeyword. If it’strue, space is enforced, otherwise space is disallowed.In object literal shorthand methods, spacing before the
*is not checked, as they lack afunctionkeyword. -
afteraims to spacing between the*and the function name. If it’strue, space is enforced, otherwise space is disallowed.In anonymous function expressions, spacing between the
*and the opening parenthesis is not checked. This is checked by the space-before-function-paren rule.
The default is {"before": true, "after": false}.
"generator-star-spacing": [2, {"before": false, "after": true}]
And the option has shorthand as a string keyword:
{"before": true, "after": false}→"before"{"before": false, "after": true}→"after"{"before": true, "after": true}→"both"{"before": false, "after": false}→"neither"
"generator-star-spacing": [2, "after"]
When using {"before": true, "after": false} this placement will be enforced:
function *generator() {}
var anonymous = function *() {};
var shorthand = { *generator() {} };
When using {"before": false, "after": true} this placement will be enforced:
function* generator() {}
var anonymous = function*() {};
var shorthand = { * generator() {} };
When using {"before": true, "after": true} this placement will be enforced:
function * generator() {}
var anonymous = function *() {};
var shorthand = { * generator() {} };
When using {"before": false, "after": false} this placement will be enforced:
function*generator() {}
var anonymous = function*() {};
var shorthand = { *generator() {} };
To use this rule you must set the generators flag to true in the ecmaFeatures configuration object.
When Not To Use It
If your project will not be using generators you do not need this rule.
Further Reading
Version
This rule was introduced in ESLint 0.17.0.