Skip to content

Commit

Permalink
Reuse the readOnlyError helper for private methods (#12792)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 12, 2021
1 parent 30dc25d commit 8063fde
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
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() {};

0 comments on commit 8063fde

Please sign in to comment.