fix duplicate declarations

This commit is contained in:
alexlamsl 2017-04-18 06:11:45 +08:00
parent 2bdf3dc1c0
commit f0f3c472c3
2 changed files with 29 additions and 7 deletions

View File

@ -277,7 +277,7 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_VarDef) {
var d = node.name.definition();
if (safe_to_assign(d, node.value)) {
if (d.fixed === undefined || safe_to_assign(d, node.value)) {
if (node.value) {
d.fixed = function() {
return node.value;
@ -297,9 +297,7 @@ merge(Compressor.prototype, {
&& node.operator == "="
&& node.left instanceof AST_SymbolRef) {
var d = node.left.definition();
if (HOP(safe_ids, d.id)
&& safe_to_read(d)
&& safe_to_assign(d, node.right)) {
if (safe_to_assign(d, node.right)) {
d.references.push(node.left);
d.fixed = function() {
return node.right;
@ -325,6 +323,7 @@ merge(Compressor.prototype, {
return true;
}
if (node instanceof AST_Function) {
push();
var iife;
if (!node.name
&& (iife = tw.parent()) instanceof AST_Call
@ -340,7 +339,6 @@ merge(Compressor.prototype, {
mark(d, true);
});
}
push();
descend();
pop();
return true;
@ -451,7 +449,8 @@ merge(Compressor.prototype, {
}
function safe_to_assign(def, value) {
if (def.fixed === undefined) return true;
if (!HOP(safe_ids, def.id)) return false;
if (!safe_to_read(def)) return false;
if (def.fixed === false) return false;
if (def.fixed != null && (!value || def.references.length > 0)) return false;
return !def.orig.some(function(sym) {

View File

@ -571,7 +571,7 @@ inner_var_label: {
}
}
inner_var_for: {
inner_var_for_1: {
options = {
evaluate: true,
reduce_vars: true,
@ -600,6 +600,29 @@ inner_var_for: {
}
}
inner_var_for_2: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
!function() {
var a = 1;
for (var b = 1; --b;) var a = 2;
console.log(a);
}();
}
expect: {
!function() {
a = 1;
for (var b = 1; --b;) var a = 2;
console.log(a);
}();
}
expect_stdout: "1"
}
inner_var_for_in_1: {
options = {
evaluate: true,