Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4486 from bryanrsmith/issue-4115
Breaking: Implement yield-star-spacing rule (fixes #4115)
- Loading branch information
Showing
7 changed files
with
524 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,6 +191,7 @@ | |
"vars-on-top": 0, | ||
"wrap-iife": 0, | ||
"wrap-regex": 0, | ||
"yield-star-spacing": 0, | ||
"yoda": [0, "never"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Enforce spacing around the `*` in `yield*` expressions (yield-star-spacing) | ||
|
||
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line. | ||
|
||
## Rule Details | ||
|
||
This rule enforces spacing around the `*` in `yield*` expressions. | ||
|
||
The rule takes one option, an object, which has two keys `before` and `after` having boolean values `true` or `false`. | ||
|
||
* `before` enforces spacing between the `yield` and the `*`. | ||
If `true`, a space is required, otherwise spaces are disallowed. | ||
|
||
* `after` enforces spacing between the `*` and the argument. | ||
If it is `true`, a space is required, otherwise spaces are disallowed. | ||
|
||
The default is `{"before": false, "after": true}`. | ||
|
||
```json | ||
"yield-star-spacing": [2, {"before": true, "after": false}] | ||
``` | ||
|
||
The option also has a string shorthand: | ||
|
||
* `{"before": false, "after": true}` → `"after"` | ||
* `{"before": true, "after": false}` → `"before"` | ||
* `{"before": true, "after": true}` → `"both"` | ||
* `{"before": false, "after": false}` → `"neither"` | ||
|
||
```json | ||
"yield-star-spacing": [2, "after"] | ||
``` | ||
|
||
When using `"after"` this spacing will be enforced: | ||
|
||
```js | ||
/*eslint yield-star-spacing: [2, "after"]*/ | ||
/*eslint-env es6*/ | ||
|
||
function *generator() { | ||
yield* other(); | ||
} | ||
``` | ||
|
||
When using `"before"` this spacing will be enforced: | ||
|
||
```js | ||
/*eslint yield-star-spacing: [2, "before"]*/ | ||
/*eslint-env es6*/ | ||
|
||
function *generator() { | ||
yield *other(); | ||
} | ||
``` | ||
|
||
When using `"both"` this spacing will be enforced: | ||
|
||
```js | ||
/*eslint yield-star-spacing: [2, "both"]*/ | ||
/*eslint-env es6*/ | ||
|
||
function *generator() { | ||
yield * other(); | ||
} | ||
``` | ||
|
||
When using `"neither"` this spacing will be enforced: | ||
|
||
```js | ||
/*eslint yield-star-spacing: [2, "neither"]*/ | ||
/*eslint-env es6*/ | ||
|
||
function *generator() { | ||
yield*other(); | ||
} | ||
``` | ||
|
||
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 or you are not concerned with spacing consistency, you do not need this rule. | ||
|
||
## Further Reading | ||
|
||
* [Understanding ES6: Generators](https://leanpub.com/understandinges6/read/#leanpub-auto-generators) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @fileoverview Rule to check the spacing around the * in yield* expressions. | ||
* @author Bryan Smith | ||
* @copyright 2015 Bryan Smith. All rights reserved. | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
var sourceCode = context.getSourceCode(); | ||
|
||
var mode = (function(option) { | ||
if (!option || typeof option === "string") { | ||
return { | ||
before: { before: true, after: false }, | ||
after: { before: false, after: true }, | ||
both: { before: true, after: true }, | ||
neither: { before: false, after: false } | ||
}[option || "after"]; | ||
} | ||
return option; | ||
}(context.options[0])); | ||
|
||
/** | ||
* Checks the spacing between two tokens before or after the star token. | ||
* @param {string} side Either "before" or "after". | ||
* @param {Token} leftToken `function` keyword token if side is "before", or | ||
* star token if side is "after". | ||
* @param {Token} rightToken Star token if side is "before", or identifier | ||
* token if side is "after". | ||
* @returns {void} | ||
*/ | ||
function checkSpacing(side, leftToken, rightToken) { | ||
if (sourceCode.isSpaceBetweenTokens(leftToken, rightToken) !== mode[side]) { | ||
var after = leftToken.value === "*"; | ||
var spaceRequired = mode[side]; | ||
var node = after ? leftToken : rightToken; | ||
var type = spaceRequired ? "Missing" : "Unexpected"; | ||
var message = type + " space " + side + " *."; | ||
context.report({ | ||
node: node, | ||
message: message, | ||
fix: function(fixer) { | ||
if (spaceRequired) { | ||
if (after) { | ||
return fixer.insertTextAfter(node, " "); | ||
} | ||
return fixer.insertTextBefore(node, " "); | ||
} | ||
return fixer.removeRange([leftToken.range[1], rightToken.range[0]]); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Enforces the spacing around the star if node is a yield* expression. | ||
* @param {ASTNode} node A yield expression node. | ||
* @returns {void} | ||
*/ | ||
function checkExpression(node) { | ||
if (!node.delegate) { | ||
return; | ||
} | ||
|
||
var tokens = sourceCode.getFirstTokens(node, 3); | ||
var yieldToken = tokens[0]; | ||
var starToken = tokens[1]; | ||
var nextToken = tokens[2]; | ||
|
||
checkSpacing("before", yieldToken, starToken); | ||
checkSpacing("after", starToken, nextToken); | ||
} | ||
|
||
return { | ||
"YieldExpression": checkExpression | ||
}; | ||
|
||
}; | ||
|
||
module.exports.schema = [ | ||
{ | ||
"oneOf": [ | ||
{ | ||
"enum": ["before", "after", "both", "neither"] | ||
}, | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"before": {"type": "boolean"}, | ||
"after": {"type": "boolean"} | ||
}, | ||
"additionalProperties": false | ||
} | ||
] | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.