Skip to content

Commit dab8ab2

Browse files
authored
assert,util: compare RegExp.lastIndex while using deep equal checks
Compare the `lastIndex` property of regular expressions next to the flags and source property. Fixes: #28766 Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: #41020 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
1 parent 34fdea1 commit dab8ab2

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

doc/api/assert.md

+10
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ An alias of [`assert.ok()`][].
465465
<!-- YAML
466466
added: v0.1.21
467467
changes:
468+
- version: REPLACEME
469+
pr-url: https://github.com/nodejs/node/pull/41020
470+
description: Regular expressions lastIndex property is now compared as well.
468471
- version:
469472
- v16.0.0
470473
- v14.18.0
@@ -539,6 +542,8 @@ are also recursively evaluated by the following rules.
539542
objects.
540543
* [`Symbol`][] properties are not compared.
541544
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values.
545+
* [`RegExp`][] lastIndex, flags and source are always compared, even if these
546+
are not enumerable properties.
542547

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

701711
```mjs
702712
import assert from 'assert/strict';

lib/internal/util/comparisons.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ const kIsMap = 3;
6363

6464
// Check if they have the same source and flags
6565
function areSimilarRegExps(a, b) {
66-
return a.source === b.source && a.flags === b.flags;
66+
return a.source === b.source &&
67+
a.flags === b.flags &&
68+
a.lastIndex === b.lastIndex;
6769
}
6870

6971
function areSimilarFloatArrays(a, b) {

test/parallel/test-assert-deep.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ assertNotDeepOrStrict(/a/igm, /a/im);
712712
{
713713
const re1 = /a/g;
714714
re1.lastIndex = 3;
715-
assert.deepEqual(re1, /a/g);
715+
assert.notDeepEqual(re1, /a/g);
716716
}
717717

718718
assert.deepEqual(4, '4');
@@ -852,7 +852,7 @@ assert.throws(
852852
{
853853
const re1 = /a/;
854854
re1.lastIndex = 3;
855-
assert.deepStrictEqual(re1, /a/);
855+
assert.notDeepStrictEqual(re1, /a/);
856856
}
857857

858858
assert.throws(

0 commit comments

Comments
 (0)