Skip to content

Commit

Permalink
check descriptor before private field access (#12910)
Browse files Browse the repository at this point in the history
* fix: check descriptor before private field access

* add test cases
  • Loading branch information
JLHwung committed Mar 3, 2021
1 parent b12a4de commit 70c77e5
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -1336,6 +1336,9 @@ helpers.classPrivateFieldSet = helper("7.0.0-beta.0")`

helpers.classPrivateFieldDestructureSet = helper("7.4.4")`
export default function _classPrivateFieldDestructureSet(receiver, privateMap) {
if (privateMap === undefined) {
throw new TypeError("attempted to set private static field before its declaration");
}
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance");
}
Expand Down Expand Up @@ -1367,6 +1370,9 @@ helpers.classStaticPrivateFieldSpecGet = helper("7.0.2")`
if (receiver !== classConstructor) {
throw new TypeError("Private static access of wrong provenance");
}
if (descriptor === undefined) {
throw new TypeError("attempted to get private static field before its declaration");
}
if (descriptor.get) {
return descriptor.get.call(receiver);
}
Expand All @@ -1379,6 +1385,9 @@ helpers.classStaticPrivateFieldSpecSet = helper("7.0.2")`
if (receiver !== classConstructor) {
throw new TypeError("Private static access of wrong provenance");
}
if (descriptor === undefined) {
throw new TypeError("attempted to set private static field before its declaration");
}
if (descriptor.set) {
descriptor.set.call(receiver, value);
} else {
Expand Down
@@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
C.#p;
}
}
}).toThrow(/attempted to use private field on non-instance/);

expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to use private field on non-instance/);

expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to use private field on non-instance/);
@@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
C.#p;
}
}
}).toThrow(/attempted to get private static field before its declaration/);

expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to set private static field before its declaration/);

expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to set private static field before its declaration/);
@@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static get #p() { return C };
constructor() {
C.#p;
}
}
}).toThrow(/attempted to use private field on non-instance/);

expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to use private field on non-instance/);

expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to use private field on non-instance/);
@@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static get #p() { return C };
constructor() {
C.#p;
}
}
}).toThrow(/attempted to use private field on non-instance/);

expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to use private field on non-instance/);

expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to use private field on non-instance/);
@@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static get #p() { return C };
constructor() {
C.#p;
}
}
}).toThrow(/attempted to get private static field before its declaration/);

expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to set private static field before its declaration/);

expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to set private static field before its declaration/);

0 comments on commit 70c77e5

Please sign in to comment.