mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:07:34 +00:00
LibJS: Implement parsing and execution of optional chains
This commit is contained in:
parent
4f7e14e0aa
commit
72ddaa31e3
6 changed files with 318 additions and 42 deletions
40
Userland/Libraries/LibJS/Tests/syntax/optional-chaining.js
Normal file
40
Userland/Libraries/LibJS/Tests/syntax/optional-chaining.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
test("parse optional-chaining", () => {
|
||||
expect(`a?.b`).toEval();
|
||||
expect(`a?.4:.5`).toEval();
|
||||
expect(`a?.[b]`).toEval();
|
||||
expect(`a?.b[b]`).toEval();
|
||||
expect(`a?.b(c)`).toEval();
|
||||
expect(`a?.b?.(c, d)`).toEval();
|
||||
expect(`a?.b?.()`).toEval();
|
||||
expect("a?.b``").not.toEval();
|
||||
expect("a?.b?.``").not.toEval();
|
||||
expect("new Foo?.bar").not.toEval();
|
||||
expect("new (Foo?.bar)").toEval();
|
||||
// FIXME: This should pass.
|
||||
// expect("(new Foo)?.bar").toEval();
|
||||
});
|
||||
|
||||
test("evaluate optional-chaining", () => {
|
||||
for (let nullishObject of [null, undefined]) {
|
||||
expect((() => nullishObject?.b)()).toBeUndefined();
|
||||
}
|
||||
|
||||
expect(
|
||||
(() => {
|
||||
let a = {};
|
||||
return a?.foo?.bar?.baz;
|
||||
})()
|
||||
).toBeUndefined();
|
||||
|
||||
expect(
|
||||
(() => {
|
||||
let a = { foo: { bar: () => 42 } };
|
||||
return `${a?.foo?.bar?.()}-${a?.foo?.baz?.()}`;
|
||||
})()
|
||||
).toBe("42-undefined");
|
||||
|
||||
expect(() => {
|
||||
let a = { foo: { bar: () => 42 } };
|
||||
return a.foo?.baz.nonExistentProperty;
|
||||
}).toThrow();
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue