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

Commit

Permalink
(require|disallow)NewlineBeforeBlockStatements: account for `SwitchSt…
Browse files Browse the repository at this point in the history
…atement`

requireNewlineBeforeBlockStatements
disallowNewlineBeforeBlockStatements

Fixes #2045
Closes gh-2059
  • Loading branch information
PaulAnnekov authored and hzoo committed Jan 25, 2016
1 parent ef9ed5e commit 87f864f
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 17 deletions.
24 changes: 23 additions & 1 deletion lib/rules/disallow-newline-before-block-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*
* - `true` always disallows newline before curly brace of block statements
* - `Array` specifies block-type keywords after which newlines are disallowed before curly brace
* - Valid types include: `['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function', 'class']`
* - Valid types include: `['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function', 'class',
* 'switch']`
* - `Object`:
* - `value`: `true` or an Array
* - `allExcept`: Array of exceptions
Expand Down Expand Up @@ -308,6 +309,27 @@ module.exports.prototype = {
}
}
});

if (setting === true || setting.indexOf('switch') !== -1) {
file.iterateNodesByType(['SwitchStatement'], function(node) {
var openingBrace = file.findNextToken(file.getLastNodeToken(node.discriminant), 'Punctuator', '{');
var prevToken = file.getPrevToken(openingBrace);

if (hasMultiLineEx !== true) {
assertSameLine(prevToken, openingBrace);
return;
}

var openingRoundBrace = file.findNextToken(file.getFirstNodeToken(node), 'Punctuator', '(');
var closingRoundBrace = file.findPrevToken(openingBrace, 'Punctuator', ')');

if (openingRoundBrace.loc.start.line !== closingRoundBrace.loc.end.line) {
assertDifferentLine(prevToken, openingBrace);
} else {
assertSameLine(prevToken, openingBrace);
}
});
}
}
};

Expand Down
25 changes: 19 additions & 6 deletions lib/rules/require-newline-before-block-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* - `true` always requires newline before curly brace of block statements
* - `Array` specifies block-type keywords after which newlines are required before curly brace
* - Valid types include: `['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function']`
* - Valid types include: `['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function', 'switch']`
*
* #### Example
*
Expand Down Expand Up @@ -182,18 +182,31 @@ module.exports.prototype = {

check: function(file, errors) {
var setting = this._setting;

function assertDifferentLine(token, nextToken) {
errors.assert.differentLine({
token: token,
nextToken: nextToken,
message: 'Newline before curly brace for block statement is required'
});
}

file.iterateNodesByType('BlockStatement', function(node) {
if (setting === true || setting.indexOf(getBlockType(node)) !== -1) {
var openingBrace = file.getFirstNodeToken(node);
var prevToken = file.getPrevToken(openingBrace);

errors.assert.differentLine({
token: prevToken,
nextToken: openingBrace,
message: 'Missing newline before curly brace for block statement'
});
assertDifferentLine(prevToken, openingBrace);
}
});
if (setting === true || setting.indexOf('switch') !== -1) {
file.iterateNodesByType(['SwitchStatement'], function(node) {
var openingBrace = file.findNextToken(file.getLastNodeToken(node.discriminant), 'Punctuator', '{');
var prevToken = file.getPrevToken(openingBrace);

assertDifferentLine(prevToken, openingBrace);
});
}
}
};

Expand Down
86 changes: 82 additions & 4 deletions test/specs/rules/disallow-newline-before-block-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,37 @@ describe('rules/disallow-newline-before-block-statements', function() {
});

reportAndFix({
name: 'disallowed newline for all 3 statements',
name: 'disallowed newline for all 4 statements',
rules: { disallowNewlineBeforeBlockStatements: true },
input: 'function test()\n{\nif(true)\n{\nreturn 1;\n}\nfor(var i in [1,2,3])\n{\n}\n}',
output: 'function test() {\nif(true) {\nreturn 1;\n}\nfor(var i in [1,2,3]) {\n}\n}',
errors: 3
input: [
'function test()',
'{',
'if(true)',
'{',
'switch (a)',
'{',
'case 1: break;',
'}',
'return 1;',
'}',
'for(var i in [1,2,3])',
'{',
'}',
'}'
].join('\n'),
output: [
'function test() {',
'if(true) {',
'switch (a) {',
'case 1: break;',
'}',
'return 1;',
'}',
'for(var i in [1,2,3]) {',
'}',
'}'
].join('\n'),
errors: 4
});

it('should not report disallowed newline before opening brace', function() {
Expand Down Expand Up @@ -152,6 +178,44 @@ describe('rules/disallow-newline-before-block-statements', function() {
});
});

describe('"switch" statements', function() {
beforeEach(function() {
checker.configure({ disallowNewlineBeforeBlockStatements: ['switch'] });
});

it('should report extra newlines when configured with "switch"', function() {
expect(checker.checkString('switch (a)\n{\n\tcase 1: break;\n}'))
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
});

it('should not report newline before opening brace when there are white-spaces between', function() {
expect(checker.checkString('switch (a) /* COOOMMMENTTT*/ {case 1: break;}')).to.have.no.errors();
});

it('should complain when configured with "switch" and no cases', function() {
expect(checker.checkString('switch (a)\n{\n}'))
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
});

it('should complain when configured with "switch" and parenthesized discriminant', function() {
expect(checker.checkString('switch ((function(){}()))\n{\n\tcase 1: break;\n}'))
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
});

it('should not complain when configured with "switch" and case on brace line', function() {
expect(checker.checkString('switch (a) {default: 1;\n}')).to.have.no.errors();
});

it('should not complain when configured with "switch" and newline not added', function() {
expect(checker.checkString('switch (a) {\n\tcase 1: break;\n}')).to.have.no.errors();
});

it('should not complain when not configured with "switch"', function() {
checker.configure({ disallowNewlineBeforeBlockStatements: ['if'] });
expect(checker.checkString('switch (a)\n{\n\tcase 1: break;\n}')).to.have.no.errors();
});
});

describe('"for...in" loops', function() {
beforeEach(function() {
checker.configure({ disallowNewlineBeforeBlockStatements: ['for'] });
Expand Down Expand Up @@ -397,6 +461,9 @@ describe('rules/disallow-newline-before-block-statements', function() {
expect(checker.checkString('function foo(a,\nb) { }'))
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');

expect(checker.checkString('switch((function(){}\n())) { }'))
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');

expect(checker.checkString('function foo() { for (var i=0; i<len; i++) { } }'))
.to.have.no.errors();

Expand Down Expand Up @@ -424,5 +491,16 @@ describe('rules/disallow-newline-before-block-statements', function() {
expect(checker.checkString('try {\n\ty++;\n} catch(e) {\n}'))
.to.have.no.errors();
});

it('"switch" statements', function() {
expect(checker.checkString('switch((function(){}\n())) { }'))
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');

expect(checker.checkString('switch((function(){}()))\n{ }'))
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');

expect(checker.checkString('switch((function(){}\n()))\n{ }'))
.to.have.no.errors();
});
});
});
85 changes: 79 additions & 6 deletions test/specs/rules/require-newline-before-block-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe('rules/require-newline-before-block-statements', function() {
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
});

it('should report missing newline before opening brace for "switch"', function() {
expect(checker.checkString('switch (a) {\n\tcase 1: break;\n}'))
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
});

it('should report missing newline before opening brace when there are white-spaces between', function() {
expect(checker.checkString('function test() /* COOOMMMENTTT*/ {abc();}'))
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
Expand All @@ -42,14 +47,38 @@ describe('rules/require-newline-before-block-statements', function() {
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
});

it('should report missing newline for all 3 statements', function() {
expect(checker.checkString('function test(){\nif(true){\nreturn 1;\n}\nfor(var i in [1,2,3]){\n}\n}'))
.to.have.error.count.equal(3);
it('should report missing newline for all 4 statements', function() {
expect(checker.checkString([
'function test(){',
'if(true){',
'switch (a){',
'case 1: break;',
'}',
'return 1;',
'}',
'for(var i in [1,2,3]){',
'}',
'}'
].join('\n'))).to.have.error.count.equal(4);
});

it('should not report missing newline', function() {
expect(checker.checkString('function test()\n{\nif(true)\n{\nreturn 1;\n}\nfor(var i in [1,2,3])\n{\n}\n}'))
.to.have.no.errors();
expect(checker.checkString([
'function test()',
'{',
'if(true)',
'{',
'switch (a)',
'{',
'case 1: break;',
'}',
'return 1;',
'}',
'for(var i in [1,2,3])',
'{',
'}',
'}'
].join('\n'))).to.have.no.errors();
});

it('should not throw error if opening parentheses is first symbol in the file', function() {
Expand Down Expand Up @@ -132,12 +161,56 @@ describe('rules/require-newline-before-block-statements', function() {
expect(checker.checkString('for (var i = 0, len = 10; i < 10; ++i)\n{\n\tx++;\n}')).to.have.no.errors();
});

it('should not complain when note configured with "for"', function() {
it('should not complain when not configured with "for"', function() {
checker.configure({ requireNewlineBeforeBlockStatements: ['if'] });
expect(checker.checkString('for (var i = 0, len = 10; i < 10; ++i) {\n\tx++;\n}')).to.have.no.errors();
});
});

describe('"switch" statements', function() {
beforeEach(function() {
checker.configure({ requireNewlineBeforeBlockStatements: ['switch'] });
});

it('should report missing newlines when configured with "switch"', function() {
expect(checker.checkString('switch (a) {\n\tcase 1: break;\n}'))
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
});

it('should report missing newline before opening brace when there are white-spaces between', function() {
expect(checker.checkString('switch (a) /* COOOMMMENTTT*/ {\n\tcase 1: break;\n}'))
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
});

it('should not report missing newline if there are more of them combined with white-spaces', function() {
expect(checker.checkString('switch (a) \n \n/*BLOCK*/ {\n\tcase 1: break;\n}'))
.to.have.no.errors();
});

it('should complain when configured with "switch" and no cases', function() {
expect(checker.checkString('switch (a) {\n}'))
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
});

it('should complain when configured with "switch" and parenthesized discriminant', function() {
expect(checker.checkString('switch ((function(){}())) {\n\tcase 1: break;\n}'))
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
});

it('should not complain when configured with "switch" and newline exists', function() {
expect(checker.checkString('switch (a)\n{\n\tcase 1: break;\n}')).to.have.no.errors();
});

it('should not complain when configured with "switch" and case on brace line', function() {
expect(checker.checkString('switch (a)\n{default: 1;\n}')).to.have.no.errors();
});

it('should not complain when not configured with "switch"', function() {
checker.configure({ requireNewlineBeforeBlockStatements: ['if'] });
expect(checker.checkString('switch (a) {\n\tdefault: 1;\n}')).to.have.no.errors();
});
});

describe('"for...in" loops', function() {
beforeEach(function() {
checker.configure({ requireNewlineBeforeBlockStatements: ['for'] });
Expand Down

0 comments on commit 87f864f

Please sign in to comment.