Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upSupport diffing keys named like Object.prototype properties #59
Conversation
#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.
|
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. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Fixes #58.
Prior to this patch, the code assumed that input objects have a
hasOwnPropertyfunction, 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.hasOwnPropertywhich usesObject.prototype.hasOwnProperty.callinstead of assuming anything about the input object, and replacing all directinputObject.hasOwnPropertycalls 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.