mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
LibJS: Check that 'let' is followed by declaration before matching it
Since 'let' is a valid variable name (in non-strict mode) let may not be the start of a declaration but just an identifier.
This commit is contained in:
parent
179c48e1a4
commit
5f344f7ca3
2 changed files with 36 additions and 4 deletions
|
@ -2748,18 +2748,49 @@ bool Parser::match_export_or_import() const
|
||||||
|| type == TokenType::Import;
|
|| type == TokenType::Import;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Parser::match_declaration() const
|
bool Parser::match_declaration()
|
||||||
{
|
{
|
||||||
auto type = m_state.current_token.type();
|
auto type = m_state.current_token.type();
|
||||||
|
|
||||||
|
if (type == TokenType::Let && !m_state.strict_mode) {
|
||||||
|
return try_match_let_declaration();
|
||||||
|
}
|
||||||
|
|
||||||
return type == TokenType::Function
|
return type == TokenType::Function
|
||||||
|| type == TokenType::Class
|
|| type == TokenType::Class
|
||||||
|| type == TokenType::Const
|
|| type == TokenType::Const
|
||||||
|| type == TokenType::Let;
|
|| type == TokenType::Let;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Parser::match_variable_declaration() const
|
bool Parser::try_match_let_declaration()
|
||||||
|
{
|
||||||
|
VERIFY(m_state.current_token.type() == TokenType::Let);
|
||||||
|
|
||||||
|
save_state();
|
||||||
|
|
||||||
|
ScopeGuard state_rollback = [&] {
|
||||||
|
load_state();
|
||||||
|
};
|
||||||
|
|
||||||
|
consume(TokenType::Let);
|
||||||
|
|
||||||
|
if (match_identifier_name() && m_state.current_token.value() != "in"sv)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (match(TokenType::CurlyOpen) || match(TokenType::BracketOpen))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Parser::match_variable_declaration()
|
||||||
{
|
{
|
||||||
auto type = m_state.current_token.type();
|
auto type = m_state.current_token.type();
|
||||||
|
|
||||||
|
if (type == TokenType::Let && !m_state.strict_mode) {
|
||||||
|
return try_match_let_declaration();
|
||||||
|
}
|
||||||
|
|
||||||
return type == TokenType::Var
|
return type == TokenType::Var
|
||||||
|| type == TokenType::Let
|
|| type == TokenType::Let
|
||||||
|| type == TokenType::Const;
|
|| type == TokenType::Const;
|
||||||
|
|
|
@ -159,8 +159,9 @@ private:
|
||||||
bool match_secondary_expression(const Vector<TokenType>& forbidden = {}) const;
|
bool match_secondary_expression(const Vector<TokenType>& forbidden = {}) const;
|
||||||
bool match_statement() const;
|
bool match_statement() const;
|
||||||
bool match_export_or_import() const;
|
bool match_export_or_import() const;
|
||||||
bool match_declaration() const;
|
bool match_declaration();
|
||||||
bool match_variable_declaration() const;
|
bool try_match_let_declaration();
|
||||||
|
bool match_variable_declaration();
|
||||||
bool match_identifier() const;
|
bool match_identifier() const;
|
||||||
bool match_identifier_name() const;
|
bool match_identifier_name() const;
|
||||||
bool match_property_key() const;
|
bool match_property_key() const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue