aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkzc <kzc@users.noreply.github.com>2017-03-02 14:51:15 -0500
committerAlex Lam S.L <alexlamsl@gmail.com>2017-03-03 03:51:15 +0800
commit4d63d4f5b30d2b46f3b6ed4cfced277f4f8e428f (patch)
treebb510a5fcd6c411b2f239dd85792d2c8ffd96e54
parent70d72ad8065421908ff9a8658539359789c4f460 (diff)
downloadtracifyjs-4d63d4f5b30d2b46f3b6ed4cfced277f4f8e428f.tar.gz
tracifyjs-4d63d4f5b30d2b46f3b6ed4cfced277f4f8e428f.zip
collapse_vars should not replace constant in for-in init section (#1538)
fixes #1537
-rw-r--r--lib/compress.js10
-rw-r--r--test/compress/collapse_vars.js14
2 files changed, 21 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js
index f67f1d2c..deb55ade 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -480,8 +480,12 @@ merge(Compressor.prototype, {
// Constant single use vars can be replaced in any scope.
if (var_decl.value.is_constant()) {
var ctt = new TreeTransformer(function(node) {
- if (node === ref)
- return replace_var(node, ctt.parent(), true);
+ if (node === ref) {
+ var parent = ctt.parent();
+ if (!(parent instanceof AST_ForIn && parent.init === node)) {
+ return replace_var(node, parent, true);
+ }
+ }
});
stat.transform(ctt);
continue;
@@ -570,7 +574,7 @@ merge(Compressor.prototype, {
// Further optimize statement after substitution.
stat.reset_opt_flags(compressor);
- compressor.warn("Replacing " + (is_constant ? "constant" : "variable") +
+ compressor.warn("Collapsing " + (is_constant ? "constant" : "variable") +
" " + var_name + " [{file}:{line},{col}]", node.start);
CHANGED = true;
return value;
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index 5f63488f..82d943ff 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -1315,3 +1315,17 @@ collapse_vars_regexp: {
})();
}
}
+
+issue_1537: {
+ options = {
+ collapse_vars: true,
+ }
+ input: {
+ var k = '';
+ for (k in {prop: 'val'}){}
+ }
+ expect: {
+ var k = '';
+ for (k in {prop: 'val'});
+ }
+}