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

assert,util: correct comparison when both contain same reference #53431

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,9 @@ function setEquiv(a, b, strict, memo) {
for (const val of b) {
// Primitive values have already been handled above.
if (typeof val === 'object' && val !== null) {
if (!setHasEqualElement(set, val, strict, memo))
if (!a.has(val) && !setHasEqualElement(set, val, strict, memo)) {
return false;
}
} else if (!strict &&

This comment was marked as resolved.

Copy link
Member Author

Choose a reason for hiding this comment

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

Which if are you matching with which else?

Copy link
Member

Choose a reason for hiding this comment

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

The else will be reached as it's the inner if. It could be optimized code wise (there is some duplication that can be prevented), but that could also be done in a later PR to check what implementation is best.

Copy link
Contributor

@chharvey chharvey Jun 13, 2024

Choose a reason for hiding this comment

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

@lemire Oops, my bad, I was matching the else on line 507 with the wrong if, the one on line 505. Indentation is hard 😜

!a.has(val) &&
!setHasEqualElement(set, val, strict, memo)) {
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,11 @@ assertOnlyDeepEqual(
new Map([[undefined, null], ['+000', 2n]]),
new Map([[null, undefined], [false, '2']]),
);

const xarray = ['x'];
assertDeepAndStrictEqual(
new Set([xarray, ['y']]),
new Set([xarray, ['y']])
);
assertOnlyDeepEqual(
new Set([null, '', 1n, 5, 2n, false]),
new Set([undefined, 0, 5n, true, '2', '-000'])
Expand Down