Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix class private properties when privateFieldsAsProperties #16275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Expand Up @@ -45,7 +45,12 @@ if (!process.env.BABEL_8_BREAKING) {
};
}

export function buildPrivateNamesMap(props: PropPath[], file: File) {
export function buildPrivateNamesMap(
className: string,
privateFieldsAsProperties: boolean,
props: PropPath[],
file: File,
) {
const privateNamesMap: PrivateNamesMap = new Map();
let classBrandId: t.Identifier;
for (const prop of props) {
Expand All @@ -58,12 +63,15 @@ export function buildPrivateNamesMap(props: PropPath[], file: File) {
let initAdded = false;
let id: t.Identifier;
if (
!privateFieldsAsProperties &&
(process.env.BABEL_8_BREAKING || newHelpers(file)) &&
isMethod &&
!isStatic
) {
initAdded = !!classBrandId;
classBrandId ??= prop.scope.generateUidIdentifier(name);
classBrandId ??= prop.scope.generateUidIdentifier(
`${className}_brand`,
);
id = classBrandId;
} else {
id = prop.scope.generateUidIdentifier(name);
Expand Down
10 changes: 6 additions & 4 deletions packages/babel-helper-create-class-features-plugin/src/index.ts
Expand Up @@ -236,10 +236,12 @@ export function createClassFeaturePlugin({
}
const classRefForDefine = ref ?? t.cloneNode(innerBinding);

// NODE: These three functions don't support decorators yet,
// but verifyUsedFeatures throws if there are both
// decorators and private fields.
const privateNamesMap = buildPrivateNamesMap(props, file);
const privateNamesMap = buildPrivateNamesMap(
classRefForDefine.name,
privateFieldsAsProperties ?? loose,
props,
file,
);
const privateNamesNodes = buildPrivateNamesNodes(
privateNamesMap,
privateFieldsAsProperties ?? loose,
Expand Down
Expand Up @@ -3,19 +3,19 @@ class Base {
return obj;
}
}
var _foo = /*#__PURE__*/new WeakSet();
var _Derived_brand = /*#__PURE__*/new WeakSet();
class Derived extends Base {
constructor(...args) {
super(...args);
babelHelpers.classPrivateMethodInitSpec(this, _foo);
babelHelpers.classPrivateMethodInitSpec(this, _Derived_brand);
}
static get(obj) {
return babelHelpers.classPrivateGetter(obj, _foo, _get_foo).call(obj);
return babelHelpers.classPrivateGetter(obj, _Derived_brand, _get_foo).call(obj);
}
}
function _get_foo() {
return 'bar';
}
function _set_foo(value) {
babelHelpers.classPrivateSetter(this, _foo, _set_foo, value);
babelHelpers.classPrivateSetter(this, _Derived_brand, _set_foo, value);
}
Expand Up @@ -3,16 +3,16 @@ class Base {
return obj;
}
}
var _foo = /*#__PURE__*/new WeakSet();
var _Derived_brand = /*#__PURE__*/new WeakSet();
class Derived extends Base {
constructor(...args) {
super(...args);
babelHelpers.classPrivateMethodInitSpec(this, _foo);
babelHelpers.classPrivateMethodInitSpec(this, _Derived_brand);
}
static get(obj) {
return babelHelpers.classPrivateMethodGet(obj, _foo, _foo2).call(obj);
return babelHelpers.classPrivateMethodGet(obj, _Derived_brand, _foo).call(obj);
}
}
function _foo2() {
function _foo() {
return 'bar';
}
@@ -1,9 +1,9 @@
var _privateMethod = /*#__PURE__*/new WeakSet();
var _X_brand = /*#__PURE__*/new WeakSet();
class X {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _privateMethod);
babelHelpers.classPrivateMethodInitSpec(this, _X_brand);
}
}
function _privateMethod2() {
function _privateMethod() {
return 42;
}
@@ -0,0 +1,18 @@
class X {
#privateMethod() {
return 1;
}
#privateMethod2() {
return 2;
}
method1() {
return this.#privateMethod();
}
method2() {
return this.#privateMethod2();
}
}

const x = new X();
expect(x.method1()).toBe(1);
expect(x.method2()).toBe(2);
Expand Up @@ -2,4 +2,7 @@ class X {
#privateMethod() {
return 42;
}
#privateMethod2() {
return 42;
}
}
@@ -1,6 +1,10 @@
var _privateMethod = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("privateMethod");
var _privateMethod3 = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("privateMethod2");
Comment on lines 1 to +2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previous output

var _privateMethod = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("privateMethod2");
var _privateMethod = /*#__PURE__*/babelHelpers.classPrivateFieldLooseKey("privateMethod2");

class X {
constructor() {
Object.defineProperty(this, _privateMethod3, {
value: _privateMethod4
});
Object.defineProperty(this, _privateMethod, {
value: _privateMethod2
});
Expand All @@ -9,3 +13,6 @@ class X {
function _privateMethod2() {
return 42;
}
function _privateMethod4() {
return 42;
}
@@ -1,13 +1,13 @@
var _A;
var _foo = /*#__PURE__*/new WeakSet();
var _A_brand = /*#__PURE__*/new WeakSet();
class A extends B {
constructor(...args) {
super(...args);
babelHelpers.classPrivateMethodInitSpec(this, _foo);
babelHelpers.classPrivateMethodInitSpec(this, _A_brand);
}
}
_A = A;
function _foo2() {
function _foo() {
let _A2;
babelHelpers.get(babelHelpers.getPrototypeOf(_A.prototype), "x", this);
}
@@ -1,11 +1,11 @@
var _initProto, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _Foo;
const dec = () => {};
var _A = /*#__PURE__*/new WeakMap();
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
var _B = /*#__PURE__*/new WeakMap();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.classPrivateFieldInitSpec(this, _A, (_initProto(this), _init_a(this)));
babelHelpers.classPrivateFieldInitSpec(this, _B, _init_b(this, 123));
}
Expand Down
@@ -1,10 +1,10 @@
const dec = () => {};
var _A = /*#__PURE__*/new WeakMap();
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
var _B = /*#__PURE__*/new WeakMap();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.classPrivateFieldInitSpec(this, _A, void 0);
babelHelpers.classPrivateFieldInitSpec(this, _B, 123);
}
Expand Down
@@ -1,13 +1,13 @@
var _initClass, _x, _A, _a, _B, _temp;
var _initClass, _x, _A, _Class_brand, _B, _temp;
const dec = () => {};
let hasX, hasA, hasM;
let _Foo;
new (_x = /*#__PURE__*/new WeakMap(), _A = /*#__PURE__*/new WeakMap(), _a = /*#__PURE__*/new WeakSet(), _B = /*#__PURE__*/new WeakMap(), (_temp = class extends babelHelpers.identity {
new (_x = /*#__PURE__*/new WeakMap(), _A = /*#__PURE__*/new WeakMap(), _Class_brand = /*#__PURE__*/new WeakSet(), _B = /*#__PURE__*/new WeakMap(), (_temp = class extends babelHelpers.identity {
constructor() {
(super(_Foo), babelHelpers.classPrivateMethodInitSpec(this, _a), babelHelpers.classPrivateFieldInitSpec(this, _x, void 0), babelHelpers.classPrivateFieldInitSpec(this, _A, void 0), babelHelpers.defineProperty(this, "x", void 0), babelHelpers.classPrivateFieldInitSpec(this, _B, void 0), this), (() => {
(super(_Foo), babelHelpers.classPrivateMethodInitSpec(this, _Class_brand), babelHelpers.classPrivateFieldInitSpec(this, _x, void 0), babelHelpers.classPrivateFieldInitSpec(this, _A, void 0), babelHelpers.defineProperty(this, "x", void 0), babelHelpers.classPrivateFieldInitSpec(this, _B, void 0), this), (() => {
hasX = o => _x.has(babelHelpers.checkInRHS(o));
hasA = o => _a.has(babelHelpers.checkInRHS(o));
hasM = o => _a.has(babelHelpers.checkInRHS(o));
hasA = o => _Class_brand.has(babelHelpers.checkInRHS(o));
hasM = o => _Class_brand.has(babelHelpers.checkInRHS(o));
})(), _initClass();
}
}, (_Foo2 => {
Expand Down
@@ -1,13 +1,13 @@
var _initProto, _call_a, _Foo;
const dec = () => {};
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
getA() {
return babelHelpers.classPrivateGetter(this, _a, _get_a);
return babelHelpers.classPrivateGetter(this, _Foo_brand, _get_a);
}
}
_Foo = Foo;
Expand Down
@@ -1,16 +1,16 @@
var _initProto, _call_a, _call_a2, _Foo;
const dec = () => {};
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
getA() {
return babelHelpers.classPrivateGetter(this, _a, _get_a);
return babelHelpers.classPrivateGetter(this, _Foo_brand, _get_a);
}
setA(v) {
babelHelpers.classPrivateSetter(this, _a, _set_a, v);
babelHelpers.classPrivateSetter(this, _Foo_brand, _set_a, v);
}
}
_Foo = Foo;
Expand Down
@@ -1,13 +1,13 @@
var _initProto, _call_a, _Foo;
const dec = () => {};
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
setA(v) {
babelHelpers.classPrivateSetter(this, _a, _set_a, v);
babelHelpers.classPrivateSetter(this, _Foo_brand, _set_a, v);
}
}
_Foo = Foo;
Expand Down
@@ -1,11 +1,11 @@
var _initProto, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _Foo;
const dec = () => {};
var _A = /*#__PURE__*/new WeakMap();
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
var _B = /*#__PURE__*/new WeakMap();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.classPrivateFieldInitSpec(this, _A, (_initProto(this), _init_a(this)));
babelHelpers.classPrivateFieldInitSpec(this, _B, _init_b(this, 123));
}
Expand Down
@@ -1,10 +1,10 @@
const dec = () => {};
var _A = /*#__PURE__*/new WeakMap();
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
var _B = /*#__PURE__*/new WeakMap();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.classPrivateFieldInitSpec(this, _A, void 0);
babelHelpers.classPrivateFieldInitSpec(this, _B, 123);
}
Expand Down
@@ -1,13 +1,13 @@
var _initClass, _x, _A, _a, _B, _temp;
var _initClass, _x, _A, _Class_brand, _B, _temp;
const dec = () => {};
let hasX, hasA, hasM;
let _Foo;
new (_x = /*#__PURE__*/new WeakMap(), _A = /*#__PURE__*/new WeakMap(), _a = /*#__PURE__*/new WeakSet(), _B = /*#__PURE__*/new WeakMap(), (_temp = class extends babelHelpers.identity {
new (_x = /*#__PURE__*/new WeakMap(), _A = /*#__PURE__*/new WeakMap(), _Class_brand = /*#__PURE__*/new WeakSet(), _B = /*#__PURE__*/new WeakMap(), (_temp = class extends babelHelpers.identity {
constructor() {
(super(_Foo), babelHelpers.classPrivateMethodInitSpec(this, _a), babelHelpers.classPrivateFieldInitSpec(this, _x, void 0), babelHelpers.classPrivateFieldInitSpec(this, _A, void 0), babelHelpers.defineProperty(this, "x", void 0), babelHelpers.classPrivateFieldInitSpec(this, _B, void 0), this), (() => {
(super(_Foo), babelHelpers.classPrivateMethodInitSpec(this, _Class_brand), babelHelpers.classPrivateFieldInitSpec(this, _x, void 0), babelHelpers.classPrivateFieldInitSpec(this, _A, void 0), babelHelpers.defineProperty(this, "x", void 0), babelHelpers.classPrivateFieldInitSpec(this, _B, void 0), this), (() => {
hasX = o => _x.has(babelHelpers.checkInRHS(o));
hasA = o => _a.has(babelHelpers.checkInRHS(o));
hasM = o => _a.has(babelHelpers.checkInRHS(o));
hasA = o => _Class_brand.has(babelHelpers.checkInRHS(o));
hasM = o => _Class_brand.has(babelHelpers.checkInRHS(o));
})(), _initClass();
}
}, (_Foo2 => {
Expand Down
@@ -1,13 +1,13 @@
var _initProto, _call_a, _Foo;
const dec = () => {};
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
getA() {
return babelHelpers.classPrivateGetter(this, _a, _get_a);
return babelHelpers.classPrivateGetter(this, _Foo_brand, _get_a);
}
}
_Foo = Foo;
Expand Down
@@ -1,16 +1,16 @@
var _initProto, _call_a, _call_a2, _Foo;
const dec = () => {};
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
getA() {
return babelHelpers.classPrivateGetter(this, _a, _get_a);
return babelHelpers.classPrivateGetter(this, _Foo_brand, _get_a);
}
setA(v) {
babelHelpers.classPrivateSetter(this, _a, _set_a, v);
babelHelpers.classPrivateSetter(this, _Foo_brand, _set_a, v);
}
}
_Foo = Foo;
Expand Down
@@ -1,13 +1,13 @@
var _initProto, _call_a, _Foo;
const dec = () => {};
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.defineProperty(this, "value", (_initProto(this), 1));
}
setA(v) {
babelHelpers.classPrivateSetter(this, _a, _set_a, v);
babelHelpers.classPrivateSetter(this, _Foo_brand, _set_a, v);
}
}
_Foo = Foo;
Expand Down
@@ -1,11 +1,11 @@
var _initProto, _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _Foo;
const dec = () => {};
var _A = /*#__PURE__*/new WeakMap();
var _a = /*#__PURE__*/new WeakSet();
var _Foo_brand = /*#__PURE__*/new WeakSet();
var _B = /*#__PURE__*/new WeakMap();
class Foo {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _a);
babelHelpers.classPrivateMethodInitSpec(this, _Foo_brand);
babelHelpers.classPrivateFieldInitSpec(this, _A, (_initProto(this), _init_a(this)));
babelHelpers.classPrivateFieldInitSpec(this, _B, _init_b(this, 123));
}
Expand All @@ -23,4 +23,4 @@ function _set_b2(v) {
function _get_b2() {
return _get_b(this);
}
[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs2301(_Foo, [[dec, 1, "a", o => babelHelpers.classPrivateFieldGet2(o, _A), (o, v) => babelHelpers.classPrivateFieldSet2(o, _A, v)], [dec, 1, "b", o => babelHelpers.classPrivateFieldGet2(o, _B), (o, v) => babelHelpers.classPrivateFieldSet2(o, _B, v)]], [], _ => _a.has(babelHelpers.checkInRHS(_))).e;
[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs2301(_Foo, [[dec, 1, "a", o => babelHelpers.classPrivateFieldGet2(o, _A), (o, v) => babelHelpers.classPrivateFieldSet2(o, _A, v)], [dec, 1, "b", o => babelHelpers.classPrivateFieldGet2(o, _B), (o, v) => babelHelpers.classPrivateFieldSet2(o, _B, v)]], [], _ => _Foo_brand.has(babelHelpers.checkInRHS(_))).e;