diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-11-17 02:36:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-17 02:36:42 +0800 |
commit | a6a0319f1c758013e8f1be9632fa30b031144835 (patch) | |
tree | b5f4463a45cc39063215ae181b5c8ebc50df3096 /lib/compress.js | |
parent | d1b2ecec27ea17e42d504503c6d760ff3c67b9ad (diff) | |
download | tracifyjs-a6a0319f1c758013e8f1be9632fa30b031144835.tar.gz tracifyjs-a6a0319f1c758013e8f1be9632fa30b031144835.zip |
compress empty for-in loops (#3590)
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/lib/compress.js b/lib/compress.js index 9366091c..e85cf5d3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3912,19 +3912,6 @@ merge(Compressor.prototype, { scope = save_scope; return node; } - - function log(sym, text, props) { - AST_Node[sym.unreferenced() ? "warn" : "info"](text, props); - } - - function template(sym) { - return { - name : sym.name, - file : sym.start.file, - line : sym.start.line, - col : sym.start.col - }; - } }, function(node, in_list) { if (node instanceof AST_For) { // Certain combination of unused name + side effect leads to invalid AST: @@ -3952,6 +3939,21 @@ merge(Compressor.prototype, { node.init = null; } return !block ? node : in_list ? MAP.splice(block.body) : block; + } else if (node instanceof AST_ForIn) { + if (!drop_vars || !compressor.option("loops")) return; + if (!(node.init instanceof AST_Definitions)) return; + var sym = node.init.definitions[0].name; + if (sym.definition().id in in_use_ids) return; + if (!is_empty(node.body)) return; + log(sym, "Dropping unused loop variable {name} [{file}:{line},{col}]", template(sym)); + var value = node.object.drop_side_effect_free(compressor); + if (value) { + AST_Node.warn("Side effects in object of for-in loop [{file}:{line},{col}]", template(sym)); + return make_node(AST_SimpleStatement, node, { + body: value + }); + } + return in_list ? MAP.skip : make_node(AST_EmptyStatement, node); } else if (node instanceof AST_Sequence) { if (node.expressions.length == 1) return node.expressions[0]; } @@ -3962,6 +3964,19 @@ merge(Compressor.prototype, { fn.name = null; }); + function log(sym, text, props) { + AST_Node[sym.unreferenced() ? "warn" : "info"](text, props); + } + + function template(sym) { + return { + name: sym.name, + file: sym.start.file, + line: sym.start.line, + col : sym.start.col + }; + } + function verify_safe_usage(def, read, modified) { if (def.id in in_use_ids) return; if (read && modified) { |