Skip to content

Commit 4d63d4f

Browse files
kzcalexlamsl
authored andcommittedMar 2, 2017
collapse_vars should not replace constant in for-in init section (#1538)
fixes #1537
1 parent 70d72ad commit 4d63d4f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed
 

‎lib/compress.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,12 @@ merge(Compressor.prototype, {
480480
// Constant single use vars can be replaced in any scope.
481481
if (var_decl.value.is_constant()) {
482482
var ctt = new TreeTransformer(function(node) {
483-
if (node === ref)
484-
return replace_var(node, ctt.parent(), true);
483+
if (node === ref) {
484+
var parent = ctt.parent();
485+
if (!(parent instanceof AST_ForIn && parent.init === node)) {
486+
return replace_var(node, parent, true);
487+
}
488+
}
485489
});
486490
stat.transform(ctt);
487491
continue;
@@ -570,7 +574,7 @@ merge(Compressor.prototype, {
570574
// Further optimize statement after substitution.
571575
stat.reset_opt_flags(compressor);
572576

573-
compressor.warn("Replacing " + (is_constant ? "constant" : "variable") +
577+
compressor.warn("Collapsing " + (is_constant ? "constant" : "variable") +
574578
" " + var_name + " [{file}:{line},{col}]", node.start);
575579
CHANGED = true;
576580
return value;

‎test/compress/collapse_vars.js

+14
Original file line numberDiff line numberDiff line change
@@ -1315,3 +1315,17 @@ collapse_vars_regexp: {
13151315
})();
13161316
}
13171317
}
1318+
1319+
issue_1537: {
1320+
options = {
1321+
collapse_vars: true,
1322+
}
1323+
input: {
1324+
var k = '';
1325+
for (k in {prop: 'val'}){}
1326+
}
1327+
expect: {
1328+
var k = '';
1329+
for (k in {prop: 'val'});
1330+
}
1331+
}

0 commit comments

Comments
 (0)
Please sign in to comment.