fix corner case in functions & unused (#3803)

fixes #3802
This commit is contained in:
Alex Lam S.L
2020-04-18 23:28:01 +01:00
committed by GitHub
parent eb6f32bfc3
commit e38754e802
2 changed files with 31 additions and 10 deletions

View File

@@ -4151,22 +4151,17 @@ merge(Compressor.prototype, {
def.value = def.value.drop_side_effect_free(compressor);
}
var var_defs = var_defs_by_id.get(sym.id);
if (var_defs.length > 1) {
if (!def.value) {
if (!def.value) {
if (var_defs.length > 1) {
AST_Node.warn("Dropping duplicated declaration of variable {name} [{file}:{line},{col}]", template(def.name));
remove(var_defs, def);
sym.eliminated++;
return;
} else {
head.push(def);
}
if (sym.orig.indexOf(def.name) > sym.eliminated) {
remove(var_defs, def);
duplicated++;
}
}
if (!def.value) {
head.push(def);
} else if (compressor.option("functions")
&& !compressor.option("ie8")
&& var_defs.length == 1
&& def.value === def.name.fixed_value()
&& def.value instanceof AST_Function
&& !(def.value.name && def.value.name.definition().assignments)
@@ -4188,6 +4183,10 @@ merge(Compressor.prototype, {
}
body.push(defun);
} else {
if (var_defs.length > 1 && sym.orig.indexOf(def.name) > sym.eliminated) {
remove(var_defs, def);
duplicated++;
}
if (side_effects.length > 0) {
if (tail.length > 0) {
side_effects.push(def.value);

View File

@@ -2488,3 +2488,25 @@ drop_duplicated_var_catch: {
}
}
}
issue_3802: {
options = {
functions: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = 0;
a += 0;
var a = function() {};
console.log(typeof a);
}
expect: {
var a = 0;
a += 0;
a = function() {};
console.log(typeof a);
}
expect_stdout: "function"
}