Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various print inefficiencies/bugs #3213

Merged
merged 2 commits into from
Dec 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();