Reuse the readOnlyError helper for private methods (#12792)
This commit is contained in:
parent
30dc25db0e
commit
8063fde0d8
@ -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),
|
||||
|
||||
@ -2092,11 +2092,14 @@ helpers.classPrivateMethodGet = helper("7.1.6")`
|
||||
}
|
||||
`;
|
||||
|
||||
helpers.classPrivateMethodSet = helper("7.1.6")`
|
||||
export default function _classPrivateMethodSet() {
|
||||
throw new TypeError("attempted to reassign private method");
|
||||
}
|
||||
`;
|
||||
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";
|
||||
|
||||
@ -14,7 +14,7 @@ class Cl {
|
||||
value: 0
|
||||
});
|
||||
|
||||
babelHelpers.classPrivateMethodSet();
|
||||
babelHelpers.readOnlyError("#privateFieldValue");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
class A {
|
||||
#method() {}
|
||||
|
||||
run() {
|
||||
this.#method = 2;
|
||||
}
|
||||
}
|
||||
|
||||
expect(() => new A().run()).toThrow(TypeError);
|
||||
@ -0,0 +1,7 @@
|
||||
class A {
|
||||
#method() {}
|
||||
|
||||
run() {
|
||||
this.#method = 2;
|
||||
}
|
||||
}
|
||||
14
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/read-only/output.js
vendored
Normal file
14
packages/babel-plugin-proposal-private-methods/test/fixtures/private-method/read-only/output.js
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
var _method = new WeakSet();
|
||||
|
||||
class A {
|
||||
constructor() {
|
||||
_method.add(this);
|
||||
}
|
||||
|
||||
run() {
|
||||
babelHelpers.readOnlyError("#method");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var _method2 = function _method2() {};
|
||||
Loading…
x
Reference in New Issue
Block a user