Skip to content

Commit

Permalink
fix unused on for-in statements (#1843)
Browse files Browse the repository at this point in the history
Only need to avoid `var` within the initialisation block.

fixes #1841
  • Loading branch information
alexlamsl committed Apr 23, 2017
1 parent 9bf72cf commit 9e62628
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/compress.js
Expand Up @@ -1970,7 +1970,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)) {
// place uninitialized names at the start
var body = [], head = [], tail = [];
// for unused names whose initialization has
Expand Down
52 changes: 52 additions & 0 deletions test/compress/functions.js
Expand Up @@ -93,3 +93,55 @@ 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"
}

0 comments on commit 9e62628

Please sign in to comment.