[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")`
|
||||
export default function _classStaticPrivateFieldSpecGet(
|
||||
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) {
|
||||
const { scope } = path;
|
||||
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 { name } = path.node.key.id;
|
||||
|
||||
@ -285,28 +271,45 @@ export default declare((api, options) => {
|
||||
...privateNameHandlerLoose,
|
||||
});
|
||||
|
||||
initNodes.push(
|
||||
template.statement`var PROP = HELPER(NAME);`({
|
||||
return {
|
||||
keyDecl: template.statement`var PROP = HELPER(NAME);`({
|
||||
PROP: prop,
|
||||
HELPER: state.addHelper("classPrivateFieldLooseKey"),
|
||||
NAME: t.stringLiteral(name),
|
||||
}),
|
||||
);
|
||||
|
||||
// Must be late evaluated in case it references another private field.
|
||||
return () =>
|
||||
template.statement`
|
||||
Object.defineProperty(REF, PROP, {
|
||||
buildInit: () =>
|
||||
template.statement.ast`
|
||||
Object.defineProperty(${ref}, ${prop}, {
|
||||
// configurable is false by default
|
||||
// enumerable is false by default
|
||||
writable: true,
|
||||
value: VALUE
|
||||
});
|
||||
`({
|
||||
REF: ref,
|
||||
PROP: prop,
|
||||
VALUE: path.node.value || scope.buildUndefinedNode(),
|
||||
value: ${path.node.value || scope.buildUndefinedNode()}
|
||||
});
|
||||
`,
|
||||
};
|
||||
}
|
||||
|
||||
function buildClassInstancePrivatePropertyLoose(ref, path, initNodes, state) {
|
||||
const { keyDecl, buildInit } = buildClassPrivatePropertyLooseHelper(
|
||||
ref,
|
||||
path,
|
||||
state,
|
||||
);
|
||||
|
||||
initNodes.push(keyDecl);
|
||||
return buildInit;
|
||||
}
|
||||
|
||||
function buildClassStaticPrivatePropertyLoose(ref, path, state) {
|
||||
const { keyDecl, buildInit } = buildClassPrivatePropertyLooseHelper(
|
||||
ref,
|
||||
path,
|
||||
state,
|
||||
);
|
||||
|
||||
const staticNodesToAdd = [keyDecl, buildInit()];
|
||||
return [staticNodesToAdd];
|
||||
}
|
||||
|
||||
function buildClassStaticPrivatePropertySpec(
|
||||
@ -351,44 +354,12 @@ export default declare((api, options) => {
|
||||
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
|
||||
? buildClassPropertyLoose
|
||||
: buildClassPropertySpec;
|
||||
|
||||
const buildClassPrivateProperty = loose
|
||||
? buildClassPrivatePropertyLoose
|
||||
? buildClassInstancePrivatePropertyLoose
|
||||
: buildClassPrivatePropertySpec;
|
||||
|
||||
const buildClassStaticPrivateProperty = loose
|
||||
|
||||
@ -7,7 +7,7 @@ class Foo {
|
||||
}
|
||||
|
||||
static test() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._foo;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Foo, _foo)[_foo];
|
||||
}
|
||||
|
||||
test() {
|
||||
@ -16,11 +16,11 @@ class Foo {
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty(Foo, "_foo", {
|
||||
value: "foo",
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
|
||||
|
||||
Object.defineProperty(Foo, _foo, {
|
||||
writable: true,
|
||||
value: "foo"
|
||||
});
|
||||
|
||||
var _bar = babelHelpers.classPrivateFieldLooseKey("bar");
|
||||
|
||||
@ -1,18 +1,16 @@
|
||||
export default (param => {
|
||||
var _class, _temp;
|
||||
var _class, _temp, _props;
|
||||
|
||||
return _temp = _class = class App {
|
||||
getParam() {
|
||||
return param;
|
||||
}
|
||||
|
||||
}, Object.defineProperty(_class, "_props", {
|
||||
}, _props = babelHelpers.classPrivateFieldLooseKey("props"), Object.defineProperty(_class, _props, {
|
||||
writable: true,
|
||||
value: {
|
||||
prop1: 'prop1',
|
||||
prop2: 'prop2'
|
||||
},
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
}
|
||||
}), _temp;
|
||||
});
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
function classFactory() {
|
||||
var _class, _temp, _foo;
|
||||
var _class, _temp, _foo, _bar;
|
||||
|
||||
return _temp = _class = class Foo {
|
||||
constructor() {
|
||||
@ -14,7 +14,7 @@ function classFactory() {
|
||||
}
|
||||
|
||||
static() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, _class)._bar;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||
}
|
||||
|
||||
static instance(inst) {
|
||||
@ -22,14 +22,12 @@ function classFactory() {
|
||||
}
|
||||
|
||||
static static() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, _class)._bar;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||
}
|
||||
|
||||
}, _foo = babelHelpers.classPrivateFieldLooseKey("foo"), Object.defineProperty(_class, "_bar", {
|
||||
value: "bar",
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
}, _foo = babelHelpers.classPrivateFieldLooseKey("foo"), _bar = babelHelpers.classPrivateFieldLooseKey("bar"), Object.defineProperty(_class, _bar, {
|
||||
writable: true,
|
||||
value: "bar"
|
||||
}), _temp;
|
||||
}
|
||||
|
||||
|
||||
@ -10,17 +10,17 @@ function () {
|
||||
babelHelpers.createClass(Foo, [{
|
||||
key: "test",
|
||||
value: function test(x) {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._foo(x);
|
||||
return babelHelpers.classPrivateFieldLooseBase(Foo, _foo)[_foo](x);
|
||||
}
|
||||
}]);
|
||||
return Foo;
|
||||
}();
|
||||
|
||||
Object.defineProperty(Foo, "_foo", {
|
||||
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
|
||||
|
||||
Object.defineProperty(Foo, _foo, {
|
||||
writable: true,
|
||||
value: function (x) {
|
||||
return x;
|
||||
},
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
}
|
||||
});
|
||||
|
||||
@ -1,14 +1,16 @@
|
||||
export class MyClass {}
|
||||
Object.defineProperty(MyClass, "_property", {
|
||||
value: value,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
|
||||
var _property = babelHelpers.classPrivateFieldLooseKey("property");
|
||||
|
||||
Object.defineProperty(MyClass, _property, {
|
||||
writable: true,
|
||||
value: value
|
||||
});
|
||||
export default class MyClass2 {}
|
||||
Object.defineProperty(MyClass2, "_property2", {
|
||||
value: value,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
|
||||
var _property2 = babelHelpers.classPrivateFieldLooseKey("property");
|
||||
|
||||
Object.defineProperty(MyClass2, _property2, {
|
||||
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", {
|
||||
value: 0,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
var Foo = (_temp = _class = class Foo {}, _num = babelHelpers.classPrivateFieldLooseKey("num"), Object.defineProperty(_class, _num, {
|
||||
writable: true,
|
||||
value: 0
|
||||
}), _temp);
|
||||
|
||||
@ -1,41 +1,41 @@
|
||||
class Base {
|
||||
static getThis() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(this, Base)._foo;
|
||||
return babelHelpers.classPrivateFieldLooseBase(this, _foo)[_foo];
|
||||
}
|
||||
|
||||
static updateThis(val) {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(this, Base)._foo = val;
|
||||
return babelHelpers.classPrivateFieldLooseBase(this, _foo)[_foo] = val;
|
||||
}
|
||||
|
||||
static getClass() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Base, Base)._foo;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Base, _foo)[_foo];
|
||||
}
|
||||
|
||||
static updateClass(val) {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Base, Base)._foo = val;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Base, _foo)[_foo] = val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty(Base, "_foo", {
|
||||
value: 1,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
var _foo = babelHelpers.classPrivateFieldLooseKey("foo");
|
||||
|
||||
Object.defineProperty(Base, _foo, {
|
||||
writable: true,
|
||||
value: 1
|
||||
});
|
||||
|
||||
class Sub1 extends Base {
|
||||
static update(val) {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(this, Sub1)._foo2 = val;
|
||||
return babelHelpers.classPrivateFieldLooseBase(this, _foo2)[_foo2] = val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty(Sub1, "_foo2", {
|
||||
value: 2,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
var _foo2 = babelHelpers.classPrivateFieldLooseKey("foo");
|
||||
|
||||
Object.defineProperty(Sub1, _foo2, {
|
||||
writable: true,
|
||||
value: 2
|
||||
});
|
||||
|
||||
class Sub2 extends Base {}
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
class Foo {
|
||||
static test() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._bar;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||
}
|
||||
|
||||
test() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._bar;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty(Foo, "_bar", {
|
||||
value: void 0,
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
var _bar = babelHelpers.classPrivateFieldLooseKey("bar");
|
||||
|
||||
Object.defineProperty(Foo, _bar, {
|
||||
writable: true,
|
||||
value: void 0
|
||||
});
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
class Foo {
|
||||
static test() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._bar;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||
}
|
||||
|
||||
test() {
|
||||
return babelHelpers.classStaticPrivateFieldLooseBase(Foo, Foo)._bar;
|
||||
return babelHelpers.classPrivateFieldLooseBase(Foo, _bar)[_bar];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Object.defineProperty(Foo, "_bar", {
|
||||
value: "foo",
|
||||
enumerable: false,
|
||||
configurable: false,
|
||||
writable: true
|
||||
var _bar = babelHelpers.classPrivateFieldLooseKey("bar");
|
||||
|
||||
Object.defineProperty(Foo, _bar, {
|
||||
writable: true,
|
||||
value: "foo"
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user