1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

LibJS: Be more strict about the lhs of a for in/of loop

This is not entirely correct as really Object- and ArrayExpressions are
not allowed but that requires a bigger refactoring of for statement
parsing.
This commit is contained in:
davidot 2021-07-28 23:09:57 +02:00 committed by Linus Groh
parent 106f9e30d7
commit 085c7df895

View file

@ -2516,8 +2516,19 @@ NonnullRefPtr<Statement> Parser::parse_for_in_of_statement(NonnullRefPtr<ASTNode
syntax_error("need exactly one variable declaration in for..in/of");
else if (declarations.first().init() != nullptr)
syntax_error("variable initializer not allowed in for..in/of");
} else if (!lhs->is_identifier() && !is<ObjectExpression>(*lhs) && !is<MemberExpression>(*lhs) && !is<ArrayExpression>(*lhs)) {
syntax_error(String::formatted("Invalid left-hand side in for-loop ('{}')", lhs->class_name()));
}
auto in_or_of = consume();
if (in_or_of.type() != TokenType::In) {
if (is<MemberExpression>(*lhs)) {
auto& member = static_cast<MemberExpression const&>(*lhs);
if (member.object().is_identifier() && static_cast<Identifier const&>(member.object()).string() == "let"sv)
syntax_error("For of statement may not start with let.");
}
}
auto rhs = parse_expression(0);
consume(TokenType::ParenClose);