diff options
Diffstat (limited to 'node_modules/uglify-js/lib/compress.js')
-rw-r--r-- | node_modules/uglify-js/lib/compress.js | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/node_modules/uglify-js/lib/compress.js b/node_modules/uglify-js/lib/compress.js index 7a16ba86b..d4a72d749 100644 --- a/node_modules/uglify-js/lib/compress.js +++ b/node_modules/uglify-js/lib/compress.js @@ -285,6 +285,10 @@ merge(Compressor.prototype, { AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) { var reduce_vars = rescan && compressor.option("reduce_vars"); + // Stack of look-up tables to keep track of whether a `SymbolDef` has been + // properly assigned before use: + // - `push()` & `pop()` when visiting conditional branches + // - backup & restore via `save_ids` when visiting out-of-order sections var safe_ids = Object.create(null); var suppressor = new TreeWalker(function(node) { if (!(node instanceof AST_Symbol)) return; @@ -391,10 +395,9 @@ merge(Compressor.prototype, { return true; } if (node instanceof AST_Accessor) { - var save_ids = safe_ids; - safe_ids = Object.create(null); + push(); descend(); - safe_ids = save_ids; + pop(); return true; } if (node instanceof AST_Binary @@ -561,6 +564,7 @@ merge(Compressor.prototype, { }); function is_lhs_read_only(lhs) { + if (lhs instanceof AST_This) return true; if (lhs instanceof AST_SymbolRef) return lhs.definition().orig[0] instanceof AST_SymbolLambda; if (lhs instanceof AST_PropAccess) { lhs = lhs.expression; @@ -693,7 +697,7 @@ merge(Compressor.prototype, { return node instanceof AST_SymbolRef && node.definition().undeclared; } - var global_names = makePredicate("Array Boolean console Error Function Math Number RegExp Object String"); + var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError"); AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) { return !this.definition().undeclared || compressor.option("unsafe") && global_names(this.name); @@ -745,7 +749,7 @@ merge(Compressor.prototype, { while (candidates.length > 0) { var candidate = candidates.pop(); var lhs = get_lhs(candidate); - if (!lhs || is_lhs_read_only(lhs)) continue; + if (!lhs || is_lhs_read_only(lhs) || lhs.has_side_effects(compressor)) continue; // Locate symbols which may execute code outside of scanning range var lvalues = get_lvalues(candidate); if (lhs instanceof AST_SymbolRef) lvalues[lhs.name] = false; @@ -841,13 +845,15 @@ merge(Compressor.prototype, { && !fn.uses_eval && (iife = compressor.parent()) instanceof AST_Call && iife.expression === fn) { + var fn_strict = compressor.has_directive("use strict"); + if (fn_strict && fn.body.indexOf(fn_strict) < 0) fn_strict = false; var names = Object.create(null); for (var i = fn.argnames.length; --i >= 0;) { var sym = fn.argnames[i]; if (sym.name in names) continue; names[sym.name] = true; var arg = iife.args[i]; - if (!arg) arg = make_node(AST_Undefined, sym); + if (!arg) arg = make_node(AST_Undefined, sym).transform(compressor); else { var tw = new TreeWalker(function(node) { if (!arg) return true; @@ -858,7 +864,7 @@ merge(Compressor.prototype, { } arg = null; } - if (node instanceof AST_This && !tw.find_parent(AST_Scope)) { + if (node instanceof AST_This && (fn_strict || !tw.find_parent(AST_Scope))) { arg = null; return true; } @@ -1980,16 +1986,27 @@ merge(Compressor.prototype, { if (!compressor.option("side_effects")) return false; if (this.pure !== undefined) return this.pure; var pure = false; - var comments, last_comment; + var comments, pure_comment; if (this.start && (comments = this.start.comments_before) && comments.length - && /[@#]__PURE__/.test((last_comment = comments[comments.length - 1]).value)) { - pure = last_comment; + && (pure_comment = find_if(function (comment) { + return /[@#]__PURE__/.test(comment.value); + }, comments))) { + pure = pure_comment; } return this.pure = pure; }); + var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError"); + AST_Call.DEFMETHOD("is_expr_pure", function(compressor) { + if (compressor.option("unsafe")) { + var expr = this.expression; + if (is_undeclared_ref(expr) && global_pure_fns(expr.name)) return true; + } + return this.has_pure_annotation(compressor) || !compressor.pure_funcs(this); + }); + // determine if expression has side effects (function(def){ def(AST_Node, return_true); @@ -1999,7 +2016,7 @@ merge(Compressor.prototype, { def(AST_This, return_false); def(AST_Call, function(compressor){ - if (!this.has_pure_annotation(compressor) && compressor.pure_funcs(this)) return true; + if (!this.is_expr_pure(compressor)) return true; for (var i = this.args.length; --i >= 0;) { if (this.args[i].has_side_effects(compressor)) return true; @@ -2423,17 +2440,18 @@ merge(Compressor.prototype, { // We fix it at this stage by moving the `var` outside the `for`. if (node instanceof AST_For) { descend(node, this); + var block; if (node.init instanceof AST_BlockStatement) { - var block = node.init; + block = node.init; node.init = block.body.pop(); block.body.push(node); - return in_list ? MAP.splice(block.body) : block; - } else if (node.init instanceof AST_SimpleStatement) { + } + if (node.init instanceof AST_SimpleStatement) { node.init = node.init.body; } else if (is_empty(node.init)) { node.init = null; } - return node; + return !block ? node : in_list ? MAP.splice(block.body) : block; } if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) { descend(node, this); @@ -2617,7 +2635,7 @@ merge(Compressor.prototype, { def(AST_Constant, return_null); def(AST_This, return_null); def(AST_Call, function(compressor, first_in_statement){ - if (!this.has_pure_annotation(compressor) && compressor.pure_funcs(this)) { + if (!this.is_expr_pure(compressor)) { if (this.expression instanceof AST_Function && (!this.expression.name || !this.expression.name.definition().references.length)) { var node = this.clone(); @@ -3112,6 +3130,7 @@ merge(Compressor.prototype, { })); if (reduce_vars) name.definition().fixed = false; } + remove(def.name.definition().orig, def.name); return a; }, []); if (assignments.length == 0) return null; @@ -3483,7 +3502,7 @@ merge(Compressor.prototype, { && (left.operator == "++" || left.operator == "--")) { left = left.expression; } else left = null; - if (!left || is_lhs_read_only(left)) { + if (!left || is_lhs_read_only(left) || left.has_side_effects(compressor)) { expressions[++i] = cdr; continue; } |