Reuse the readOnlyError helper for private methods (#12792)

This commit is contained in:
Nicolò Ribaudo 2021-02-12 14:33:23 +01:00 committed by GitHub
parent 30dc25db0e
commit 8063fde0d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 7 deletions

View File

@ -294,7 +294,9 @@ const privateNameHandlerSpec = {
value,
]);
}
return t.callExpression(file.addHelper("classPrivateMethodSet"), []);
return t.callExpression(file.addHelper("readOnlyError"), [
t.stringLiteral(`#${name}`),
]);
}
return t.callExpression(file.addHelper("classPrivateFieldSet"), [
this.receiver(member),

View File

@ -2092,11 +2092,14 @@ helpers.classPrivateMethodGet = helper("7.1.6")`
}
`;
helpers.classPrivateMethodSet = helper("7.1.6")`
if (!process.env.BABEL_8_BREAKING) {
// Use readOnlyError instead
helpers.classPrivateMethodSet = helper("7.1.6")`
export default function _classPrivateMethodSet() {
throw new TypeError("attempted to reassign private method");
}
`;
`;
}
helpers.wrapRegExp = helper("7.2.6")`
import wrapNativeSuper from "wrapNativeSuper";

View File

@ -14,7 +14,7 @@ class Cl {
value: 0
});
babelHelpers.classPrivateMethodSet();
babelHelpers.readOnlyError("#privateFieldValue");
}
}

View File

@ -0,0 +1,9 @@
class A {
#method() {}
run() {
this.#method = 2;
}
}
expect(() => new A().run()).toThrow(TypeError);

View File

@ -0,0 +1,7 @@
class A {
#method() {}
run() {
this.#method = 2;
}
}

View File

@ -0,0 +1,14 @@
var _method = new WeakSet();
class A {
constructor() {
_method.add(this);
}
run() {
babelHelpers.readOnlyError("#method");
}
}
var _method2 = function _method2() {};