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

Commit cf134a1

Browse files
committed
Fix: account for async in function spacing rules
Fixes #1873 Closes gh-1878
1 parent 466e6fb commit cf134a1

12 files changed

+78
-1
lines changed

lib/rules/disallow-spaces-in-anonymous-function-expression.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ module.exports.prototype = {
102102

103103
if (beforeOpeningRoundBrace) {
104104
var functionToken = file.getFirstNodeToken(functionNode);
105+
if (node.async && functionToken.value === 'async') {
106+
functionToken = file.getNextToken(functionToken);
107+
}
105108
errors.assert.noWhitespaceBetween({
106109
token: functionToken,
107110
nextToken: file.getNextToken(functionToken),

lib/rules/disallow-spaces-in-function-declaration.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ module.exports.prototype = {
8282

8383
if (beforeOpeningRoundBrace) {
8484
var functionToken = file.getFirstNodeToken(node.id);
85+
if (node.async && functionToken.value === 'async') {
86+
functionToken = file.getNextToken(functionToken);
87+
}
8588
errors.assert.noWhitespaceBetween({
8689
token: functionToken,
8790
nextToken: file.getNextToken(functionToken),

lib/rules/disallow-spaces-in-function.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ module.exports.prototype = {
100100

101101
if (beforeOpeningRoundBrace) {
102102
var functionToken = file.getFirstNodeToken(functionNode);
103+
if (node.async && functionToken.value === 'async') {
104+
functionToken = file.getNextToken(functionToken);
105+
}
103106
errors.assert.noWhitespaceBetween({
104107
token: functionToken,
105108
nextToken: file.getNextToken(functionToken),

lib/rules/disallow-spaces-in-named-function-expression.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ module.exports.prototype = {
9393
if (node.id) {
9494
if (beforeOpeningRoundBrace) {
9595
var functionToken = file.getFirstNodeToken(functionNode);
96+
if (node.async && functionToken.value === 'async') {
97+
functionToken = file.getNextToken(functionToken);
98+
}
9699
errors.assert.noWhitespaceBetween({
97100
token: functionToken,
98101
nextToken: file.getNextToken(functionToken),

lib/rules/require-spaces-in-anonymous-function-expression.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ module.exports.prototype = {
148148

149149
if (beforeOpeningRoundBrace) {
150150
var functionToken = file.getFirstNodeToken(functionNode);
151+
if (node.async && functionToken.value === 'async') {
152+
functionToken = file.getNextToken(functionToken);
153+
}
151154
errors.assert.whitespaceBetween({
152155
token: functionToken,
153156
nextToken: file.getNextToken(functionToken),

lib/rules/require-spaces-in-function-declaration.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ module.exports.prototype = {
8383
if (beforeOpeningRoundBrace) {
8484
// for a named function, use node.id
8585
var functionToken = file.getFirstNodeToken(node.id || node);
86+
if (node.async && functionToken.value === 'async') {
87+
functionToken = file.getNextToken(functionToken);
88+
}
8689
errors.assert.whitespaceBetween({
8790
token: functionToken,
8891
nextToken: file.getNextToken(functionToken),

lib/rules/require-spaces-in-function.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
* var x = function () {};
3636
* var x = function a () {};
3737
* function a () {}
38+
* var x = async function () {};
39+
* var x = async function a () {};
40+
* async function a () {}
3841
* ```
3942
*
4043
* ##### Valid for mode `{ "beforeOpeningRoundBrace": true }`
@@ -43,6 +46,9 @@
4346
* var x = function (){};
4447
* var x = function a (){};
4548
* function a (){}
49+
* var x = async function (){};
50+
* var x = async function a (){};
51+
* async function a (){}
4652
* ```
4753
*
4854
* ##### Valid for mode `{ "beforeOpeningCurlyBrace": true }`
@@ -51,6 +57,9 @@
5157
* var x = function() {};
5258
* var x = function a() {};
5359
* function a() {}
60+
* var x = async function() {};
61+
* var x = async function a() {};
62+
* async function a() {}
5463
* ```
5564
*
5665
* ##### Invalid for mode `{ "beforeOpeningRoundBrace": true, "beforeOpeningCurlyBrace": true }`
@@ -65,6 +74,15 @@
6574
* function a() {}
6675
* function a (){}
6776
* function a(){}
77+
* var x = async function() {};
78+
* var x = async function (){};
79+
* var x = async function(){};
80+
* var x = async function a() {};
81+
* var x = async function a (){};
82+
* var x = async function a(){};
83+
* async function a() {}
84+
* async function a (){}
85+
* async function a(){}
6886
* ```
6987
*/
7088

@@ -129,6 +147,9 @@ module.exports.prototype = {
129147

130148
if (beforeOpeningRoundBrace) {
131149
var functionToken = file.getFirstNodeToken(functionNode);
150+
if (node.async && functionToken.value === 'async') {
151+
functionToken = file.getNextToken(functionToken);
152+
}
132153
errors.assert.whitespaceBetween({
133154
token: functionToken,
134155
nextToken: file.getNextToken(functionToken),

lib/rules/require-spaces-in-named-function-expression.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ module.exports.prototype = {
118118
if (node.id) {
119119
if (beforeOpeningRoundBrace) {
120120
var functionToken = file.getFirstNodeToken(functionNode);
121+
if (node.async && functionToken.value === 'async') {
122+
functionToken = file.getNextToken(functionToken);
123+
}
121124
errors.assert.whitespaceBetween({
122125
token: functionToken,
123126
nextToken: file.getNextToken(functionToken),

test/specs/rules/require-spaces-in-anonymous-function-expression.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ describe('rules/require-spaces-in-anonymous-function-expression', function() {
8989
output: 'var x = function (){}'
9090
});
9191

92+
reportAndFix({
93+
name: 'missing space before round brace in async FunctionExpression',
94+
rules: rules,
95+
errors: 1,
96+
input: 'var x = async function(){}',
97+
output: 'var x = async function (){}'
98+
});
99+
92100
reportAndFix({
93101
name: 'missing space before round brace in method shorthand',
94102
rules: rules,

test/specs/rules/require-spaces-in-function-declaration.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Checker = require('../../../lib/checker');
22
var expect = require('chai').expect;
3+
var reportAndFix = require('../../lib/assertHelpers').reportAndFix;
34

45
describe('rules/require-spaces-in-function-declaration', function() {
56
var checker;
@@ -9,8 +10,10 @@ describe('rules/require-spaces-in-function-declaration', function() {
910
});
1011

1112
describe('beforeOpeningRoundBrace', function() {
13+
var rules = { esnext: true, requireSpacesInFunctionDeclaration: { beforeOpeningRoundBrace: true } };
14+
1215
beforeEach(function() {
13-
checker.configure({ requireSpacesInFunctionDeclaration: { beforeOpeningRoundBrace: true } });
16+
checker.configure(rules);
1417
});
1518

1619
it('should report missing space before round brace in FunctionDeclaration', function() {
@@ -26,6 +29,14 @@ describe('rules/require-spaces-in-function-declaration', function() {
2629
checker.configure({ esnext: true });
2730
expect(checker.checkString('export default function (){}')).to.have.no.errors();
2831
});
32+
33+
reportAndFix({
34+
name: 'missing space before round brace in async FunctionDeclaration',
35+
rules: rules,
36+
errors: 1,
37+
input: 'async function a(){}',
38+
output: 'async function a (){}'
39+
});
2940
});
3041

3142
describe('beforeOpeningCurlyBrace', function() {

test/specs/rules/require-spaces-in-function.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ describe('rules/require-spaces-in-function', function() {
8888
output: 'var x = function (){}'
8989
});
9090

91+
reportAndFix({
92+
name: 'missing space before round brace in async FunctionExpression',
93+
rules: rules,
94+
errors: 1,
95+
input: 'var x = async function(){}',
96+
output: 'var x = async function (){}'
97+
});
98+
9199
reportAndFix({
92100
name: 'missing space before round brace in method shorthand',
93101
rules: rules,

test/specs/rules/require-spaces-in-named-function-expression.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ describe('rules/require-spaces-in-named-function-expression', function() {
5555
input: 'var x = function a(){}',
5656
output: 'var x = function a (){}'
5757
});
58+
59+
reportAndFix({
60+
name: 'missing space before round brace in named async FunctionExpression',
61+
rules: rules,
62+
errors: 1,
63+
input: 'var x = async function a(){}',
64+
output: 'var x = async function a (){}'
65+
});
5866
});
5967

6068
describe('beforeOpeningCurlyBrace', function() {

0 commit comments

Comments
 (0)