Merge pull request #3092 from amasad/nested-if
Print a block when the consequent is an if statement
This commit is contained in:
commit
f8501bdee3
19
packages/babel-core/test/fixtures/plugins/nested-if-alternate/exec.js
vendored
Normal file
19
packages/babel-core/test/fixtures/plugins/nested-if-alternate/exec.js
vendored
Normal 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);
|
||||
@ -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 ");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user