From fe4e9f9d97dcc6594a8fc49e04630aa619ff1866 Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Tue, 5 Jan 2016 13:56:52 +0200 Subject: [PATCH] Fix hoisting the var in ForIn Close #913 --- lib/compress.js | 5 ++++- test/compress/issue-913.js | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/compress/issue-913.js 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; + })(); + } +} -- 2.34.1