Skip to content

Commit

Permalink
lib,test: use regular expression literals
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Trott authored and addaleax committed May 5, 2017
1 parent feb90d3 commit 6bcf65d
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 33 deletions.
4 changes: 2 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const outHeadersKey = require('internal/http').outHeadersKey;
const CRLF = common.CRLF;
const debug = common.debug;

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

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,8 @@ Buffer.poolSize = ps;
assert.throws(() => Buffer.allocUnsafe(10).copy(),
/TypeError: argument should be a Buffer/);

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

assert.throws(() => Buffer.from(), regErrorMsg);
assert.throws(() => Buffer.from(null), regErrorMsg);
Expand Down
8 changes: 3 additions & 5 deletions test/parallel/test-crypto-dh.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ assert.strictEqual(secret2.toString('base64'), secret1);
assert.strictEqual(dh1.verifyError, 0);
assert.strictEqual(dh2.verifyError, 0);

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

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

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

// Run this one twice to make sure that the dh3 clears its error properly
{
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-crypto-rsa-dsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const dsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_dsa_privkey.pem',
const dsaKeyPemEncrypted = fs.readFileSync(
common.fixturesDir + '/test_dsa_privkey_encrypted.pem', 'ascii');

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

// Test RSA encryption/decryption
{
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-dgram-send-address-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ client.send(buf, port, undefined, onMessage);
// valid address: not provided
client.send(buf, port, onMessage);

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

// invalid address: object
assert.throws(() => {
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-fs-write-stream-throw-type-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const assert = require('assert');
const fs = require('fs');
const path = require('path');

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

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

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

Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-tls-env-bad-extra-ca.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ fork(__filename, opts)
assert.strictEqual(status, 0, 'client did not succeed in connecting');
}))
.on('close', common.mustCall(function() {
assert(stderr.match(new RegExp(
'Warning: Ignoring extra certs from.*no-such-file-exists' +
'.* load failed:.*No such file or directory'
)), stderr);
assert(stderr.match(
/Warning: Ignoring extra certs from.*no-such-file-exists.* load failed:.*No such file or directory/
), stderr);
}))
.stderr.setEncoding('utf8').on('data', function(str) {
stderr += str;
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-tls-key-mismatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ if (!common.hasCrypto) {
const assert = require('assert');
const tls = require('tls');
const fs = require('fs');
const errorMessageRegex = new RegExp('^Error: error:0B080074:x509 ' +
'certificate routines:X509_check_private_key:key values mismatch$');
const errorMessageRegex =
/^Error: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch$/;

const options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-util-inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ require('../common');
const assert = require('assert');
const inherits = require('util').inherits;
const errCheck =
new RegExp('^TypeError: The super constructor to "inherits" must not be ' +
'null or undefined$');
/^TypeError: The super constructor to "inherits" must not be null or undefined$/;


// super constructor
Expand Down
6 changes: 2 additions & 4 deletions test/parallel/test-whatwg-url-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ assert.strictEqual(url.searchParams, oldParams); // [SameObject]
// non-writable property should throw.
// Note: this error message is subject to change in V8 updates
assert.throws(() => url.origin = 'http://foo.bar.com:22',
new RegExp('TypeError: Cannot set property origin of' +
' \\[object URL\\] which has only a getter'));
/TypeError: Cannot set property origin of \[object URL\] which has only a getter$/);
assert.strictEqual(url.origin, 'http://foo.bar.com:21');
assert.strictEqual(url.toString(),
'http://user:[email protected]:21/aaa/zzz?l=25#test');
Expand Down Expand Up @@ -120,8 +119,7 @@ assert.strictEqual(url.hash, '#abcd');
// non-writable property should throw.
// Note: this error message is subject to change in V8 updates
assert.throws(() => url.searchParams = '?k=88',
new RegExp('TypeError: Cannot set property searchParams of' +
' \\[object URL\\] which has only a getter'));
/^TypeError: Cannot set property searchParams of \[object URL\] which has only a getter$/);
assert.strictEqual(url.searchParams, oldParams);
assert.strictEqual(url.toString(),
'https://user2:[email protected]:23/aaa/bbb?k=99#abcd');
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-zlib-deflate-constructors.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,5 @@ assert.throws(
// Throws if opts.dictionary is not a Buffer
assert.throws(
() => { new zlib.Deflate({dictionary: 'not a buffer'}); },
new RegExp('^TypeError: Invalid dictionary: it should be a Buffer, ' +
'TypedArray, or DataView$')
/^TypeError: Invalid dictionary: it should be a Buffer, TypedArray, or DataView$/
);
3 changes: 1 addition & 2 deletions test/parallel/test-zlib-not-string-or-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ require('../common');
const assert = require('assert');
const zlib = require('zlib');

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

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

0 comments on commit 6bcf65d

Please sign in to comment.