Skip to content

Commit

Permalink
fix line break before "else if". closes #426. closes #438
Browse files Browse the repository at this point in the history
  • Loading branch information
millermedeiros committed May 22, 2016
1 parent a038876 commit a3599b7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/hooks/IfStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ var _limit = require('../limit');

exports.format = function IfStatement(node) {

var startBody = node.consequent.startToken;
var endBody = node.consequent.endToken;

var conditionalStart = _tk.findPrev(node.test.startToken, '(');
var conditionalEnd = _tk.findNext(node.test.endToken, ')');

Expand All @@ -23,6 +20,8 @@ exports.format = function IfStatement(node) {

if (alt.type === 'IfStatement') {
// ElseIfStatement

// space between "else if" (`alt.startToken` is "if")
_br.limitBefore(alt.startToken, 0);
_ws.limitBefore(alt.startToken, 1);

Expand All @@ -31,7 +30,15 @@ exports.format = function IfStatement(node) {
_br.limitBefore(alt.consequent.endToken, 'ElseIfStatementClosingBrace');
}

_br.limitBefore(elseKeyword, 'ElseIfStatement');
// only remove line break before "else" if previous char is "}"; white
// space is handled by "whiteSpace.after.IfStatementClosingBrace"
_br.limitBefore(
elseKeyword,
node.consequent.type === 'BlockStatement' ?
'ElseIfStatement' :
1
);

if (!alt.alternate) {
// we only limit the line breaks after the ElseIfStatement if it is not
// followed by an ElseStatement, otherwise it would add line breaks
Expand All @@ -47,16 +54,16 @@ exports.format = function IfStatement(node) {
_br.limitBefore(elseKeyword, 'ElseStatement');
_br.limitAfter(alt.endToken, 'ElseStatement');

_ws.limitBefore(elseKeyword, 1);

_limit.around(alt.endToken, 'ElseStatementClosingBrace');
} else {
// ElseStatement without curly braces
_ws.limitAfter(elseKeyword, 1);
}
}

// only handle braces if block statement
var startBody = node.consequent.startToken;
var endBody = node.consequent.endToken;

if (node.consequent.type === 'BlockStatement') {
_limit.around(startBody, 'IfStatementOpeningBrace');
if (!alt) {
Expand Down
22 changes: 22 additions & 0 deletions test/compare/default/if_statement-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,25 @@ if (foo) {

baz();
}

// # 426
if (foo)
bar
else if (baz)
done
else
dolor

if (foo)
bar
else
dolor

// #438
if (a == 1) {
b;
}else if (a == 2) { // notice the space before else
c;
} else {
d;
}
22 changes: 22 additions & 0 deletions test/compare/default/if_statement-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,25 @@ if (foo) {

baz();
}

// # 426
if (foo)
bar
else if (baz)
done
else
dolor

if (foo)
bar
else
dolor

// #438
if (a == 1) {
b;
} else if (a == 2) { // notice the space before else
c;
} else {
d;
}

0 comments on commit a3599b7

Please sign in to comment.