enhance if_return (#3560)

This commit is contained in:
Alex Lam S.L
2019-11-01 02:08:31 +08:00
committed by GitHub
parent 1e9b576ee9
commit 815eff1f7c
2 changed files with 47 additions and 12 deletions

View File

@@ -1749,9 +1749,7 @@ merge(Compressor.prototype, {
if (stat instanceof AST_If) { if (stat instanceof AST_If) {
var ab = aborts(stat.body); var ab = aborts(stat.body);
if (can_merge_flow(ab)) { if (can_merge_flow(ab)) {
if (ab.label) { if (ab.label) remove(ab.label.thedef.references, ab);
remove(ab.label.thedef.references, ab);
}
CHANGED = true; CHANGED = true;
stat = stat.clone(); stat = stat.clone();
stat.condition = stat.condition.negate(compressor); stat.condition = stat.condition.negate(compressor);
@@ -1779,23 +1777,34 @@ merge(Compressor.prototype, {
} }
} }
var ab = aborts(stat.alternative); var alt = aborts(stat.alternative);
if (can_merge_flow(ab)) { if (can_merge_flow(alt)) {
if (ab.label) { if (alt.label) remove(alt.label.thedef.references, alt);
remove(ab.label.thedef.references, ab);
}
CHANGED = true; CHANGED = true;
stat = stat.clone(); stat = stat.clone();
stat.body = make_node(AST_BlockStatement, stat.body, { stat.body = make_node(AST_BlockStatement, stat.body, {
body: as_statement_array(stat.body).concat(extract_functions()) body: as_statement_array(stat.body).concat(extract_functions())
}); });
var body = as_statement_array_with_return(stat.alternative, ab); var body = as_statement_array_with_return(stat.alternative, alt);
stat.alternative = make_node(AST_BlockStatement, stat.alternative, { stat.alternative = make_node(AST_BlockStatement, stat.alternative, {
body: body body: body
}); });
statements[i] = stat.transform(compressor); statements[i] = stat.transform(compressor);
continue; continue;
} }
if (compressor.option("typeofs")) {
if (ab && !alt) {
mark_locally_defined(stat.condition, null, make_node(AST_BlockStatement, self, {
body: statements.slice(i + 1)
}));
}
if (!ab && alt) {
mark_locally_defined(stat.condition, make_node(AST_BlockStatement, self, {
body: statements.slice(i + 1)
}));
}
}
} }
if (stat instanceof AST_If && stat.body instanceof AST_Return) { if (stat instanceof AST_If && stat.body instanceof AST_Return) {
@@ -1940,9 +1949,7 @@ merge(Compressor.prototype, {
&& loop_body(lct) === self && loop_body(lct) === self
|| stat instanceof AST_Continue || stat instanceof AST_Continue
&& loop_body(lct) === self) { && loop_body(lct) === self) {
if (stat.label) { if (stat.label) remove(stat.label.thedef.references, stat);
remove(stat.label.thedef.references, stat);
}
} else { } else {
statements[n++] = stat; statements[n++] = stat;
} }

View File

@@ -409,3 +409,31 @@ typeof_defined_4: {
"object" != typeof A || "object" == typeof B || B; "object" != typeof A || "object" == typeof B || B;
} }
} }
emberjs_global: {
options = {
comparisons: true,
conditionals: true,
if_return: true,
passes: 2,
side_effects: true,
toplevel: true,
typeofs: true,
unused: true,
}
input: {
var a;
if (typeof A === "object") {
a = A;
} else if (typeof B === "object") {
a = B;
} else {
throw new Error("PASS");
}
}
expect: {
if ("object" != typeof A && "object" != typeof B)
throw new Error("PASS");
}
expect_stdout: Error("PASS")
}