Skip to content

Commit

Permalink
enhance functions & reduce_vars (#5045)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jul 3, 2021
1 parent 668f966 commit 972b9f0
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 160 deletions.
331 changes: 192 additions & 139 deletions lib/compress.js

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions test/compress/arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,38 @@ reduce_iife_3: {
node_version: ">=4"
}

reduce_lambda: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var f = () => {
console.log(a, b);
};
var a = "foo", b = 42;
f();
b = "bar";
f();
}
expect: {
var f = () => {
console.log("foo", b);
};
var b = 42;
f();
b = "bar";
f();
}
expect_stdout: [
"foo 42",
"foo bar",
]
node_version: ">=4"
}

single_use_recursive: {
options = {
reduce_vars: true,
Expand Down
3 changes: 2 additions & 1 deletion test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -3081,7 +3081,8 @@ issue_4235: {
}
expect: {
void function() {
var f = console.log(f);
var f;
console.log(f);
}();
}
expect_stdout: "undefined"
Expand Down
2 changes: 2 additions & 0 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2905,6 +2905,7 @@ issue_2437: {
issue_2485_1: {
options = {
functions: true,
passes: 2,
reduce_funcs: true,
reduce_vars: true,
unused: true,
Expand Down Expand Up @@ -2955,6 +2956,7 @@ issue_2485_2: {
options = {
functions: true,
inline: true,
passes: 2,
reduce_funcs: true,
reduce_vars: true,
unused: true,
Expand Down
3 changes: 2 additions & 1 deletion test/compress/hoist_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ issue_4487: {
}
expect: {
function a() {
var f = console.log(typeof f);
var f;
console.log(typeof f);
}
a();
}
Expand Down
35 changes: 35 additions & 0 deletions test/compress/let.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,41 @@ reduce_vars_3: {
node_version: ">=4"
}

reduce_lambda: {
options = {
evaluate: true,
functions: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
"use strict";
let f = function() {
console.log(a, b);
};
let a = "foo", b = 42;
f();
b = "bar";
f();
}
expect: {
"use strict";
function f() {
console.log("foo", b);
}
let b = 42;
f();
b = "bar";
f();
}
expect_stdout: [
"foo 42",
"foo bar",
]
node_version: ">=4"
}

hoist_props: {
options = {
hoist_props: true,
Expand Down
1 change: 1 addition & 0 deletions test/compress/pure_getters.js
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,7 @@ issue_2878: {

issue_3427: {
options = {
assignments: true,
evaluate: true,
inline: true,
pure_getters: "strict",
Expand Down
37 changes: 19 additions & 18 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -3092,7 +3092,7 @@ accessor_1: {
a = 2;
return a;
},
b: 1
b: 1,
}.b, a);
}
expect: {
Expand All @@ -3102,7 +3102,7 @@ accessor_1: {
a = 2;
return a;
},
b: 1
b: 1,
}.b, a);
}
expect_stdout: "1 1"
Expand All @@ -3122,15 +3122,15 @@ accessor_2: {
var B = {
get c() {
console.log(A);
}
},
};
B.c;
}
expect: {
({
get c() {
console.log(1);
}
},
}).c;
}
expect_stdout: "1"
Expand Down Expand Up @@ -3176,15 +3176,15 @@ obj_var_1: {
var obj = {
bar: function() {
return C + C;
}
},
};
console.log(obj.bar());
}
expect: {
console.log({
bar: function() {
return 2;
}
},
}.bar());
}
expect_stdout: "2"
Expand All @@ -3208,7 +3208,7 @@ obj_var_2: {
var obj = {
bar: function() {
return C + C;
}
},
};
console.log(obj.bar());
}
Expand Down Expand Up @@ -4422,6 +4422,7 @@ perf_2: {

perf_3: {
options = {
passes: 2,
reduce_funcs: true,
reduce_vars: true,
toplevel: true,
Expand All @@ -4430,10 +4431,10 @@ perf_3: {
input: {
var foo = function(x, y, z) {
return x < y ? x * y + z : x * z - y;
}
};
var indirect_foo = function(x, y, z) {
return foo(x, y, z);
}
};
var sum = 0;
for (var i = 0; i < 100; ++i)
sum += indirect_foo(i, i + 1, 3 * i);
Expand Down Expand Up @@ -4462,10 +4463,10 @@ perf_4: {
input: {
var foo = function(x, y, z) {
return x < y ? x * y + z : x * z - y;
}
};
var indirect_foo = function(x, y, z) {
return foo(x, y, z);
}
};
var sum = 0;
for (var i = 0; i < 100; ++i)
sum += indirect_foo(i, i + 1, 3 * i);
Expand All @@ -4474,10 +4475,10 @@ perf_4: {
expect: {
var foo = function(x, y, z) {
return x < y ? x * y + z : x * z - y;
}
};
var indirect_foo = function(x, y, z) {
return foo(x, y, z);
}
};
var sum = 0;
for (var i = 0; i < 100; ++i)
sum += indirect_foo(i, i + 1, 3 * i);
Expand Down Expand Up @@ -4566,9 +4567,9 @@ perf_7: {
var indirect_foo = function(x, y, z) {
var foo = function(x, y, z) {
return x < y ? x * y + z : x * z - y;
}
};
return foo(x, y, z);
}
};
var sum = 0;
for (var i = 0; i < 100; ++i)
sum += indirect_foo(i, i + 1, 3 * i);
Expand Down Expand Up @@ -4598,9 +4599,9 @@ perf_8: {
var indirect_foo = function(x, y, z) {
var foo = function(x, y, z) {
return x < y ? x * y + z : x * z - y;
}
};
return foo(x, y, z);
}
};
var sum = 0;
for (var i = 0; i < 100; ++i)
sum += indirect_foo(i, i + 1, 3 * i);
Expand All @@ -4611,7 +4612,7 @@ perf_8: {
return function(x, y, z) {
return x < y ? x * y + z : x * z - y;
}(x, y, z);
}
};
var sum = 0;
for (var i = 0; i < 100; ++i)
sum += indirect_foo(i, i + 1, 3 * i);
Expand Down
2 changes: 1 addition & 1 deletion test/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ function to_statement_init(node) {
return node instanceof U.AST_Const || node instanceof U.AST_Let ? new U.AST_BlockStatement({
body: [ node ],
start: {},
}) : to_statement(node);;
}) : to_statement(node);
}

function wrap_with_console_log(node) {
Expand Down

0 comments on commit 972b9f0

Please sign in to comment.