diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-04-17 17:11:29 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-17 17:11:29 +0800 |
commit | 4ffb6fce7668a1199284e4ce8be91fdaeaf2df0e (patch) | |
tree | b3c15bc9198c44edfb7aae4a87cfa0c61e564b91 /lib/compress.js | |
parent | 71a8d0d236d29015df2d1df18de11d661d17af2e (diff) | |
download | tracifyjs-4ffb6fce7668a1199284e4ce8be91fdaeaf2df0e.tar.gz tracifyjs-4ffb6fce7668a1199284e4ce8be91fdaeaf2df0e.zip |
compress duplicated variable definitions (#1817)
These are surprisingly common, as people reuse the same variable name within loops or switch branches.
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/lib/compress.js b/lib/compress.js index cc42c46e..7324fe0e 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1813,6 +1813,7 @@ merge(Compressor.prototype, { } }); } + var var_defs_by_id = new Dictionary(); var initializations = new Dictionary(); // pass 1: find out which symbols are directly used in // this scope (not in nested scopes). @@ -1832,8 +1833,11 @@ merge(Compressor.prototype, { } if (node instanceof AST_Definitions && scope === self) { node.definitions.forEach(function(def){ + var node_def = def.name.definition(); + if (def.name instanceof AST_SymbolVar) { + var_defs_by_id.add(node_def.id, def); + } if (!drop_vars) { - var node_def = def.name.definition(); if (!(node_def.id in in_use_ids)) { in_use_ids[node_def.id] = true; in_use.push(node_def); @@ -1943,19 +1947,29 @@ merge(Compressor.prototype, { } if (drop_vars && node instanceof AST_Definitions && !(tt.parent() instanceof AST_ForIn)) { var def = node.definitions.filter(function(def){ - if (def.value) def.value = def.value.transform(tt); - var sym = def.name.definition(); - if (sym.id in in_use_ids) return true; - if (sym.orig[0] instanceof AST_SymbolCatch) { - def.value = def.value && def.value.drop_side_effect_free(compressor); - return true; - } var w = { name : def.name.name, file : def.name.start.file, line : def.name.start.line, col : def.name.start.col }; + if (def.value) def.value = def.value.transform(tt); + var sym = def.name.definition(); + if (sym.id in in_use_ids) { + if (def.name instanceof AST_SymbolVar) { + var var_defs = var_defs_by_id.get(sym.id); + if (var_defs.length > 1 && !def.value) { + compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", w); + var_defs.splice(var_defs.indexOf(def), 1); + return false; + } + } + return true; + } + if (sym.orig[0] instanceof AST_SymbolCatch) { + def.value = def.value && def.value.drop_side_effect_free(compressor); + return true; + } if (def.value && (def._unused_side_effects = def.value.drop_side_effect_free(compressor))) { compressor.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", w); return true; @@ -1963,6 +1977,28 @@ merge(Compressor.prototype, { compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", w); return false; }); + if (def.length == 1 + && def[0].value + && !def[0]._unused_side_effects + && def[0].name instanceof AST_SymbolVar) { + var var_defs = var_defs_by_id.get(def[0].name.definition().id); + if (var_defs.length > 1) { + compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", { + name : def[0].name.name, + file : def[0].name.start.file, + line : def[0].name.start.line, + col : def[0].name.start.col + }); + var_defs.splice(var_defs.indexOf(def[0]), 1); + return make_node(AST_SimpleStatement, node, { + body: make_node(AST_Assign, def[0], { + operator: "=", + left: make_node(AST_SymbolRef, def[0].name, def[0].name), + right: def[0].value + }) + }); + } + } // place uninitialized names at the start def = mergeSort(def, function(a, b){ if (!a.value && b.value) return -1; |