aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMihai Bazon <mihai.bazon@gmail.com>2016-01-05 13:56:52 +0200
committerMihai Bazon <mihai.bazon@gmail.com>2016-01-05 13:56:52 +0200
commitfe4e9f9d97dcc6594a8fc49e04630aa619ff1866 (patch)
tree9f2ede884eade57300136f8a7d965f28db787fc7
parent174404c0f37bf14aff23c41889e6bab1fd87c1d7 (diff)
downloadtracifyjs-fe4e9f9d97dcc6594a8fc49e04630aa619ff1866.tar.gz
tracifyjs-fe4e9f9d97dcc6594a8fc49e04630aa619ff1866.zip
Fix hoisting the var in ForIn
Close #913
-rw-r--r--lib/compress.js5
-rw-r--r--test/compress/issue-913.js20
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 44e19799..1f5988f7 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1276,7 +1276,10 @@ merge(Compressor.prototype, {
var seq = node.to_assignments();
var p = tt.parent();
if (p instanceof AST_ForIn && p.init === node) {
- if (seq == null) return node.definitions[0].name;
+ if (seq == null) {
+ var def = node.definitions[0].name;
+ return make_node(AST_SymbolRef, def, def);
+ }
return seq;
}
if (p instanceof AST_For && p.init === node) {
diff --git a/test/compress/issue-913.js b/test/compress/issue-913.js
new file mode 100644
index 00000000..9d34d9d9
--- /dev/null
+++ b/test/compress/issue-913.js
@@ -0,0 +1,20 @@
+keep_var_for_in: {
+ options = {
+ hoist_vars: true,
+ unused: true
+ };
+ input: {
+ (function(obj){
+ var foo = 5;
+ for (var i in obj)
+ return foo;
+ })();
+ }
+ expect: {
+ (function(obj){
+ var i, foo = 5;
+ for (i in obj)
+ return foo;
+ })();
+ }
+}