Ver código fonte

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.
davidot 4 anos atrás
pai
commit
085c7df895
1 arquivos alterados com 11 adições e 0 exclusões
  1. 11 0
      Userland/Libraries/LibJS/Parser.cpp

+ 11 - 0
Userland/Libraries/LibJS/Parser.cpp

@@ -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);