This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Fix: account for async in function spacing rules

Fixes #1873
Closes gh-1878
  • Loading branch information...
hzoo committed Oct 17, 2015
1 parent 466e6fb commit cf134a12c1ab0bb7a23c7197780593bfdb8682e2
@@ -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),
@@ -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),
@@ -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),
@@ -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),
@@ -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),
@@ -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),
@@ -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 }`
@@ -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 }`
@@ -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 }`
@@ -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(){}
* ```
*/
@@ -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),
@@ -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),
@@ -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,
@@ -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;
@@ -9,8 +10,10 @@ describe('rules/require-spaces-in-function-declaration', function() {
});
describe('beforeOpeningRoundBrace', function() {
var rules = { esnext: true, requireSpacesInFunctionDeclaration: { beforeOpeningRoundBrace: true } };
beforeEach(function() {
checker.configure({ requireSpacesInFunctionDeclaration: { beforeOpeningRoundBrace: true } });
checker.configure(rules);
});
it('should report missing space before round brace in FunctionDeclaration', function() {
@@ -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 a(){}',
output: 'async function a (){}'
});
});
describe('beforeOpeningCurlyBrace', function() {
@@ -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,
@@ -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() {

0 comments on commit cf134a1

Please sign in to comment.