[Pkg-javascript-commits] [uglifyjs] 300/491: improve code reuse (#2542)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:46 UTC 2018


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit 172079a47f2f7bf09d2a5b4e4cf05691a1206358
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Fri Dec 1 03:40:46 2017 +0800

    improve code reuse (#2542)
---
 lib/ast.js        | 18 ------------------
 lib/compress.js   | 40 ++++++++++++++++++++++++++++++----------
 lib/propmangle.js |  2 +-
 lib/scope.js      |  7 ++++++-
 4 files changed, 37 insertions(+), 30 deletions(-)

diff --git a/lib/ast.js b/lib/ast.js
index a2aa2b4..997486c 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -898,24 +898,6 @@ TreeWalker.prototype = {
             }
         }
     },
-    in_boolean_context: function() {
-        var stack = this.stack;
-        var i = stack.length, self = stack[--i];
-        while (i > 0) {
-            var p = stack[--i];
-            if ((p instanceof AST_If           && p.condition === self) ||
-                (p instanceof AST_Conditional  && p.condition === self) ||
-                (p instanceof AST_DWLoop       && p.condition === self) ||
-                (p instanceof AST_For          && p.condition === self) ||
-                (p instanceof AST_UnaryPrefix  && p.operator == "!" && p.expression === self))
-            {
-                return true;
-            }
-            if (!(p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")))
-                return false;
-            self = p;
-        }
-    },
     loopcontrol_target: function(node) {
         var stack = this.stack;
         if (node.label) for (var i = stack.length; --i >= 0;) {
diff --git a/lib/compress.js b/lib/compress.js
index 6bdd159..206c77f 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -145,6 +145,26 @@ merge(Compressor.prototype, {
                 return true;
         return false;
     },
+    in_boolean_context: function() {
+        if (!this.option("booleans")) return false;
+        var self = this.self();
+        for (var i = 0, p; p = this.parent(i); i++) {
+            if (p instanceof AST_SimpleStatement
+                || p instanceof AST_Conditional && p.condition === self
+                || p instanceof AST_DWLoop && p.condition === self
+                || p instanceof AST_For && p.condition === self
+                || p instanceof AST_If && p.condition === self
+                || p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self) {
+                return true;
+            }
+            if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")
+                || p.tail_node() === self) {
+                self = p;
+            } else {
+                return false;
+            }
+        }
+    },
     compress: function(node) {
         if (this.option("expression")) {
             node.process_expression(true);
@@ -1547,7 +1567,7 @@ merge(Compressor.prototype, {
                 || this.alternative._dot_throw(compressor);
         })
         def(AST_Sequence, function(compressor) {
-            return this.expressions[this.expressions.length - 1]._dot_throw(compressor);
+            return this.tail_node()._dot_throw(compressor);
         });
         def(AST_SymbolRef, function(compressor) {
             if (this.is_undefined) return true;
@@ -1584,7 +1604,7 @@ merge(Compressor.prototype, {
             return this.operator == "=" && this.right.is_boolean();
         });
         def(AST_Sequence, function(){
-            return this.expressions[this.expressions.length - 1].is_boolean();
+            return this.tail_node().is_boolean();
         });
         def(AST_True, return_true);
         def(AST_False, return_true);
@@ -1611,7 +1631,7 @@ merge(Compressor.prototype, {
                 || this.operator == "=" && this.right.is_number(compressor);
         });
         def(AST_Sequence, function(compressor){
-            return this.expressions[this.expressions.length - 1].is_number(compressor);
+            return this.tail_node().is_number(compressor);
         });
         def(AST_Conditional, function(compressor){
             return this.consequent.is_number(compressor) && this.alternative.is_number(compressor);
@@ -1635,7 +1655,7 @@ merge(Compressor.prototype, {
             return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
         });
         def(AST_Sequence, function(compressor){
-            return this.expressions[this.expressions.length - 1].is_string(compressor);
+            return this.tail_node().is_string(compressor);
         });
         def(AST_Conditional, function(compressor){
             return this.consequent.is_string(compressor) && this.alternative.is_string(compressor);
@@ -2974,7 +2994,7 @@ merge(Compressor.prototype, {
             return make_sequence(this, [ expression, property ]);
         });
         def(AST_Sequence, function(compressor){
-            var last = this.expressions[this.expressions.length - 1];
+            var last = this.tail_node();
             var expr = last.drop_side_effect_free(compressor);
             if (expr === last) return this;
             var expressions = this.expressions.slice(0, -1);
@@ -3826,7 +3846,7 @@ merge(Compressor.prototype, {
                 return make_node(AST_Undefined, self).optimize(compressor);
             }
         }
-        if (compressor.option("booleans") && compressor.in_boolean_context()) {
+        if (compressor.in_boolean_context()) {
             switch (self.operator) {
               case "!":
                 if (e instanceof AST_UnaryPrefix && e.operator == "!") {
@@ -3979,7 +3999,7 @@ merge(Compressor.prototype, {
             }
             break;
         }
-        if (compressor.option("booleans") && self.operator == "+" && compressor.in_boolean_context()) {
+        if (self.operator == "+" && compressor.in_boolean_context()) {
             var ll = self.left.evaluate(compressor);
             var rr = self.right.evaluate(compressor);
             if (ll && typeof ll == "string") {
@@ -4044,7 +4064,7 @@ merge(Compressor.prototype, {
                     compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start);
                     return maintain_this_binding(compressor.parent(), compressor.self(), self.right).optimize(compressor);
                 }
-                if (compressor.option("booleans") && compressor.in_boolean_context()) {
+                if (compressor.in_boolean_context()) {
                     var rr = self.right.evaluate(compressor);
                     if (!rr) {
                         compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start);
@@ -4067,7 +4087,7 @@ merge(Compressor.prototype, {
                     compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start);
                     return maintain_this_binding(compressor.parent(), compressor.self(), self.left).optimize(compressor);
                 }
-                if (compressor.option("booleans") && compressor.in_boolean_context()) {
+                if (compressor.in_boolean_context()) {
                     var rr = self.right.evaluate(compressor);
                     if (!rr) {
                         compressor.warn("Dropping side-effect-free || in boolean context [{file}:{line},{col}]", self.start);
@@ -4841,7 +4861,7 @@ merge(Compressor.prototype, {
     });
 
     function literals_in_boolean_context(self, compressor) {
-        if (compressor.option("booleans") && compressor.in_boolean_context()) {
+        if (compressor.in_boolean_context()) {
             return best_of(compressor, self, make_sequence(self, [
                 self,
                 make_node(AST_True, self)
diff --git a/lib/propmangle.js b/lib/propmangle.js
index 36a67e8..c2f27c4 100644
--- a/lib/propmangle.js
+++ b/lib/propmangle.js
@@ -84,7 +84,7 @@ function reserve_quoted_keys(ast, reserved) {
 function addStrings(node, add) {
     node.walk(new TreeWalker(function(node) {
         if (node instanceof AST_Sequence) {
-            addStrings(node.expressions[node.expressions.length - 1], add);
+            addStrings(node.tail_node(), add);
         } else if (node instanceof AST_String) {
             add(node.value);
         } else if (node instanceof AST_Conditional) {
diff --git a/lib/scope.js b/lib/scope.js
index f3010e7..bbfa037 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -510,6 +510,11 @@ AST_Toplevel.DEFMETHOD("expand_names", function(options) {
     }
 });
 
+AST_Node.DEFMETHOD("tail_node", return_this);
+AST_Sequence.DEFMETHOD("tail_node", function() {
+    return this.expressions[this.expressions.length - 1];
+});
+
 AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
     options = this._default_mangler_options(options);
     try {
@@ -538,7 +543,7 @@ AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
             skip_string(node.consequent);
             skip_string(node.alternative);
         } else if (node instanceof AST_Sequence) {
-            skip_string(node.expressions[node.expressions.length - 1]);
+            skip_string(node.tail_node());
         }
     }
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list