mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
LibJS: Throw a syntax error when an identifier is a reserved word
From the specification: It is a Syntax Error if StringValue of IdentifierName is the same String value as the StringValue of any ReservedWord except for yield or await. It is a Syntax Error if this phrase is contained in strict mode code and the StringValue of IdentifierName is: "implements", "interface", "let", "package", "private", "protected", "public", "static", or "yield".
This commit is contained in:
parent
d6df955305
commit
70697a5999
Notes:
sideshowbarker
2024-07-18 12:08:41 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/70697a59992 Pull-request: https://github.com/SerenityOS/serenity/pull/8103
1 changed files with 12 additions and 1 deletions
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
#include "Parser.h"
|
||||
#include <AK/Array.h>
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/ScopeGuard.h>
|
||||
|
@ -2251,12 +2252,22 @@ void Parser::consume_or_insert_semicolon()
|
|||
expected("Semicolon");
|
||||
}
|
||||
|
||||
static constexpr AK::Array<StringView, 38> reserved_words = { "await", "break", "case", "catch", "class", "const", "continue", "debugger", "default", "delete", "do", "else", "enum", "export", "extends", "false", "finally", "for", "function", "if", "import", "in", "instanceof", "new", "null", "return", "super", "switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with", "yield" };
|
||||
static constexpr AK::Array<StringView, 9> strict_reserved_words = { "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield" };
|
||||
|
||||
Token Parser::consume(TokenType expected_type)
|
||||
{
|
||||
if (!match(expected_type)) {
|
||||
expected(Token::name(expected_type));
|
||||
}
|
||||
return consume();
|
||||
auto token = consume();
|
||||
if (expected_type == TokenType::Identifier) {
|
||||
if (any_of(reserved_words.begin(), reserved_words.end(), [&](auto const& word) { return word == token.value(); }))
|
||||
syntax_error("Identifier must not be a reserved word");
|
||||
if (m_parser_state.m_strict_mode && any_of(strict_reserved_words.begin(), strict_reserved_words.end(), [&](auto const& word) { return word == token.value(); }))
|
||||
syntax_error("Identifier must not be a class-related reserved word in strict mode");
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
Token Parser::consume_and_validate_numeric_literal()
|
||||
|
|
Loading…
Reference in a new issue