ladybird/Tests/LibJS/Runtime/destructured-parameter-locals.js
Andreas Kling 938df563bb Tests/LibJS: Add tests for destructured parameter locals
Add both runtime (test-js) and AST dump (test-js-ast) coverage for
destructured parameter bindings. The runtime tests verify that functions
with destructured parameters produce correct results. The AST tests
show that destructured parameter bindings currently receive no local
index annotations, unlike plain parameters which get [argument:N].
2026-02-10 02:05:20 +01:00

56 lines
1.3 KiB
JavaScript

test("array destructured parameter produces correct locals", () => {
function f([x, y]) {
return x + y;
}
expect(f([10, 20])).toBe(30);
});
test("object destructured parameter produces correct locals", () => {
function f({ a, b }) {
return a * b;
}
expect(f({ a: 3, b: 7 })).toBe(21);
});
test("mixed plain and destructured parameters", () => {
function f(first, { x }, ...rest) {
return first + x + rest.length;
}
expect(f(100, { x: 20 }, "a", "b")).toBe(122);
});
test("nested destructured parameters", () => {
function f({ a: { b } }) {
return b;
}
expect(f({ a: { b: 42 } })).toBe(42);
});
test("destructured parameter with default value", () => {
function f({ x = 10 } = {}) {
return x;
}
expect(f()).toBe(10);
expect(f({ x: 5 })).toBe(5);
});
test("destructured parameter with aliased name", () => {
function f({ a: renamed }) {
return renamed;
}
expect(f({ a: 99 })).toBe(99);
});
test("array destructured with rest element", () => {
function f([first, ...rest]) {
return first + rest.length;
}
expect(f([10, 20, 30])).toBe(12);
});
test("multiple destructured parameters", () => {
function f({ a }, [b, c]) {
return a + b + c;
}
expect(f({ a: 1 }, [2, 3])).toBe(6);
});