1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 17:58:12 +00:00

LibJS: Allow Unicode escape sequences in identifiers

For example, "property.br\u{64}wn" should resolve to "property.brown".

To support this behavior, this commit changes the Token class to hold
both the evaluated identifier name and a view into the original source
for the unevaluated name. There are some contexts in which identifiers
are not allowed to contain Unicode escape sequences; for example, export
statements of the form "export {} from foo.js" forbid escapes in the
identifier "from".

The test file is added to .prettierignore because prettier will replace
all escaped Unicode sequences with their unescaped value.
This commit is contained in:
Timothy Flynn 2021-08-18 16:34:25 -04:00 committed by Andreas Kling
parent c5b5c779ff
commit 1259dc3623
7 changed files with 163 additions and 54 deletions

View file

@ -210,7 +210,6 @@ constexpr OperatorPrecedenceTable g_operator_precedence;
Parser::ParserState::ParserState(Lexer l, Program::Type program_type)
: lexer(move(l))
, current_token(TokenType::Invalid, {}, {}, {}, {}, 0, 0, 0)
{
if (program_type == Program::Type::Module)
lexer.disallow_html_comments();
@ -680,7 +679,7 @@ NonnullRefPtr<ClassExpression> Parser::parse_class_expression(bool expect_class_
if (match_property_key()) {
StringView name;
if (!is_generator && m_state.current_token.value() == "static"sv) {
if (!is_generator && m_state.current_token.original_value() == "static"sv) {
if (match(TokenType::Identifier)) {
consume();
is_static = true;
@ -2524,7 +2523,7 @@ NonnullRefPtr<Statement> Parser::parse_for_statement()
{
auto rule_start = push_start();
auto match_for_in_of = [&]() {
return match(TokenType::In) || (match(TokenType::Identifier) && m_state.current_token.value() == "of");
return match(TokenType::In) || (match(TokenType::Identifier) && m_state.current_token.original_value() == "of");
};
consume(TokenType::For);
@ -3019,7 +3018,7 @@ NonnullRefPtr<ImportStatement> Parser::parse_import_statement(Program& program)
};
auto match_as = [&] {
return match(TokenType::Identifier) && m_state.current_token.value() == "as"sv;
return match(TokenType::Identifier) && m_state.current_token.original_value() == "as"sv;
};
bool continue_parsing = true;
@ -3134,11 +3133,15 @@ NonnullRefPtr<ExportStatement> Parser::parse_export_statement(Program& program)
syntax_error("Cannot use export statement outside a module");
auto match_as = [&] {
return match(TokenType::Identifier) && m_state.current_token.value() == "as"sv;
return match(TokenType::Identifier) && m_state.current_token.original_value() == "as"sv;
};
auto match_from = [&] {
return match(TokenType::Identifier) && m_state.current_token.value() == "from"sv;
return match(TokenType::Identifier) && m_state.current_token.original_value() == "from"sv;
};
auto match_default = [&] {
return match(TokenType::Default) && m_state.current_token.original_value() == "default"sv;
};
consume(TokenType::Export);
@ -3158,7 +3161,7 @@ NonnullRefPtr<ExportStatement> Parser::parse_export_statement(Program& program)
RefPtr<ASTNode> expression = {};
if (match(TokenType::Default)) {
if (match_default()) {
auto default_position = position();
consume(TokenType::Default);