Skip to content

Commit

Permalink
fix SwitchCaseColon when not immediately followed by another case. cl…
Browse files Browse the repository at this point in the history
…oses #392
  • Loading branch information
stutrek authored and millermedeiros committed Jan 28, 2016
1 parent 2ddc6d7 commit f103c24
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
19 changes: 9 additions & 10 deletions lib/hooks/SwitchCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ exports.format = function SwitchCase(node) {
}
var endToken = node.endToken;

if (endToken.value === ':') {
limit.before(endToken, 'SwitchCaseColon');
limit.after(endToken, 'SwitchCaseColon');
} else {
// endToken might be ":" or "break" or ";"
var breakKeyword = _tk.findPrev(endToken.next, 'break');
if (breakKeyword) {
limit.before(breakKeyword, 'BreakKeyword');
limit.after(endToken, 'BreakKeyword');
}
var colon = _tk.findNext(node.startToken, ':');
limit.before(colon, 'SwitchCaseColon');
limit.after(colon, 'SwitchCaseColon');

// endToken might be ":" or "break" or ";"
var breakKeyword = _tk.findInBetweenFromEnd(node.startToken, endToken.next, 'break');
if (breakKeyword) {
limit.before(breakKeyword, 'BreakKeyword');
limit.after(endToken, 'BreakKeyword');
}
};

Expand Down
1 change: 1 addition & 0 deletions lib/preset/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"ReturnStatement" : -1,
"SwitchOpeningBrace" : 0,
"SwitchClosingBrace" : ">=1",
"SwitchCaseColon": 0,
"ThisExpression" : -1,
"ThrowStatement" : ">=1",
"TryKeyword": -1,
Expand Down
12 changes: 12 additions & 0 deletions test/compare/custom/switch_statement-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ switch ( event.keyCode ) {
x();
}

switch ( event.keyCode ) {
case $.ui.keyCode.ENTER :
whatever = 'nothing';
break
case $.ui.keyCode.ESCAPE
:
whatever = 'something';
break;
default:
x();
}

call(function(){
switch (fruit) {
case Fruit.APPLE:
Expand Down
14 changes: 14 additions & 0 deletions test/compare/custom/switch_statement-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ default:
x();
}

switch ( event.keyCode )
{
case $.ui.keyCode.ENTER:
whatever = 'nothing';
break

case $.ui.keyCode.ESCAPE:
whatever = 'something';
break;

default:
x();
}

call(function() {
switch ( fruit )
{
Expand Down

0 comments on commit f103c24

Please sign in to comment.