Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: Check space after anonymous generator star (fixes #5435)
  • Loading branch information
alberto committed Mar 1, 2016
1 parent 9b54ed3 commit e1bbe45
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 62 deletions.
8 changes: 3 additions & 5 deletions docs/rules/generator-star-spacing.md
Expand Up @@ -51,11 +51,9 @@ The rule takes one option, an object, which has two keys `before` and `after` ha

In object literal shorthand methods, spacing before the `*` is not checked, as they lack a `function` keyword.

* `after` enforces spacing between the `*` and the function name.
* `after` enforces spacing between the `*` and the function name (or the opening parenthesis for anonymous generator functions).
If it is `true`, a space is required, otherwise spaces are disallowed.

In anonymous function expressions, spacing between the `*` and the opening parenthesis is not checked. This is checked by the [space-before-function-paren](space-before-function-paren.md) rule.

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

```json
Expand Down Expand Up @@ -94,7 +92,7 @@ When using `{"before": false, "after": true}` this placement will be enforced:

function* generator() {}

var anonymous = function*() {};
var anonymous = function* () {};

var shorthand = { * generator() {} };
```
Expand All @@ -107,7 +105,7 @@ When using `{"before": true, "after": true}` this placement will be enforced:

function * generator() {}

var anonymous = function *() {};
var anonymous = function * () {};

var shorthand = { * generator() {} };
```
Expand Down
5 changes: 1 addition & 4 deletions lib/rules/generator-star-spacing.js
Expand Up @@ -81,11 +81,8 @@ module.exports = function(context) {
checkSpacing("before", prevToken, starToken);
}

// Only check after when followed by an identifier
nextToken = context.getTokenAfter(starToken);
if (nextToken.type === "Identifier") {
checkSpacing("after", starToken, nextToken);
}
checkSpacing("after", starToken, nextToken);
}

return {
Expand Down
89 changes: 36 additions & 53 deletions tests/lib/rules/generator-star-spacing.js
Expand Up @@ -43,10 +43,6 @@ ruleTester.run("generator-star-spacing", rule, {
code: "var foo = function *(){};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function * (){};",
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = { *foo(){} };",
parserOptions: { ecmaVersion: 6 }
Expand Down Expand Up @@ -93,11 +89,6 @@ ruleTester.run("generator-star-spacing", rule, {
options: ["before"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function * (){};",
options: ["before"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = { *foo(){} };",
options: ["before"],
Expand Down Expand Up @@ -149,11 +140,6 @@ ruleTester.run("generator-star-spacing", rule, {
options: ["after"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function*(){};",
options: ["after"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = {* foo(){} };",
options: ["after"],
Expand Down Expand Up @@ -205,11 +191,6 @@ ruleTester.run("generator-star-spacing", rule, {
options: ["both"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function *(){};",
options: ["both"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = { * foo(){} };",
options: ["both"],
Expand Down Expand Up @@ -261,11 +242,6 @@ ruleTester.run("generator-star-spacing", rule, {
options: ["neither"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function* (){};",
options: ["neither"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = {*foo(){} };",
options: ["neither"],
Expand Down Expand Up @@ -317,11 +293,6 @@ ruleTester.run("generator-star-spacing", rule, {
options: [{"before": true, "after": false}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function * (){};",
options: [{"before": true, "after": false}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = { *foo(){} };",
options: [{"before": true, "after": false}],
Expand Down Expand Up @@ -373,11 +344,6 @@ ruleTester.run("generator-star-spacing", rule, {
options: [{"before": false, "after": true}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function*(){};",
options: [{"before": false, "after": true}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = {* foo(){} };",
options: [{"before": false, "after": true}],
Expand Down Expand Up @@ -429,11 +395,6 @@ ruleTester.run("generator-star-spacing", rule, {
options: [{"before": true, "after": true}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function *(){};",
options: [{"before": true, "after": true}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = { * foo(){} };",
options: [{"before": true, "after": true}],
Expand Down Expand Up @@ -485,11 +446,6 @@ ruleTester.run("generator-star-spacing", rule, {
options: [{"before": false, "after": false}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = function* (){};",
options: [{"before": false, "after": false}],
parserOptions: { ecmaVersion: 6 }
},
{
code: "var foo = {*foo(){} };",
options: [{"before": false, "after": false}],
Expand Down Expand Up @@ -553,11 +509,14 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function* (){};",
output: "var foo = function * (){};",
output: "var foo = function *(){};",
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Missing space before *.",
type: "Punctuator"
}, {
message: "Unexpected space after *.",
type: "Punctuator"
}]
},
{
Expand Down Expand Up @@ -627,12 +586,15 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function* (){};",
output: "var foo = function * (){};",
output: "var foo = function *(){};",
options: ["before"],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Missing space before *.",
type: "Punctuator"
}, {
message: "Unexpected space after *.",
type: "Punctuator"
}]
},
{
Expand Down Expand Up @@ -695,12 +657,15 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function *(){};",
output: "var foo = function*(){};",
output: "var foo = function* (){};",
options: ["after"],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Unexpected space before *.",
type: "Punctuator"
}, {
message: "Missing space after *.",
type: "Punctuator"
}]
},
{
Expand Down Expand Up @@ -779,12 +744,15 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function*(){};",
output: "var foo = function *(){};",
output: "var foo = function * (){};",
options: ["both"],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Missing space before *.",
type: "Punctuator"
}, {
message: "Missing space after *.",
type: "Punctuator"
}]
},
{
Expand Down Expand Up @@ -863,12 +831,15 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function * (){};",
output: "var foo = function* (){};",
output: "var foo = function*(){};",
options: ["neither"],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Unexpected space before *.",
type: "Punctuator"
}, {
message: "Unexpected space after *.",
type: "Punctuator"
}]
},
{
Expand Down Expand Up @@ -941,12 +912,15 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function* (){};",
output: "var foo = function * (){};",
output: "var foo = function *(){};",
options: [{"before": true, "after": false}],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Missing space before *.",
type: "Punctuator"
}, {
message: "Unexpected space after *.",
type: "Punctuator"
}]
},
{
Expand Down Expand Up @@ -1009,12 +983,15 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function *(){};",
output: "var foo = function*(){};",
output: "var foo = function* (){};",
options: [{"before": false, "after": true}],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Unexpected space before *.",
type: "Punctuator"
}, {
message: "Missing space after *.",
type: "Punctuator"
}]
},
{
Expand Down Expand Up @@ -1093,12 +1070,15 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function*(){};",
output: "var foo = function *(){};",
output: "var foo = function * (){};",
options: [{"before": true, "after": true}],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Missing space before *.",
type: "Punctuator"
}, {
message: "Missing space after *.",
type: "Punctuator"
}]
},
{
Expand Down Expand Up @@ -1177,12 +1157,15 @@ ruleTester.run("generator-star-spacing", rule, {
},
{
code: "var foo = function * (){};",
output: "var foo = function* (){};",
output: "var foo = function*(){};",
options: [{"before": false, "after": false}],
parserOptions: { ecmaVersion: 6 },
errors: [{
message: "Unexpected space before *.",
type: "Punctuator"
}, {
message: "Unexpected space after *.",
type: "Punctuator"
}]
},
{
Expand Down

0 comments on commit e1bbe45

Please sign in to comment.