Class static private field destructure set (#12917)

* fix: support static private field destructure set ([C.#p] = [0])

* 🚧

* fix: add compatibility warning for older @babel/helper versions

* refactor: extract common routines among classPrivateFiled helpers

* More 🚧
This commit is contained in:
Huáng Jùnliàng
2021-03-03 16:38:16 -05:00
committed by GitHub
parent 70c77e550c
commit bdb207cb75
49 changed files with 520 additions and 134 deletions

View File

@@ -7,6 +7,7 @@ class Cl {
constructor() {
expect(() => this.#privateFieldValue = 1).toThrow(TypeError);
expect(() => ([this.#privateFieldValue] = [1])).toThrow(TypeError);
}
}

View File

@@ -7,5 +7,6 @@ class Cl {
constructor() {
this.#privateFieldValue = 1;
([this.#privateFieldValue] = [1]);
}
}

View File

@@ -13,6 +13,7 @@ class Cl {
value: 0
});
babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue] = 1;
[babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue]] = [1];
}
}

View File

@@ -7,6 +7,7 @@ class Cl {
constructor() {
expect(() => this.#privateFieldValue = 1).toThrow(TypeError);
expect(() => ([this.#privateFieldValue] = [1])).toThrow(TypeError);
}
}

View File

@@ -7,5 +7,6 @@ class Cl {
constructor() {
this.#privateFieldValue = 1;
([this.#privateFieldValue] = [1]);
}
}

View File

@@ -13,6 +13,7 @@ class Cl {
value: 0
});
babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue] = 1;
[babelHelpers.classPrivateFieldLooseBase(this, _privateFieldValue)[_privateFieldValue]] = [1];
}
}

View File

@@ -14,6 +14,8 @@ class Cl {
constructor() {
expect(() => this.self.#privateFieldValue = 1).toThrow(TypeError);
expect(this.counter).toBe(1);
expect(() => ([this.self.#privateFieldValue] = [1])).toThrow(TypeError);
expect(this.counter).toBe(2);
}
}

View File

@@ -13,6 +13,7 @@ class Cl {
constructor() {
this.self.#privateFieldValue = 1
([this.self.#privateFieldValue] = [1]);
}
}

View File

@@ -20,7 +20,7 @@ class Cl {
});
babelHelpers.defineProperty(this, "counter", 0);
this.self, 1, babelHelpers.readOnlyError("#privateFieldValue");
this.self, 1([babelHelpers.classPrivateFieldDestructureSet(this.self, _privateFieldValue).value] = [1]), babelHelpers.readOnlyError("#privateFieldValue");
}
}

View File

@@ -9,6 +9,7 @@ class A {
constructor() {
expect(() => this.self().#method = 2).toThrow(TypeError);
expect(this.counter).toBe(1);
expect(() => ([this.#method] = [2])).toThrow(TypeError);
}
}

View File

@@ -8,5 +8,6 @@ class A {
constructor() {
this.self().#method = 2;
([this.#method] = [2]);
}
}

View File

@@ -11,6 +11,7 @@ class A {
babelHelpers.defineProperty(this, "counter", 0);
this.self(), 2, babelHelpers.readOnlyError("#method");
[babelHelpers.classPrivateFieldDestructureSet(this, _method).value] = [2];
}
}

View File

@@ -0,0 +1,10 @@
class A {
static #method() {}
run() {
expect(() => A.#method = 2).toThrow(TypeError);
expect(() => ([A.#method] = [2])).toThrow(TypeError);
}
}

View File

@@ -0,0 +1,8 @@
class A {
static #method() {}
run() {
A.#method = 2;
([A.#method] = [2]);
}
}

View File

@@ -0,0 +1,9 @@
class A {
run() {
babelHelpers.classStaticPrivateMethodSet(A, A, _method, 2);
[babelHelpers.classStaticPrivateFieldDestructureSet(A, A, _method).value] = [2];
}
}
var _method = function _method() {};

View File

@@ -0,0 +1,10 @@
class C {
static set #p(v) { C.#q = v }
static #q;
constructor() {
([C.#p] = [0]);
expect(C.#q).toBe(0);
}
}
new C;

View File

@@ -0,0 +1,9 @@
class C {
static set #p(v) { C.#q = v }
static #q;
constructor() {
([C.#p] = [0]);
}
}
new C;

View File

@@ -0,0 +1,24 @@
var _p = babelHelpers.classPrivateFieldLooseKey("p");
var _q = babelHelpers.classPrivateFieldLooseKey("q");
class C {
constructor() {
[babelHelpers.classPrivateFieldLooseBase(C, _p)[_p]] = [0];
}
}
var _set_p = function (v) {
babelHelpers.classPrivateFieldLooseBase(C, _q)[_q] = v;
};
Object.defineProperty(C, _p, {
get: void 0,
set: _set_p
});
Object.defineProperty(C, _q, {
writable: true,
value: void 0
});
new C();

View File

@@ -1,13 +1,14 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;
static #privateField = 0;
static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
static get #privateFieldValue() {
return this.#privateField;
}
static setPrivateStaticFieldValue() {
Cl.#privateStaticFieldValue = 1;
constructor() {
expect(() => Cl.#privateFieldValue = 1).toThrow(TypeError);
expect(() => ([Cl.#privateFieldValue] = [1])).toThrow(TypeError);
}
}
expect(() => Cl.setPrivateStaticFieldValue()).toThrow(TypeError);
const cl = new Cl();

View File

@@ -1,11 +1,15 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;
static #privateField = 0;
static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
static get #privateFieldValue() {
return this.#privateField;
}
static setPrivateStaticFieldValue() {
Cl.#privateStaticFieldValue = 1;
constructor() {
Cl.#privateFieldValue = 1;
([Cl.#privateFieldValue] = [1]);
}
}
}
const cl = new Cl();

View File

@@ -1,23 +1,25 @@
var _PRIVATE_STATIC_FIELD = babelHelpers.classPrivateFieldLooseKey("PRIVATE_STATIC_FIELD");
var _privateField = babelHelpers.classPrivateFieldLooseKey("privateField");
var _privateStaticFieldValue = babelHelpers.classPrivateFieldLooseKey("privateStaticFieldValue");
var _privateFieldValue = babelHelpers.classPrivateFieldLooseKey("privateFieldValue");
class Cl {
static setPrivateStaticFieldValue() {
babelHelpers.classPrivateFieldLooseBase(Cl, _privateStaticFieldValue)[_privateStaticFieldValue] = 1;
constructor() {
babelHelpers.classPrivateFieldLooseBase(Cl, _privateFieldValue)[_privateFieldValue] = 1;
[babelHelpers.classPrivateFieldLooseBase(Cl, _privateFieldValue)[_privateFieldValue]] = [1];
}
}
var _get_privateStaticFieldValue = function () {
return babelHelpers.classPrivateFieldLooseBase(Cl, _PRIVATE_STATIC_FIELD)[_PRIVATE_STATIC_FIELD];
var _get_privateFieldValue = function () {
return babelHelpers.classPrivateFieldLooseBase(this, _privateField)[_privateField];
};
Object.defineProperty(Cl, _PRIVATE_STATIC_FIELD, {
Object.defineProperty(Cl, _privateField, {
writable: true,
value: 0
});
Object.defineProperty(Cl, _privateStaticFieldValue, {
get: _get_privateStaticFieldValue,
Object.defineProperty(Cl, _privateFieldValue, {
get: _get_privateFieldValue,
set: void 0
});
var cl = new Cl();

View File

@@ -0,0 +1,10 @@
class C {
static set #p(v) { C.#q = v }
static #q;
constructor() {
([C.#p] = [0]);
expect(C.#q).toBe(0);
}
}
new C;

View File

@@ -0,0 +1,9 @@
class C {
static set #p(v) { C.#q = v }
static #q;
constructor() {
([C.#p] = [0]);
}
}
new C;

View File

@@ -0,0 +1,24 @@
var _p = babelHelpers.classPrivateFieldLooseKey("p");
var _q = babelHelpers.classPrivateFieldLooseKey("q");
class C {
constructor() {
[babelHelpers.classPrivateFieldLooseBase(C, _p)[_p]] = [0];
}
}
var _set_p = function (v) {
babelHelpers.classPrivateFieldLooseBase(C, _q)[_q] = v;
};
Object.defineProperty(C, _p, {
get: void 0,
set: _set_p
});
Object.defineProperty(C, _q, {
writable: true,
value: void 0
});
new C();

View File

@@ -1,13 +1,14 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;
static #privateField = 0;
static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
static get #privateFieldValue() {
return this.#privateField;
}
static setPrivateStaticFieldValue() {
Cl.#privateStaticFieldValue = 1;
constructor() {
expect(() => Cl.#privateFieldValue = 1).toThrow(TypeError);
expect(() => ([Cl.#privateFieldValue] = [1])).toThrow(TypeError);
}
}
expect(() => Cl.setPrivateStaticFieldValue()).toThrow(TypeError);
const cl = new Cl();

View File

@@ -1,11 +1,15 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;
static #privateField = 0;
static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
static get #privateFieldValue() {
return this.#privateField;
}
static setPrivateStaticFieldValue() {
Cl.#privateStaticFieldValue = 1;
constructor() {
Cl.#privateFieldValue = 1;
([Cl.#privateFieldValue] = [1]);
}
}
}
const cl = new Cl();

View File

@@ -1,23 +1,25 @@
var _PRIVATE_STATIC_FIELD = babelHelpers.classPrivateFieldLooseKey("PRIVATE_STATIC_FIELD");
var _privateField = babelHelpers.classPrivateFieldLooseKey("privateField");
var _privateStaticFieldValue = babelHelpers.classPrivateFieldLooseKey("privateStaticFieldValue");
var _privateFieldValue = babelHelpers.classPrivateFieldLooseKey("privateFieldValue");
class Cl {
static setPrivateStaticFieldValue() {
babelHelpers.classPrivateFieldLooseBase(Cl, _privateStaticFieldValue)[_privateStaticFieldValue] = 1;
constructor() {
babelHelpers.classPrivateFieldLooseBase(Cl, _privateFieldValue)[_privateFieldValue] = 1;
[babelHelpers.classPrivateFieldLooseBase(Cl, _privateFieldValue)[_privateFieldValue]] = [1];
}
}
var _get_privateStaticFieldValue = function () {
return babelHelpers.classPrivateFieldLooseBase(Cl, _PRIVATE_STATIC_FIELD)[_PRIVATE_STATIC_FIELD];
var _get_privateFieldValue = function () {
return babelHelpers.classPrivateFieldLooseBase(this, _privateField)[_privateField];
};
Object.defineProperty(Cl, _PRIVATE_STATIC_FIELD, {
Object.defineProperty(Cl, _privateField, {
writable: true,
value: 0
});
Object.defineProperty(Cl, _privateStaticFieldValue, {
get: _get_privateStaticFieldValue,
Object.defineProperty(Cl, _privateFieldValue, {
get: _get_privateFieldValue,
set: void 0
});
const cl = new Cl();

View File

@@ -0,0 +1,10 @@
class C {
static set #p(v) { C.#q = v }
static #q;
constructor() {
([C.#p] = [0]);
expect(C.#q).toBe(0);
}
}
new C;

View File

@@ -0,0 +1,9 @@
class C {
static set #p(v) { C.#q = v }
static #q;
constructor() {
([C.#p] = [0]);
}
}
new C;

View File

@@ -0,0 +1,20 @@
class C {
constructor() {
[babelHelpers.classStaticPrivateFieldDestructureSet(C, C, _p).value] = [0];
}
}
var _set_p = function (v) {
babelHelpers.classStaticPrivateFieldSpecSet(C, C, _q, v);
};
var _p = {
get: void 0,
set: _set_p
};
var _q = {
writable: true,
value: void 0
};
new C();

View File

@@ -1,13 +1,14 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;
static #privateField = 0;
static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
static get #privateFieldValue() {
return this.#privateField;
}
static setPrivateStaticFieldValue() {
Cl.#privateStaticFieldValue = 1;
constructor() {
expect(() => Cl.#privateFieldValue = 1).toThrow(TypeError);
expect(() => ([Cl.#privateFieldValue] = [1])).toThrow(TypeError);
}
}
expect(() => Cl.setPrivateStaticFieldValue()).toThrow(TypeError);
const cl = new Cl();

View File

@@ -1,11 +1,15 @@
class Cl {
static #PRIVATE_STATIC_FIELD = 0;
static #privateField = 0;
static get #privateStaticFieldValue() {
return Cl.#PRIVATE_STATIC_FIELD;
static get #privateFieldValue() {
return this.#privateField;
}
static setPrivateStaticFieldValue() {
Cl.#privateStaticFieldValue = 1;
constructor() {
Cl.#privateFieldValue = 1;
([Cl.#privateFieldValue] = [1]);
}
}
}
const cl = new Cl();

View File

@@ -1,19 +1,21 @@
class Cl {
static setPrivateStaticFieldValue() {
babelHelpers.classStaticPrivateFieldSpecSet(Cl, Cl, _privateStaticFieldValue, 1);
constructor() {
babelHelpers.classStaticPrivateFieldSpecSet(Cl, Cl, _privateFieldValue, 1);
[babelHelpers.classStaticPrivateFieldDestructureSet(Cl, Cl, _privateFieldValue).value] = [1];
}
}
var _get_privateStaticFieldValue = function () {
return babelHelpers.classStaticPrivateFieldSpecGet(Cl, Cl, _PRIVATE_STATIC_FIELD);
var _get_privateFieldValue = function () {
return babelHelpers.classStaticPrivateFieldSpecGet(this, Cl, _privateField);
};
var _PRIVATE_STATIC_FIELD = {
var _privateField = {
writable: true,
value: 0
};
var _privateStaticFieldValue = {
get: _get_privateStaticFieldValue,
var _privateFieldValue = {
get: _get_privateFieldValue,
set: void 0
};
var cl = new Cl();