diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp index f5d1dccab6..30749baa65 100644 --- a/Userland/Libraries/LibJS/Parser.cpp +++ b/Userland/Libraries/LibJS/Parser.cpp @@ -1548,6 +1548,9 @@ NonnullRefPtr Parser::parse_unary_prefixed_expression() return create_ast_node({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Typeof, parse_expression(precedence, associativity)); case TokenType::Void: consume(); + // FIXME: This check is really hiding the fact that we don't deal with different expressions correctly. + if (match(TokenType::Yield) && m_state.in_generator_function_context) + syntax_error("'yield' is not an identifier in generator function context"); return create_ast_node({ m_state.current_token.filename(), rule_start.position(), position() }, UnaryOp::Void, parse_expression(precedence, associativity)); case TokenType::Delete: { consume(); diff --git a/Userland/Libraries/LibJS/Tests/operators/void-basic.js b/Userland/Libraries/LibJS/Tests/operators/void-basic.js index a23becae96..5992af1dc4 100644 --- a/Userland/Libraries/LibJS/Tests/operators/void-basic.js +++ b/Userland/Libraries/LibJS/Tests/operators/void-basic.js @@ -12,3 +12,15 @@ test("basic functionality", () => { expect(void (() => "hello friends")()).toBeUndefined(); expect((() => void "hello friends")()).toBeUndefined(); }); + +describe("errors", () => { + test("treats yield in generator as non variable", () => { + expect("function f() { void yield; }").toEval(); + expect("async function f() { void yield; }").toEval(); + expect("function *f() { void yield; }").not.toEval(); + expect("async function *f() { void yield; }").not.toEval(); + + expect("class C { *f() { void yield; } }").not.toEval(); + expect("var obj = { async function *f() { void yield; } }").not.toEval(); + }); +});