Truly independent web browser
LibJS: Allow async of arrow functions inside the head of for await
Shannon Bootha992901d1d70
diff --git a/Libraries/LibJS/Parser/Rust/src/base.rs b/Libraries/LibJS/Parser/Rust/src/base.rsindex eb6d958d254..6e27fa1610e 100644--- a/Libraries/LibJS/Parser/Rust/src/base.rs+++ b/Libraries/LibJS/Parser/Rust/src/base.rs@@ -367,8 +367,6 @@ pub struct Parser<'a, 'k, U: Unit> {/// Where the expression in the parentheses opened last starts. A function there is likely to be called, but not one/// that starts after the `(` of a list of arguments, which an arrow function usually is.pub(crate) parenthesized_expression_start: u32,- /// Whether this is the left side of `for await`, where `async of` is no arrow function.- pub(crate) for_await_head: bool,/// The state of each name, by its id. A parse of a function again has the states of its reparse context, which/// are all clean when it starts and ends.@@ -627,7 +625,6 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {duplicate_parameter: NONE,last_arrow: 0,parenthesized_expression_start: NONE,- for_await_head: false,names,uses: ZoneVec::with_capacity_in(estimated_uses, zone),decls: ZoneVec::with_capacity_in(estimated_uses / 16, zone),diff --git a/Libraries/LibJS/Parser/Rust/src/expr.rs b/Libraries/LibJS/Parser/Rust/src/expr.rsindex a3e8a7f2237..241df59a944 100644--- a/Libraries/LibJS/Parser/Rust/src/expr.rs+++ b/Libraries/LibJS/Parser/Rust/src/expr.rs@@ -806,23 +806,21 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {if let Some(arrow) = self.skip_arrow_function(start, param_pos) {return arrow;}- if !(self.for_await_head && param == N_OF && !self.tok_escaped) {- self.next();- if self.tok != T::Arrow || self.nl_before {- self.unexpected();- return 0;- }- // 15.9 Async Arrow Function Definitions, https://tc39.es/ecma262/#sec-async-arrow-function-definitions- // AsyncArrowBindingIdentifier[Yield] : BindingIdentifier[?Yield, +Await]- // 13.1.1 Static Semantics: Early Errors, https://tc39.es/ecma262/#sec-identifiers-static-semantics-early-errors- // BindingIdentifier[Yield, Await] : yield- // It is a Syntax Error if this production has a [Yield] parameter.- if param == N_YIELD && self.ctx & ctx::GENERATOR != 0 {- self.fail(param_pos, "'yield' is not allowed as an identifier in this context");- return 0;- }- return self.parse_arrow_single(start, param, param_pos, true, no_in);+ self.next();+ if self.tok != T::Arrow || self.nl_before {+ self.unexpected();+ return 0;+ }+ // 15.9 Async Arrow Function Definitions, https://tc39.es/ecma262/#sec-async-arrow-function-definitions+ // AsyncArrowBindingIdentifier[Yield] : BindingIdentifier[?Yield, +Await]+ // 13.1.1 Static Semantics: Early Errors, https://tc39.es/ecma262/#sec-identifiers-static-semantics-early-errors+ // BindingIdentifier[Yield, Await] : yield+ // It is a Syntax Error if this production has a [Yield] parameter.+ if param == N_YIELD && self.ctx & ctx::GENERATOR != 0 {+ self.fail(param_pos, "'yield' is not allowed as an identifier in this context");+ return 0;}+ return self.parse_arrow_single(start, param, param_pos, true, no_in);}}if self.tok == T::Arrow && can_be_arrow && !self.nl_before {diff --git a/Libraries/LibJS/Parser/Rust/src/stmt.rs b/Libraries/LibJS/Parser/Rust/src/stmt.rsindex cb68593235a..f2504b212c9 100644--- a/Libraries/LibJS/Parser/Rust/src/stmt.rs+++ b/Libraries/LibJS/Parser/Rust/src/stmt.rs@@ -793,13 +793,14 @@ impl<'a: 'k, 'k, U: Unit> Parser<'a, 'k, U> {let init_start = self.tok_start;let saved_cover_error = self.cover_error;self.cover_error = NONE;- self.for_await_head = await_at != NONE;let init = if await_at != NONE {+ // 14.7.5 The for-in, for-of, and for-await-of Statements, https://tc39.es/ecma262/#sec-for-in-and-for-of-statements+ // ForInOfStatement : for await ( [lookahead ≠ let] LeftHandSideExpression of AssignmentExpression ) Statement+ // NB: `async of` at the start of a LeftHandSideExpression is the name `async`.self.parse_left_hand_side(false)} else {self.parse_expression(true, true)};- self.for_await_head = false;let is_of = self.is_contextual(N_OF);if self.tok == T::KIn || is_of {if await_at != NONE && !is_of {diff --git a/Tests/LibJS/Runtime/async-arrow-function-syntax.js b/Tests/LibJS/Runtime/async-arrow-function-syntax.jsindex acb12372f6b..ccec95e989a 100644--- a/Tests/LibJS/Runtime/async-arrow-function-syntax.js+++ b/Tests/LibJS/Runtime/async-arrow-function-syntax.js@@ -6,3 +6,13 @@ test("yield as the parameter of an async arrow function", () => {expect("(function () { return async yield => 1; })").toEval();expect("'use strict'; (function () { return async yield => 1; })").not.toEval();});++test("async of in the head of a for statement", () => {+ expect("async function f(y) { for await (async of y); }").toEval();+ expect("async function f(y) { for await (x[async of => 1] of y); }").toEval();+ expect("async function f(y) { for await (x[(() => { async of => 1; })()] of y); }").toEval();+ expect("async function f(y) { for await ((async of => 1).x of y); }").toEval();+ expect("async function f(y) { for await (async of => 1 of y); }").not.toEval();+ expect("for (async of => 1; false; );").toEval();+ expect("for (async of []);").not.toEval();+});