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

Merge 59db652 into d97aa5f

  • Loading branch information
tybenz committed Aug 26, 2014
2 parents d97aa5f + 59db652 commit aa772ffd12046a7cc0dde605df3f9787456fb807
Showing with 128 additions and 2 deletions.
  1. +25 −1 README.md
  2. +51 −1 lib/rules/require-spaces-inside-parentheses.js
  3. +52 −0 test/rules/require-spaces-inside-parentheses.js
@@ -1233,7 +1233,9 @@ Requires space after opening round bracket and before closing.

Type: `String`

Values: `"all"` for strict mode, `"allButNested"` ignores nested brackets in a row.
Values: `"all"` for strict mode, `"allButNested"` ignores nested brackets in a
row, `"allButSolitaryPunctuators"` ignores a list of punctuators followed by a
newline, or a list of puncutuators on their own line.

#### Example

@@ -1259,6 +1261,28 @@ var x = Math.pow(( 1 + 2 ), ( 3 + 4 ));
var x = Math.pow(1 + 2, 3 + 4);
```

##### Valid for mode `"allButSolitaryPunctuators"`

```js
foo( function() {
return 'bar';
});
```

##### Invalid

```js
foo( function() {
return 'bar';
} )
```

```js
foo(function() {
return 'bar';
});
```


### disallowQuotedKeysInObjects

@@ -7,7 +7,8 @@ module.exports.prototype = {
configure: function(mode) {
var modes = {
'all': true,
'allButNested': true
'allButNested': true,
'allButSolitaryPunctuators': true
};
assert(
typeof mode === 'string' &&
@@ -32,8 +33,13 @@ module.exports.prototype = {

// Iterate punctuators since '(', ')' can exist in multiple contexts.
file.iterateTokensByType('Punctuator', function(token, index, tokens) {
var foundNonPunctuator;
var currentToken;
var lastToken;
var i;
if (token.value === '(') {
var nextToken = tokens[index + 1];
var nextTokens = tokens.slice(index + 1);

// Skip the cases where we allow no space even if spaces are required.
if (nextToken.type === 'Punctuator' && nextToken.value === ')') {
@@ -45,6 +51,28 @@ module.exports.prototype = {
return;
}

if (mode === 'allButSolitaryPunctuators') {
// allows for no space between punctuators on their own
// line e.g. function({
lastToken = null;
foundNonPunctuator = false;
var len = nextTokens.length;
for (i = 0; i < len; i++) {
currentToken = nextTokens[i];
if (nextTokens[i - 1]) { lastToken = nextTokens[i - 1]; }
if (currentToken.loc.start.line !== token.loc.start.line ||
(lastToken && currentToken.loc.start.line !== lastToken.loc.start.line)) {
break;
}
if (currentToken.type !== 'Punctuator') {
foundNonPunctuator = true;
}
}
if (!foundNonPunctuator) {
return;
}
}

if ((token.range[1] === nextToken.range[0] &&
token.loc.end.line === nextToken.loc.start.line) ||
isComment(token.range[1])) {
@@ -54,6 +82,7 @@ module.exports.prototype = {

if (token.value === ')') {
var prevToken = tokens[index - 1];
var prevTokens = tokens.slice(0, index);

// Skip the cases where we allow no space even if spaces are required.
if (prevToken.type === 'Punctuator' && prevToken.value === '(') {
@@ -65,6 +94,27 @@ module.exports.prototype = {
return;
}

if (mode === 'allButSolitaryPunctuators') {
// allows for no space between punctuators on their own
// line e.g. })
lastToken = null;
foundNonPunctuator = false;
for (i = prevTokens.length - 1; i >= 0; i--) {
currentToken = prevTokens[i];
if (prevTokens[i + 1]) { lastToken = prevTokens[i + 1]; }
if (currentToken.loc.start.line !== token.loc.start.line ||
(lastToken && currentToken.loc.start.line !== lastToken.loc.start.line)) {
break;
}
if (currentToken.type !== 'Punctuator') {
foundNonPunctuator = true;
}
}
if (!foundNonPunctuator) {
return;
}
}

if ((token.range[0] === prevToken.range[1] &&
token.loc.end.line === prevToken.loc.start.line) ||
isComment(token.range[0] - 1)) {
@@ -79,4 +79,56 @@ describe('rules/require-spaces-inside-parentheses', function() {
});
});

describe('allButSolitaryPunctuators', function() {
beforeEach(function() {
checker.configure({ requireSpacesInsideParentheses: 'allButSolitaryPunctuators' });
});

it('should allow no space between solitary punctuators', function() {
assert(checker.checkString('foo( function() {\n return;\n});').isEmpty());
assert(checker.checkString('foo({\n foo: "bar"\n}, "bar" );').isEmpty());
assert(checker.checkString('foo({\n foo: "bar",\n bar: "baz"\n});').isEmpty());
});

it('should require spaces between nonpunctuators and parentheses', function() {
assert(checker.checkString('foo( function() {\n return;\n}, "bar");').getErrorCount() === 1);
assert(checker.checkString('foo(function() {\n return;\n}, "bar" );').getErrorCount() === 1);
assert(checker.checkString('foo(function() {\n return;\n}, "bar");').getErrorCount() === 2);
});

it('should require between simple arguments', function() {
assert(checker.checkString('foo(a,b);').getErrorCount() === 2);
assert(checker.checkString('foo( a,b);').getErrorCount() === 1);
assert(checker.checkString('foo( a,b );').isEmpty());
});

it('should not require spaces for empty arguments list', function() {
assert(checker.checkString('foo();').isEmpty());
});

it('should require spaces for grouping parentheses', function() {
assert(checker.checkString('var test = (true ? true : false)').getErrorCount() === 2);
assert(checker.checkString('var test = ( true ? true : false)').getErrorCount() === 1);
assert(checker.checkString('var test = ( true ? true : false )').isEmpty());
});

it('should require spaces for "for" keyword', function() {
assert(checker.checkString('for (var i = 0; i < 100; i++) {}').getErrorCount() === 2);
assert(checker.checkString('for ( var i = 0; i < 100; i++) {}').getErrorCount() === 1);
assert(checker.checkString('for ( var i = 0; i < 100; i++ ) {}').isEmpty());
});

it('should require spaces for "while" keyword', function() {
assert(checker.checkString('while (!condition) {}').getErrorCount() === 2);
assert(checker.checkString('while ( !condition) {}').getErrorCount() === 1);
assert(checker.checkString('while ( !condition ) {}').isEmpty());
});

it('should require spaces for "try...catch" statement', function() {
assert(checker.checkString('try {} catch(e) {}').getErrorCount() === 2);
assert(checker.checkString('try {} catch( e) {}').getErrorCount() === 1);
assert(checker.checkString('try {} catch( e ) {}').isEmpty());
});
});

});

0 comments on commit aa772ff

Please sign in to comment.
You can’t perform that action at this time.