Wrong private fields scope in class computed keys that contain another class
Categories
(Core :: JavaScript Engine, task)
Tracking
()
Tracking | Status | |
---|---|---|
firefox126 | --- | fixed |
People
(Reporter: nicolo.ribaudo, Assigned: anba)
Details
Attachments
(1 file)
Code:
let capturedPrivateAccess;
class A {
static #x = 42;
static [(class {}, capturedPrivateAccess = () => A.#x)];
}
console.log(capturedPrivateAccess());
This should log 42, but it throws SyntaxError: reference to undeclared private field or method #x.
We noticed it because Babel generates a similar pattern when transpiling decorators, and the generated code doesn't work in Firefox (https://github.com/babel/babel/issues/16379).
As a workaround, we are probably going to wrap the computed key in an arrow function, as this code works:
let capturedPrivateAccess;
class A {
static #x = 42;
static [(() => (class {}, capturedPrivateAccess = () => A.#x))()];
}
console.log(capturedPrivateAccess());
Comment 1•10 months ago
|
||
I took a quick peek at this. Here's an even more reduced version:
class A {
static #x = 42;
static [class {}];
}
I'm not an expert here, but it looks like there's something going wrong with used name tracking and nested classes. When we parse the inner class, isInClass
is false for some reason.
Looks like Matt is taking the week off. Arai, do you know how this is supposed to work, or should we just wait for Matt to get back?
Assignee | ||
Comment 2•10 months ago
|
||
SharedContext::inClass()
returns whether the current SharedContext
is within
a class definition. A class definition itself doesn't create a new SharedContext
,
so an inner nested class definition within a computed property key of an outer
class definition was treated as if it wasn't nested within a class definition. This
led to reporting a SyntaxError when checking for unbound private names.
Calling ParseContext::findInnermostStatement()
in addition to SharedContext::inClass()
fixes this issue. (We need to keep SharedContext::inClass()
to correctly handle
lazily parsed classes.)
Updated•10 months ago
|
Updated•10 months ago
|
Comment 4•10 months ago
|
||
bugherder |
Description
•