Skip to content

Commit

Permalink
fix: throw error when accessing private method without a getter (#12689)
Browse files Browse the repository at this point in the history
* fix: class private accessor without getter

* Provide default return if 'writeOnlyError' is not available

* Add warning for old `@babel/helpers`

* Add missing helper call
  • Loading branch information
fedeci authored Jan 26, 2021
1 parent b727e67 commit 5a020b2
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/babel-helper-create-class-features-plugin/src/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ const privateNameHandlerSpec = {

if (isMethod) {
if (isAccessor) {
if (!getId && setId) {
if (file.availableHelper("writeOnlyError")) {
return t.callExpression(file.addHelper("writeOnlyError"), [
t.stringLiteral(name),
]);
}
console.warn(
`@babel/helpers is outdated, update it to silence this warning.`,
);
}
return t.callExpression(file.addHelper("classPrivateFieldGet"), [
this.receiver(member),
t.cloneNode(id),
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-helpers/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,12 @@ helpers.readOnlyError = helper("7.0.0-beta.0")`
}
`;

helpers.writeOnlyError = helper("7.12.12")`
export default function _writeOnlyError(name) {
throw new TypeError("\\"" + name + "\\" is write-only");
}
`;

helpers.classNameTDZError = helper("7.0.0-beta.0")`
export default function _classNameTDZError(name) {
throw new Error("Class \\"" + name + "\\" cannot be referenced in computed property keys.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Cl {
}

constructor() {
expect(this.#privateFieldValue).toBeUndefined();
expect(() => this.#privateFieldValue).toThrow(TypeError);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Cl {
value: 0
});

this.publicField = babelHelpers.classPrivateFieldGet(this, _privateFieldValue);
this.publicField = babelHelpers.writeOnlyError("privateFieldValue");
}

}
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-runtime-corejs2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"./helpers/taggedTemplateLiteral": "./helpers/taggedTemplateLiteral.js",
"./helpers/taggedTemplateLiteralLoose": "./helpers/taggedTemplateLiteralLoose.js",
"./helpers/readOnlyError": "./helpers/readOnlyError.js",
"./helpers/writeOnlyError": "./helpers/writeOnlyError.js",
"./helpers/classNameTDZError": "./helpers/classNameTDZError.js",
"./helpers/temporalUndefined": "./helpers/temporalUndefined.js",
"./helpers/tdz": "./helpers/tdz.js",
Expand Down Expand Up @@ -140,6 +141,7 @@
"./helpers/esm/taggedTemplateLiteral": "./helpers/esm/taggedTemplateLiteral.js",
"./helpers/esm/taggedTemplateLiteralLoose": "./helpers/esm/taggedTemplateLiteralLoose.js",
"./helpers/esm/readOnlyError": "./helpers/esm/readOnlyError.js",
"./helpers/esm/writeOnlyError": "./helpers/esm/writeOnlyError.js",
"./helpers/esm/classNameTDZError": "./helpers/esm/classNameTDZError.js",
"./helpers/esm/temporalUndefined": "./helpers/esm/temporalUndefined.js",
"./helpers/esm/tdz": "./helpers/esm/tdz.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-runtime-corejs3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"./helpers/taggedTemplateLiteral": "./helpers/taggedTemplateLiteral.js",
"./helpers/taggedTemplateLiteralLoose": "./helpers/taggedTemplateLiteralLoose.js",
"./helpers/readOnlyError": "./helpers/readOnlyError.js",
"./helpers/writeOnlyError": "./helpers/writeOnlyError.js",
"./helpers/classNameTDZError": "./helpers/classNameTDZError.js",
"./helpers/temporalUndefined": "./helpers/temporalUndefined.js",
"./helpers/tdz": "./helpers/tdz.js",
Expand Down Expand Up @@ -139,6 +140,7 @@
"./helpers/esm/taggedTemplateLiteral": "./helpers/esm/taggedTemplateLiteral.js",
"./helpers/esm/taggedTemplateLiteralLoose": "./helpers/esm/taggedTemplateLiteralLoose.js",
"./helpers/esm/readOnlyError": "./helpers/esm/readOnlyError.js",
"./helpers/esm/writeOnlyError": "./helpers/esm/writeOnlyError.js",
"./helpers/esm/classNameTDZError": "./helpers/esm/classNameTDZError.js",
"./helpers/esm/temporalUndefined": "./helpers/esm/temporalUndefined.js",
"./helpers/esm/tdz": "./helpers/esm/tdz.js",
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"./helpers/taggedTemplateLiteral": "./helpers/taggedTemplateLiteral.js",
"./helpers/taggedTemplateLiteralLoose": "./helpers/taggedTemplateLiteralLoose.js",
"./helpers/readOnlyError": "./helpers/readOnlyError.js",
"./helpers/writeOnlyError": "./helpers/writeOnlyError.js",
"./helpers/classNameTDZError": "./helpers/classNameTDZError.js",
"./helpers/temporalUndefined": "./helpers/temporalUndefined.js",
"./helpers/tdz": "./helpers/tdz.js",
Expand Down Expand Up @@ -139,6 +140,7 @@
"./helpers/esm/taggedTemplateLiteral": "./helpers/esm/taggedTemplateLiteral.js",
"./helpers/esm/taggedTemplateLiteralLoose": "./helpers/esm/taggedTemplateLiteralLoose.js",
"./helpers/esm/readOnlyError": "./helpers/esm/readOnlyError.js",
"./helpers/esm/writeOnlyError": "./helpers/esm/writeOnlyError.js",
"./helpers/esm/classNameTDZError": "./helpers/esm/classNameTDZError.js",
"./helpers/esm/temporalUndefined": "./helpers/esm/temporalUndefined.js",
"./helpers/esm/tdz": "./helpers/esm/tdz.js",
Expand Down

0 comments on commit 5a020b2

Please sign in to comment.