From 81312986fe07496e3d35e1d7d10d20d9aba8d1fb Mon Sep 17 00:00:00 2001 From: davidot Date: Sun, 19 Dec 2021 20:19:15 +0100 Subject: [PATCH] LibJS: Disallow async generator functions called 'await' or 'yield' --- Userland/Libraries/LibJS/Parser.cpp | 3 +++ Userland/Libraries/LibJS/Tests/syntax/async-await.js | 3 +++ Userland/Libraries/LibJS/Tests/syntax/async-generators.js | 3 +++ Userland/Libraries/LibJS/Tests/syntax/generators.js | 3 +++ 4 files changed, 12 insertions(+) diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index 30749baa65..91e643e8fc 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -2476,6 +2476,9 @@ NonnullRefPtr Parser::parse_function_node(u8 parse_options) check_identifier_name_for_assignment_validity(name); + if (function_kind == FunctionKind::AsyncGenerator && (name == "await"sv || name == "yield"sv)) + syntax_error(String::formatted("async generator function is not allowed to be called '{}'", name)); + if (m_state.in_class_static_init_block && name == "await"sv) syntax_error("'await' is a reserved word"); } diff --git a/Userland/Libraries/LibJS/Tests/syntax/async-await.js b/Userland/Libraries/LibJS/Tests/syntax/async-await.js index 652f0e810e..d86c9eea8e 100644 --- a/Userland/Libraries/LibJS/Tests/syntax/async-await.js +++ b/Userland/Libraries/LibJS/Tests/syntax/async-await.js @@ -4,6 +4,9 @@ describe("parsing freestanding async functions", () => { // Although it does not create an async function it is valid. expect(`async function foo() {}`).toEval(); + + expect(`async function await() {}`).toEval(); + expect(`async function yield() {}`).toEval(); }); test("await expression", () => { expect(`async function foo() { await bar(); }`).toEval(); diff --git a/Userland/Libraries/LibJS/Tests/syntax/async-generators.js b/Userland/Libraries/LibJS/Tests/syntax/async-generators.js index 68c9550dbb..ce30721efd 100644 --- a/Userland/Libraries/LibJS/Tests/syntax/async-generators.js +++ b/Userland/Libraries/LibJS/Tests/syntax/async-generators.js @@ -4,6 +4,9 @@ describe("parsing freestanding generators", () => { expect(`async function *foo() {}`).toEval(); expect(`async function *foo() {}`).toEval(); + + expect(`async function *await() {}`).not.toEval(); + expect(`async function *yield() {}`).not.toEval(); }); test("yield & await expression", () => { expect(`async function* foo() { yield; await 1; }`).toEval(); diff --git a/Userland/Libraries/LibJS/Tests/syntax/generators.js b/Userland/Libraries/LibJS/Tests/syntax/generators.js index c70bc6cf72..010a1c78e5 100644 --- a/Userland/Libraries/LibJS/Tests/syntax/generators.js +++ b/Userland/Libraries/LibJS/Tests/syntax/generators.js @@ -4,6 +4,9 @@ describe("parsing freestanding generators", () => { expect(`function *foo() {}`).toEval(); expect(`function *foo() {}`).toEval(); + + expect(`function *await() {}`).toEval(); + expect(`function *yield() {}`).toEval(); }); test("yield expression", () => { expect(`function* foo() { yield; }`).toEval();