support generator functions (#4620)

This commit is contained in:
Alex Lam S.L
2021-02-07 22:44:20 +00:00
committed by GitHub
parent c44b6399c3
commit fd4caf7a9c
12 changed files with 1186 additions and 182 deletions

View File

@@ -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) {