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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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") { | ||||||
return call; | ||||||
} else { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This 'else' is not needed. You already return in the line above. |
||||||
throw new TypeError("Invalid attempt to return primitive value"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on matching v8's error?
Suggested change
|
||||||
} | ||||||
} | ||||||
|
||||||
return assertThisInitialized(self); | ||||||
} | ||||||
`; | ||||||
|
10 changes: 10 additions & 0 deletions
10
...ugin-transform-classes/test/fixtures/spec/derived-constructor-no-primitive-return/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Bar {} | ||
|
||
class Foo extends Bar { | ||
constructor() { | ||
super(); | ||
return 3; | ||
} | ||
} | ||
|
||
expect(() => new Foo()).toThrow("Invalid attempt to return primitive value"); |
6 changes: 6 additions & 0 deletions
6
...gin-transform-classes/test/fixtures/spec/derived-constructor-no-primitive-return/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Foo extends Bar { | ||
constructor() { | ||
super(); | ||
return 3; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...in-transform-classes/test/fixtures/spec/derived-constructor-no-primitive-return/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var Foo = /*#__PURE__*/function (_Bar) { | ||
"use strict"; | ||
|
||
babelHelpers.inherits(Foo, _Bar); | ||
|
||
var _super = babelHelpers.createSuper(Foo); | ||
|
||
function Foo() { | ||
var _this; | ||
|
||
babelHelpers.classCallCheck(this, Foo); | ||
_this = _super.call(this); | ||
return babelHelpers.possibleConstructorReturn(_this, 3); | ||
} | ||
|
||
return Foo; | ||
}(Bar); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic doesn't work if the constructor returns, for example,
0
ornull
(can you add a test for a falsy value?).It should be something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My version is 3 bytes shorter when using terser 😛
Also, I think yours incorrectly allows returning
null
.This comment was marked as off-topic.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but use of 'typeof' is a bad idea and kill perf.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you suggest to use instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well. 'typeof obj === 'object';' is almost ~50% slower than other 'typeof'. And 'typeof number' return NaN even if it's not an number - in some cases.
There exist 2 popular utility libraries that has an optimized check for this.
Personally I would have used some kind of meta data and bitwise masks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only found
is-object
(which still usestypeof === "object"
) andlodash.isObject
(which also usestypeof
and checks if it's"object"
). Why wouldtypeof === "object"
be slower than the other types?o
can be any possible JS value present in the JS code of our users, we cannot use bitwise maskThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot the history behind this, but AngularJS had some good checks in their earlier versions. 1x of their library. I think they checked against the constructor. Also Google had a library a few years back - forgot the name - with fast checks.