X Tutup
The Wayback Machine - https://web.archive.org/web/20201112181105/https://github.com/mattphillips/deep-object-diff/pull/59
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

Open
wants to merge 3 commits into
base: master
from

Conversation

@anko
Copy link

@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.

anko added 3 commits Oct 9, 2020
#58
"Comparing object with "hasOwnProperty" keys fails"
Fixes #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 #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 #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.
@coveralls
Copy link

@coveralls coveralls commented Oct 9, 2020

Coverage Status

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

@foolip
foolip approved these changes Oct 21, 2020
Copy link

@foolip foolip left a comment

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants
You can’t perform that action at this time.
X Tutup