diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-04-07 17:06:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-07 17:06:01 +0800 |
commit | c2a1bceb773aab8875e0ffabf9f6b5199462f091 (patch) | |
tree | 07e991df35146f4a2f591140e3369c07b94d72fc /lib/compress.js | |
parent | e3c9c22c757112327b83f598b124690baf13ac52 (diff) | |
download | tracifyjs-c2a1bceb773aab8875e0ffabf9f6b5199462f091.tar.gz tracifyjs-c2a1bceb773aab8875e0ffabf9f6b5199462f091.zip |
fix `pure_getters` for chained property access (#1798)
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/lib/compress.js b/lib/compress.js index b001d35c..03b1fefb 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1165,13 +1165,16 @@ merge(Compressor.prototype, { // may_eq_null() // returns true if this node may evaluate to null or undefined (function(def) { - function is_strict(compressor) { - return /strict/.test(compressor.option("pure_getters")); + AST_Node.DEFMETHOD("may_eq_null", function(compressor) { + var pure_getters = compressor.option("pure_getters"); + return !pure_getters || this._eq_null(pure_getters); + }); + + function is_strict(pure_getters) { + return /strict/.test(pure_getters); } - def(AST_Node, function(compressor) { - return !is_strict(compressor); - }); + def(AST_Node, is_strict); def(AST_Null, return_true); def(AST_Undefined, return_true); def(AST_Constant, return_false); @@ -1182,36 +1185,36 @@ merge(Compressor.prototype, { def(AST_UnaryPrefix, function() { return this.operator == "void"; }); - def(AST_Binary, function(compressor) { + def(AST_Binary, function(pure_getters) { switch (this.operator) { case "&&": - return this.left.may_eq_null(compressor); + return this.left._eq_null(pure_getters); case "||": - return this.left.may_eq_null(compressor) - && this.right.may_eq_null(compressor); + return this.left._eq_null(pure_getters) + && this.right._eq_null(pure_getters); default: return false; } }) - def(AST_Assign, function(compressor) { + def(AST_Assign, function(pure_getters) { return this.operator == "=" - && this.right.may_eq_null(compressor); + && this.right._eq_null(pure_getters); }) - def(AST_Conditional, function(compressor) { - return this.consequent.may_eq_null(compressor) - || this.alternative.may_eq_null(compressor); + def(AST_Conditional, function(pure_getters) { + return this.consequent._eq_null(pure_getters) + || this.alternative._eq_null(pure_getters); }) - def(AST_Seq, function(compressor) { - return this.cdr.may_eq_null(compressor); + def(AST_Seq, function(pure_getters) { + return this.cdr._eq_null(pure_getters); }); - def(AST_SymbolRef, function(compressor) { + def(AST_SymbolRef, function(pure_getters) { if (this.is_undefined) return true; - if (!is_strict(compressor)) return false; + if (!is_strict(pure_getters)) return false; var fixed = this.fixed_value(); - return !fixed || fixed.may_eq_null(compressor); + return !fixed || fixed._eq_null(pure_getters); }); })(function(node, func) { - node.DEFMETHOD("may_eq_null", func); + node.DEFMETHOD("_eq_null", func); }); /* -----[ boolean/negation helpers ]----- */ @@ -1754,12 +1757,10 @@ merge(Compressor.prototype, { return any(this.elements, compressor); }); def(AST_Dot, function(compressor){ - if (!compressor.option("pure_getters")) return true; return this.expression.may_eq_null(compressor) || this.expression.has_side_effects(compressor); }); def(AST_Sub, function(compressor){ - if (!compressor.option("pure_getters")) return true; return this.expression.may_eq_null(compressor) || this.expression.has_side_effects(compressor) || this.property.has_side_effects(compressor); @@ -2327,12 +2328,10 @@ merge(Compressor.prototype, { return values && AST_Seq.from_array(values); }); def(AST_Dot, function(compressor, first_in_statement){ - if (!compressor.option("pure_getters")) return this; if (this.expression.may_eq_null(compressor)) return this; return this.expression.drop_side_effect_free(compressor, first_in_statement); }); def(AST_Sub, function(compressor, first_in_statement){ - if (!compressor.option("pure_getters")) return this; if (this.expression.may_eq_null(compressor)) return this; var expression = this.expression.drop_side_effect_free(compressor, first_in_statement); if (!expression) return this.property.drop_side_effect_free(compressor, first_in_statement); |