Skip to content

Commit

Permalink
fix: privateFieldsAsProperties global counter (#15389)
Browse files Browse the repository at this point in the history
When babel-helpers are inlined, classPrivateFieldLooseKey()
defines a global variable `id` and initialized it to zero
in every generated file. This can lead to name clashes
between #-private members in subclass and superclass,
which again leads to incorrect semantics.
In the new solution, instead of trying to produce a
globally unique private property name string, a Symbol
is created. The helper function classPrivateFieldLooseKey()
is no longer used, but kept for backwards-compatibility.
In environments where Symbol is not available (Internet
Explorer), a polyfill must be included.

For details and discussion of other solutions, see
#15389

For easier review, the test output updates follow in
a separate commit.
  • Loading branch information
fwienber committed Feb 9, 2023
1 parent 41aac8b commit 33e6aec
Showing 1 changed file with 2 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ export function buildPrivateNamesMap(props: PropPath[]) {
export function buildPrivateNamesNodes(
privateNamesMap: PrivateNamesMap,
privateFieldsAsProperties: boolean,
state: File,
) {
const initNodes: t.Statement[] = [];

for (const [name, value] of privateNamesMap) {
// When the privateFieldsAsProperties assumption is enabled,
// both static and instance fields are transpiled using a
// secret non-enumerable property. Hence, we also need to generate that
// key (using the classPrivateFieldLooseKey helper).
// key, using a Symbol.
// In spec mode, only instance fields need a "private name" initializer
// because static fields are directly assigned to a variable in the
// buildPrivateStaticFieldInitSpec function.
Expand All @@ -77,9 +76,7 @@ export function buildPrivateNamesNodes(
let init: t.Expression;

if (privateFieldsAsProperties) {
init = t.callExpression(state.addHelper("classPrivateFieldLooseKey"), [
t.stringLiteral(name),
]);
init = t.callExpression(t.identifier("Symbol"), [t.stringLiteral(name)]);
} else if (!isStatic) {
init = t.newExpression(
t.identifier(!isMethod || isAccessor ? "WeakMap" : "WeakSet"),
Expand Down

0 comments on commit 33e6aec

Please sign in to comment.