Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check descriptor before private field access #12910

Merged
merged 2 commits into from Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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/);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly #12905, the Babel behaviour on static accessors are not aligned with V8. I will try to tackle in a separate PR. Before that it is just a test case parallel to properties.


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/);