LibJS: Fix scope detection for ids in default function params

This change fixes an issue where identifiers used in default function
parameters were being "registered" in the function's parent scope
instead of its own scope. This bug resulted in incorrectly detected
local variables. (Variables used in the default function parameter
expression should be considered 'captured by nested function'.)

To resolve this issue, the function scope is now created before parsing
function parameters. Since function parameters can no longer be passed
in the constructor, a setter function has been introduced to set them
later, when they are ready.
This commit is contained in:
Aliaksandr Kalenik 2023-07-07 23:14:03 +02:00 committed by Andreas Kling
parent 996c020b0d
commit 2f85faef0f
Notes: sideshowbarker 2024-07-17 22:41:14 +09:00
4 changed files with 164 additions and 114 deletions

View file

@ -141,3 +141,13 @@ test("parameter with an object default value", () => {
expect(arrowFunc()).toBe("bar");
expect(arrowFunc({ foo: "baz" })).toBe("baz");
});
test("use variable as default function parameter", () => {
let a = 1;
function func(param = a) {
return param;
}
expect(func()).toBe(a);
});