mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-03 04:52:06 +02:00
LibJS: Implement parsing and execution of optional chains
This commit is contained in:
committed by
Linus Groh
parent
4f7e14e0aa
commit
72ddaa31e3
Notes:
sideshowbarker
2024-07-18 03:58:17 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/72ddaa31e3b Pull-request: https://github.com/SerenityOS/serenity/pull/10026 Reviewed-by: https://github.com/davidot Reviewed-by: https://github.com/linusg ✅
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();
|
||||
});
|
||||
Reference in New Issue
Block a user