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: compare RegExp.lastIndex while using deep equal checks #41020

Merged
merged 1 commit into from
Dec 2, 2021
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
10 changes: 10 additions & 0 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ An alias of [`assert.ok()`][].
<!-- YAML
added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41020
description: Regular expressions lastIndex property is now compared as well.
- version:
- v16.0.0
- v14.18.0
Expand Down Expand Up @@ -539,6 +542,8 @@ are also recursively evaluated by the following rules.
objects.
* [`Symbol`][] properties are not compared.
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values.
* [`RegExp`][] lastIndex, flags and source are always compared, even if these
are not enumerable properties.

The following example does not throw an [`AssertionError`][] because the
primitives are considered equal by the [Abstract Equality Comparison][]
Expand Down Expand Up @@ -642,6 +647,9 @@ parameter is an instance of an [`Error`][] then it will be thrown instead of the
<!-- YAML
added: v1.2.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41020
description: Regular expressions lastIndex property is now compared as well.
- version: v9.0.0
pr-url: https://github.com/nodejs/node/pull/15169
description: Enumerable symbol properties are now compared.
Expand Down Expand Up @@ -697,6 +705,8 @@ are recursively evaluated also by the following rules.
reference.
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See
below for further details.
* [`RegExp`][] lastIndex, flags and source are always compared, even if these
are not enumerable properties.

```mjs
import assert from 'assert/strict';
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ const kIsMap = 3;

// Check if they have the same source and flags
function areSimilarRegExps(a, b) {
return a.source === b.source && a.flags === b.flags;
return a.source === b.source &&
a.flags === b.flags &&
a.lastIndex === b.lastIndex;
}

function areSimilarFloatArrays(a, b) {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ assertNotDeepOrStrict(/a/igm, /a/im);
{
const re1 = /a/g;
re1.lastIndex = 3;
assert.deepEqual(re1, /a/g);
assert.notDeepEqual(re1, /a/g);
}

assert.deepEqual(4, '4');
Expand Down Expand Up @@ -852,7 +852,7 @@ assert.throws(
{
const re1 = /a/;
re1.lastIndex = 3;
assert.deepStrictEqual(re1, /a/);
assert.notDeepStrictEqual(re1, /a/);
}

assert.throws(
Expand Down