[static private] Unify loose handling of static and instance props (#8614)
This commit is contained in:
parent
262787bd92
commit
d4e23b5b2a
@ -1054,15 +1054,6 @@ helpers.classPrivateFieldSet = helper("7.0.0-beta.0")`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
helpers.classStaticPrivateFieldLooseBase = helper("7.0.1")`
|
|
||||||
export default function _classStaticPrivateFieldLooseBase(receiver, classConstructor) {
|
|
||||||
if (receiver !== classConstructor) {
|
|
||||||
throw new TypeError("Private static access of wrong provenance");
|
|
||||||
}
|
|
||||||
return classConstructor;
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
helpers.classStaticPrivateFieldSpecGet = helper("7.0.1")`
|
helpers.classStaticPrivateFieldSpecGet = helper("7.0.1")`
|
||||||
export default function _classStaticPrivateFieldSpecGet(
|
export default function _classStaticPrivateFieldSpecGet(
|
||||||
receiver, classConstructor, privateClass, privateId
|
receiver, classConstructor, privateClass, privateId
|
||||||
|
|||||||
@ -200,20 +200,6 @@ export default declare((api, options) => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const staticPrivatePropertyHandlerLoose = {
|
|
||||||
handle(member) {
|
|
||||||
const { file, privateId, classRef } = this;
|
|
||||||
member.replaceWith(
|
|
||||||
template.expression`BASE(RECEIVER, CLASS).PRIVATE_ID`({
|
|
||||||
BASE: file.addHelper("classStaticPrivateFieldLooseBase"),
|
|
||||||
RECEIVER: member.node.object,
|
|
||||||
CLASS: classRef,
|
|
||||||
PRIVATE_ID: privateId,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
function buildClassPropertySpec(ref, path, state) {
|
function buildClassPropertySpec(ref, path, state) {
|
||||||
const { scope } = path;
|
const { scope } = path;
|
||||||
const { key, value, computed } = path.node;
|
const { key, value, computed } = path.node;
|
||||||
@ -272,7 +258,7 @@ export default declare((api, options) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildClassPrivatePropertyLoose(ref, path, initNodes, state) {
|
function buildClassPrivatePropertyLooseHelper(ref, path, state) {
|
||||||
const { parentPath, scope } = path;
|
const { parentPath, scope } = path;
|
||||||
const { name } = path.node.key.id;
|
const { name } = path.node.key.id;
|
||||||
|
|
||||||
@ -285,28 +271,45 @@ export default declare((api, options) => {
|
|||||||
...privateNameHandlerLoose,
|
...privateNameHandlerLoose,
|
||||||
});
|
});
|
||||||
|
|
||||||
initNodes.push(
|
return {
|
||||||
template.statement`var PROP = HELPER(NAME);`({
|
keyDecl: template.statement`var PROP = HELPER(NAME);`({
|
||||||
PROP: prop,
|
PROP: prop,
|
||||||
HELPER: state.addHelper("classPrivateFieldLooseKey"),
|
HELPER: state.addHelper("classPrivateFieldLooseKey"),
|
||||||
NAME: t.stringLiteral(name),
|
NAME: t.stringLiteral(name),
|
||||||
}),
|
}),
|
||||||
|
// Must be late evaluated in case it references another private field.
|
||||||
|
buildInit: () =>
|
||||||
|
template.statement.ast`
|
||||||
|
Object.defineProperty(${ref}, ${prop}, {
|
||||||
|
// configurable is false by default
|
||||||
|
// enumerable is false by default
|
||||||
|
writable: true,
|
||||||
|
value: ${path.node.value || scope.buildUndefinedNode()}
|
||||||
|
});
|
||||||
|
`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildClassInstancePrivatePropertyLoose(ref, path, initNodes, state) {
|
||||||
|
const { keyDecl, buildInit } = buildClassPrivatePropertyLooseHelper(
|
||||||
|
ref,
|
||||||
|
path,
|
||||||
|
state,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Must be late evaluated in case it references another private field.
|
initNodes.push(keyDecl);
|
||||||
return () =>
|
return buildInit;
|
||||||
template.statement`
|
}
|
||||||
Object.defineProperty(REF, PROP, {
|
|
||||||
// configurable is false by default
|
function buildClassStaticPrivatePropertyLoose(ref, path, state) {
|
||||||
// enumerable is false by default
|
const { keyDecl, buildInit } = buildClassPrivatePropertyLooseHelper(
|
||||||
writable: true,
|
ref,
|
||||||
value: VALUE
|
path,
|
||||||
});
|
state,
|
||||||
`({
|
);
|
||||||
REF: ref,
|
|
||||||
PROP: prop,
|
const staticNodesToAdd = [keyDecl, buildInit()];
|
||||||
VALUE: path.node.value || scope.buildUndefinedNode(),
|
return [staticNodesToAdd];
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildClassStaticPrivatePropertySpec(
|
function buildClassStaticPrivatePropertySpec(
|
||||||
@ -351,44 +354,12 @@ export default declare((api, options) => {
|
|||||||
return [staticNodesToAdd, privateClassId];
|
return [staticNodesToAdd, privateClassId];
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildClassStaticPrivatePropertyLoose(ref, path, state) {
|
|
||||||
const { scope, parentPath } = path;
|
|
||||||
const { key, value } = path.node;
|
|
||||||
const { name } = key.id;
|
|
||||||
const privateId = scope.generateUidIdentifier(name);
|
|
||||||
|
|
||||||
parentPath.traverse(privateNameVisitor, {
|
|
||||||
name,
|
|
||||||
privateId,
|
|
||||||
classRef: ref,
|
|
||||||
file: state,
|
|
||||||
...staticPrivatePropertyHandlerLoose,
|
|
||||||
});
|
|
||||||
|
|
||||||
const staticNodesToAdd = [
|
|
||||||
template.statement`
|
|
||||||
Object.defineProperty(OBJ, KEY, {
|
|
||||||
value: VALUE,
|
|
||||||
enumerable: false,
|
|
||||||
configurable: false,
|
|
||||||
writable: true
|
|
||||||
});
|
|
||||||
`({
|
|
||||||
OBJ: ref,
|
|
||||||
KEY: t.stringLiteral(privateId.name),
|
|
||||||
VALUE: value || scope.buildUndefinedNode(),
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
|
|
||||||
return [staticNodesToAdd];
|
|
||||||
}
|
|
||||||
|
|
||||||
const buildClassProperty = loose
|
const buildClassProperty = loose
|
||||||
? buildClassPropertyLoose
|
? buildClassPropertyLoose
|
||||||
: buildClassPropertySpec;
|
: buildClassPropertySpec;
|
||||||
|
|
||||||
const buildClassPrivateProperty = loose
|
const buildClassPrivateProperty = loose
|
||||||
? buildClassPrivatePropertyLoose
|
? buildClassInstancePrivatePropertyLoose
|
||||||
: buildClassPrivatePropertySpec;
|
: buildClassPrivatePropertySpec;
|
||||||
|
|
||||||
const buildClassStaticPrivateProperty = loose
|
const buildClassStaticPrivateProperty = loose
|
||||||
|
|||||||
@ -7,7 +7,7 @@ class Foo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static test() {
|
static test() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._foo;
|
return babelHelpers.classPrivateFieldLooseBase(Foo, _foo)[_foo];
|
||||||
}
|
}
|
||||||
|
|
||||||
test() {
|
test() {
|
||||||
@ -16,11 +16,11 @@ class Foo {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(Foo, "_foo", {
|
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
|
||||||
value: "foo",
|
|
||||||
enumerable: false,
|
Object.defineProperty(Foo, _foo, {
|
||||||
configurable: false,
|
writable: true,
|
||||||
writable: true
|
value: "foo"
|
||||||
});
|
});
|
||||||
|
|
||||||
var _bar = babelHelpers.classPrivateFieldLooseKey("bar");
|
var _bar = babelHelpers.classPrivateFieldLooseKey("bar");
|
||||||
|
|||||||
@ -1,18 +1,16 @@
|
|||||||
export default (param => {
|
export default (param => {
|
||||||
var _class, _temp;
|
var _class, _temp, _props;
|
||||||
|
|
||||||
return _temp = _class = class App {
|
return _temp = _class = class App {
|
||||||
getParam() {
|
getParam() {
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
|
|
||||||
}, Object.defineProperty(_class, "_props", {
|
}, _props = babelHelpers.classPrivateFieldLooseKey("props"), Object.defineProperty(_class, _props, {
|
||||||
|
writable: true,
|
||||||
value: {
|
value: {
|
||||||
prop1: 'prop1',
|
prop1: 'prop1',
|
||||||
prop2: 'prop2'
|
prop2: 'prop2'
|
||||||
},
|
}
|
||||||
enumerable: false,
|
|
||||||
configurable: false,
|
|
||||||
writable: true
|
|
||||||
}), _temp;
|
}), _temp;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
function classFactory() {
|
function classFactory() {
|
||||||
var _class, _temp, _foo;
|
var _class, _temp, _foo, _bar;
|
||||||
|
|
||||||
return _temp = _class = class Foo {
|
return _temp = _class = class Foo {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -14,7 +14,7 @@ function classFactory() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static() {
|
static() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, _class)._bar;
|
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||||
}
|
}
|
||||||
|
|
||||||
static instance(inst) {
|
static instance(inst) {
|
||||||
@ -22,14 +22,12 @@ function classFactory() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static static() {
|
static static() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, _class)._bar;
|
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||||
}
|
}
|
||||||
|
|
||||||
}, _foo = babelHelpers.classPrivateFieldLooseKey("foo"), Object.defineProperty(_class, "_bar", {
|
}, _foo = babelHelpers.classPrivateFieldLooseKey("foo"), _bar = babelHelpers.classPrivateFieldLooseKey("bar"), Object.defineProperty(_class, _bar, {
|
||||||
value: "bar",
|
writable: true,
|
||||||
enumerable: false,
|
value: "bar"
|
||||||
configurable: false,
|
|
||||||
writable: true
|
|
||||||
}), _temp;
|
}), _temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,17 +10,17 @@ function () {
|
|||||||
babelHelpers.createClass(Foo, [{
|
babelHelpers.createClass(Foo, [{
|
||||||
key: "test",
|
key: "test",
|
||||||
value: function test(x) {
|
value: function test(x) {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._foo(x);
|
return babelHelpers.classPrivateFieldLooseBase(Foo, _foo)[_foo](x);
|
||||||
}
|
}
|
||||||
}]);
|
}]);
|
||||||
return Foo;
|
return Foo;
|
||||||
}();
|
}();
|
||||||
|
|
||||||
Object.defineProperty(Foo, "_foo", {
|
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
|
||||||
|
|
||||||
|
Object.defineProperty(Foo, _foo, {
|
||||||
|
writable: true,
|
||||||
value: function (x) {
|
value: function (x) {
|
||||||
return x;
|
return x;
|
||||||
},
|
}
|
||||||
enumerable: false,
|
|
||||||
configurable: false,
|
|
||||||
writable: true
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,14 +1,16 @@
|
|||||||
export class MyClass {}
|
export class MyClass {}
|
||||||
Object.defineProperty(MyClass, "_property", {
|
|
||||||
value: value,
|
var _property = babelHelpers.classPrivateFieldLooseKey("property");
|
||||||
enumerable: false,
|
|
||||||
configurable: false,
|
Object.defineProperty(MyClass, _property, {
|
||||||
writable: true
|
writable: true,
|
||||||
|
value: value
|
||||||
});
|
});
|
||||||
export default class MyClass2 {}
|
export default class MyClass2 {}
|
||||||
Object.defineProperty(MyClass2, "_property2", {
|
|
||||||
value: value,
|
var _property2 = babelHelpers.classPrivateFieldLooseKey("property");
|
||||||
enumerable: false,
|
|
||||||
configurable: false,
|
Object.defineProperty(MyClass2, _property2, {
|
||||||
writable: true
|
writable: true,
|
||||||
|
value: value
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
var _class, _temp;
|
var _class, _temp, _num;
|
||||||
|
|
||||||
var Foo = (_temp = _class = class Foo {}, Object.defineProperty(_class, "_num", {
|
var Foo = (_temp = _class = class Foo {}, _num = babelHelpers.classPrivateFieldLooseKey("num"), Object.defineProperty(_class, _num, {
|
||||||
value: 0,
|
writable: true,
|
||||||
enumerable: false,
|
value: 0
|
||||||
configurable: false,
|
|
||||||
writable: true
|
|
||||||
}), _temp);
|
}), _temp);
|
||||||
|
|||||||
@ -1,41 +1,41 @@
|
|||||||
class Base {
|
class Base {
|
||||||
static getThis() {
|
static getThis() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(this, Base)._foo;
|
return babelHelpers.classPrivateFieldLooseBase(this, _foo)[_foo];
|
||||||
}
|
}
|
||||||
|
|
||||||
static updateThis(val) {
|
static updateThis(val) {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(this, Base)._foo = val;
|
return babelHelpers.classPrivateFieldLooseBase(this, _foo)[_foo] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static getClass() {
|
static getClass() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Base, Base)._foo;
|
return babelHelpers.classPrivateFieldLooseBase(Base, _foo)[_foo];
|
||||||
}
|
}
|
||||||
|
|
||||||
static updateClass(val) {
|
static updateClass(val) {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Base, Base)._foo = val;
|
return babelHelpers.classPrivateFieldLooseBase(Base, _foo)[_foo] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(Base, "_foo", {
|
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
|
||||||
value: 1,
|
|
||||||
enumerable: false,
|
Object.defineProperty(Base, _foo, {
|
||||||
configurable: false,
|
writable: true,
|
||||||
writable: true
|
value: 1
|
||||||
});
|
});
|
||||||
|
|
||||||
class Sub1 extends Base {
|
class Sub1 extends Base {
|
||||||
static update(val) {
|
static update(val) {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(this, Sub1)._foo2 = val;
|
return babelHelpers.classPrivateFieldLooseBase(this, _foo2)[_foo2] = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(Sub1, "_foo2", {
|
var _foo2 = babelHelpers.classPrivateFieldLooseKey("foo");
|
||||||
value: 2,
|
|
||||||
enumerable: false,
|
Object.defineProperty(Sub1, _foo2, {
|
||||||
configurable: false,
|
writable: true,
|
||||||
writable: true
|
value: 2
|
||||||
});
|
});
|
||||||
|
|
||||||
class Sub2 extends Base {}
|
class Sub2 extends Base {}
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
class Foo {
|
class Foo {
|
||||||
static test() {
|
static test() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._bar;
|
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||||
}
|
}
|
||||||
|
|
||||||
test() {
|
test() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._bar;
|
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(Foo, "_bar", {
|
var _bar = babelHelpers.classPrivateFieldLooseKey("bar");
|
||||||
value: void 0,
|
|
||||||
enumerable: false,
|
Object.defineProperty(Foo, _bar, {
|
||||||
configurable: false,
|
writable: true,
|
||||||
writable: true
|
value: void 0
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
class Foo {
|
class Foo {
|
||||||
static test() {
|
static test() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._bar;
|
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||||
}
|
}
|
||||||
|
|
||||||
test() {
|
test() {
|
||||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._bar;
|
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(Foo, "_bar", {
|
var _bar = babelHelpers.classPrivateFieldLooseKey("bar");
|
||||||
value: "foo",
|
|
||||||
enumerable: false,
|
Object.defineProperty(Foo, _bar, {
|
||||||
configurable: false,
|
writable: true,
|
||||||
writable: true
|
value: "foo"
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user