-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Support diffing keys named like Object.prototype properties #59
Merged
Conversation
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
foolip
approved these changes
Oct 21, 2020
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 have tried this fix in the context where I encountered the problem, and confirm that it runs correctly now, and that the resulting diffs look correct.
mattphillips
force-pushed
the
no-prototype-builtins
branch
from
January 25, 2022 13:55
cc08b6e
to
e310b60
Compare
mattphillips#58 "Comparing object with "hasOwnProperty" keys fails"
Fixes mattphillips#58. Previously, the code assumed that input objects have a `hasOwnProperty` function, or that at least they will acquire one when passed through `utils.properObject`. However, this assumption is flawed: As noted in issue mattphillips#58, when given input objects have a property on them called `hasOwnProperty`, it overrides the prototype's function property that the code relied on, causing any diffing function to error out with Uncaught TypeError: r.hasOwnProperty is not a function The solution taken here is to forget about `utils.properObject`, and instead introduce `utils.hasOwnProperty` which uses `Object.prototype.hasOwnProperty.call` instead of assuming anything about the input object, and replacing all direct `inputObject.hasOwnProperty` calls with it instead.
The way to prevent issues like mattphillips#58 is not obvious, and I would expect not all programmers to have memorised the contents of `Object.prototype`, so I think it would be prudent to check for such issues automatically. The no-prototype-builtins rule is enforced automatically by the set of rules enabled in `.eslintrc.json` by "extends": "eslint:recommended" It's a great sign that all the other enabled rules also pass without modifications! I've added the eslint invocation to `package.json`'s `posttest` field. This should make the check minimally intrusive while developing, as it will only run when the functional tests (via `jest`) pass first.
mattphillips
force-pushed
the
no-prototype-builtins
branch
from
January 25, 2022 14:00
e310b60
to
02e3984
Compare
Thanks @anko sorry for the massive delay in looking at this 😅 |
All good. Nobody's made of time. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #58.
Prior to this patch, the code assumed that input objects have a
hasOwnProperty
function, or that at least they will acquire one when passed throughutils.properObject
.However, this assumption is flawed: As noted in issue #58, when given input objects have a property on them called
hasOwnProperty
, it overrides the prototype's function property that the code relied on, causing any diffing function to error out withThe solution taken in this PR is to forget about
utils.properObject
, and instead introduceutils.hasOwnProperty
which usesObject.prototype.hasOwnProperty.call
instead of assuming anything about the input object, and replacing all directinputObject.hasOwnProperty
calls with it instead.Tests are separate in 401457d, for ease of checking that they fail initially.
Also separate in e310b60 is automatic checking for this type of issue through eslint, which includes the no-prototype-builtins rule in its default recommended set, as suggested by @papb. All other eslint rules passed without modification. It is in a separate commit in case you are not interested in taking on a dev dependency.