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: https://github.com/nodejs/node/issues/28766

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Ruben Bridgewater 2021-12-02 09:01:40 +01:00 committed by GitHub
parent 34fdea1c57
commit dab8ab2837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View File

@ -465,6 +465,9 @@ An alias of [`assert.ok()`][].
<!-- YAML <!-- YAML
added: v0.1.21 added: v0.1.21
changes: changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/41020
description: Regular expressions lastIndex property is now compared as well.
- version: - version:
- v16.0.0 - v16.0.0
- v14.18.0 - v14.18.0
@ -539,6 +542,8 @@ are also recursively evaluated by the following rules.
objects. objects.
* [`Symbol`][] properties are not compared. * [`Symbol`][] properties are not compared.
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. * [`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 The following example does not throw an [`AssertionError`][] because the
primitives are considered equal by the [Abstract Equality Comparison][] 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
<!-- YAML <!-- YAML
added: v1.2.0 added: v1.2.0
changes: 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 - version: v9.0.0
pr-url: https://github.com/nodejs/node/pull/15169 pr-url: https://github.com/nodejs/node/pull/15169
description: Enumerable symbol properties are now compared. description: Enumerable symbol properties are now compared.
@ -697,6 +705,8 @@ are recursively evaluated also by the following rules.
reference. reference.
* [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See * [`WeakMap`][] and [`WeakSet`][] comparison does not rely on their values. See
below for further details. below for further details.
* [`RegExp`][] lastIndex, flags and source are always compared, even if these
are not enumerable properties.
```mjs ```mjs
import assert from 'assert/strict'; import assert from 'assert/strict';

View File

@ -63,7 +63,9 @@ const kIsMap = 3;
// Check if they have the same source and flags // Check if they have the same source and flags
function areSimilarRegExps(a, b) { 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) { function areSimilarFloatArrays(a, b) {

View File

@ -712,7 +712,7 @@ assertNotDeepOrStrict(/a/igm, /a/im);
{ {
const re1 = /a/g; const re1 = /a/g;
re1.lastIndex = 3; re1.lastIndex = 3;
assert.deepEqual(re1, /a/g); assert.notDeepEqual(re1, /a/g);
} }
assert.deepEqual(4, '4'); assert.deepEqual(4, '4');
@ -852,7 +852,7 @@ assert.throws(
{ {
const re1 = /a/; const re1 = /a/;
re1.lastIndex = 3; re1.lastIndex = 3;
assert.deepStrictEqual(re1, /a/); assert.notDeepStrictEqual(re1, /a/);
} }
assert.throws( assert.throws(