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

Throw a TypeError when reassigning a const #12252

Merged
merged 2 commits into from Oct 25, 2020
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
2 changes: 1 addition & 1 deletion packages/babel-helpers/src/helpers.js
Expand Up @@ -871,7 +871,7 @@ helpers.taggedTemplateLiteralLoose = helper("7.0.0-beta.0")`

helpers.readOnlyError = helper("7.0.0-beta.0")`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't update this to avoid inlining the helper if a user is using an older @babel/runtime, since the plugins aren't relying on the TypeError anyway.

export default function _readOnlyError(name) {
throw new Error("\\"" + name + "\\" is read-only");
throw new TypeError("\\"" + name + "\\" is read-only");
}
`;

Expand Down
@@ -1,4 +1,4 @@
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
function _readOnlyError(name) { throw new TypeError("\"" + name + "\" is read-only"); }

(function () {
var a = "foo";
Expand Down
@@ -1,4 +1,4 @@
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
function _readOnlyError(name) { throw new TypeError("\"" + name + "\" is read-only"); }

var a = 1,
b = 2;
Expand Down
@@ -1,4 +1,4 @@
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
function _readOnlyError(name) { throw new TypeError("\"" + name + "\" is read-only"); }

for (var i = 0; i < 3; i = (_readOnlyError("i"), i + 1)) {
console.log(i);
Expand Down
@@ -1,4 +1,4 @@
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
function _readOnlyError(name) { throw new TypeError("\"" + name + "\" is read-only"); }

var c = 17;
var a = 0;
Expand Down
@@ -1,4 +1,4 @@
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
function _readOnlyError(name) { throw new TypeError("\"" + name + "\" is read-only"); }

var MULTIPLIER = 5;
MULTIPLIER = (_readOnlyError("MULTIPLIER"), "overwrite");
@@ -1,4 +1,4 @@
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
function _readOnlyError(name) { throw new TypeError("\"" + name + "\" is read-only"); }

var MULTIPLIER = 5;

Expand Down
@@ -1,4 +1,4 @@
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
function _readOnlyError(name) { throw new TypeError("\"" + name + "\" is read-only"); }

var a = "str";
_readOnlyError("a"), --a;
@@ -1,4 +1,4 @@
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
function _readOnlyError(name) { throw new TypeError("\"" + name + "\" is read-only"); }

var foo = 1;
_readOnlyError("foo"), foo++;
@@ -0,0 +1,2 @@
const a = 1;
expect(() => { a = 2 }).toThrow(TypeError);