Merge pull request #3092 from amasad/nested-if

Print a block when the consequent is an if statement
This commit is contained in:
Sebastian McKenzie 2015-11-17 22:54:21 -08:00
commit f8501bdee3
2 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,19 @@
var res = transform('', {
plugins: function (b) {
var t = b.types;
return {
visitor: {
Program: function(path) {
if (this.done) return;
// if (false) { if (true) 42; } else 23;
var inner = t.ifStatement(t.booleanLiteral(true), t.expressionStatement(t.numericLiteral(42)), null);
var outer = t.ifStatement(t.booleanLiteral(false), inner, t.expressionStatement(t.numericLiteral(23)));
path.replaceWith(t.program([outer]));
this.done = true;
}
}
}
}
});
assert.equal(eval(res.code), 23);

View File

@ -16,8 +16,21 @@ export function IfStatement(node: Object) {
this.push(")");
this.space();
let needsBlock = node.alternate && t.isIfStatement(node.consequent);
if (needsBlock) {
this.push("{");
this.newline();
this.indent();
}
this.printAndIndentOnComments(node.consequent, node);
if (needsBlock) {
this.dedent();
this.newline();
this.push("}");
}
if (node.alternate) {
if (this.isLast("}")) this.space();
this.push("else ");