From fd4caf7a9cbfaa482d8cc77d2bc8cd00e8bc256a Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 7 Feb 2021 22:44:20 +0000 Subject: support generator functions (#4620) --- lib/output.js | 55 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 17 deletions(-) (limited to 'lib/output.js') diff --git a/lib/output.js b/lib/output.js index a171c7e7..b2213f74 100644 --- a/lib/output.js +++ b/lib/output.js @@ -673,7 +673,9 @@ function OutputStream(options) { } } PARENS(AST_AsyncFunction, needs_parens_function); + PARENS(AST_AsyncGeneratorFunction, needs_parens_function); PARENS(AST_Function, needs_parens_function); + PARENS(AST_GeneratorFunction, needs_parens_function); // same goes for an object literal, because otherwise it would be // interpreted as a block of code. @@ -682,14 +684,19 @@ function OutputStream(options) { } PARENS(AST_Object, needs_parens_obj); - PARENS(AST_Unary, function(output) { + function needs_parens_unary(output) { var p = output.parent(); // (-x) ** y if (p instanceof AST_Binary) return p.operator == "**" && p.left === this; - // (x++).toString(3) - // (typeof x).length - return (p instanceof AST_Call || p instanceof AST_PropAccess) && p.expression === this; - }); + // (await x)(y) + // new (await x) + if (p instanceof AST_Call) return p.expression === this; + // (x++)[y] + // (typeof x).y + if (p instanceof AST_PropAccess) return p.expression === this; + } + PARENS(AST_Await, needs_parens_unary); + PARENS(AST_Unary, needs_parens_unary); PARENS(AST_Sequence, function(output) { var p = output.parent(); @@ -719,7 +726,9 @@ function OutputStream(options) { // !(foo, bar, baz) || p instanceof AST_Unary // var a = (1, 2), b = a + a; ---> b == 4 - || p instanceof AST_VarDef; + || p instanceof AST_VarDef + // yield (foo, bar) + || p instanceof AST_Yield; }); PARENS(AST_Binary, function(output) { @@ -826,17 +835,8 @@ function OutputStream(options) { PARENS(AST_Conditional, function(output) { return needs_parens_assign_cond(this, output); }); - - PARENS(AST_Await, function(output) { - var p = output.parent(); - // (await x) ** y - if (p instanceof AST_Binary) return p.operator == "**" && p.left === this; - // new (await foo) - // (await foo)(bar) - if (p instanceof AST_Call) return p.expression === this; - // (await foo).prop - // (await foo)["prop"] - if (p instanceof AST_PropAccess) return p.expression === this; + PARENS(AST_Yield, function(output) { + return needs_parens_assign_cond(this, output); }); /* -----[ PRINTERS ]----- */ @@ -1061,6 +1061,20 @@ function OutputStream(options) { } DEFPRINT(AST_AsyncDefun, print_async); DEFPRINT(AST_AsyncFunction, print_async); + function print_async_generator(output) { + output.print("async"); + output.space(); + output.print("function*"); + print_lambda(this, output); + } + DEFPRINT(AST_AsyncGeneratorDefun, print_async_generator); + DEFPRINT(AST_AsyncGeneratorFunction, print_async_generator); + function print_generator(output) { + output.print("function*"); + print_lambda(this, output); + } + DEFPRINT(AST_GeneratorDefun, print_generator); + DEFPRINT(AST_GeneratorFunction, print_generator); /* -----[ jumps ]----- */ function print_jump(kind, prop) { @@ -1360,6 +1374,13 @@ function OutputStream(options) { output.space(); this.expression.print(output); }); + DEFPRINT(AST_Yield, function(output) { + output.print(this.nested ? "yield*" : "yield"); + if (this.expression) { + output.space(); + this.expression.print(output); + } + }); /* -----[ literals ]----- */ DEFPRINT(AST_Array, function(output) { -- cgit v1.2.3