Skip to content

Commit

Permalink
Do not remove default_arg = undefined, as it changes Function.length. C…
Browse files Browse the repository at this point in the history
…loses #1295
  • Loading branch information
fabiosantoscode committed Feb 12, 2023
1 parent 12b7709 commit ca51c11
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/compress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,13 @@ def_optimize(AST_DefaultAssign, function(self, compressor) {
var evaluateRight = self.right.evaluate(compressor);

// `[x = undefined] = foo` ---> `[x] = foo`
if (evaluateRight === undefined) {
// `(arg = undefined) => ...` ---> `(arg) => ...` (unless `keep_fargs`)
if (
evaluateRight === undefined &&
(compressor.parent() instanceof AST_Lambda
? compressor.option("keep_fargs") === false
: true)
) {
self = self.left;
} else if (evaluateRight !== self.right) {
evaluateRight = make_node_from_constant(evaluateRight, self.right);
Expand Down
15 changes: 15 additions & 0 deletions test/compress/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ default_arguments: {
expect_exact: "function x(a=6){}function x(a=6+5){}function x({foo}={},[bar]=[1]){}"
}

keep_default_arg_when_undefined: {
options = {
keep_fargs: true,
evaluate: true,
}
input: {
function x(a = void 0) { }
console.log(x.length)
}
expect: {
function x(a = undefined) { }
console.log(x.length)
}
}

default_values_in_destructurings: {
beautify = {
ecma: 2015
Expand Down

0 comments on commit ca51c11

Please sign in to comment.