Files
ladybird/Tests/LibJS/Runtime/functions/function-duplicate-parameters.js
Andreas Kling 3efd1a1bb5 LibJS: Reject duplicate params across destructuring patterns in C++
The C++ parser was not rejecting duplicate parameter names across
destructuring patterns in non-simple parameter lists. For example,
`function f({ bar, ...a }, { bar, ...b }) {}` was accepted despite
being a syntax error per spec.

The existing inline duplicate check only ran for identifier parameters,
missing the case where both parameters are binding patterns. Add a
post-parse pass that collects all bound names and checks for duplicates
when the parameter list is non-simple (or in strict mode/arrows).

Also fix existing tests that relied on the incorrect behavior and add
new test coverage for destructuring duplicate detection.
2026-03-19 09:43:11 +01:00

61 lines
1.4 KiB
JavaScript

test("function with duplicate parameter names", () => {
function foo(bar, _, bar) {
return bar;
}
expect(foo(1, 2, 3)).toBe(3);
});
test("syntax errors", () => {
// Regular function in strict mode
expect(`
"use strict";
function foo(bar, bar) {}
`).not.toEval();
// Arrow function in strict mode
expect(`
"use strict";
const foo = (bar, bar) => {};
`).not.toEval();
// Arrow function in non-strict mode
expect(`
const foo = (bar, bar) => {};
`).not.toEval();
// Regular function with rest parameter
expect(`
function foo(bar, ...bar) {}
`).not.toEval();
// Arrow function with rest parameter
expect(`
const foo = (bar, ...bar) => {};
`).not.toEval();
// Regular function with default parameter
expect(`
function foo(bar, bar = 1) {}
`).not.toEval();
// Arrow function with default parameter
expect(`
const foo = (bar, bar = 1) => {};
`).not.toEval();
// Duplicate across destructuring parameters
expect(`
function foo({ bar }, { bar }) {}
`).not.toEval();
// Duplicate between identifier and destructuring parameter
expect(`
function foo(bar, { bar }) {}
`).not.toEval();
// Duplicate between destructuring and identifier parameter
expect(`
function foo({ bar }, bar) {}
`).not.toEval();
});