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

Support diffing keys named like Object.prototype properties #59

Merged
merged 3 commits into from
Jan 25, 2022

Conversation

anko
Copy link
Contributor

@anko anko commented Oct 9, 2020

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 through utils.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 with

Uncaught TypeError: r.hasOwnProperty is not a function

The solution taken in this PR 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.


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.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling e310b60 on anko:no-prototype-builtins into 6296889 on mattphillips:master.

Copy link

@foolip foolip left a 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 mattphillips force-pushed the no-prototype-builtins branch from cc08b6e to e310b60 Compare January 25, 2022 13:55
anko added 3 commits January 25, 2022 13:58
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 mattphillips force-pushed the no-prototype-builtins branch from e310b60 to 02e3984 Compare January 25, 2022 14:00
@mattphillips mattphillips merged commit 98d05ee into mattphillips:master Jan 25, 2022
@mattphillips
Copy link
Owner

Thanks @anko sorry for the massive delay in looking at this 😅

@anko anko deleted the no-prototype-builtins branch January 25, 2022 16:13
@anko
Copy link
Contributor Author

anko commented Jan 25, 2022

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comparing object with "hasOwnProperty" keys fails
4 participants