support arrow function (#4385)

This commit is contained in:
Alex Lam S.L
2020-12-17 10:23:41 +00:00
committed by GitHub
parent 75e9fd8417
commit a96f087ac3
11 changed files with 732 additions and 117 deletions

View File

@@ -678,7 +678,7 @@ function OutputStream(options) {
// same goes for an object literal, because otherwise it would be
// interpreted as a block of code.
function needs_parens_obj(output) {
return !output.has_parens() && first_in_statement(output);
return !output.has_parens() && first_in_statement(output, true);
}
PARENS(AST_Object, needs_parens_obj);
@@ -691,6 +691,8 @@ function OutputStream(options) {
var p = output.parent();
// [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ]
return p instanceof AST_Array
// () => (foo, bar)
|| p instanceof AST_Arrow && p.value === this
// await (foo, bar)
|| p instanceof AST_Await
// 1 + (2, 3) + 4 ==> 8
@@ -798,6 +800,9 @@ function OutputStream(options) {
// !(a = false) → true
if (p instanceof AST_Unary) return true;
}
PARENS(AST_Arrow, function(output) {
return needs_parens_assign_cond(this, output);
});
PARENS(AST_Assign, function(output) {
if (needs_parens_assign_cond(this, output)) return true;
// v8 parser bug => workaround
@@ -985,6 +990,25 @@ 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() {
self.argnames.forEach(function(arg, i) {
if (i) output.comma();
arg.print(output);
});
});
output.space();
output.print("=>");
output.space();
if (self.value) {
self.value.print(output);
} else {
print_braced(self, output, true);
}
});
function print_lambda(self, output) {
if (self.name) {
output.space();