support rest parameters (#4515)

This commit is contained in:
Alex Lam S.L
2021-01-07 02:04:09 +00:00
committed by GitHub
parent 68497d0258
commit c3d358a5b8
12 changed files with 819 additions and 135 deletions

View File

@@ -992,16 +992,26 @@ function OutputStream(options) {
});
/* -----[ functions ]----- */
DEFPRINT(AST_Arrow, function(output) {
var self = this;
if (self.argnames.length == 1 && self.argnames[0] instanceof AST_SymbolFunarg) {
self.argnames[0].print(output);
} else output.with_parens(function() {
function print_funargs(self, output) {
output.with_parens(function() {
self.argnames.forEach(function(arg, i) {
if (i) output.comma();
arg.print(output);
});
if (self.rest) {
if (self.argnames.length) output.comma();
output.print("...");
self.rest.print(output);
}
});
}
DEFPRINT(AST_Arrow, function(output) {
var self = this;
if (self.argnames.length == 1 && self.argnames[0] instanceof AST_SymbolFunarg && !self.rest) {
self.argnames[0].print(output);
} else {
print_funargs(self, output);
}
output.space();
output.print("=>");
output.space();
@@ -1016,12 +1026,7 @@ function OutputStream(options) {
output.space();
self.name.print(output);
}
output.with_parens(function() {
self.argnames.forEach(function(arg, i) {
if (i) output.comma();
arg.print(output);
});
});
print_funargs(self, output);
output.space();
print_braced(self, output, true);
}
@@ -1355,25 +1360,30 @@ function OutputStream(options) {
} : noop);
});
DEFPRINT(AST_DestructuredArray, function(output) {
var a = this.elements, len = a.length;
output.with_square(len > 0 ? function() {
var a = this.elements, len = a.length, rest = this.rest;
output.with_square(len || rest ? function() {
output.space();
a.forEach(function(exp, i) {
if (i) output.comma();
exp.print(output);
});
if (rest) {
if (len) output.comma();
output.print("...");
rest.print(output);
} else if (a[len - 1] instanceof AST_Hole) {
// If the final element is a hole, we need to make sure it
// doesn't look like a trailing comma, by inserting an actual
// trailing comma.
if (i === len - 1 && exp instanceof AST_Hole)
output.comma();
});
output.comma();
}
output.space();
} : noop);
});
DEFPRINT(AST_DestructuredKeyVal, print_key_value);
DEFPRINT(AST_DestructuredObject, function(output) {
var props = this.properties;
if (props.length > 0) output.with_block(function() {
var props = this.properties, len = props.length, rest = this.rest;
if (len || rest) output.with_block(function() {
props.forEach(function(prop, i) {
if (i) {
output.print(",");
@@ -1382,6 +1392,15 @@ function OutputStream(options) {
output.indent();
prop.print(output);
});
if (rest) {
if (len) {
output.print(",");
output.newline();
}
output.indent();
output.print("...");
rest.print(output);
}
output.newline();
});
else print_braced_empty(this, output);