lib,test: use regular expression literals

Replace RegExp constructors with regular expression literals where
possible.

PR-URL: https://github.com/nodejs/node/pull/12807
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Rich Trott 2017-05-02 23:12:14 -07:00 committed by Anna Henningsen
parent feb90d37ff
commit 6bcf65d4a7
No known key found for this signature in database
GPG Key ID: D8B9F5AEAE84E4CF
12 changed files with 25 additions and 33 deletions

View File

@ -35,8 +35,8 @@ const outHeadersKey = require('internal/http').outHeadersKey;
const CRLF = common.CRLF; const CRLF = common.CRLF;
const debug = common.debug; const debug = common.debug;
var RE_FIELDS = new RegExp('^(?:Connection|Transfer-Encoding|Content-Length|' + var RE_FIELDS =
'Date|Expect|Trailer|Upgrade)$', 'i'); /^(?:Connection|Transfer-Encoding|Content-Length|Date|Expect|Trailer|Upgrade)$/i;
var RE_CONN_VALUES = /(?:^|\W)close|upgrade(?:$|\W)/ig; var RE_CONN_VALUES = /(?:^|\W)close|upgrade(?:$|\W)/ig;
var RE_TE_CHUNKED = common.chunkExpression; var RE_TE_CHUNKED = common.chunkExpression;

View File

@ -927,8 +927,8 @@ Buffer.poolSize = ps;
assert.throws(() => Buffer.allocUnsafe(10).copy(), assert.throws(() => Buffer.allocUnsafe(10).copy(),
/TypeError: argument should be a Buffer/); /TypeError: argument should be a Buffer/);
const regErrorMsg = new RegExp('First argument must be a string, Buffer, ' + const regErrorMsg =
'ArrayBuffer, Array, or array-like object\\.'); /First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object\./;
assert.throws(() => Buffer.from(), regErrorMsg); assert.throws(() => Buffer.from(), regErrorMsg);
assert.throws(() => Buffer.from(null), regErrorMsg); assert.throws(() => Buffer.from(null), regErrorMsg);

View File

@ -23,9 +23,8 @@ assert.strictEqual(secret2.toString('base64'), secret1);
assert.strictEqual(dh1.verifyError, 0); assert.strictEqual(dh1.verifyError, 0);
assert.strictEqual(dh2.verifyError, 0); assert.strictEqual(dh2.verifyError, 0);
const argumentsError = new RegExp('^TypeError: First argument should be ' + const argumentsError =
'number, string, Buffer, TypedArray, or ' + /^TypeError: First argument should be number, string, Buffer, TypedArray, or DataView$/;
'DataView$');
assert.throws(() => { assert.throws(() => {
crypto.createDiffieHellman([0x1, 0x2]); crypto.createDiffieHellman([0x1, 0x2]);
@ -61,8 +60,7 @@ const secret3 = dh3.computeSecret(key2, 'hex', 'base64');
assert.strictEqual(secret1, secret3); assert.strictEqual(secret1, secret3);
const wrongBlockLength = const wrongBlockLength =
new RegExp('^Error: error:0606506D:digital envelope' + /^Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length$/;
' routines:EVP_DecryptFinal_ex:wrong final block length$');
// Run this one twice to make sure that the dh3 clears its error properly // Run this one twice to make sure that the dh3 clears its error properly
{ {

View File

@ -26,8 +26,8 @@ const dsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_dsa_privkey.pem',
const dsaKeyPemEncrypted = fs.readFileSync( const dsaKeyPemEncrypted = fs.readFileSync(
common.fixturesDir + '/test_dsa_privkey_encrypted.pem', 'ascii'); common.fixturesDir + '/test_dsa_privkey_encrypted.pem', 'ascii');
const decryptError = new RegExp('^Error: error:06065064:digital envelope ' + const decryptError =
'routines:EVP_DecryptFinal_ex:bad decrypt$'); /^Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt$/;
// Test RSA encryption/decryption // Test RSA encryption/decryption
{ {

View File

@ -30,8 +30,8 @@ client.send(buf, port, undefined, onMessage);
// valid address: not provided // valid address: not provided
client.send(buf, port, onMessage); client.send(buf, port, onMessage);
const expectedError = new RegExp('^TypeError: Invalid arguments: address ' + const expectedError =
'must be a nonempty string or falsy$'); /^TypeError: Invalid arguments: address must be a nonempty string or falsy$/;
// invalid address: object // invalid address: object
assert.throws(() => { assert.throws(() => {

View File

@ -4,11 +4,11 @@ const assert = require('assert');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const numberError = new RegExp('^TypeError: "options" must be a string ' + const numberError =
'or an object, got number instead\\.$'); /^TypeError: "options" must be a string or an object, got number instead\.$/;
const booleanError = new RegExp('^TypeError: "options" must be a string ' + const booleanError =
'or an object, got boolean instead\\.$'); /^TypeError: "options" must be a string or an object, got boolean instead\.$/;
const example = path.join(common.tmpDir, 'dummy'); const example = path.join(common.tmpDir, 'dummy');

View File

@ -33,10 +33,9 @@ fork(__filename, opts)
assert.strictEqual(status, 0, 'client did not succeed in connecting'); assert.strictEqual(status, 0, 'client did not succeed in connecting');
})) }))
.on('close', common.mustCall(function() { .on('close', common.mustCall(function() {
assert(stderr.match(new RegExp( assert(stderr.match(
'Warning: Ignoring extra certs from.*no-such-file-exists' + /Warning: Ignoring extra certs from.*no-such-file-exists.* load failed:.*No such file or directory/
'.* load failed:.*No such file or directory' ), stderr);
)), stderr);
})) }))
.stderr.setEncoding('utf8').on('data', function(str) { .stderr.setEncoding('utf8').on('data', function(str) {
stderr += str; stderr += str;

View File

@ -29,8 +29,8 @@ if (!common.hasCrypto) {
const assert = require('assert'); const assert = require('assert');
const tls = require('tls'); const tls = require('tls');
const fs = require('fs'); const fs = require('fs');
const errorMessageRegex = new RegExp('^Error: error:0B080074:x509 ' + const errorMessageRegex =
'certificate routines:X509_check_private_key:key values mismatch$'); /^Error: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch$/;
const options = { const options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),

View File

@ -4,8 +4,7 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const inherits = require('util').inherits; const inherits = require('util').inherits;
const errCheck = const errCheck =
new RegExp('^TypeError: The super constructor to "inherits" must not be ' + /^TypeError: The super constructor to "inherits" must not be null or undefined$/;
'null or undefined$');
// super constructor // super constructor

View File

@ -44,8 +44,7 @@ assert.strictEqual(url.searchParams, oldParams); // [SameObject]
// non-writable property should throw. // non-writable property should throw.
// Note: this error message is subject to change in V8 updates // Note: this error message is subject to change in V8 updates
assert.throws(() => url.origin = 'http://foo.bar.com:22', assert.throws(() => url.origin = 'http://foo.bar.com:22',
new RegExp('TypeError: Cannot set property origin of' + /TypeError: Cannot set property origin of \[object URL\] which has only a getter$/);
' \\[object URL\\] which has only a getter'));
assert.strictEqual(url.origin, 'http://foo.bar.com:21'); assert.strictEqual(url.origin, 'http://foo.bar.com:21');
assert.strictEqual(url.toString(), assert.strictEqual(url.toString(),
'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test'); 'http://user:pass@foo.bar.com:21/aaa/zzz?l=25#test');
@ -120,8 +119,7 @@ assert.strictEqual(url.hash, '#abcd');
// non-writable property should throw. // non-writable property should throw.
// Note: this error message is subject to change in V8 updates // Note: this error message is subject to change in V8 updates
assert.throws(() => url.searchParams = '?k=88', assert.throws(() => url.searchParams = '?k=88',
new RegExp('TypeError: Cannot set property searchParams of' + /^TypeError: Cannot set property searchParams of \[object URL\] which has only a getter$/);
' \\[object URL\\] which has only a getter'));
assert.strictEqual(url.searchParams, oldParams); assert.strictEqual(url.searchParams, oldParams);
assert.strictEqual(url.toString(), assert.strictEqual(url.toString(),
'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#abcd'); 'https://user2:pass2@foo.bar.org:23/aaa/bbb?k=99#abcd');

View File

@ -107,6 +107,5 @@ assert.throws(
// Throws if opts.dictionary is not a Buffer // Throws if opts.dictionary is not a Buffer
assert.throws( assert.throws(
() => { new zlib.Deflate({dictionary: 'not a buffer'}); }, () => { new zlib.Deflate({dictionary: 'not a buffer'}); },
new RegExp('^TypeError: Invalid dictionary: it should be a Buffer, ' + /^TypeError: Invalid dictionary: it should be a Buffer, TypedArray, or DataView$/
'TypedArray, or DataView$')
); );

View File

@ -7,8 +7,7 @@ require('../common');
const assert = require('assert'); const assert = require('assert');
const zlib = require('zlib'); const zlib = require('zlib');
const expected = new RegExp('^TypeError: "buffer" argument must be a string, ' + const expected = /^TypeError: "buffer" argument must be a string, Buffer, TypedArray, or DataView$/;
'Buffer, TypedArray, or DataView$');
assert.throws(() => { zlib.deflateSync(undefined); }, expected); assert.throws(() => { zlib.deflateSync(undefined); }, expected);
assert.throws(() => { zlib.deflateSync(null); }, expected); assert.throws(() => { zlib.deflateSync(null); }, expected);