[Pkg-javascript-commits] [uglifyjs] 247/491: fix `unsafe` `reduce_vars` on arrays & objects (#2397)
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 74ae16f9f88600c90438e85829a615b2377f6740
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date: Tue Oct 24 22:10:36 2017 +0800
fix `unsafe` `reduce_vars` on arrays & objects (#2397)
---
lib/compress.js | 10 +++++++---
test/compress/reduce_vars.js | 27 ++++++++++++++++++++++++++-
2 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/lib/compress.js b/lib/compress.js
index 25718a5..670a3b0 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -567,18 +567,18 @@ merge(Compressor.prototype, {
function read_property(obj, key) {
if (key instanceof AST_Constant) key = key.getValue();
if (key instanceof AST_Node) return null;
+ var value;
if (obj instanceof AST_Array) {
- return obj.elements[key];
+ value = obj.elements[key];
} else if (obj instanceof AST_Object) {
var props = obj.properties;
- var value;
for (var i = props.length; --i >= 0;) {
var prop = props[i];
if (!(prop instanceof AST_ObjectKeyVal)) return;
if (!value && props[i].key === key) value = props[i].value;
}
- return value;
}
+ return value instanceof AST_SymbolRef ? value.fixed_value() : value;
}
function is_modified(node, value, level, immutable) {
@@ -589,6 +589,8 @@ merge(Compressor.prototype, {
&& parent.expression === node
&& (!(value instanceof AST_Function) || value.contains_this())) {
return true;
+ } else if (parent instanceof AST_Array || parent instanceof AST_Object) {
+ return is_modified(parent, parent, level + 1);
} else if (parent instanceof AST_PropAccess && parent.expression === node) {
return !immutable && is_modified(parent, read_property(value, parent.property), level + 1);
}
@@ -602,6 +604,8 @@ merge(Compressor.prototype, {
|| parent instanceof AST_Return && node === parent.value && node.scope !== d.scope
|| parent instanceof AST_VarDef && node === parent.value) {
d.escaped = true;
+ } else if (parent instanceof AST_Array || parent instanceof AST_Object) {
+ mark_escaped(d, parent, parent, level + 1);
} else if (parent instanceof AST_PropAccess && node === parent.expression) {
mark_escaped(d, parent, read_property(value, parent.property), level + 1);
}
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index f516b9d..ac4fa40 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -211,7 +211,7 @@ unsafe_evaluate: {
}
}
-unsafe_evaluate_side_effect_free: {
+unsafe_evaluate_side_effect_free_1: {
options = {
evaluate: true,
reduce_vars: true,
@@ -221,10 +221,31 @@ unsafe_evaluate_side_effect_free: {
input: {
console.log(function(){ var o={p:1}; console.log(o.p); return o.p; }());
console.log(function(){ var o={p:2}; console.log(o.p); return o; }());
+ console.log(function(){ var o={p:3}; console.log([o][0].p); return o.p; }());
}
expect: {
console.log(function(){ console.log(1); return 1; }());
console.log(function(){ var o={p:2}; console.log(2); return o; }());
+ console.log(function(){ console.log(3); return 3; }());
+ }
+ expect_stdout: true
+}
+
+unsafe_evaluate_side_effect_free_2: {
+ options = {
+ collapse_vars: true,
+ evaluate: true,
+ passes: 2,
+ pure_getters: "strict",
+ reduce_vars: true,
+ unsafe: true,
+ unused: true,
+ }
+ input: {
+ console.log(function(){ var o={p:1},a=[o]; console.log(a[0].p); return o.p; }());
+ }
+ expect: {
+ console.log(function(){ console.log(1); return 1; }());
}
expect_stdout: true
}
@@ -239,10 +260,12 @@ unsafe_evaluate_escaped: {
input: {
console.log(function(){ var o={p:1}; console.log(o, o.p); return o.p; }());
console.log(function(){ var o={p:2}; console.log(o.p, o); return o.p; }());
+ console.log(function(){ var o={p:3},a=[o]; console.log(a[0].p++); return o.p; }());
}
expect: {
console.log(function(){ var o={p:1}; console.log(o, o.p); return o.p; }());
console.log(function(){ var o={p:2}; console.log(o.p, o); return o.p; }());
+ console.log(function(){ var o={p:3},a=[o]; console.log(a[0].p++); return o.p; }());
}
expect_stdout: true
}
@@ -262,6 +285,7 @@ unsafe_evaluate_modified: {
console.log(function(){ var o={p:5}; o.p = -9; console.log(o.p); return o.p; }());
function inc() { this.p++; }
console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }());
+ console.log(function(){ var o={p:7}; console.log([o][0].p++); return o.p; }());
}
expect: {
console.log(function(){ var o={p:1}; o.p++; console.log(o.p); return o.p; }());
@@ -271,6 +295,7 @@ unsafe_evaluate_modified: {
console.log(function(){ var o={p:5}; o.p = -9; console.log(o.p); return o.p; }());
function inc() { this.p++; }
console.log(function(){ var o={p:6}; inc.call(o); console.log(o.p); return o.p; }());
+ console.log(function(){ var o={p:7}; console.log([o][0].p++); return o.p; }());
}
expect_stdout: true
}
--
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