소스 검색

LibJS: Make assignment to CallExpression a syntax error in strict mode

Linus Groh 4 년 전
부모
커밋
f4d0babd5d
2개의 변경된 파일6개의 추가작업 그리고 0개의 파일을 삭제
  1. 2 0
      Libraries/LibJS/Parser.cpp
  2. 4 0
      Libraries/LibJS/Tests/invalid-lhs-in-assignment.js

+ 2 - 0
Libraries/LibJS/Parser.cpp

@@ -1129,6 +1129,8 @@ NonnullRefPtr<AssignmentExpression> Parser::parse_assignment_expression(Assignme
         auto name = static_cast<const Identifier&>(*lhs).string();
         if (name == "eval" || name == "arguments")
             syntax_error(String::formatted("'{}' cannot be assigned to in strict mode code", name));
+    } else if (m_parser_state.m_strict_mode && lhs->is_call_expression()) {
+        syntax_error("Cannot assign to function call");
     }
     return create_ast_node<AssignmentExpression>(assignment_op, move(lhs), parse_expression(min_precedence, associativity));
 }

+ 4 - 0
Libraries/LibJS/Tests/invalid-lhs-in-assignment.js

@@ -5,6 +5,10 @@ test("assignment to function call", () => {
     }).toThrowWithMessage(ReferenceError, "Invalid left-hand side in assignment");
 });
 
+test("assignment to function call in strict mode", () => {
+    expect("'use strict'; foo() = 'foo'").not.toEval();
+});
+
 test("assignment to inline function call", () => {
     expect(() => {
         (function () {})() = "foo";