Skip to content

Commit

Permalink
fix terser#510: inline identity functions
Browse files Browse the repository at this point in the history
  • Loading branch information
michens committed Nov 15, 2019
1 parent 6589965 commit c1ab100
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
13 changes: 13 additions & 0 deletions lib/compress/index.js
Expand Up @@ -5214,6 +5214,19 @@ def_optimize(AST_Call, function(self, compressor) {
var args = self.args.concat(value);
return make_sequence(self, args).optimize(compressor);
}

// optimize identity function
if (
fn.argnames.length === 1
&& value.start.type === "name"
&& value.name === fn.argnames[0].name
) {
// replace call with first argument or undefined if none passed
return make_sequence(
self,
[self.args[0] || make_node(AST_Undefined)]
).optimize(compressor);
}
}
if (can_inline) {
var scope, in_loop, level = -1;
Expand Down
5 changes: 2 additions & 3 deletions test/compress/async.js
Expand Up @@ -165,9 +165,8 @@ async_inline: {
!async function(){await 3}();
!async function(x){await console.log(4)}();

function echo(x){return x}
echo(async function(){return await 1}());
echo(async function(x){await console.log(2)}());
!async function(){await 1}();
!async function(x){await console.log(2)}();

console.log("top");

Expand Down
9 changes: 2 additions & 7 deletions test/compress/functions.js
Expand Up @@ -409,11 +409,7 @@ inner_ref: {
}(2));
}
expect: {
console.log(function(a) {
return a;
}(1), function(a) {
return a;
}());
console.log(1, void 0);
}
expect_stdout: "1 undefined"
}
Expand Down Expand Up @@ -2107,8 +2103,7 @@ duplicate_arg_var: {
}("PASS"));
}
expect: {
console.log((b = "PASS", b));
var b;
console.log("PASS");
}
expect_stdout: "PASS"
}
Expand Down
61 changes: 61 additions & 0 deletions test/compress/identity.js
@@ -0,0 +1,61 @@
inline_identity: {
options = {
defaults: true,
toplevel: true
}
input: {
const id = x => x;
console.log(id(1), id(2));
}
expect: {
console.log(1, 2);
}
expect_stdout: "1 2"
}

inline_identity_no_params: {
options = {
defaults: true,
toplevel: true
}
input: {
const id = x => x;
console.log(id(), id());
}
expect: {
console.log(void 0, void 0);
}
expect_stdout: "undefined undefined"
}

inline_identity_extra_params: {
options = {
defaults: true,
toplevel: true
}
input: {
const id = x => x;
console.log(id(1, 2), id(3, 4));
}
expect: {
console.log(1, 3);
}
expect_stdout: "1 3"
}

inline_identity_higher_order: {
options = {
defaults: true,
toplevel: true
}
input: {
const id = x => x;
const inc = x => x + 1;
console.log(id(inc)(1), id(inc)(2));
}
expect: {
const inc = x => x + 1;
console.log(inc(1), inc(2));
}
expect_stdout: "2 3"
}
6 changes: 2 additions & 4 deletions test/compress/reduce_vars.js
Expand Up @@ -1429,6 +1429,7 @@ defun_inline_3: {

defun_call: {
options = {
evaluate: true,
inline: true,
reduce_funcs: true,
reduce_vars: true,
Expand All @@ -1447,10 +1448,7 @@ defun_call: {
}
expect: {
function f() {
return 4 + h(1) - h(4);
function h(a) {
return a;
}
return 1;
}
}
}
Expand Down

0 comments on commit c1ab100

Please sign in to comment.