1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:58:12 +00:00

LibJS: Syntax error for a unary expression followed by exponentiation

This change makes LibJS correctly report a syntax error when a unary
expression is followed by exponentiation, as the spec requires.
Apparently this is due to that expression being ambiguous ordering.

Strangely this check does not seem to apply in the same way for '++' and
'--' for reasons that I don't fully understand. For example

```
let x = 5;
++x ** 2
```

Since `--5` and `++5` on it's own results in a syntax error anyway, it
seems we do not need to perform this exponentiation check in those
places.

Diff Tests:
    +6     -6 
This commit is contained in:
Shannon Booth 2023-09-17 09:04:14 +12:00 committed by Andreas Kling
parent 5e7a82a853
commit 2d8b2328fd
3 changed files with 50 additions and 2 deletions

View file

@ -14,8 +14,26 @@ test("exponentiation with negatives", () => {
expect(2 ** -3).toBe(0.125);
expect((-2) ** 3).toBe(-8);
// FIXME: This should fail :)
// expect("-2 ** 3").not.toEval();
expect("-2 ** 3").not.toEval();
});
test("exponentiation with PlusPlus and MinusMinus", () => {
let value = 5;
// prettier-ignore
expect(++value ** 2).toBe(36);
value = 5;
expect((++value) ** 2).toBe(36);
value = 5;
// prettier-ignore
expect(--value ** 2).toBe(16);
value = 5;
expect((--value) ** 2).toBe(16);
expect("++5 ** 2").not.toEval();
expect("--5 ** 2").not.toEval();
});
test("exponentiation with non-numeric primitives", () => {
@ -50,3 +68,10 @@ test("exponentiation with infinities", () => {
expect((-Infinity) ** 0).toBe(1);
expect((-Infinity) ** 1).toBe(-Infinity);
});
test("unary expression before exponentiation with brackets", () => {
expect((!1) ** 2).toBe(0);
expect((~5) ** 2).toBe(36);
expect((+5) ** 2).toBe(25);
expect((-5) ** 2).toBe(25);
});