aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-03-07 15:37:52 +0800
committerGitHub <noreply@github.com>2017-03-07 15:37:52 +0800
commit8153b7bd8a70ad94666904bd41f12ebd6be684c8 (patch)
tree58b10802868a7102418e6136adadfd4e8175b2e4 /test
parentd787d70127af1b8df57eee2274c720d34092aef2 (diff)
downloadtracifyjs-8153b7bd8a70ad94666904bd41f12ebd6be684c8.tar.gz
tracifyjs-8153b7bd8a70ad94666904bd41f12ebd6be684c8.zip
transform function calls to IIFEs (#1560)
- expose function body to call sites for potential optimisations - suppress substitution of variable used within `AST_Defun`
Diffstat (limited to 'test')
-rw-r--r--test/compress/reduce_vars.js238
-rw-r--r--test/mocha/cli.js2
-rw-r--r--test/mocha/glob.js12
3 files changed, 236 insertions, 16 deletions
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index 27f77fb5..a373de29 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -744,12 +744,11 @@ toplevel_on_loops_1: {
while (x);
}
expect: {
- function bar() {
- console.log("bar:", --x);
- }
var x = 3;
do
- bar();
+ (function() {
+ console.log("bar:", --x);
+ })();
while (x);
}
}
@@ -800,10 +799,9 @@ toplevel_on_loops_2: {
while (x);
}
expect: {
- function bar() {
+ for (;;) (function() {
console.log("bar:");
- }
- for (;;) bar();
+ })();
}
}
@@ -869,3 +867,229 @@ toplevel_off_loops_3: {
for (;x;) bar();
}
}
+
+defun_reference: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ }
+ input: {
+ function f() {
+ function g() {
+ x();
+ return a;
+ }
+ var a = h();
+ var b = 2;
+ return a + b;
+ function h() {
+ y();
+ return b;
+ }
+ }
+ }
+ expect: {
+ function f() {
+ function g() {
+ x();
+ return a;
+ }
+ var a = h();
+ var b = 2;
+ return a + b;
+ function h() {
+ y();
+ return b;
+ }
+ }
+ }
+}
+
+defun_inline_1: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ function f() {
+ return g(2) + h();
+ function g(b) {
+ return b;
+ }
+ function h() {
+ return h();
+ }
+ }
+ }
+ expect: {
+ function f() {
+ return function(b) {
+ return b;
+ }(2) + h();
+ function h() {
+ return h();
+ }
+ }
+ }
+}
+
+defun_inline_2: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ function f() {
+ function g(b) {
+ return b;
+ }
+ function h() {
+ return h();
+ }
+ return g(2) + h();
+ }
+ }
+ expect: {
+ function f() {
+ function h() {
+ return h();
+ }
+ return function(b) {
+ return b;
+ }(2) + h();
+ }
+ }
+}
+
+defun_inline_3: {
+ options = {
+ evaluate: true,
+ passes: 2,
+ reduce_vars: true,
+ side_effects: true,
+ unused: true,
+ }
+ input: {
+ function f() {
+ return g(2);
+ function g(b) {
+ return b;
+ }
+ }
+ }
+ expect: {
+ function f() {
+ return 2;
+ }
+ }
+}
+
+defun_call: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ function f() {
+ return g() + h(1) - h(g(), 2, 3);
+ function g() {
+ return 4;
+ }
+ function h(a) {
+ return a;
+ }
+ }
+ }
+ expect: {
+ function f() {
+ return 4 + h(1) - h(4);
+ function h(a) {
+ return a;
+ }
+ }
+ }
+}
+
+defun_redefine: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ function f() {
+ function g() {
+ return 1;
+ }
+ function h() {
+ return 2;
+ }
+ g = function() {
+ return 3;
+ };
+ return g() + h();
+ }
+ }
+ expect: {
+ function f() {
+ function g() {
+ return 1;
+ }
+ g = function() {
+ return 3;
+ };
+ return g() + 2;
+ }
+ }
+}
+
+func_inline: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ function f() {
+ var g = function() {
+ return 1;
+ };
+ console.log(g() + h());
+ var h = function() {
+ return 2;
+ };
+ }
+ }
+ expect: {
+ function f() {
+ console.log(1 + h());
+ var h = function() {
+ return 2;
+ };
+ }
+ }
+}
+
+func_modified: {
+ options = {
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ function f(a) {
+ function a() { return 1; }
+ function b() { return 2; }
+ function c() { return 3; }
+ b.inject = [];
+ c = function() { return 4; };
+ return a() + b() + c();
+ }
+ }
+ expect: {
+ function f(a) {
+ function b() { return 2; }
+ function c() { return 3; }
+ b.inject = [];
+ c = function() { return 4; };
+ return 1 + 2 + c();
+ }
+ }
+}
diff --git a/test/mocha/cli.js b/test/mocha/cli.js
index c07eeee7..e8e07cb5 100644
--- a/test/mocha/cli.js
+++ b/test/mocha/cli.js
@@ -82,7 +82,7 @@ describe("bin/uglifyjs", function () {
});
});
it("Should work with --keep-fnames (mangle & compress)", function (done) {
- var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c';
+ var command = uglifyjscmd + ' test/input/issue-1431/sample.js --keep-fnames -m -c unused=false';
exec(command, function (err, stdout) {
if (err) throw err;
diff --git a/test/mocha/glob.js b/test/mocha/glob.js
index c2fc9464..30313656 100644
--- a/test/mocha/glob.js
+++ b/test/mocha/glob.js
@@ -3,17 +3,13 @@ var assert = require("assert");
describe("minify() with input file globs", function() {
it("minify() with one input file glob string.", function() {
- var result = Uglify.minify("test/input/issue-1242/foo.*", {
- compress: { collapse_vars: true }
- });
+ var result = Uglify.minify("test/input/issue-1242/foo.*");
assert.strictEqual(result.code, 'function foo(o){print("Foo:",2*o)}var print=console.log.bind(console);');
});
it("minify() with an array of one input file glob.", function() {
var result = Uglify.minify([
"test/input/issue-1242/b*.es5",
- ], {
- compress: { collapse_vars: true }
- });
+ ]);
assert.strictEqual(result.code, 'function bar(n){return 3*n}function baz(n){return n/2}');
});
it("minify() with an array of multiple input file globs.", function() {
@@ -21,8 +17,8 @@ describe("minify() with input file globs", function() {
"test/input/issue-1242/???.es5",
"test/input/issue-1242/*.js",
], {
- compress: { collapse_vars: true }
+ compress: { toplevel: true }
});
- assert.strictEqual(result.code, 'function bar(n){return 3*n}function baz(n){return n/2}function foo(n){print("Foo:",2*n)}var print=console.log.bind(console);print("qux",bar(3),baz(12)),foo(11);');
+ assert.strictEqual(result.code, 'var print=console.log.bind(console);print("qux",function(n){return 3*n}(3),function(n){return n/2}(12)),function(n){print("Foo:",2*n)}(11);');
});
});