diff options
-rw-r--r-- | lib/compress.js | 41 | ||||
-rw-r--r-- | test/compress/loops.js | 51 |
2 files changed, 77 insertions, 15 deletions
diff --git a/lib/compress.js b/lib/compress.js index 273c5a75..7355ff53 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4642,16 +4642,7 @@ merge(Compressor.prototype, { body: make_sequence(node, side_effects) })); } - switch (body.length) { - case 0: - return in_list ? List.skip : make_node(AST_EmptyStatement, node); - case 1: - return body[0]; - default: - return in_list ? List.splice(body) : make_node(AST_BlockStatement, node, { - body: body - }); - } + return insert_statements(body, node, in_list); } if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) { // Certain combination of unused name + side effect leads to invalid AST: @@ -4714,12 +4705,19 @@ merge(Compressor.prototype, { if (def.id in in_use_ids) return; if (def.scope !== self && member(def, self.enclosed)) return; log(sym, "Dropping unused loop variable {name}"); + if (for_ins[def.id] === node) delete for_ins[def.id]; + var body = []; var value = node.object.drop_side_effect_free(compressor); - if (!value) return in_list ? List.skip : make_node(AST_EmptyStatement, node); - AST_Node.warn("Side effects in object of for-in loop [{file}:{line},{col}]", value.start); - return make_node(AST_SimpleStatement, node, { - body: value - }); + if (value) { + AST_Node.warn("Side effects in object of for-in loop [{file}:{line},{col}]", value.start); + body.push(make_node(AST_SimpleStatement, node, { + body: value + })); + } + if (node.init instanceof AST_Definitions && def.orig[0] instanceof AST_SymbolCatch) { + body.push(node.init); + } + return insert_statements(body, node, in_list); } else if (node instanceof AST_Sequence) { if (node.expressions.length == 1) return node.expressions[0]; } @@ -4752,6 +4750,19 @@ merge(Compressor.prototype, { }; } + function insert_statements(body, orig, in_list) { + switch (body.length) { + case 0: + return in_list ? List.skip : make_node(AST_EmptyStatement, orig); + case 1: + return body[0]; + default: + return in_list ? List.splice(body) : make_node(AST_BlockStatement, orig, { + body: body + }); + } + } + function track_assigns(def, node) { if (def.scope !== self) return false; if (!def.fixed || !node.fixed) assign_in_use[def.id] = false; diff --git a/test/compress/loops.js b/test/compress/loops.js index 878b77cb..cbc1166f 100644 --- a/test/compress/loops.js +++ b/test/compress/loops.js @@ -1037,3 +1037,54 @@ issue_4084: { } expect_stdout: "undefined" } + +issue_4091_1: { + options = { + loops: true, + toplevel: true, + unused: true, + } + input: { + try { + throw "FAIL"; + } catch (e) { + for (var e in 42); + } + console.log(e && e); + } + expect: { + try { + throw "FAIL"; + } catch (e) { + var e; + } + console.log(e && e); + } + expect_stdout: "undefined" +} + +issue_4091_2: { + options = { + loops: true, + toplevel: true, + unused: true, + } + input: { + try { + throw "FAIL"; + } catch (e) { + for (e in 42); + var e; + } + console.log(e && e); + } + expect: { + try { + throw "FAIL"; + } catch (e) { + var e; + } + console.log(e && e); + } + expect_stdout: "undefined" +} |