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

Derived constructors should not be allowed to return primitives #13571

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -615,9 +615,14 @@ helpers.possibleConstructorReturn = helper("7.0.0-beta.0")`
import assertThisInitialized from "assertThisInitialized";

export default function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
if (call) {
if(typeof call === "object" || typeof call === "function"){
dhrubesh marked this conversation as resolved.
Show resolved Hide resolved
return call;
}else{
dhrubesh marked this conversation as resolved.
Show resolved Hide resolved
throw new TypeError("Invalid attempt to return primitive value");
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on matching v8's error?

Suggested change
throw new TypeError("Invalid attempt to return primitive value");
throw new TypeError("Derived constructors may only return object or undefined");

}
}

return assertThisInitialized(self);
}
`;
Expand Down
@@ -0,0 +1,10 @@
class Bar {}

dhrubesh marked this conversation as resolved.
Show resolved Hide resolved
class Foo extends Bar {
constructor() {
super();
return 3;
}
}

expect(() => new Foo()).toThrow("Invalid attempt to return primitive value");
@@ -0,0 +1,6 @@
class Foo extends Bar {
dhrubesh marked this conversation as resolved.
Show resolved Hide resolved
constructor() {
super();
return 3;
}
}