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

Commit 87f864f

Browse files
PaulAnnekovhzoo
authored andcommitted
(require|disallow)NewlineBeforeBlockStatements: account for SwitchStatement
requireNewlineBeforeBlockStatements disallowNewlineBeforeBlockStatements Fixes #2045 Closes gh-2059
1 parent ef9ed5e commit 87f864f

File tree

4 files changed

+203
-17
lines changed

4 files changed

+203
-17
lines changed

lib/rules/disallow-newline-before-block-statements.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*
88
* - `true` always disallows newline before curly brace of block statements
99
* - `Array` specifies block-type keywords after which newlines are disallowed before curly brace
10-
* - Valid types include: `['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function', 'class']`
10+
* - Valid types include: `['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function', 'class',
11+
* 'switch']`
1112
* - `Object`:
1213
* - `value`: `true` or an Array
1314
* - `allExcept`: Array of exceptions
@@ -308,6 +309,27 @@ module.exports.prototype = {
308309
}
309310
}
310311
});
312+
313+
if (setting === true || setting.indexOf('switch') !== -1) {
314+
file.iterateNodesByType(['SwitchStatement'], function(node) {
315+
var openingBrace = file.findNextToken(file.getLastNodeToken(node.discriminant), 'Punctuator', '{');
316+
var prevToken = file.getPrevToken(openingBrace);
317+
318+
if (hasMultiLineEx !== true) {
319+
assertSameLine(prevToken, openingBrace);
320+
return;
321+
}
322+
323+
var openingRoundBrace = file.findNextToken(file.getFirstNodeToken(node), 'Punctuator', '(');
324+
var closingRoundBrace = file.findPrevToken(openingBrace, 'Punctuator', ')');
325+
326+
if (openingRoundBrace.loc.start.line !== closingRoundBrace.loc.end.line) {
327+
assertDifferentLine(prevToken, openingBrace);
328+
} else {
329+
assertSameLine(prevToken, openingBrace);
330+
}
331+
});
332+
}
311333
}
312334
};
313335

lib/rules/require-newline-before-block-statements.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* - `true` always requires newline before curly brace of block statements
99
* - `Array` specifies block-type keywords after which newlines are required before curly brace
10-
* - Valid types include: `['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function']`
10+
* - Valid types include: `['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function', 'switch']`
1111
*
1212
* #### Example
1313
*
@@ -182,18 +182,31 @@ module.exports.prototype = {
182182

183183
check: function(file, errors) {
184184
var setting = this._setting;
185+
186+
function assertDifferentLine(token, nextToken) {
187+
errors.assert.differentLine({
188+
token: token,
189+
nextToken: nextToken,
190+
message: 'Newline before curly brace for block statement is required'
191+
});
192+
}
193+
185194
file.iterateNodesByType('BlockStatement', function(node) {
186195
if (setting === true || setting.indexOf(getBlockType(node)) !== -1) {
187196
var openingBrace = file.getFirstNodeToken(node);
188197
var prevToken = file.getPrevToken(openingBrace);
189198

190-
errors.assert.differentLine({
191-
token: prevToken,
192-
nextToken: openingBrace,
193-
message: 'Missing newline before curly brace for block statement'
194-
});
199+
assertDifferentLine(prevToken, openingBrace);
195200
}
196201
});
202+
if (setting === true || setting.indexOf('switch') !== -1) {
203+
file.iterateNodesByType(['SwitchStatement'], function(node) {
204+
var openingBrace = file.findNextToken(file.getLastNodeToken(node.discriminant), 'Punctuator', '{');
205+
var prevToken = file.getPrevToken(openingBrace);
206+
207+
assertDifferentLine(prevToken, openingBrace);
208+
});
209+
}
197210
}
198211
};
199212

test/specs/rules/disallow-newline-before-block-statements.js

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,37 @@ describe('rules/disallow-newline-before-block-statements', function() {
3030
});
3131

3232
reportAndFix({
33-
name: 'disallowed newline for all 3 statements',
33+
name: 'disallowed newline for all 4 statements',
3434
rules: { disallowNewlineBeforeBlockStatements: true },
35-
input: 'function test()\n{\nif(true)\n{\nreturn 1;\n}\nfor(var i in [1,2,3])\n{\n}\n}',
36-
output: 'function test() {\nif(true) {\nreturn 1;\n}\nfor(var i in [1,2,3]) {\n}\n}',
37-
errors: 3
35+
input: [
36+
'function test()',
37+
'{',
38+
'if(true)',
39+
'{',
40+
'switch (a)',
41+
'{',
42+
'case 1: break;',
43+
'}',
44+
'return 1;',
45+
'}',
46+
'for(var i in [1,2,3])',
47+
'{',
48+
'}',
49+
'}'
50+
].join('\n'),
51+
output: [
52+
'function test() {',
53+
'if(true) {',
54+
'switch (a) {',
55+
'case 1: break;',
56+
'}',
57+
'return 1;',
58+
'}',
59+
'for(var i in [1,2,3]) {',
60+
'}',
61+
'}'
62+
].join('\n'),
63+
errors: 4
3864
});
3965

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

181+
describe('"switch" statements', function() {
182+
beforeEach(function() {
183+
checker.configure({ disallowNewlineBeforeBlockStatements: ['switch'] });
184+
});
185+
186+
it('should report extra newlines when configured with "switch"', function() {
187+
expect(checker.checkString('switch (a)\n{\n\tcase 1: break;\n}'))
188+
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
189+
});
190+
191+
it('should not report newline before opening brace when there are white-spaces between', function() {
192+
expect(checker.checkString('switch (a) /* COOOMMMENTTT*/ {case 1: break;}')).to.have.no.errors();
193+
});
194+
195+
it('should complain when configured with "switch" and no cases', function() {
196+
expect(checker.checkString('switch (a)\n{\n}'))
197+
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
198+
});
199+
200+
it('should complain when configured with "switch" and parenthesized discriminant', function() {
201+
expect(checker.checkString('switch ((function(){}()))\n{\n\tcase 1: break;\n}'))
202+
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
203+
});
204+
205+
it('should not complain when configured with "switch" and case on brace line', function() {
206+
expect(checker.checkString('switch (a) {default: 1;\n}')).to.have.no.errors();
207+
});
208+
209+
it('should not complain when configured with "switch" and newline not added', function() {
210+
expect(checker.checkString('switch (a) {\n\tcase 1: break;\n}')).to.have.no.errors();
211+
});
212+
213+
it('should not complain when not configured with "switch"', function() {
214+
checker.configure({ disallowNewlineBeforeBlockStatements: ['if'] });
215+
expect(checker.checkString('switch (a)\n{\n\tcase 1: break;\n}')).to.have.no.errors();
216+
});
217+
});
218+
155219
describe('"for...in" loops', function() {
156220
beforeEach(function() {
157221
checker.configure({ disallowNewlineBeforeBlockStatements: ['for'] });
@@ -397,6 +461,9 @@ describe('rules/disallow-newline-before-block-statements', function() {
397461
expect(checker.checkString('function foo(a,\nb) { }'))
398462
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
399463

464+
expect(checker.checkString('switch((function(){}\n())) { }'))
465+
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
466+
400467
expect(checker.checkString('function foo() { for (var i=0; i<len; i++) { } }'))
401468
.to.have.no.errors();
402469

@@ -424,5 +491,16 @@ describe('rules/disallow-newline-before-block-statements', function() {
424491
expect(checker.checkString('try {\n\ty++;\n} catch(e) {\n}'))
425492
.to.have.no.errors();
426493
});
494+
495+
it('"switch" statements', function() {
496+
expect(checker.checkString('switch((function(){}\n())) { }'))
497+
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
498+
499+
expect(checker.checkString('switch((function(){}()))\n{ }'))
500+
.to.have.one.validation.error.from('disallowNewlineBeforeBlockStatements');
501+
502+
expect(checker.checkString('switch((function(){}\n()))\n{ }'))
503+
.to.have.no.errors();
504+
});
427505
});
428506
});

test/specs/rules/require-newline-before-block-statements.js

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ describe('rules/require-newline-before-block-statements', function() {
1919
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
2020
});
2121

22+
it('should report missing newline before opening brace for "switch"', function() {
23+
expect(checker.checkString('switch (a) {\n\tcase 1: break;\n}'))
24+
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
25+
});
26+
2227
it('should report missing newline before opening brace when there are white-spaces between', function() {
2328
expect(checker.checkString('function test() /* COOOMMMENTTT*/ {abc();}'))
2429
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
@@ -42,14 +47,38 @@ describe('rules/require-newline-before-block-statements', function() {
4247
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
4348
});
4449

45-
it('should report missing newline for all 3 statements', function() {
46-
expect(checker.checkString('function test(){\nif(true){\nreturn 1;\n}\nfor(var i in [1,2,3]){\n}\n}'))
47-
.to.have.error.count.equal(3);
50+
it('should report missing newline for all 4 statements', function() {
51+
expect(checker.checkString([
52+
'function test(){',
53+
'if(true){',
54+
'switch (a){',
55+
'case 1: break;',
56+
'}',
57+
'return 1;',
58+
'}',
59+
'for(var i in [1,2,3]){',
60+
'}',
61+
'}'
62+
].join('\n'))).to.have.error.count.equal(4);
4863
});
4964

5065
it('should not report missing newline', function() {
51-
expect(checker.checkString('function test()\n{\nif(true)\n{\nreturn 1;\n}\nfor(var i in [1,2,3])\n{\n}\n}'))
52-
.to.have.no.errors();
66+
expect(checker.checkString([
67+
'function test()',
68+
'{',
69+
'if(true)',
70+
'{',
71+
'switch (a)',
72+
'{',
73+
'case 1: break;',
74+
'}',
75+
'return 1;',
76+
'}',
77+
'for(var i in [1,2,3])',
78+
'{',
79+
'}',
80+
'}'
81+
].join('\n'))).to.have.no.errors();
5382
});
5483

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

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

170+
describe('"switch" statements', function() {
171+
beforeEach(function() {
172+
checker.configure({ requireNewlineBeforeBlockStatements: ['switch'] });
173+
});
174+
175+
it('should report missing newlines when configured with "switch"', function() {
176+
expect(checker.checkString('switch (a) {\n\tcase 1: break;\n}'))
177+
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
178+
});
179+
180+
it('should report missing newline before opening brace when there are white-spaces between', function() {
181+
expect(checker.checkString('switch (a) /* COOOMMMENTTT*/ {\n\tcase 1: break;\n}'))
182+
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
183+
});
184+
185+
it('should not report missing newline if there are more of them combined with white-spaces', function() {
186+
expect(checker.checkString('switch (a) \n \n/*BLOCK*/ {\n\tcase 1: break;\n}'))
187+
.to.have.no.errors();
188+
});
189+
190+
it('should complain when configured with "switch" and no cases', function() {
191+
expect(checker.checkString('switch (a) {\n}'))
192+
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
193+
});
194+
195+
it('should complain when configured with "switch" and parenthesized discriminant', function() {
196+
expect(checker.checkString('switch ((function(){}())) {\n\tcase 1: break;\n}'))
197+
.to.have.one.validation.error.from('requireNewlineBeforeBlockStatements');
198+
});
199+
200+
it('should not complain when configured with "switch" and newline exists', function() {
201+
expect(checker.checkString('switch (a)\n{\n\tcase 1: break;\n}')).to.have.no.errors();
202+
});
203+
204+
it('should not complain when configured with "switch" and case on brace line', function() {
205+
expect(checker.checkString('switch (a)\n{default: 1;\n}')).to.have.no.errors();
206+
});
207+
208+
it('should not complain when not configured with "switch"', function() {
209+
checker.configure({ requireNewlineBeforeBlockStatements: ['if'] });
210+
expect(checker.checkString('switch (a) {\n\tdefault: 1;\n}')).to.have.no.errors();
211+
});
212+
});
213+
141214
describe('"for...in" loops', function() {
142215
beforeEach(function() {
143216
checker.configure({ requireNewlineBeforeBlockStatements: ['for'] });

0 commit comments

Comments
 (0)