Files
ladybird/Tests/LibJS/Runtime/invalid-lhs-in-assignment.js
Andreas Kling c55418bc60 LibJS: Fix AssignmentTargetType for NewExpression and strict mode
Fix the static semantics for AssignmentTargetType in both the C++ and
Rust parsers:

- NewExpression is never a valid assignment target. Previously, the C++
  parser's is<CallExpression> check matched NewExpression since it
  inherits from CallExpression in our AST. Add explicit
  !is<NewExpression> guards everywhere.

- CallExpression as assignment target is only valid in non-strict mode
  (web-compat "runtime error for function call assignment targets").
  Pass strict_mode to is_simple_assignment_target and reject call
  expressions in strict mode for assignment, compound assignment,
  prefix/postfix update, and for-in/of LHS positions.

- Parenthesized ObjectLiteral/ArrayLiteral (e.g. `({}) = 1`) must not
  be treated as destructuring patterns. Track whether the primary
  expression was parenthesized and skip binding pattern synthesis.

Update existing tests that were testing incorrect behavior:
- `'use strict'; foo() = 'foo'` is now correctly a SyntaxError
- for-in/of with call expression LHS: use toEval() instead of
  toEvalTo() (which runs eval inside a class method, i.e. strict mode)
2026-03-13 13:08:22 -05:00

63 lines
1.9 KiB
JavaScript

test("assignment to function call", () => {
expect(() => {
function foo() {}
foo() = "foo";
}).toThrowWithMessage(ReferenceError, "Invalid left-hand side in assignment");
});
test("Postfix operator after function call", () => {
expect(() => {
function foo() {}
foo()++;
}).toThrow(ReferenceError);
});
test("assignment to function call in strict mode is a SyntaxError", () => {
expect("'use strict'; foo() = 'foo'").not.toEval();
});
test("assignment to inline function call", () => {
expect(() => {
(function () {})() = "foo";
}).toThrowWithMessage(ReferenceError, "Invalid left-hand side in assignment");
});
test("assignment to invalid LHS is syntax error", () => {
expect("1 += 1").not.toEval();
expect("1 -= 1").not.toEval();
expect("1 *= 1").not.toEval();
expect("1 /= 1").not.toEval();
expect("1 %= 1").not.toEval();
expect("1 **= 1").not.toEval();
expect("1 &= 1").not.toEval();
expect("1 |= 1").not.toEval();
expect("1 ^= 1").not.toEval();
expect("1 <<= 1").not.toEval();
expect("1 >>= 1").not.toEval();
expect("1 >>>= 1").not.toEval();
expect("1 = 1").not.toEval();
expect("1 &&= 1").not.toEval();
expect("1 ||= 1").not.toEval();
expect("1 ??= 1").not.toEval();
});
test("assignment to call LHS is only syntax error for new operators", () => {
expect("f() += 1").toEval();
expect("f() -= 1").toEval();
expect("f() *= 1").toEval();
expect("f() /= 1").toEval();
expect("f() %= 1").toEval();
expect("f() **= 1").toEval();
expect("f() &= 1").toEval();
expect("f() |= 1").toEval();
expect("f() ^= 1").toEval();
expect("f() <<= 1").toEval();
expect("f() >>= 1").toEval();
expect("f() >>>= 1").toEval();
expect("f() = 1").toEval();
expect("f() &&= 1").not.toEval();
expect("f() ||= 1").not.toEval();
expect("f() ??= 1").not.toEval();
});