From 2cbbf5c375a0fae88345c3ed3bc2829b4b1ac250 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 6 Dec 2020 21:22:40 +0000 Subject: support async function (#4333) --- lib/output.js | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'lib/output.js') diff --git a/lib/output.js b/lib/output.js index 5830f2e7..bf243862 100644 --- a/lib/output.js +++ b/lib/output.js @@ -661,7 +661,7 @@ function OutputStream(options) { // a function expression needs parens around it when it's provably // the first token to appear in a statement. - PARENS(AST_Function, function(output) { + function needs_parens_function(output) { if (!output.has_parens() && first_in_statement(output)) return true; if (output.option("webkit")) { var p = output.parent(); @@ -671,7 +671,9 @@ function OutputStream(options) { var p = output.parent(); if (p instanceof AST_Call && p.expression === this) return true; } - }); + } + PARENS(AST_AsyncFunction, needs_parens_function); + PARENS(AST_Function, needs_parens_function); // same goes for an object literal, because otherwise it would be // interpreted as a block of code. @@ -689,6 +691,8 @@ function OutputStream(options) { var p = output.parent(); // [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ] return p instanceof AST_Array + // await (foo, bar) + || p instanceof AST_Await // 1 + (2, 3) + 4 ==> 8 || p instanceof AST_Binary // new (foo, bar) or foo(1, (2, 3), 4) @@ -712,6 +716,8 @@ function OutputStream(options) { PARENS(AST_Binary, function(output) { var p = output.parent(); + // await (foo && bar) + if (p instanceof AST_Await) return true; // this deals with precedence: 3 * (2 + 1) if (p instanceof AST_Binary) { var po = p.operator, pp = PRECEDENCE[po]; @@ -779,6 +785,8 @@ function OutputStream(options) { function needs_parens_assign_cond(self, output) { var p = output.parent(); + // await (a = foo) + if (p instanceof AST_Await) return true; // 1 + (a = 2) + 3 β†’ 6, side effect setting a = 2 if (p instanceof AST_Binary) return !(p instanceof AST_Assign); // (a = func)() β€”orβ€” new (a = Object)() @@ -967,11 +975,7 @@ function OutputStream(options) { }); /* -----[ functions ]----- */ - DEFPRINT(AST_Lambda, function(output, nokeyword) { - var self = this; - if (!nokeyword) { - output.print("function"); - } + function print_lambda(self, output) { if (self.name) { output.space(); self.name.print(output); @@ -984,7 +988,19 @@ function OutputStream(options) { }); output.space(); print_braced(self, output, true); + } + DEFPRINT(AST_Lambda, function(output) { + output.print("function"); + print_lambda(this, output); }); + function print_async(output) { + output.print("async"); + output.space(); + output.print("function"); + print_lambda(this, output); + } + DEFPRINT(AST_AsyncDefun, print_async); + DEFPRINT(AST_AsyncFunction, print_async); /* -----[ jumps ]----- */ function print_jump(kind, prop) { @@ -1272,6 +1288,11 @@ function OutputStream(options) { output.colon(); self.alternative.print(output); }); + DEFPRINT(AST_Await, function(output) { + output.print("await"); + output.space(); + this.expression.print(output); + }); /* -----[ literals ]----- */ DEFPRINT(AST_Array, function(output) { @@ -1375,7 +1396,7 @@ function OutputStream(options) { output.print(type); output.space(); print_property_key(self, output); - self.value._codegen(output, true); + print_lambda(self.value, output); }; } DEFPRINT(AST_ObjectGetter, print_accessor("get")); -- cgit v1.2.3