From a75a046abbc1f93fbb2235c99a1fc1d543faf9e9 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 2 Mar 2018 11:22:09 +0800 Subject: compress `arguments[index]` (#2967) - always replace with existing parameter - only introduce new parameter if `keep_fargs` is disabled --- test/compress/arguments.js | 119 +++++++++++++++++++++++++++++++++++++++++++ test/compress/hoist_props.js | 12 ++--- 2 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 test/compress/arguments.js (limited to 'test/compress') diff --git a/test/compress/arguments.js b/test/compress/arguments.js new file mode 100644 index 00000000..e8cc690f --- /dev/null +++ b/test/compress/arguments.js @@ -0,0 +1,119 @@ +replace_index: { + options = { + arguments: true, + evaluate: true, + properties: true, + } + input: { + console.log(arguments && arguments[0]); + (function() { + console.log(arguments[1], arguments["1"], arguments["foo"]); + })("bar", 42); + (function(a, b) { + console.log(arguments[1], arguments["1"], arguments["foo"]); + })("bar", 42); + (function(arguments) { + console.log(arguments[1], arguments["1"], arguments["foo"]); + })("bar", 42); + (function() { + var arguments; + console.log(arguments[1], arguments["1"], arguments["foo"]); + })("bar", 42); + } + expect: { + console.log(arguments && arguments[0]); + (function() { + console.log(arguments[1], arguments[1], arguments.foo); + })("bar", 42); + (function(a, b) { + console.log(b, b, arguments.foo); + })("bar", 42); + (function(arguments) { + console.log(arguments[1], arguments[1], arguments.foo); + })("bar", 42); + (function() { + var arguments; + console.log(arguments[1], arguments[1], arguments.foo); + })("bar", 42); + } + expect_stdout: [ + "undefined", + "42 42 undefined", + "42 42 undefined", + "a a undefined", + "42 42 undefined", + ] +} + +replace_index_keep_fargs: { + options = { + arguments: true, + evaluate: true, + keep_fargs: false, + properties: true, + } + input: { + console.log(arguments && arguments[0]); + (function() { + console.log(arguments[1], arguments["1"], arguments["foo"]); + })("bar", 42); + (function(a, b) { + console.log(arguments[1], arguments["1"], arguments["foo"]); + })("bar", 42); + (function(arguments) { + console.log(arguments[1], arguments["1"], arguments["foo"]); + })("bar", 42); + (function() { + var arguments; + console.log(arguments[1], arguments["1"], arguments["foo"]); + })("bar", 42); + } + expect: { + console.log(arguments && arguments[0]); + (function(argument_0, argument_1) { + console.log(argument_1, argument_1, arguments.foo); + })("bar", 42); + (function(a, b) { + console.log(b, b, arguments.foo); + })("bar", 42); + (function(arguments) { + console.log(arguments[1], arguments[1], arguments.foo); + })("bar", 42); + (function() { + var arguments; + console.log(arguments[1], arguments[1], arguments.foo); + })("bar", 42); + } + expect_stdout: [ + "undefined", + "42 42 undefined", + "42 42 undefined", + "a a undefined", + "42 42 undefined", + ] +} + +modified: { + options = { + arguments: true, + } + input: { + (function(a, b) { + var c = arguments[0]; + var d = arguments[1]; + a = "foo"; + b++; + console.log(a, b, c, d, arguments[0], arguments[1]); + })("bar", 42); + } + expect: { + (function(a, b) { + var c = a; + var d = b; + a = "foo"; + b++; + console.log(a, b, c, d, a, b); + })("bar", 42); + } + expect_stdout: "foo 43 bar 42 foo 43" +} diff --git a/test/compress/hoist_props.js b/test/compress/hoist_props.js index 03867f78..26887af2 100644 --- a/test/compress/hoist_props.js +++ b/test/compress/hoist_props.js @@ -239,14 +239,14 @@ name_collision_2: { input: { var o = { p: 1, - 0: function(x) { + "+": function(x) { return x; }, - 1: function(x) { + "-": function(x) { return x + 1; } }, o__$0 = 2, o__$1 = 3; - console.log(o.p === o.p, o[0](4), o[1](5), o__$0, o__$1); + console.log(o.p === o.p, o["+"](4), o["-"](5), o__$0, o__$1); } expect: { var o_p = 1, @@ -273,14 +273,14 @@ name_collision_3: { input: { var o = { p: 1, - 0: function(x) { + "+": function(x) { return x; }, - 1: function(x) { + "-": function(x) { return x + 1; } }, o__$0 = 2, o__$1 = 3; - console.log(o.p === o.p, o[0](4), o[1](5)); + console.log(o.p === o.p, o["+"](4), o["-"](5)); } expect: { var o_p = 1, -- cgit v1.2.3