[private methods] Define private methods before executing initializers (#9248)

This commit is contained in:
Nicolò Ribaudo 2019-01-03 20:33:44 +01:00 committed by GitHub
parent 49f52bbcb3
commit 865eb93c2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 127 additions and 2 deletions

View File

@ -366,7 +366,7 @@ export function buildFieldsInitNodes(
);
break;
case isInstance && isPrivate && isMethod && loose:
instanceNodes.push(
instanceNodes.unshift(
buildPrivateMethodInitLoose(
t.thisExpression(),
prop,
@ -378,7 +378,7 @@ export function buildFieldsInitNodes(
);
break;
case isInstance && isPrivate && isMethod && !loose:
instanceNodes.push(
instanceNodes.unshift(
buildPrivateInstanceMethodInitSpec(
t.thisExpression(),
prop,

View File

@ -0,0 +1,16 @@
class Cl {
prop = this.#method(1);
#priv = this.#method(2);
#method(x) {
return x;
}
getPriv() {
return this.#priv;
}
}
expect(new Cl().prop).toBe(1);
expect(new Cl().getPriv()).toBe(2);

View File

@ -0,0 +1,13 @@
class Cl {
prop = this.#method(1);
#priv = this.#method(2);
#method(x) {
return x;
}
getPriv() {
return this.#priv;
}
}

View File

@ -0,0 +1,33 @@
var Cl =
/*#__PURE__*/
function () {
"use strict";
function Cl() {
babelHelpers.classCallCheck(this, Cl);
Object.defineProperty(this, _method, {
value: _method2
});
this.prop = babelHelpers.classPrivateFieldLooseBase(this, _method)[_method](1);
Object.defineProperty(this, _priv, {
writable: true,
value: babelHelpers.classPrivateFieldLooseBase(this, _method)[_method](2)
});
}
babelHelpers.createClass(Cl, [{
key: "getPriv",
value: function getPriv() {
return babelHelpers.classPrivateFieldLooseBase(this, _priv)[_priv];
}
}]);
return Cl;
}();
var _priv = babelHelpers.classPrivateFieldLooseKey("priv");
var _method = babelHelpers.classPrivateFieldLooseKey("method");
var _method2 = function _method2(x) {
return x;
};

View File

@ -0,0 +1,16 @@
class Cl {
prop = this.#method(1);
#priv = this.#method(2);
#method(x) {
return x;
}
getPriv() {
return this.#priv;
}
}
expect(new Cl().prop).toBe(1);
expect(new Cl().getPriv()).toBe(2);

View File

@ -0,0 +1,13 @@
class Cl {
prop = this.#method(1);
#priv = this.#method(2);
#method(x) {
return x;
}
getPriv() {
return this.#priv;
}
}

View File

@ -0,0 +1,34 @@
var Cl =
/*#__PURE__*/
function () {
"use strict";
function Cl() {
babelHelpers.classCallCheck(this, Cl);
_method.add(this);
babelHelpers.defineProperty(this, "prop", babelHelpers.classPrivateMethodGet(this, _method, _method2).call(this, 1));
_priv.set(this, {
writable: true,
value: babelHelpers.classPrivateMethodGet(this, _method, _method2).call(this, 2)
});
}
babelHelpers.createClass(Cl, [{
key: "getPriv",
value: function getPriv() {
return babelHelpers.classPrivateFieldGet(this, _priv);
}
}]);
return Cl;
}();
var _priv = new WeakMap();
var _method = new WeakSet();
var _method2 = function _method2(x) {
return x;
};