Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Fix: account for generators in function spacing rules
Browse files Browse the repository at this point in the history
Fixes #1175
  • Loading branch information
hzoo committed Oct 17, 2015
1 parent 7952b17 commit a2c009f
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/rules/disallow-spaces-in-anonymous-function-expression.js
Expand Up @@ -105,6 +105,10 @@ module.exports.prototype = {
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/disallow-spaces-in-function-declaration.js
Expand Up @@ -85,6 +85,10 @@ module.exports.prototype = {
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/disallow-spaces-in-function-expression.js
Expand Up @@ -105,6 +105,10 @@ module.exports.prototype = {
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/disallow-spaces-in-function.js
Expand Up @@ -103,6 +103,10 @@ module.exports.prototype = {
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/require-spaces-in-anonymous-function-expression.js
Expand Up @@ -151,6 +151,10 @@ module.exports.prototype = {
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.whitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/require-spaces-in-function-declaration.js
Expand Up @@ -86,6 +86,10 @@ module.exports.prototype = {
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.whitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/require-spaces-in-function-expression.js
Expand Up @@ -104,6 +104,10 @@ module.exports.prototype = {
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.whitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/require-spaces-in-function.js
Expand Up @@ -150,6 +150,10 @@ module.exports.prototype = {
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
// if generator, set token to be * instead
if (node.generator && functionToken.value === 'function') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.whitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
24 changes: 20 additions & 4 deletions test/specs/rules/require-spaces-in-function-expression.js
Expand Up @@ -37,21 +37,21 @@ describe('rules/require-spaces-in-function-expression', function() {
expect(checker.checkString('var x = function a (){}')).to.have.no.errors();
});

it('should report missing space before round brace in FunctionExpression', function() {
it('should report missing space before round brace in async FunctionExpression', function() {
expect(checker.checkString('var x = async function(){}'))
.to.have.one.validation.error.from('requireSpacesInFunctionExpression');
});

it('should report missing space before round brace in named FunctionExpression', function() {
it('should report missing space before round brace in named async FunctionExpression', function() {
expect(checker.checkString('var x = async function a(){}'))
.to.have.one.validation.error.from('requireSpacesInFunctionExpression');
});

it('should not report space before round brace in FunctionExpression', function() {
it.only('should not report space before round brace in async FunctionExpression', function() {
expect(checker.checkString('var x = async function (){}')).to.have.no.errors();
});

it('should not report space before round brace in named FunctionExpression', function() {
it('should not report space before round brace in named async FunctionExpression', function() {
expect(checker.checkString('var x = async function a (){}')).to.have.no.errors();
});

Expand Down Expand Up @@ -101,6 +101,22 @@ describe('rules/require-spaces-in-function-expression', function() {
output: 'var x = function (){}'
});

reportAndFix({
name: 'missing space before round brace in generator FunctionExpression',
rules: rules,
errors: 1,
input: 'var x = function*(){}',
output: 'var x = function* (){}'
});

reportAndFix({
name: 'missing space before round brace in generator FunctionExpression',
rules: rules,
errors: 1,
input: 'var x = function *(){}',
output: 'var x = function * (){}'
});

reportAndFix({
name: 'missing space before round brace in method shorthand',
rules: rules,
Expand Down

0 comments on commit a2c009f

Please sign in to comment.