fix corner case

This commit is contained in:
alexlamsl 2018-01-04 00:11:39 +08:00
parent 373d897e6c
commit 35f0003fe0
2 changed files with 25 additions and 10 deletions

View File

@ -2720,6 +2720,7 @@ merge(Compressor.prototype, {
};
var in_use = [];
var in_use_ids = Object.create(null); // avoid expensive linear scans of in_use
var fixed_ids = Object.create(null);
if (self instanceof AST_Toplevel && compressor.top_retain) {
self.variables.each(function(def) {
if (compressor.top_retain(def) && !(def.id in in_use_ids)) {
@ -2730,7 +2731,6 @@ merge(Compressor.prototype, {
}
var var_defs_by_id = new Dictionary();
var initializations = new Dictionary();
var fixed_values = Object.create(null);
// pass 1: find out which symbols are directly used in
// this scope (not in nested scopes).
var scope = this;
@ -2767,9 +2767,8 @@ merge(Compressor.prototype, {
if (def.value.has_side_effects(compressor)) {
def.value.walk(tw);
}
var fixed = def.name.fixed_value();
if (fixed === def.value) {
fixed_values[node_def.id] = fixed;
if (def.name.fixed_value() === def.value) {
fixed_ids[node_def.id] = true;
}
}
});
@ -2799,8 +2798,8 @@ merge(Compressor.prototype, {
var in_use = def.id in in_use_ids;
if (node instanceof AST_Assign) {
if (!in_use
|| def.id in fixed_values
&& fixed_values[def.id] !== node.right) {
|| def.id in fixed_ids
&& node.left.fixed_value() !== node.right) {
return maintain_this_binding(parent, node, node.right.transform(tt));
}
} else if (!in_use) return make_node(AST_Number, node, {
@ -2870,7 +2869,7 @@ merge(Compressor.prototype, {
return;
}
}
if (def.value && sym.id in fixed_values && fixed_values[sym.id] !== def.value) {
if (def.value && sym.id in fixed_ids && def.name.fixed_value() !== def.value) {
def.value = def.value.drop_side_effect_free(compressor);
}
if (def.value) {
@ -2982,9 +2981,8 @@ merge(Compressor.prototype, {
&& self.variables.get(sym.name) === (node_def = sym.definition())) {
if (node instanceof AST_Assign) {
node.right.walk(tw);
var fixed = node.left.fixed_value();
if (fixed === node.right) {
fixed_values[node_def.id] = fixed;
if (node.left.fixed_value() === node.right) {
fixed_ids[node_def.id] = true;
}
}
return true;

View File

@ -1627,3 +1627,20 @@ double_assign_3: {
var a;
}
}
cascade_drop_assign: {
options = {
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a, b = a = "PASS";
console.log(b);
}
expect: {
var b = "PASS";
console.log(b);
}
expect_stdout: "PASS"
}