[Pkg-javascript-commits] [uglifyjs] 34/228: introduce `unsafe_proto` - `Array.prototype.slice` => `[].slice`

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:14 UTC 2017


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

js pushed a commit to branch master
in repository uglifyjs.

commit ec64acd2c8d8573abd5b77f8f8946767444841bb
Author: alexlamsl <alexlamsl at gmail.com>
Date:   Sat Feb 18 19:34:54 2017 +0800

    introduce `unsafe_proto`
    - `Array.prototype.slice` => `[].slice`
    
    closes #1491
---
 README.md                   |  3 +++
 lib/compress.js             | 23 +++++++++++++++++++++++
 test/compress/properties.js | 16 ++++++++++++++++
 3 files changed, 42 insertions(+)

diff --git a/README.md b/README.md
index 1d1f2fc..06ffa0e 100644
--- a/README.md
+++ b/README.md
@@ -346,6 +346,9 @@ to set `true`; it's effectively a shortcut for `foo=true`).
   comparison are switching. Compression only works if both `comparisons` and
   `unsafe_comps` are both set to true.
 
+- `unsafe_proto` (default: false) -- optimize expressions like
+  `Array.prototype.slice.call(a)` into `[].slice.call(a)`
+
 - `conditionals` -- apply optimizations for `if`-s and conditional
   expressions
 
diff --git a/lib/compress.js b/lib/compress.js
index cb99a17..237af72 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -54,6 +54,7 @@ function Compressor(options, false_by_default) {
         drop_debugger : !false_by_default,
         unsafe        : false,
         unsafe_comps  : false,
+        unsafe_proto  : false,
         conditionals  : !false_by_default,
         comparisons   : !false_by_default,
         evaluate      : !false_by_default,
@@ -3148,6 +3149,28 @@ merge(Compressor.prototype, {
                 })
             }).optimize(compressor);
         }
+        if (compressor.option("unsafe_proto")
+            && self.expression instanceof AST_Dot
+            && self.expression.property == "prototype") {
+            var exp = self.expression.expression;
+            if (exp instanceof AST_SymbolRef && exp.undeclared()) switch (exp.name) {
+              case "Array":
+                self.expression = make_node(AST_Array, self.expression, {
+                    elements: []
+                });
+                break;
+              case "Object":
+                self.expression = make_node(AST_Object, self.expression, {
+                    properties: []
+                });
+                break;
+              case "String":
+                self.expression = make_node(AST_String, self.expression, {
+                    value: ""
+                });
+                break;
+            }
+        }
         return self.evaluate(compressor)[0];
     });
 
diff --git a/test/compress/properties.js b/test/compress/properties.js
index 7ad54eb..29bdfe2 100644
--- a/test/compress/properties.js
+++ b/test/compress/properties.js
@@ -539,3 +539,19 @@ first_256_hex_chars_as_properties: {
         };
     }
 }
+
+native_prototype: {
+    options = {
+        unsafe_proto: true,
+    }
+    input: {
+        Array.prototype.splice.apply(a, [1, 2, b, c]);
+        Object.prototype.hasOwnProperty.call(d, "foo");
+        String.prototype.indexOf.call(e, "bar");
+    }
+    expect: {
+        [].splice.apply(a, [1, 2, b, c]);
+        ({}).hasOwnProperty.call(d, "foo");
+        "".indexOf.call(e, "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