Print a block when encountering consequents that are if statements

This commit is contained in:
Amjad Masad
2015-11-17 19:07:20 -08:00
parent 507557c48e
commit 35e575cf93
2 changed files with 14 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ var res = transform('', {
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]));

View File

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