[Pkg-javascript-commits] [uglifyjs] 251/491: fix `reduce_vars` on `AST_Array.length` (#2404)
Jonas Smedegaard
dr at jones.dk
Wed Feb 14 19:51:41 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 9b0f86f5a136f57dece826c3ec9b632a0d0cb53a
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date: Fri Oct 27 02:33:37 2017 +0800
fix `reduce_vars` on `AST_Array.length` (#2404)
---
lib/compress.js | 4 +++-
test/compress/arrays.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 59 insertions(+), 2 deletions(-)
diff --git a/lib/compress.js b/lib/compress.js
index 073399b..a2dd243 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -572,7 +572,9 @@ merge(Compressor.prototype, {
if (key instanceof AST_Node) return null;
var value;
if (obj instanceof AST_Array) {
- value = obj.elements[key];
+ var elements = obj.elements;
+ if (key == "length") return make_node_from_constant(elements.length, obj);
+ if (typeof key == "number" && key in elements) value = elements[key];
} else if (obj instanceof AST_Object) {
var props = obj.properties;
for (var i = props.length; --i >= 0;) {
diff --git a/test/compress/arrays.js b/test/compress/arrays.js
index 539dfb0..2740090 100644
--- a/test/compress/arrays.js
+++ b/test/compress/arrays.js
@@ -131,6 +131,7 @@ for_loop: {
evaluate: true,
reduce_vars: true,
unsafe: true,
+ unused: true,
};
input: {
function f0() {
@@ -166,7 +167,7 @@ for_loop: {
function f1() {
var a = [1, 2, 3];
var b = 0;
- for (var i = 0, len = a.length; i < len; i++)
+ for (var i = 0; i < 3; i++)
b += a[i];
return b;
}
@@ -180,3 +181,57 @@ for_loop: {
}
expect_stdout: "6 6 4"
}
+
+index: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ unsafe: true,
+ unused: true,
+ }
+ input: {
+ var a = [ 1, 2 ];
+ console.log(a[0], a[1]);
+ }
+ expect: {
+ console.log(1, 2);
+ }
+ expect_stdout: "1 2"
+}
+
+length: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ unsafe: true,
+ unused: true,
+ }
+ input: {
+ var a = [ 1, 2 ];
+ console.log(a.length);
+ }
+ expect: {
+ console.log(2);
+ }
+ expect_stdout: "2"
+}
+
+index_length: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ unsafe: true,
+ unused: true,
+ }
+ input: {
+ var a = [ 1, 2 ];
+ console.log(a[0], a.length);
+ }
+ expect: {
+ console.log(1, 2);
+ }
+ expect_stdout: "1 2"
+}
--
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