fix unused on for-in statements (#1843)

Only need to avoid `var` within the initialisation block.

fixes #1841
This commit is contained in:
Alex Lam S.L
2017-04-24 03:14:01 +08:00
committed by alexlamsl
parent d0faa471db
commit 2c21dc5e8e
2 changed files with 55 additions and 1 deletions

View File

@@ -1992,7 +1992,7 @@ merge(Compressor.prototype, {
}
return node;
}
if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) {
if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn && tt.parent().init === node)) {
var def = node.definitions.filter(function(def){
if (def.value) def.value = def.value.transform(tt);
var sym = def.name.definition();

View File

@@ -93,3 +93,57 @@ issue_485_crashing_1530: {
this, void 0;
}
}
issue_1841_1: {
options = {
keep_fargs: false,
pure_getters: "strict",
reduce_vars: true,
unused: true,
}
input: {
var b = 10;
!function(arg) {
for (var key in "hi")
var n = arg.baz, n = [ b = 42 ];
}(--b);
console.log(b);
}
expect: {
var b = 10;
!function() {
for (var key in "hi") {
b = 42;
}
}(--b);
console.log(b);
}
expect_exact: "42"
}
issue_1841_2: {
options = {
keep_fargs: false,
pure_getters: false,
reduce_vars: true,
unused: true,
}
input: {
var b = 10;
!function(arg) {
for (var key in "hi")
var n = arg.baz, n = [ b = 42 ];
}(--b);
console.log(b);
}
expect: {
var b = 10;
!function(arg) {
for (var key in "hi") {
arg.baz, b = 42;
}
}(--b);
console.log(b);
}
expect_exact: "42"
}