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

Reuse the readOnlyError helper for private methods #12792

Merged
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
Expand Up @@ -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),
Expand Down
13 changes: 8 additions & 5 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -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";
Expand Down
Expand Up @@ -14,7 +14,7 @@ class Cl {
value: 0
});

babelHelpers.classPrivateMethodSet();
babelHelpers.readOnlyError("#privateFieldValue");
}

}
Expand Down
@@ -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;
}
}
@@ -0,0 +1,14 @@
var _method = new WeakSet();

class A {
constructor() {
_method.add(this);
}

run() {
babelHelpers.readOnlyError("#method");
}

}

var _method2 = function _method2() {};