mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
LibJS: Replace the verify in private identifier with a syntax error
Since sometimes expressions are parsed without checking we can hit this expression without it being followed by an 'in'.
This commit is contained in:
parent
b7c7d54167
commit
c2ebaa9d87
Notes:
sideshowbarker
2024-07-17 23:17:54 +09:00
Author: https://github.com/davidot Commit: https://github.com/SerenityOS/serenity/commit/c2ebaa9d87f Pull-request: https://github.com/SerenityOS/serenity/pull/11088 Issue: https://github.com/SerenityOS/serenity/issues/11078 Reviewed-by: https://github.com/IdanHo Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/linusg ✅
2 changed files with 15 additions and 2 deletions
|
@ -1439,7 +1439,8 @@ Parser::PrimaryExpressionParseResult Parser::parse_primary_expression()
|
|||
goto read_as_identifier;
|
||||
return { parse_await_expression() };
|
||||
case TokenType::PrivateIdentifier:
|
||||
VERIFY(next_token().type() == TokenType::In);
|
||||
if (next_token().type() != TokenType::In)
|
||||
syntax_error("Cannot have a private identifier in expression if not followed by 'in'");
|
||||
if (!is_private_identifier_valid())
|
||||
syntax_error(String::formatted("Reference to undeclared private field or method '{}'", m_state.current_token.value()));
|
||||
return { create_ast_node<PrivateIdentifier>({ m_state.current_token.filename(), rule_start.position(), position() }, consume().value()) };
|
||||
|
@ -3483,7 +3484,7 @@ bool Parser::match_expression() const
|
|||
|| type == TokenType::TemplateLiteralStart
|
||||
|| type == TokenType::NullLiteral
|
||||
|| match_identifier()
|
||||
|| (type == TokenType::PrivateIdentifier && next_token().type() == TokenType::In)
|
||||
|| type == TokenType::PrivateIdentifier
|
||||
|| type == TokenType::Await
|
||||
|| type == TokenType::New
|
||||
|| type == TokenType::Class
|
||||
|
|
|
@ -96,6 +96,18 @@ test("slash after private identifier is treated as division", () => {
|
|||
expect(A.getDivided()).toBe(2);
|
||||
});
|
||||
|
||||
test("private identifier not followed by 'in' throws", () => {
|
||||
expect(`class A { #field = 2; method() { return #field instanceof 1; }}`).not.toEval();
|
||||
expect(`class A { #field = 2; method() { return #field < 1; }}`).not.toEval();
|
||||
expect(`class A { #field = 2; method() { return #field + 1; }}`).not.toEval();
|
||||
expect(`class A { #field = 2; method() { return #field ** 1; }}`).not.toEval();
|
||||
expect(`class A { #field = 2; method() { return !#field; } }`).not.toEval();
|
||||
expect(`class A { #field = 2; method() { return ~#field; } }`).not.toEval();
|
||||
expect(`class A { #field = 2; method() { return ++#field; } }`).not.toEval();
|
||||
|
||||
expect(`class A { #field = 2; method() { return #field in 1; }}`).toEval();
|
||||
});
|
||||
|
||||
test("cannot have static and non static field with the same description", () => {
|
||||
expect("class A { static #simple; #simple; }").not.toEval();
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue