[Pkg-javascript-commits] [uglifyjs] 92/190: Mark vars with /** @const */ pragma as consts so they can be eliminated.

Antonio Terceiro terceiro at moszumanska.debian.org
Sun Aug 7 23:17:16 UTC 2016


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

terceiro pushed a commit to annotated tag upstream/2.7.0
in repository uglifyjs.

commit 8b71c6559b0e1773bb3455c68701ff512fc18277
Author: Samuel Reed <samuel.trace.reed at gmail.com>
Date:   Tue Jan 19 13:12:32 2016 -0600

    Mark vars with /** @const */ pragma as consts so they can be eliminated.
    
    Fixes older browser support for consts and allows more flexibility
    in dead code removal.
---
 lib/scope.js               | 12 +++++++++++-
 test/compress/dead-code.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/lib/scope.js b/lib/scope.js
index 5e93020..22fb150 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -154,7 +154,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
         else if (node instanceof AST_SymbolVar
                  || node instanceof AST_SymbolConst) {
             var def = defun.def_variable(node);
-            def.constant = node instanceof AST_SymbolConst;
+            def.constant = node instanceof AST_SymbolConst || node.has_const_pragma();
             def.init = tw.parent().value;
         }
         else if (node instanceof AST_SymbolCatch) {
@@ -357,6 +357,16 @@ AST_Symbol.DEFMETHOD("global", function(){
     return this.definition().global;
 });
 
+AST_Symbol.DEFMETHOD("has_const_pragma", function() {
+    var token = this.scope.body[0] && this.scope.body[0].start;
+    var comments = token && token.comments_before;
+    if (comments && comments.length > 0) {
+        var last = comments[comments.length - 1];
+        return /@const/.test(last.value);
+    }
+    return false;
+})
+
 AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
     return defaults(options, {
         except      : [],
diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js
index 5009ae1..f79b04d 100644
--- a/test/compress/dead-code.js
+++ b/test/compress/dead-code.js
@@ -87,3 +87,49 @@ dead_code_constant_boolean_should_warn_more: {
         var moo;
     }
 }
+
+dead_code_const_declaration: {
+    options = {
+        dead_code    : true,
+        loops        : true,
+        booleans     : true,
+        conditionals : true,
+        evaluate     : true
+    };
+    input: {
+        const CONST_FOO = false;
+        if (CONST_FOO) {
+            console.log("unreachable");
+            var moo;
+            function bar() {}
+        }
+    }
+    expect: {
+        const CONST_FOO = !1;
+        var moo;
+        function bar() {}
+    }
+}
+
+dead_code_const_annotation: {
+    options = {
+        dead_code    : true,
+        loops        : true,
+        booleans     : true,
+        conditionals : true,
+        evaluate     : true
+    };
+    input: {
+        /** @const*/ var CONST_FOO_ANN = false;
+        if (CONST_FOO_ANN) {
+            console.log("unreachable");
+            var moo;
+            function bar() {}
+        }
+    }
+    expect: {
+        var CONST_FOO_ANN = !1;
+        var moo;
+        function bar() {}
+    }
+}

-- 
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