Skip to content

Commit 6bcf65d

Browse files
Trottaddaleax
authored andcommitted
lib,test: use regular expression literals
Replace RegExp constructors with regular expression literals where possible. PR-URL: #12807 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent feb90d3 commit 6bcf65d

12 files changed

+25
-33
lines changed

lib/_http_outgoing.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const outHeadersKey = require('internal/http').outHeadersKey;
3535
const CRLF = common.CRLF;
3636
const debug = common.debug;
3737

38-
var RE_FIELDS = new RegExp('^(?:Connection|Transfer-Encoding|Content-Length|' +
39-
'Date|Expect|Trailer|Upgrade)$', 'i');
38+
var RE_FIELDS =
39+
/^(?:Connection|Transfer-Encoding|Content-Length|Date|Expect|Trailer|Upgrade)$/i;
4040
var RE_CONN_VALUES = /(?:^|\W)close|upgrade(?:$|\W)/ig;
4141
var RE_TE_CHUNKED = common.chunkExpression;
4242

test/parallel/test-buffer-alloc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ Buffer.poolSize = ps;
927927
assert.throws(() => Buffer.allocUnsafe(10).copy(),
928928
/TypeError: argument should be a Buffer/);
929929

930-
const regErrorMsg = new RegExp('First argument must be a string, Buffer, ' +
931-
'ArrayBuffer, Array, or array-like object\\.');
930+
const regErrorMsg =
931+
/First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object\./;
932932

933933
assert.throws(() => Buffer.from(), regErrorMsg);
934934
assert.throws(() => Buffer.from(null), regErrorMsg);

test/parallel/test-crypto-dh.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ assert.strictEqual(secret2.toString('base64'), secret1);
2323
assert.strictEqual(dh1.verifyError, 0);
2424
assert.strictEqual(dh2.verifyError, 0);
2525

26-
const argumentsError = new RegExp('^TypeError: First argument should be ' +
27-
'number, string, Buffer, TypedArray, or ' +
28-
'DataView$');
26+
const argumentsError =
27+
/^TypeError: First argument should be number, string, Buffer, TypedArray, or DataView$/;
2928

3029
assert.throws(() => {
3130
crypto.createDiffieHellman([0x1, 0x2]);
@@ -61,8 +60,7 @@ const secret3 = dh3.computeSecret(key2, 'hex', 'base64');
6160
assert.strictEqual(secret1, secret3);
6261

6362
const wrongBlockLength =
64-
new RegExp('^Error: error:0606506D:digital envelope' +
65-
' routines:EVP_DecryptFinal_ex:wrong final block length$');
63+
/^Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length$/;
6664

6765
// Run this one twice to make sure that the dh3 clears its error properly
6866
{

test/parallel/test-crypto-rsa-dsa.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const dsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_dsa_privkey.pem',
2626
const dsaKeyPemEncrypted = fs.readFileSync(
2727
common.fixturesDir + '/test_dsa_privkey_encrypted.pem', 'ascii');
2828

29-
const decryptError = new RegExp('^Error: error:06065064:digital envelope ' +
30-
'routines:EVP_DecryptFinal_ex:bad decrypt$');
29+
const decryptError =
30+
/^Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt$/;
3131

3232
// Test RSA encryption/decryption
3333
{

test/parallel/test-dgram-send-address-types.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ client.send(buf, port, undefined, onMessage);
3030
// valid address: not provided
3131
client.send(buf, port, onMessage);
3232

33-
const expectedError = new RegExp('^TypeError: Invalid arguments: address ' +
34-
'must be a nonempty string or falsy$');
33+
const expectedError =
34+
/^TypeError: Invalid arguments: address must be a nonempty string or falsy$/;
3535

3636
// invalid address: object
3737
assert.throws(() => {

test/parallel/test-fs-write-stream-throw-type-error.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const assert = require('assert');
44
const fs = require('fs');
55
const path = require('path');
66

7-
const numberError = new RegExp('^TypeError: "options" must be a string ' +
8-
'or an object, got number instead\\.$');
7+
const numberError =
8+
/^TypeError: "options" must be a string or an object, got number instead\.$/;
99

10-
const booleanError = new RegExp('^TypeError: "options" must be a string ' +
11-
'or an object, got boolean instead\\.$');
10+
const booleanError =
11+
/^TypeError: "options" must be a string or an object, got boolean instead\.$/;
1212

1313
const example = path.join(common.tmpDir, 'dummy');
1414

test/parallel/test-tls-env-bad-extra-ca.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ fork(__filename, opts)
3333
assert.strictEqual(status, 0, 'client did not succeed in connecting');
3434
}))
3535
.on('close', common.mustCall(function() {
36-
assert(stderr.match(new RegExp(
37-
'Warning: Ignoring extra certs from.*no-such-file-exists' +
38-
'.* load failed:.*No such file or directory'
39-
)), stderr);
36+
assert(stderr.match(
37+
/Warning: Ignoring extra certs from.*no-such-file-exists.* load failed:.*No such file or directory/
38+
), stderr);
4039
}))
4140
.stderr.setEncoding('utf8').on('data', function(str) {
4241
stderr += str;

test/parallel/test-tls-key-mismatch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ if (!common.hasCrypto) {
2929
const assert = require('assert');
3030
const tls = require('tls');
3131
const fs = require('fs');
32-
const errorMessageRegex = new RegExp('^Error: error:0B080074:x509 ' +
33-
'certificate routines:X509_check_private_key:key values mismatch$');
32+
const errorMessageRegex =
33+
/^Error: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch$/;
3434

3535
const options = {
3636
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),

test/parallel/test-util-inherits.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ require('../common');
44
const assert = require('assert');
55
const inherits = require('util').inherits;
66
const errCheck =
7-
new RegExp('^TypeError: The super constructor to "inherits" must not be ' +
8-
'null or undefined$');
7+
/^TypeError: The super constructor to "inherits" must not be null or undefined$/;
98

109

1110
// super constructor

test/parallel/test-whatwg-url-properties.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ assert.strictEqual(url.searchParams, oldParams); // [SameObject]
4444
// non-writable property should throw.
4545
// Note: this error message is subject to change in V8 updates
4646
assert.throws(() => url.origin = 'http://foo.bar.com:22',
47-
new RegExp('TypeError: Cannot set property origin of' +
48-
' \\[object URL\\] which has only a getter'));
47+
/TypeError: Cannot set property origin of \[object URL\] which has only a getter$/);
4948
assert.strictEqual(url.origin, 'http://foo.bar.com:21');
5049
assert.strictEqual(url.toString(),
5150
'http://user:[email protected]:21/aaa/zzz?l=25#test');
@@ -120,8 +119,7 @@ assert.strictEqual(url.hash, '#abcd');
120119
// non-writable property should throw.
121120
// Note: this error message is subject to change in V8 updates
122121
assert.throws(() => url.searchParams = '?k=88',
123-
new RegExp('TypeError: Cannot set property searchParams of' +
124-
' \\[object URL\\] which has only a getter'));
122+
/^TypeError: Cannot set property searchParams of \[object URL\] which has only a getter$/);
125123
assert.strictEqual(url.searchParams, oldParams);
126124
assert.strictEqual(url.toString(),
127125
'https://user2:[email protected]:23/aaa/bbb?k=99#abcd');

test/parallel/test-zlib-deflate-constructors.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,5 @@ assert.throws(
107107
// Throws if opts.dictionary is not a Buffer
108108
assert.throws(
109109
() => { new zlib.Deflate({dictionary: 'not a buffer'}); },
110-
new RegExp('^TypeError: Invalid dictionary: it should be a Buffer, ' +
111-
'TypedArray, or DataView$')
110+
/^TypeError: Invalid dictionary: it should be a Buffer, TypedArray, or DataView$/
112111
);

test/parallel/test-zlib-not-string-or-buffer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ require('../common');
77
const assert = require('assert');
88
const zlib = require('zlib');
99

10-
const expected = new RegExp('^TypeError: "buffer" argument must be a string, ' +
11-
'Buffer, TypedArray, or DataView$');
10+
const expected = /^TypeError: "buffer" argument must be a string, Buffer, TypedArray, or DataView$/;
1211

1312
assert.throws(() => { zlib.deflateSync(undefined); }, expected);
1413
assert.throws(() => { zlib.deflateSync(null); }, expected);

0 commit comments

Comments
 (0)