aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2019-04-29 08:55:46 +0800
committerGitHub <noreply@github.com>2019-04-29 08:55:46 +0800
commit413bbe0480d28d18833fbc3ebdb68fa74138d758 (patch)
tree81fd4736065aa6a8d319568a05959d3f314aa7f6
parent34075fc4c44eaf3369ffa76a1c7fa4f8281456bd (diff)
downloadtracifyjs-413bbe0480d28d18833fbc3ebdb68fa74138d758.tar.gz
tracifyjs-413bbe0480d28d18833fbc3ebdb68fa74138d758.zip
fix corner case in `evaluate` (#3388)
fixes #3387
-rw-r--r--lib/compress.js6
-rw-r--r--test/compress/evaluate.js49
-rw-r--r--test/compress/reduce_vars.js33
3 files changed, 55 insertions, 33 deletions
diff --git a/lib/compress.js b/lib/compress.js
index a7585a60..1a1b64f9 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2553,9 +2553,6 @@ merge(Compressor.prototype, {
return this.tail_node().is_string(compressor);
});
def(AST_String, return_true);
- def(AST_Sub, function(compressor) {
- return this.expression.is_string(compressor) && this.property instanceof AST_Number;
- });
def(AST_SymbolRef, function(compressor) {
var fixed = this.fixed_value();
if (!fixed) return false;
@@ -2994,7 +2991,8 @@ merge(Compressor.prototype, {
val = global_objs[exp.name];
} else {
val = exp._eval(compressor, cached, depth + 1);
- if (!val || val === exp || !HOP(val, key)) return this;
+ if (!val || val === exp) return this;
+ if (typeof val == "object" && !HOP(val, key)) return this;
if (typeof val == "function") switch (key) {
case "name":
return val.node.name ? val.node.name.name : "";
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index 89bfe2a8..0ac117fe 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -246,7 +246,7 @@ unsafe_constant: {
}
expect: {
console.log(
- true.a,
+ void 0,
false.a,
null.a,
(void 0).a
@@ -278,7 +278,7 @@ unsafe_object: {
o + 1,
2,
o.b + 1,
- 1..b + 1
+ NaN
);
}
expect_stdout: true
@@ -365,7 +365,7 @@ unsafe_object_repeated: {
o + 1,
2,
o.b + 1,
- 1..b + 1
+ NaN
);
}
expect_stdout: true
@@ -444,8 +444,8 @@ unsafe_integer_key: {
2,
2,
({0:1})[1] + 1,
- 1[1] + 1,
- 1["1"] + 1
+ NaN,
+ NaN
);
}
expect_stdout: true
@@ -500,8 +500,8 @@ unsafe_float_key: {
2,
2,
({2.72:1})[3.14] + 1,
- 1[3.14] + 1,
- 1["3.14"] + 1
+ NaN,
+ NaN
);
}
expect_stdout: true
@@ -635,12 +635,12 @@ unsafe_string_bad_index: {
}
expect: {
console.log(
- "1234".a + 1,
- "1234"["a"] + 1,
- "1234"[3.14] + 1
+ NaN,
+ NaN,
+ NaN
);
}
- expect_stdout: true
+ expect_stdout: "NaN NaN NaN"
}
prototype_function: {
@@ -1730,3 +1730,30 @@ unsafe_string_replace: {
}
expect_stdout: "PASS"
}
+
+issue_3387_1: {
+ options = {
+ evaluate: true,
+ }
+ input: {
+ console.log(1 + (2 + "3"[4]));
+ }
+ expect: {
+ console.log(1 + (2 + "3"[4]));
+ }
+ expect_stdout: "NaN"
+}
+
+issue_3387_2: {
+ options = {
+ evaluate: true,
+ unsafe: true,
+ }
+ input: {
+ console.log(1 + (2 + "3"[4]));
+ }
+ expect: {
+ console.log(NaN);
+ }
+ expect_stdout: "NaN"
+}
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index 7ad8ab94..1796ecbe 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -192,38 +192,35 @@ unsafe_evaluate: {
unused: true,
}
input: {
- function f0(){
- var a = {
- b:1
- };
+ function f0() {
+ var a = { b: 1 };
console.log(a.b + 3);
}
-
- function f1(){
+ function f1() {
var a = {
- b:{
- c:1
- },
- d:2
+ b: { c: 1 },
+ d: 2
};
console.log(a.b + 3, a.d + 4, a.b.c + 5, a.d.c + 6);
}
+ f0();
+ f1();
}
expect: {
- function f0(){
+ function f0() {
console.log(4);
}
-
- function f1(){
+ function f1() {
var a = {
- b:{
- c:1
- },
- d:2
+ b: { c: 1 },
+ d: 2
};
- console.log(a.b + 3, 6, 6, 2..c + 6);
+ console.log(a.b + 3, 6, 6, NaN);
}
+ f0();
+ f1();
}
+ expect_stdout: true
}
unsafe_evaluate_side_effect_free_1: {