diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-05-29 01:57:24 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-29 08:57:24 +0800 |
commit | 7fa1dea9d032c1ed282f84507e79cee57294945d (patch) | |
tree | dd54d245723192c1c98aebd8878a18c8f3edeac0 /lib/compress.js | |
parent | d40631fd44165936158633cc81c4d62cc2a6a5a9 (diff) | |
download | tracifyjs-7fa1dea9d032c1ed282f84507e79cee57294945d.tar.gz tracifyjs-7fa1dea9d032c1ed282f84507e79cee57294945d.zip |
fix corner cases in `collapse_vars` (#4978)
fixes #4977
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/lib/compress.js b/lib/compress.js index 838a912e..6419c363 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -652,6 +652,7 @@ merge(Compressor.prototype, { d.escaped.push(parent); if (depth > 1 && !(value && value.is_constant_expression(scope))) depth = 1; if (!d.escaped.depth || d.escaped.depth > depth) d.escaped.depth = depth; + if (d.scope !== scope) d.escaped.cross_scope = true; return; } else if (value_in_use(node, parent)) { mark_escaped(tw, d, scope, parent, parent, level + 1, depth); @@ -1973,6 +1974,7 @@ merge(Compressor.prototype, { var read_toplevel = false; var modify_toplevel = false; // Locate symbols which may execute code outside of scanning range + var well_defined = true; var lvalues = get_lvalues(candidate); var lhs_local = is_lhs_local(lhs); var rvalue = get_rvalue(candidate); @@ -2173,11 +2175,18 @@ merge(Compressor.prototype, { } if (node instanceof AST_ObjectIdentity) return symbol_in_lvalues(node, parent); if (node instanceof AST_PropAccess) { + if (side_effects) return true; var exp = node.expression; - return side_effects - || exp instanceof AST_SymbolRef && is_arguments(exp.definition()) - || !value_def && (in_try || !lhs_local) - && !node.optional && exp.may_throw_on_access(compressor); + if (exp instanceof AST_SymbolRef && is_arguments(exp.definition())) return true; + if (compressor.option("unsafe")) { + if (is_undeclared_ref(exp) && global_names[exp.name]) return false; + if (is_static_fn(exp)) return false; + } + if (!well_defined) return true; + if (value_def) return false; + if (!in_try && lhs_local) return false; + if (node.optional) return false; + return exp.may_throw_on_access(compressor); } if (node instanceof AST_Spread) return true; if (node instanceof AST_SymbolRef) { @@ -2537,8 +2546,7 @@ merge(Compressor.prototype, { } if (parent instanceof AST_PropAccess) { var exp = parent.expression; - if (exp === node) return find_stop_value(parent, level + 1); - return find_stop_expr(exp, find_stop_value, node, parent, level); + return exp === node ? find_stop_value(parent, level + 1) : node; } if (parent instanceof AST_Sequence) { return (parent.tail_node() === node ? find_stop_value : find_stop_unused)(parent, level + 1); @@ -2770,13 +2778,23 @@ merge(Compressor.prototype, { function get_lvalues(expr) { var lvalues = new Dictionary(); - if (expr instanceof AST_VarDef) lvalues.add(expr.name.name, lhs); + if (expr instanceof AST_VarDef) { + if (!expr.name.definition().fixed) well_defined = false; + lvalues.add(expr.name.name, lhs); + } var find_arguments = scope.uses_arguments && !compressor.has_directive("use strict"); var scan_toplevel = scope instanceof AST_Toplevel; var tw = new TreeWalker(function(node) { var value; if (node instanceof AST_SymbolRef) { - value = node.fixed_value() || node; + value = node.fixed_value(); + if (!value) { + value = node; + var def = node.definition(); + if (!def.undeclared && (def.assignments || !def.escaped || def.escaped.cross_scope)) { + well_defined = false; + } + } } else if (node instanceof AST_ObjectIdentity) { value = node; } @@ -2784,6 +2802,7 @@ merge(Compressor.prototype, { if (find_arguments && node instanceof AST_Sub) { scope.each_argname(function(argname) { if (!compressor.option("reduce_vars") || argname.definition().assignments) { + if (!argname.definition().fixed) well_defined = false; lvalues.add(argname.name, true); } }); @@ -4290,6 +4309,14 @@ merge(Compressor.prototype, { ], }); + function is_static_fn(node) { + if (!(node instanceof AST_Dot)) return false; + var expr = node.expression; + if (!is_undeclared_ref(expr)) return false; + var static_fn = static_fns[expr.name]; + return static_fn && (static_fn[node.property] || expr.name == "Math" && node.property == "random"); + } + // Accomodate when compress option evaluate=false // as well as the common constant expressions !0 and -1 (function(def) { @@ -4912,11 +4939,7 @@ merge(Compressor.prototype, { if (global_pure_fns[expr.name]) return true; if (this instanceof AST_New && global_pure_constructors[expr.name]) return true; } - if (expr instanceof AST_Dot && is_undeclared_ref(expr.expression)) { - var static_fn = static_fns[expr.expression.name]; - return static_fn && (static_fn[expr.property] - || expr.expression.name == "Math" && expr.property == "random"); - } + if (is_static_fn(expr)) return true; } return compressor.option("annotations") && this.pure || !compressor.pure_funcs(this); }); |