aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-02-28 02:25:44 +0800
committerGitHub <noreply@github.com>2017-02-28 02:25:44 +0800
commit858e6c78a44f236e6e4d460a2ac187eef59823c8 (patch)
treea8e8ade5e968ae8a7791de37409ef8c001d7b41f /test
parent0b0296eb2aebbb6f3df72d4e4ef22d447fe396ec (diff)
downloadtracifyjs-858e6c78a44f236e6e4d460a2ac187eef59823c8.tar.gz
tracifyjs-858e6c78a44f236e6e4d460a2ac187eef59823c8.zip
warn & drop `#__PURE__` iff IIFE is dropped (#1511)
- consolidate `side-effects` optimisations - improve string `+` optimisation - enhance literal & `conditionals` optimisations
Diffstat (limited to 'test')
-rw-r--r--test/compress/conditionals.js40
-rw-r--r--test/compress/dead-code.js3
-rw-r--r--test/compress/evaluate.js42
-rw-r--r--test/compress/issue-1261.js58
-rw-r--r--test/compress/issue-1275.js2
-rw-r--r--test/compress/typeof.js5
-rw-r--r--test/mocha/minify.js11
7 files changed, 136 insertions, 25 deletions
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js
index d88c5b90..074d2a65 100644
--- a/test/compress/conditionals.js
+++ b/test/compress/conditionals.js
@@ -50,7 +50,8 @@ ifs_3_should_warn: {
conditionals : true,
dead_code : true,
evaluate : true,
- booleans : true
+ booleans : true,
+ side_effects : true,
};
input: {
var x, y;
@@ -135,16 +136,28 @@ ifs_6: {
comparisons: true
};
input: {
- var x;
+ var x, y;
if (!foo && !bar && !baz && !boo) {
x = 10;
} else {
x = 20;
}
+ if (y) {
+ x[foo] = 10;
+ } else {
+ x[foo] = 20;
+ }
+ if (foo) {
+ x[bar] = 10;
+ } else {
+ x[bar] = 20;
+ }
}
expect: {
- var x;
+ var x, y;
x = foo || bar || baz || boo ? 20 : 10;
+ x[foo] = y ? 10 : 20;
+ foo ? x[bar] = 10 : x[bar] = 20;
}
}
@@ -159,10 +172,16 @@ cond_1: {
} else {
do_something(y);
}
+ if (some_condition()) {
+ side_effects(x);
+ } else {
+ side_effects(y);
+ }
}
expect: {
var do_something;
do_something(some_condition() ? x : y);
+ some_condition() ? side_effects(x) : side_effects(y);
}
}
@@ -213,10 +232,16 @@ cond_4: {
} else {
do_something();
}
+ if (some_condition()) {
+ side_effects();
+ } else {
+ side_effects();
+ }
}
expect: {
var do_something;
some_condition(), do_something();
+ some_condition(), side_effects();
}
}
@@ -250,7 +275,8 @@ cond_5: {
cond_7: {
options = {
conditionals: true,
- evaluate : true
+ evaluate : true,
+ side_effects: true,
};
input: {
var x, y, z, a, b;
@@ -714,6 +740,7 @@ issue_1154: {
conditionals: true,
evaluate : true,
booleans : true,
+ side_effects: true,
};
input: {
function f1(x) { return x ? -1 : -1; }
@@ -742,7 +769,7 @@ issue_1154: {
function g2() { return g(), 2; }
function g3() { return g(), -4; }
function g4() { return g(), !1; }
- function g5() { return g(), void 0; }
+ function g5() { return void g(); }
function g6() { return g(), "number"; }
}
}
@@ -750,7 +777,8 @@ issue_1154: {
no_evaluate: {
options = {
conditionals: true,
- evaluate : false
+ evaluate : false,
+ side_effects: true,
}
input: {
function f(b) {
diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js
index 2596e80e..cd96d02d 100644
--- a/test/compress/dead-code.js
+++ b/test/compress/dead-code.js
@@ -64,7 +64,8 @@ dead_code_constant_boolean_should_warn_more: {
loops : true,
booleans : true,
conditionals : true,
- evaluate : true
+ evaluate : true,
+ side_effects : true,
};
input: {
while (!((foo && bar) || (x + "0"))) {
diff --git a/test/compress/evaluate.js b/test/compress/evaluate.js
index 5cefadc8..6bed73fb 100644
--- a/test/compress/evaluate.js
+++ b/test/compress/evaluate.js
@@ -624,25 +624,35 @@ in_boolean_context: {
options = {
booleans: true,
evaluate: true,
+ sequences: true,
+ side_effects: true,
}
input: {
- !42;
- !"foo";
- ![1, 2];
- !/foo/;
- !b(42);
- !b("foo");
- !b([1, 2]);
- !b(/foo/);
+ console.log(
+ !42,
+ !"foo",
+ ![1, 2],
+ !/foo/,
+ !b(42),
+ !b("foo"),
+ !b([1, 2]),
+ !b(/foo/),
+ ![1, foo()],
+ ![1, foo(), 2]
+ );
}
expect: {
- !1;
- !1;
- !1;
- !1;
- !b(42);
- !b("foo");
- !b([1, 2]);
- !b(/foo/);
+ console.log(
+ !1,
+ !1,
+ !1,
+ !1,
+ !b(42),
+ !b("foo"),
+ !b([1, 2]),
+ !b(/foo/),
+ ![1, foo()],
+ (foo(), !1)
+ );
}
}
diff --git a/test/compress/issue-1261.js b/test/compress/issue-1261.js
index dfbe2100..a872c578 100644
--- a/test/compress/issue-1261.js
+++ b/test/compress/issue-1261.js
@@ -116,3 +116,61 @@ pure_function_calls_toplevel: {
"WARN: Dropping unused variable iife1 [test/compress/issue-1261.js:84,12]",
]
}
+
+should_warn: {
+ options = {
+ booleans: true,
+ conditionals: true,
+ evaluate: true,
+ side_effects: true,
+ }
+ input: {
+ /* @__PURE__ */(function(){x})(), void/* @__PURE__ */(function(){y})();
+ /* @__PURE__ */(function(){x})() || true ? foo() : bar();
+ true || /* @__PURE__ */(function(){y})() ? foo() : bar();
+ /* @__PURE__ */(function(){x})() && false ? foo() : bar();
+ false && /* @__PURE__ */(function(){y})() ? foo() : bar();
+ /* @__PURE__ */(function(){x})() + "foo" ? bar() : baz();
+ "foo" + /* @__PURE__ */(function(){y})() ? bar() : baz();
+ /* @__PURE__ */(function(){x})() ? foo() : foo();
+ [/* @__PURE__ */(function(){x})()] ? foo() : bar();
+ !{ foo: /* @__PURE__ */(function(){x})() } ? bar() : baz();
+ }
+ expect: {
+ foo();
+ foo();
+ bar();
+ bar();
+ bar();
+ bar();
+ foo();
+ foo();
+ baz();
+ }
+ expect_warnings: [
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:128,61]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:128,23]",
+ "WARN: Dropping side-effect-free statement [test/compress/issue-1261.js:128,23]",
+ "WARN: Boolean || always true [test/compress/issue-1261.js:129,23]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:129,23]",
+ "WARN: Condition always true [test/compress/issue-1261.js:129,23]",
+ "WARN: Boolean || always true [test/compress/issue-1261.js:130,8]",
+ "WARN: Condition always true [test/compress/issue-1261.js:130,8]",
+ "WARN: Boolean && always false [test/compress/issue-1261.js:131,23]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:131,23]",
+ "WARN: Condition always false [test/compress/issue-1261.js:131,23]",
+ "WARN: Boolean && always false [test/compress/issue-1261.js:132,8]",
+ "WARN: Condition always false [test/compress/issue-1261.js:132,8]",
+ "WARN: + in boolean context always true [test/compress/issue-1261.js:133,23]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:133,23]",
+ "WARN: Condition always true [test/compress/issue-1261.js:133,23]",
+ "WARN: + in boolean context always true [test/compress/issue-1261.js:134,8]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:134,31]",
+ "WARN: Condition always true [test/compress/issue-1261.js:134,8]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:135,23]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:136,24]",
+ "WARN: Condition always true [test/compress/issue-1261.js:136,8]",
+ "WARN: Dropping __PURE__ call [test/compress/issue-1261.js:137,31]",
+ "WARN: Condition always false [test/compress/issue-1261.js:137,8]",
+ ]
+}
diff --git a/test/compress/issue-1275.js b/test/compress/issue-1275.js
index e88e284c..51f696af 100644
--- a/test/compress/issue-1275.js
+++ b/test/compress/issue-1275.js
@@ -35,7 +35,7 @@ string_plus_optimization: {
throw "nope";
}
try {
- console.log('0' + throwing_function() ? "yes" : "no");
+ console.log((throwing_function(), "yes"));
} catch (ex) {
console.log(ex);
}
diff --git a/test/compress/typeof.js b/test/compress/typeof.js
index fb391573..7bf8e5e3 100644
--- a/test/compress/typeof.js
+++ b/test/compress/typeof.js
@@ -29,6 +29,7 @@ typeof_in_boolean_context: {
booleans : true,
evaluate : true,
conditionals : true,
+ side_effects : true,
};
input: {
function f1(x) { return typeof x ? "yes" : "no"; }
@@ -36,12 +37,14 @@ typeof_in_boolean_context: {
typeof 0 ? foo() : bar();
!typeof console.log(1);
var a = !typeof console.log(2);
+ if (typeof (1 + foo()));
}
expect: {
function f1(x) { return "yes"; }
function f2() { return g(), "Yes"; }
foo();
- !(console.log(1), !0);
+ console.log(1);
var a = !(console.log(2), !0);
+ foo();
}
}
diff --git a/test/mocha/minify.js b/test/mocha/minify.js
index 0cf8c5c1..baac2a41 100644
--- a/test/mocha/minify.js
+++ b/test/mocha/minify.js
@@ -154,6 +154,17 @@ describe("minify", function() {
var code = result.code;
assert.strictEqual(code, "// comment1 comment2\nbar();");
});
+ it("should not drop #__PURE__ hint if function is retained", function() {
+ var result = Uglify.minify("var a = /*#__PURE__*/(function(){return 1})();", {
+ fromString: true,
+ output: {
+ comments: "all",
+ beautify: false,
+ }
+ });
+ var code = result.code;
+ assert.strictEqual(code, "var a=/*#__PURE__*/function(){return 1}();");
+ })
});
describe("JS_Parse_Error", function() {