ladybird/Userland/Libraries/LibJS/Tests/syntax/coalesce-logic-expression-mixing.js
Anonymous 602190f66f LibJS: Fix mixing of logical and coalescing operators
The same expression is not allowed to contain both the
logical && and || operators, and the coalescing ?? operator.

This patch changes how "forbidden" tokens are handled, using a
finite set instead of an Vector. This supports much more efficient
merging of the forbidden tokens when propagating forward, and
allowing the return of forbidden tokens to parent contexts.
2022-02-16 11:18:41 +00:00

28 lines
1.2 KiB
JavaScript

test("mixing coalescing and logical operators isn't allowed", () => {
expect("if (0) a ?? b || c").not.toEval();
expect("if (0) a ?? b && c").not.toEval();
expect("if (0) a ?? b * c || d").not.toEval();
expect("if (0) a ?? b * c && d").not.toEval();
expect("if (0) a && b ?? c").not.toEval();
expect("if (0) a || b ?? c").not.toEval();
expect("if (0) a && b * c ?? d").not.toEval();
expect("if (0) a || b * c ?? d").not.toEval();
});
test("mixing coalescing and logical operators with parens", () => {
expect("if (0) a ?? (b || c)").toEval();
expect("if (0) (a ?? b) && c").toEval();
expect("if (0) a ?? (b * c || d)").toEval();
expect("if (0) (a ?? b * c) && d").toEval();
expect("if (0) a && (b ?? c)").toEval();
expect("if (0) (a || b) ?? c").toEval();
expect("if (0) a && (b * c) ?? d").not.toEval();
expect("if (0) a || (b * c) ?? d").not.toEval();
});
test("mixing coalescing and logical operators when 'in' isn't allowed", () => {
expect("for (a ?? b || c in a; false;);").not.toEval();
expect("for (a ?? b && c in a; false;);").not.toEval();
expect("for (a || b ?? c in a; false;);").not.toEval();
expect("for (a && b ?? c in a; false;);").not.toEval();
});