support asynchronous arrow functions (#4530)
This commit is contained in:
55
lib/parse.js
55
lib/parse.js
@@ -796,13 +796,14 @@ function parse($TEXT, options) {
|
||||
next();
|
||||
return function_(AST_AsyncDefun);
|
||||
}
|
||||
break;
|
||||
case "await":
|
||||
if (S.in_async) return simple_statement();
|
||||
default:
|
||||
return is_token(peek(), "punc", ":")
|
||||
? labeled_statement()
|
||||
: simple_statement();
|
||||
break;
|
||||
}
|
||||
return is_token(peek(), "punc", ":")
|
||||
? labeled_statement()
|
||||
: simple_statement();
|
||||
|
||||
case "punc":
|
||||
switch (S.token.value) {
|
||||
@@ -1094,16 +1095,19 @@ function parse($TEXT, options) {
|
||||
end: node.end,
|
||||
});
|
||||
}
|
||||
if (node instanceof AST_SymbolFunarg) return node;
|
||||
if (node instanceof AST_SymbolRef) return new AST_SymbolFunarg(node);
|
||||
token_error(node.start, "Invalid arrow parameter");
|
||||
}
|
||||
|
||||
function arrow(exprs, start) {
|
||||
function arrow(exprs, start, async) {
|
||||
var was_async = S.in_async;
|
||||
S.in_async = false;
|
||||
S.in_async = async;
|
||||
var was_funarg = S.in_funarg;
|
||||
S.in_funarg = S.in_function;
|
||||
var argnames = exprs.map(to_funarg);
|
||||
var rest = exprs.rest || null;
|
||||
if (rest) rest = to_funarg(rest);
|
||||
S.in_funarg = was_funarg;
|
||||
expect("=>");
|
||||
var body, value;
|
||||
@@ -1129,10 +1133,10 @@ function parse($TEXT, options) {
|
||||
S.in_loop = loop;
|
||||
S.labels = labels;
|
||||
S.in_async = was_async;
|
||||
return new AST_Arrow({
|
||||
return new (async ? AST_AsyncArrow : AST_Arrow)({
|
||||
start: start,
|
||||
argnames: argnames,
|
||||
rest: exprs.rest || null,
|
||||
rest: rest,
|
||||
body: body,
|
||||
value: value,
|
||||
end: prev(),
|
||||
@@ -1431,16 +1435,9 @@ function parse($TEXT, options) {
|
||||
}
|
||||
unexpected();
|
||||
}
|
||||
var ctor;
|
||||
if (is("name", "async") && is_token(peek(), "keyword", "function")) {
|
||||
if (is("keyword", "function")) {
|
||||
next();
|
||||
ctor = AST_AsyncFunction;
|
||||
} else if (is("keyword", "function")) {
|
||||
ctor = AST_Function;
|
||||
}
|
||||
if (ctor) {
|
||||
next();
|
||||
var func = function_(ctor);
|
||||
var func = function_(AST_Function);
|
||||
func.start = start;
|
||||
func.end = prev();
|
||||
return subscripts(func, allow_calls);
|
||||
@@ -1448,6 +1445,30 @@ function parse($TEXT, options) {
|
||||
if (is("name")) {
|
||||
var sym = _make_symbol(AST_SymbolRef, start);
|
||||
next();
|
||||
if (sym.name == "async") {
|
||||
if (is("keyword", "function")) {
|
||||
next();
|
||||
var func = function_(AST_AsyncFunction);
|
||||
func.start = start;
|
||||
func.end = prev();
|
||||
return subscripts(func, allow_calls);
|
||||
}
|
||||
if (is("name")) {
|
||||
start = S.token;
|
||||
sym = _make_symbol(AST_SymbolRef, start);
|
||||
next();
|
||||
return arrow([ sym ], start, true);
|
||||
}
|
||||
if (is("punc", "(")) {
|
||||
var call = subscripts(sym, allow_calls);
|
||||
if (!is("punc", "=>")) return call;
|
||||
var args = call.args;
|
||||
if (args[args.length - 1] instanceof AST_Spread) {
|
||||
args.rest = args.pop().expression;
|
||||
}
|
||||
return arrow(args, start, true);
|
||||
}
|
||||
}
|
||||
return is("punc", "=>") ? arrow([ sym ], start) : subscripts(sym, allow_calls);
|
||||
}
|
||||
if (ATOMIC_START_TOKEN[S.token.type]) {
|
||||
|
||||
Reference in New Issue
Block a user