Skip to content

Commit

Permalink
assert,util: compare RegExp.lastIndex while using deep equal checks
Browse files Browse the repository at this point in the history
Compare the `lastIndex` property of regular expressions next to the
flags and source property.

Fixes: #28766

Signed-off-by: Ruben Bridgewater <[email protected]>

PR-URL: #41020
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
  • Loading branch information
BridgeAR authored Dec 2, 2021
1 parent 34fdea1 commit dab8ab2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
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

0 comments on commit dab8ab2

Please sign in to comment.