Skip to content
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
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>
  • Loading branch information
BridgeAR committed Nov 29, 2021
commit 83c189f365eb6a4967cfb0f2b84bc7c0624c6d76
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