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

Fix bug in Record.equals when comparing against Map #1903

Merged
merged 1 commit into from
May 9, 2022

Conversation

jmtoung
Copy link
Contributor

@jmtoung jmtoung commented May 8, 2022

This commit fixes a bug introduced by this change, which removed a check that '._keys' exists on the object being compared in Record.equals. This leads to a runtime uncaught error when a Record instance is compared against a Map instance.

Copy link
Collaborator

@leebyron leebyron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thank you for spotting and resolving this

src/Record.js Outdated
this === other || (other && recordSeq(this).equals(recordSeq(other)))
this === other ||
(other &&
this._keys &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._keys should always be set because the instance doing the comparison is a Record

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix

it('if compared against Map should return false', () => {
const MyType = Record({ a: 1, b: 2 });
const t1 = MyType();
expect(t1.equals(Map({ a: 1, b: 2 }))).toBeFalsy();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness can you add a test which flips the operands to ensure the symmetric property?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

src/Record.js Outdated
this === other ||
(other &&
this._keys &&
other._keys &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the proper way to do this should be isRecord(other)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely semantically more obvious what the check is doing but it depends on the guarantee that Record will always have _keys instantiated, which we will be alerted on if that is ever violated with this newly added unit test.

@@ -120,7 +120,8 @@ export class Record {

equals(other) {
return (
this === other || (other && recordSeq(this).equals(recordSeq(other)))
this === other ||
(isRecord(other) && recordSeq(this).equals(recordSeq(other)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More correct and more clear. Nice!

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.

None yet

2 participants