Fix various print inefficiencies/bugs

This commit is contained in:
Amjad Masad 2015-12-27 01:23:45 -08:00
parent fd998cb9af
commit ed5a057e4e
6 changed files with 58 additions and 2 deletions

View File

@ -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);
@ -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);
}

View File

@ -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;

View File

@ -0,0 +1,7 @@
function x() {
return -1;
return --i;
return !2;
}
throw -1;

View File

@ -0,0 +1 @@
function x(){return-1;return--i;return!2}throw-1;

View File

@ -0,0 +1,13 @@
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);

View File

@ -0,0 +1,13 @@
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);