Skip to content

Commit

Permalink
fix corner case in default_values (#5337)
Browse files Browse the repository at this point in the history
fixes #5336
  • Loading branch information
alexlamsl committed Feb 3, 2022
1 parent fa30960 commit 9a91a7a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6830,8 +6830,8 @@ Compressor.prototype.compress = function(node) {
unused_fn_names.push(node);
}
if (!(node instanceof AST_Accessor)) {
var args, spread;
if (parent instanceof AST_Call && parent.expression === node) {
var args, spread, trim = compressor.drop_fargs(node, parent);
if (trim && parent instanceof AST_Call && parent.expression === node) {
args = parent.args;
for (spread = 0; spread < args.length; spread++) {
if (args[spread] instanceof AST_Spread) break;
Expand Down Expand Up @@ -6861,8 +6861,8 @@ Compressor.prototype.compress = function(node) {
}
}
node.rest = rest;
if (rest) trim = false;
}
var trim = compressor.drop_fargs(node, parent) && !node.rest;
var default_length = trim ? -1 : node.length();
for (var i = argnames.length; --i >= 0;) {
var sym = argnames[i];
Expand Down
39 changes: 33 additions & 6 deletions test/compress/default-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,7 @@ issue_5192: {

issue_5246_1: {
options = {
keep_fargs: false,
pure_getters: true,
unused: true,
}
Expand All @@ -2016,20 +2017,20 @@ issue_5246_1: {
}("foo"));
}
expect: {
console.log(function({} = 0) {
console.log(function() {
return "PASS";
}("foo"));
}());
}
expect_stdout: "PASS"
expect_warnings: [
"INFO: Dropping unused default argument value {}=42 [test/compress/default-values.js:1,29]",
"INFO: Dropping unused default argument value {}=0 [test/compress/default-values.js:1,29]",
"INFO: Dropping unused default argument {}=42 [test/compress/default-values.js:1,29]",
]
node_version: ">=6"
}

issue_5246_2: {
options = {
keep_fargs: false,
unused: true,
}
input: {
Expand All @@ -2038,9 +2039,9 @@ issue_5246_2: {
})("PASS", []);
}
expect: {
(function(a = "FAIL", []) {
(function(a = "FAIL") {
console.log(a);
})("PASS", []);
})("PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
Expand All @@ -2049,6 +2050,7 @@ issue_5246_2: {
issue_5246_3: {
options = {
default_values: true,
keep_fargs: false,
unused: true,
}
input: {
Expand Down Expand Up @@ -2121,3 +2123,28 @@ issue_5314_2: {
expect_stdout: "PASS"
node_version: ">=6"
}

issue_5336: {
options = {
default_values: true,
unused: true,
}
input: {
var a;
do {
(function f(b = console.log("PASS")) {
a = f;
})(42);
} while (a());
}
expect: {
var a;
do {
(function f(b = console.log("PASS")) {
a = f;
})(42);
} while (a());
}
expect_stdout: "PASS"
node_version: ">=6"
}
5 changes: 3 additions & 2 deletions test/compress/destructured.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ drop_unused_2: {
}
f();
}
expect:{
expect: {
(function(a) {
console.log("PASS");
})();
Expand Down Expand Up @@ -3406,6 +3406,7 @@ issue_5288: {
options = {
conditionals: true,
inline: true,
keep_fargs: false,
reduce_vars: true,
toplevel: true,
unused: true,
Expand All @@ -3421,7 +3422,7 @@ issue_5288: {
}() ]));
}
expect: {
while ([ [ console ? console.log("PASS") : 0 ] ], void 0);
while (console ? console.log("PASS") : 0, void 0);
}
expect_stdout: "PASS"
node_version: ">=6"
Expand Down
13 changes: 8 additions & 5 deletions test/compress/rests.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ retain_funarg_destructured_object_1: {

retain_funarg_destructured_object_2: {
options = {
keep_fargs: false,
unused: true,
}
input: {
Expand Down Expand Up @@ -1091,6 +1092,7 @@ issue_5100_2: {
issue_5108: {
options = {
evaluate: true,
keep_fargs: false,
reduce_vars: true,
rests: true,
unsafe: true,
Expand All @@ -1102,9 +1104,7 @@ issue_5108: {
}([ "PASS", "FAIL" ]));
}
expect: {
console.log(function([]) {
return "PASS";
}([]));
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
Expand Down Expand Up @@ -1208,6 +1208,7 @@ issue_5165_2: {

issue_5246_1: {
options = {
keep_fargs: false,
reduce_vars: true,
rests: true,
unused: true,
Expand All @@ -1218,16 +1219,17 @@ issue_5246_1: {
}([ , function(){} ])[0]);
}
expect: {
console.log(typeof function([]) {
console.log(typeof function() {
return this && [ function(){} ];
}([])[0]);
}()[0]);
}
expect_stdout: "function"
node_version: ">=6"
}

issue_5246_2: {
options = {
keep_fargs: false,
reduce_vars: true,
rests: true,
toplevel: true,
Expand All @@ -1249,6 +1251,7 @@ issue_5246_2: {

issue_5246_3: {
options = {
keep_fargs: false,
unused: true,
}
input: {
Expand Down
2 changes: 2 additions & 0 deletions test/compress/yields.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,7 @@ issue_5076_1: {
options = {
evaluate: true,
hoist_vars: true,
keep_fargs: false,
pure_getters: "strict",
sequences: true,
side_effects: true,
Expand Down Expand Up @@ -1404,6 +1405,7 @@ issue_5076_2: {
options = {
evaluate: true,
hoist_vars: true,
keep_fargs: false,
passes: 2,
pure_getters: "strict",
sequences: true,
Expand Down
2 changes: 1 addition & 1 deletion test/mocha/awaits.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("async", function() {
"function await() {}",
"function(await) {}",
"function() { await; }",
"function() { await:{} }",
"function() { await: {} }",
"function() { var await; }",
"function() { function await() {} }",
"function() { try {} catch (await) {} }",
Expand Down
2 changes: 1 addition & 1 deletion test/mocha/yields.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("generator", function() {
[
"function yield() {}",
"function(yield) {}",
"function() { yield:{} }",
"function() { yield: {} }",
"function() { var yield; }",
"function() { function yield() {} }",
"function() { try {} catch (yield) {} }",
Expand Down

0 comments on commit 9a91a7a

Please sign in to comment.