diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-10-21 04:08:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-21 04:08:26 +0800 |
commit | c927cea6322788b654128f9bbbf3a06441b964c2 (patch) | |
tree | 774308d30281391b20b17f93aed7db8ba4523c6e /lib/compress.js | |
parent | 9f4b98f8e461dabf3a3c97c247d9c72f981aa28d (diff) | |
download | tracifyjs-c927cea6322788b654128f9bbbf3a06441b964c2.tar.gz tracifyjs-c927cea6322788b654128f9bbbf3a06441b964c2.zip |
`unsafe` fix-ups for #2351 (#2379)
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 55 |
1 files changed, 30 insertions, 25 deletions
diff --git a/lib/compress.js b/lib/compress.js index 374456ac..1f58b390 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -151,7 +151,7 @@ merge(Compressor.prototype, { var last_count = 1 / 0; for (var pass = 0; pass < passes; pass++) { if (pass > 0 || this.option("reduce_vars")) - node.reset_opt_flags(this, true); + node.reset_opt_flags(this); node = node.transform(this); if (passes > 1) { var count = 0; @@ -283,8 +283,9 @@ merge(Compressor.prototype, { self.transform(tt); }); - AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) { - var reduce_vars = rescan && compressor.option("reduce_vars"); + AST_Node.DEFMETHOD("reset_opt_flags", function(compressor) { + var reduce_vars = compressor.option("reduce_vars"); + var unused = compressor.option("unused"); // Stack of look-up tables to keep track of whether a `SymbolDef` has been // properly assigned before use: // - `push()` & `pop()` when visiting conditional branches @@ -308,16 +309,31 @@ merge(Compressor.prototype, { if (node instanceof AST_SymbolRef) { var d = node.definition(); d.references.push(node); - if (d.fixed === undefined || !safe_to_read(d) - || is_modified(node, 0, is_immutable(node))) { + if (d.fixed === undefined || !safe_to_read(d) || d.single_use == "m") { d.fixed = false; } else { - var parent = tw.parent(); - if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right - || parent instanceof AST_Call && node !== parent.expression - || parent instanceof AST_Return && node === parent.value && node.scope !== d.scope - || parent instanceof AST_VarDef && node === parent.value) { - d.escaped = true; + var value = node.fixed_value(); + if (unused) { + d.single_use = value + && d.references.length == 1 + && loop_ids[d.id] === in_loop + && d.scope === node.scope + && value.is_constant_expression(); + } + if (is_modified(node, 0, is_immutable(value))) { + if (d.single_use) { + d.single_use = "m"; + } else { + d.fixed = false; + } + } else { + var parent = tw.parent(); + if (parent instanceof AST_Assign && parent.operator == "=" && node === parent.right + || parent instanceof AST_Call && node !== parent.expression + || parent instanceof AST_Return && node === parent.value && node.scope !== d.scope + || parent instanceof AST_VarDef && node === parent.value) { + d.escaped = true; + } } } } @@ -550,20 +566,8 @@ merge(Compressor.prototype, { def.single_use = undefined; } - function is_immutable(node) { - var value = node.fixed_value(); - if (!value) return false; - if (value.is_constant()) return true; - if (compressor.option("unused")) { - var d = node.definition(); - if (d.single_use === undefined) { - d.single_use = loop_ids[d.id] === in_loop - && d.scope === node.scope - && value.is_constant_expression(); - } - if (d.references.length == 1 && d.single_use) return true; - } - return value instanceof AST_Lambda; + function is_immutable(value) { + return value && (value.is_constant() || value instanceof AST_Lambda); } function is_modified(node, level, immutable) { @@ -4107,6 +4111,7 @@ merge(Compressor.prototype, { d.fixed = fixed = make_node(AST_Function, fixed, fixed); } if (compressor.option("unused") + && fixed && d.references.length == 1 && (d.single_use || fixed instanceof AST_Function && !(d.scope.uses_arguments && d.orig[0] instanceof AST_SymbolFunarg) |