Skip to content

Commit

Permalink
Merge pull request #3213 from babel/print-bugs
Browse files Browse the repository at this point in the history
Fix various print inefficiencies/bugs
  • Loading branch information
amasad committed Dec 28, 2015
2 parents 082c785 + 5be9d9d commit 570b50c
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/babel-generator/src/generators/statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ function buildLabelStatement(prefix, key = "label") {

let label = node[key];
if (label) {
this.push(" ");
if (!(this.format.minified && (t.isUnaryExpression(label, { prefix: true }) ||
t.isUpdateExpression(label, { prefix: true })))) {
this.push(" ");

}

let terminatorState = this.startTerminatorless();
this.print(label, node);
this.endTerminatorless(terminatorState);
Expand Down Expand Up @@ -153,7 +158,8 @@ export function CatchClause(node: Object) {
this.keyword("catch");
this.push("(");
this.print(node.param, node);
this.push(") ");
this.push(")");
this.space();
this.print(node.body, node);
}

Expand Down
16 changes: 16 additions & 0 deletions packages/babel-generator/src/node/parentheses.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ export function SequenceExpression(node: Object, parent: Object): boolean {
return false;
}

if (t.isSwitchStatement(parent) && parent.discriminant === node) {
return false;
}

if (t.isWhileStatement(parent) && parent.test === node) {
return false;
}

if (t.isIfStatement(parent) && parent.test === node) {
return false;
}

if (t.isForInStatement(parent) && parent.right === node) {
return false;
}

// Otherwise err on the side of overparenthesization, adding
// explicit exceptions above if this proves overzealous.
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function x() {
return -1;
return --i;
return !2;
}

throw -1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function x(){return-1;return--i;return!2}throw-1;
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
a && (a.b && a.b.c()) && function() {}() && {a: 1}.a;
a && (a.b && a.b.c()) && function() {}() && {a: 1}.a
!function () {}();
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
a && a.b && a.b.c() && function () {}() && { a: 1 }.a;
!function () {}();
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function foo() {
return a, b;
}

if (a, b, c) d();

throw a, b, c;

switch (a, b, c) {}

for (a in b, c);

while (a, b, c);

!(function () {})(), a();
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function foo() {
return a, b;
}

if (a, b, c) d();

throw a, b, c;

switch (a, b, c) {}

for (a in b, c);

while (a, b, c);

!function () {}(), a();

0 comments on commit 570b50c

Please sign in to comment.