Skip to content

Commit

Permalink
Fix: account for async in function spacing rules
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Oct 17, 2015
1 parent 466e6fb commit ef924e9
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/rules/disallow-spaces-in-anonymous-function-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ module.exports.prototype = {

if (beforeOpeningRoundBrace) {
var functionToken = file.getFirstNodeToken(functionNode);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/disallow-spaces-in-function-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ module.exports.prototype = {

if (beforeOpeningRoundBrace) {
var functionToken = file.getFirstNodeToken(node.id);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/disallow-spaces-in-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ module.exports.prototype = {

if (beforeOpeningRoundBrace) {
var functionToken = file.getFirstNodeToken(functionNode);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/disallow-spaces-in-named-function-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ module.exports.prototype = {
if (node.id) {
if (beforeOpeningRoundBrace) {
var functionToken = file.getFirstNodeToken(functionNode);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.noWhitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/require-spaces-in-anonymous-function-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ module.exports.prototype = {

if (beforeOpeningRoundBrace) {
var functionToken = file.getFirstNodeToken(functionNode);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.whitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/require-spaces-in-function-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ module.exports.prototype = {
if (beforeOpeningRoundBrace) {
// for a named function, use node.id
var functionToken = file.getFirstNodeToken(node.id || node);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.whitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
21 changes: 21 additions & 0 deletions lib/rules/require-spaces-in-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
* var x = function () {};
* var x = function a () {};
* function a () {}
* var x = async function () {};
* var x = async function a () {};
* async function a () {}
* ```
*
* ##### Valid for mode `{ "beforeOpeningRoundBrace": true }`
Expand All @@ -43,6 +46,9 @@
* var x = function (){};
* var x = function a (){};
* function a (){}
* var x = async function (){};
* var x = async function a (){};
* async function a (){}
* ```
*
* ##### Valid for mode `{ "beforeOpeningCurlyBrace": true }`
Expand All @@ -51,6 +57,9 @@
* var x = function() {};
* var x = function a() {};
* function a() {}
* var x = async function() {};
* var x = async function a() {};
* async function a() {}
* ```
*
* ##### Invalid for mode `{ "beforeOpeningRoundBrace": true, "beforeOpeningCurlyBrace": true }`
Expand All @@ -65,6 +74,15 @@
* function a() {}
* function a (){}
* function a(){}
* var x = async function() {};
* var x = async function (){};
* var x = async function(){};
* var x = async function a() {};
* var x = async function a (){};
* var x = async function a(){};
* async function a() {}
* async function a (){}
* async function a(){}
* ```
*/

Expand Down Expand Up @@ -129,6 +147,9 @@ module.exports.prototype = {

if (beforeOpeningRoundBrace) {
var functionToken = file.getFirstNodeToken(functionNode);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.whitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/require-spaces-in-named-function-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ module.exports.prototype = {
if (node.id) {
if (beforeOpeningRoundBrace) {
var functionToken = file.getFirstNodeToken(functionNode);
if (node.async && functionToken.value === 'async') {
functionToken = file.getNextToken(functionToken);
}
errors.assert.whitespaceBetween({
token: functionToken,
nextToken: file.getNextToken(functionToken),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ describe('rules/require-spaces-in-anonymous-function-expression', function() {
output: 'var x = function (){}'
});

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

reportAndFix({
name: 'missing space before round brace in method shorthand',
rules: rules,
Expand Down
13 changes: 12 additions & 1 deletion test/specs/rules/require-spaces-in-function-declaration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Checker = require('../../../lib/checker');
var expect = require('chai').expect;
var reportAndFix = require('../../lib/assertHelpers').reportAndFix;

describe('rules/require-spaces-in-function-declaration', function() {
var checker;
Expand All @@ -9,8 +10,10 @@ describe('rules/require-spaces-in-function-declaration', function() {
});

describe('beforeOpeningRoundBrace', function() {
var rules = { requireSpacesInFunctionDeclaration: { beforeOpeningRoundBrace: true } };

beforeEach(function() {
checker.configure({ requireSpacesInFunctionDeclaration: { beforeOpeningRoundBrace: true } });
checker.configure(rules);
});

it('should report missing space before round brace in FunctionDeclaration', function() {
Expand All @@ -26,6 +29,14 @@ describe('rules/require-spaces-in-function-declaration', function() {
checker.configure({ esnext: true });
expect(checker.checkString('export default function (){}')).to.have.no.errors();
});

reportAndFix({
name: 'missing space before round brace in async FunctionDeclaration',
rules: rules,
errors: 1,
input: 'async function(){}',
output: 'async function (){}'
});
});

describe('beforeOpeningCurlyBrace', function() {
Expand Down
8 changes: 8 additions & 0 deletions test/specs/rules/require-spaces-in-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ describe('rules/require-spaces-in-function', function() {
output: 'var x = function (){}'
});

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

reportAndFix({
name: 'missing space before round brace in method shorthand',
rules: rules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ describe('rules/require-spaces-in-named-function-expression', function() {
input: 'var x = function a(){}',
output: 'var x = function a (){}'
});

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

describe('beforeOpeningCurlyBrace', function() {
Expand Down

0 comments on commit ef924e9

Please sign in to comment.