fix corner case in join_vars (#3224)

This commit is contained in:
Alex Lam S.L
2018-07-27 19:34:44 +08:00
committed by GitHub
parent 304db15a20
commit d47547dc71
2 changed files with 48 additions and 24 deletions

View File

@@ -2000,7 +2000,7 @@ merge(Compressor.prototype, {
statements.length = n; statements.length = n;
} }
function join_object_assignments(defn, body) { function join_assigns(defn, body) {
var exprs; var exprs;
if (body instanceof AST_Assign) { if (body instanceof AST_Assign) {
exprs = [ body ]; exprs = [ body ];
@@ -2010,7 +2010,7 @@ merge(Compressor.prototype, {
if (!exprs) return; if (!exprs) return;
if (defn instanceof AST_Definitions) { if (defn instanceof AST_Definitions) {
var def = defn.definitions[defn.definitions.length - 1]; var def = defn.definitions[defn.definitions.length - 1];
if (trim_object_assignments(def.name, def.value, exprs)) return exprs; if (trim_assigns(def.name, def.value, exprs)) return exprs;
} }
for (var i = exprs.length - 1; --i >= 0;) { for (var i = exprs.length - 1; --i >= 0;) {
var expr = exprs[i]; var expr = exprs[i];
@@ -2018,12 +2018,12 @@ merge(Compressor.prototype, {
if (expr.operator != "=") continue; if (expr.operator != "=") continue;
if (!(expr.left instanceof AST_SymbolRef)) continue; if (!(expr.left instanceof AST_SymbolRef)) continue;
var tail = exprs.slice(i + 1); var tail = exprs.slice(i + 1);
if (!trim_object_assignments(expr.left, expr.right, tail)) continue; if (!trim_assigns(expr.left, expr.right, tail)) continue;
return exprs.slice(0, i + 1).concat(tail); return exprs.slice(0, i + 1).concat(tail);
} }
} }
function trim_object_assignments(name, value, exprs) { function trim_assigns(name, value, exprs) {
if (!(value instanceof AST_Object)) return; if (!(value instanceof AST_Object)) return;
var trimmed = false; var trimmed = false;
do { do {
@@ -2074,9 +2074,9 @@ merge(Compressor.prototype, {
defs = stat; defs = stat;
} }
} else if (stat instanceof AST_Exit) { } else if (stat instanceof AST_Exit) {
stat.value = extract_object_assignments(stat.value); stat.value = join_assigns_expr(stat.value);
} else if (stat instanceof AST_For) { } else if (stat instanceof AST_For) {
var exprs = join_object_assignments(prev, stat.init); var exprs = join_assigns(prev, stat.init);
if (exprs) { if (exprs) {
CHANGED = true; CHANGED = true;
stat.init = exprs.length ? make_sequence(stat.init, exprs) : null; stat.init = exprs.length ? make_sequence(stat.init, exprs) : null;
@@ -2097,11 +2097,11 @@ merge(Compressor.prototype, {
statements[++j] = stat; statements[++j] = stat;
} }
} else if (stat instanceof AST_ForIn) { } else if (stat instanceof AST_ForIn) {
stat.object = extract_object_assignments(stat.object); stat.object = join_assigns_expr(stat.object);
} else if (stat instanceof AST_If) { } else if (stat instanceof AST_If) {
stat.condition = extract_object_assignments(stat.condition); stat.condition = join_assigns_expr(stat.condition);
} else if (stat instanceof AST_SimpleStatement) { } else if (stat instanceof AST_SimpleStatement) {
var exprs = join_object_assignments(prev, stat.body); var exprs = join_assigns(prev, stat.body);
if (exprs) { if (exprs) {
CHANGED = true; CHANGED = true;
if (!exprs.length) continue; if (!exprs.length) continue;
@@ -2109,29 +2109,23 @@ merge(Compressor.prototype, {
} }
statements[++j] = stat; statements[++j] = stat;
} else if (stat instanceof AST_Switch) { } else if (stat instanceof AST_Switch) {
stat.expression = extract_object_assignments(stat.expression); stat.expression = join_assigns_expr(stat.expression);
} else if (stat instanceof AST_With) { } else if (stat instanceof AST_With) {
stat.expression = extract_object_assignments(stat.expression); stat.expression = join_assigns_expr(stat.expression);
} else { } else {
statements[++j] = stat; statements[++j] = stat;
} }
} }
statements.length = j + 1; statements.length = j + 1;
function extract_object_assignments(value) { function join_assigns_expr(value) {
statements[++j] = stat; statements[++j] = stat;
var exprs = join_object_assignments(prev, value); var exprs = join_assigns(prev, value);
if (exprs) { if (!exprs) return value;
CHANGED = true; CHANGED = true;
if (exprs.length) { var tail = value.tail_node();
return make_sequence(value, exprs); if (exprs[exprs.length - 1] !== tail) exprs.push(tail.left);
} else if (value instanceof AST_Sequence) { return make_sequence(value, exprs);
return value.tail_node().left;
} else {
return value.left;
}
}
return value;
} }
} }
} }

View File

@@ -1832,3 +1832,33 @@ issue_3188_3: {
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }
join_expr: {
options = {
evaluate: true,
join_vars: true,
}
input: {
var c = "FAIL";
(function() {
var a = 0;
switch ((a = {}) && (a.b = 0)) {
case 0:
c = "PASS";
}
})();
console.log(c);
}
expect: {
var c = "FAIL";
(function() {
var a = 0;
switch (a = { b: 0 }, a.b) {
case 0:
c = "PASS";
}
})();
console.log(c);
}
expect_stdout: "PASS"
}