From ac737a1ac59680dbe497bda1eaea1c02f80b4983 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Bargull?= Date: Wed, 27 Mar 2024 07:35:40 +0000 Subject: [PATCH] Bug 1887677: Correctly compute inClass predicate when validating unbound private names. r=dminor `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.) Differential Revision: https://phabricator.services.mozilla.com/D205609 --- js/src/frontend/Parser.cpp | 6 +++++- .../nested-class-in-computed-property-key.js | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 js/src/tests/non262/PrivateName/nested-class-in-computed-property-key.js diff --git a/js/src/frontend/Parser.cpp b/js/src/frontend/Parser.cpp index eca2c0f35d30..bb5ad66fb16d 100644 --- a/js/src/frontend/Parser.cpp +++ b/js/src/frontend/Parser.cpp @@ -8006,7 +8006,11 @@ GeneralParser::classDefinition( // position in order to provide it for the nodes created later. TokenPos namePos = pos(); - bool isInClass = pc_->sc()->inClass(); + auto isClass = [](ParseContext::Statement* stmt) { + return stmt->kind() == StatementKind::Class; + }; + + bool isInClass = pc_->sc()->inClass() || pc_->findInnermostStatement(isClass); // Push a ParseContext::ClassStatement to keep track of the constructor // funbox. diff --git a/js/src/tests/non262/PrivateName/nested-class-in-computed-property-key.js b/js/src/tests/non262/PrivateName/nested-class-in-computed-property-key.js new file mode 100644 index 000000000000..7596dfe5428e --- /dev/null +++ b/js/src/tests/non262/PrivateName/nested-class-in-computed-property-key.js @@ -0,0 +1,17 @@ +let capturedPrivateAccess; +class A { + // Declare private name in outer class. + static #x = 42; + + static [( + // Inner class in computed property key. + class {}, + + // Access to private name from outer class. + capturedPrivateAccess = () => A.#x + )]; +} +assertEq(capturedPrivateAccess(), 42); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); -- 2.11.4.GIT