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
fix: coerce pollutes argv #2161
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 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
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
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.
I believe this can just be:
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'm afraid this is not correct and it should be
Object.prototype.hasOwnProperty
.The changes relating to this should be reverted.
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.
Object.hasOwnProperty.call({a: 5}, 'a')
andObject.prototype.hasOwnProperty.call({a: 5}, 'a')
both returntrue
. (I thought there might be an issue when using a null object, but both returnfalse
whenObject.create(null)
is used as the first argument.) I don't know of any scenarios where their behavior differs. Do you have an example where it could cause problems?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.
Also,
Object.hasOwnProperty.call === Object.prototype.hasOwnProperty.call
is true. I'm assuming they are a reference to the same objectThere 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.
Notwithstanding the fact that this convention of using the
prototype
instead of theObject
class directly is widely adopted.It's the same reason to not use
{a: 1}.hasOwnProperty('a')
. The function could have been overridden via a global polyfill or patch meaning you're not using native behaviour.I assume that
{}.hasOwnProperty.call === Object.hasOwnProperty.call
as well as the prototype. The point is that they can be overridden and are, thus, unreliable to use unless you use the underlying prototype.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 mean,
Object.prototype
can just as easily be overwritten:We would have to rewrite all of yargs to use primordials that are frozen at Node.js' startup to really avoid this issue.
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 is how Node.js itself approaches the problem:
https://github.com/nodejs/node/blob/master/lib/internal/per_context/primordials.js
But it's made easier for them by the fact that they can do so during bootstrapping.
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.
Yeah, this is definitely not an issue that is limited to
yargs
, for better or worse it's a language issue.Whilst I think overriding the built-in methods like
Object.hasOwnProperty()
is pretty crazy, for those crazy enough to do it I think there is enough expectation that the prototype is left alone that it can be used reliably. Hence why it's enforced by standard linting rules in eslint and recommendations elsewhere.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.
@dhensby makes sense to me.