Skip to content

Commit

Permalink
fixup: use object instead of Object is most cases
Browse files Browse the repository at this point in the history
  • Loading branch information
BridgeAR committed Dec 19, 2019
1 parent 14309b2 commit b033324
Show file tree
Hide file tree
Showing 39 changed files with 83 additions and 64 deletions.
2 changes: 1 addition & 1 deletion lib/_http_server.js
Expand Up @@ -323,7 +323,7 @@ function Server(options, requestListener) {
} else if (options == null || typeof options === 'object') {
options = { ...options };
} else {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
}

this[kIncomingMessage] = options.IncomingMessage || IncomingMessage;
Expand Down
4 changes: 2 additions & 2 deletions lib/child_process.js
Expand Up @@ -412,7 +412,7 @@ function normalizeSpawnArguments(file, args, options) {
} else if (args == null) {
args = [];
} else if (typeof args !== 'object') {
throw new ERR_INVALID_ARG_TYPE('args', 'Object', args);
throw new ERR_INVALID_ARG_TYPE('args', 'object', args);
} else {
options = args;
args = [];
Expand All @@ -421,7 +421,7 @@ function normalizeSpawnArguments(file, args, options) {
if (options === undefined)
options = {};
else if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

// Validate the cwd, if present.
if (options.cwd != null &&
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/child_process.js
Expand Up @@ -707,7 +707,7 @@ function setupChannel(target, channel, serializationMode) {
typeof message !== 'number' &&
typeof message !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE(
'message', ['string', 'Object', 'number', 'boolean'], message);
'message', ['string', 'object', 'number', 'boolean'], message);
}

// Support legacy function signature
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/console/constructor.js
Expand Up @@ -111,7 +111,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
}
optionsMap.set(this, inspectOptions);
} else if (inspectOptions !== undefined) {
throw new ERR_INVALID_ARG_TYPE('inspectOptions', 'Object', inspectOptions);
throw new ERR_INVALID_ARG_TYPE('inspectOptions', 'object', inspectOptions);
}

// Bind the prototype functions to this Console instance
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/keygen.js
Expand Up @@ -134,11 +134,11 @@ function check(type, options, callback) {
let cipher, passphrase, publicType, publicFormat, privateType, privateFormat;

if (options !== undefined && typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

function needOptions() {
if (options == null)
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
return options;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/keys.js
Expand Up @@ -47,7 +47,7 @@ class KeyObject {
if (type !== 'secret' && type !== 'public' && type !== 'private')
throw new ERR_INVALID_ARG_VALUE('type', type);
if (typeof handle !== 'object')
throw new ERR_INVALID_ARG_TYPE('handle', 'Object', handle);
throw new ERR_INVALID_ARG_TYPE('handle', 'object', handle);

this[kKeyType] = type;

Expand Down Expand Up @@ -178,7 +178,7 @@ function isStringOrBuffer(val) {

function parseKeyEncoding(enc, keyType, isPublic, objName) {
if (enc === null || typeof enc !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object', enc);
throw new ERR_INVALID_ARG_TYPE('options', 'object', enc);

const isInput = keyType === undefined;

Expand Down
26 changes: 22 additions & 4 deletions lib/internal/errors.js
Expand Up @@ -24,8 +24,18 @@ const messages = new Map();
const codes = {};

const classRegExp = /^([A-Z][a-z0-9]*)+$/;
// Sorted by a rough estimate on most frequently used entries.
const kTypes = [
'string', 'boolean', 'number', 'symbol', 'bigint', 'function'
'string',
'function',
'number',
'object',
// Accept 'Function' and 'Object' as alternative to the lower cased version.
'Function',
'Object',
'boolean',
'bigint',
'symbol'
];

const { kMaxLength } = internalBinding('buffer');
Expand Down Expand Up @@ -940,9 +950,7 @@ E('ERR_INVALID_ARG_TYPE',
assert(typeof value === 'string',
'All expected entries have to be of type string');
if (kTypes.includes(value)) {
types.push(value);
} else if (value === 'Function') { // 'Function' should be handled as type
types.push('function');
types.push(value.toLowerCase());
} else if (classRegExp.test(value)) {
instances.push(value);
} else {
Expand All @@ -952,6 +960,16 @@ E('ERR_INVALID_ARG_TYPE',
}
}

// Special handle `object` in case other instances are allowed to outline
// the differences between each other.
if (instances.length > 0) {
const pos = types.indexOf('object');
if (pos !== -1) {
types.splice(pos, 1);
instances.push('Object');
}
}

if (types.length > 0) {
if (types.length > 2) {
const last = types.pop();
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/utils.js
Expand Up @@ -580,7 +580,7 @@ const validateRmdirOptions = hideStackFrames((options) => {
if (options === undefined)
return defaultRmdirOptions;
if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

options = { ...defaultRmdirOptions, ...options };

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Expand Up @@ -1505,7 +1505,7 @@ class ServerHttp2Session extends Http2Session {
// be invalid.
if (typeof origin !== 'string') {
throw new ERR_INVALID_ARG_TYPE('originOrStream',
['string', 'number', 'URL', 'Object'],
['string', 'number', 'URL', 'object'],
originOrStream);
} else if (origin === 'null' || origin.length === 0) {
throw new ERR_HTTP2_ALTSVC_INVALID_ORIGIN();
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/process/per_thread.js
Expand Up @@ -58,7 +58,7 @@ function wrapProcessMethods(binding) {
if (prevValue) {
if (!previousValueIsValid(prevValue.user)) {
if (typeof prevValue !== 'object')
throw new ERR_INVALID_ARG_TYPE('prevValue', 'Object', prevValue);
throw new ERR_INVALID_ARG_TYPE('prevValue', 'object', prevValue);

if (typeof prevValue.user !== 'number') {
throw new ERR_INVALID_ARG_TYPE('prevValue.user',
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/end-of-stream.js
Expand Up @@ -20,7 +20,7 @@ function eos(stream, opts, callback) {
} else if (opts == null) {
opts = {};
} else if (typeof opts !== 'object') {
throw new ERR_INVALID_ARG_TYPE('opts', 'Object', opts);
throw new ERR_INVALID_ARG_TYPE('opts', 'object', opts);
}
if (typeof callback !== 'function') {
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/worker.js
Expand Up @@ -118,7 +118,7 @@ class Worker extends EventEmitter {
} else if (options.env !== SHARE_ENV) {
throw new ERR_INVALID_ARG_TYPE(
'options.env',
['Object', 'undefined', 'null', 'worker_threads.SHARE_ENV'],
['object', 'undefined', 'null', 'worker_threads.SHARE_ENV'],
options.env);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/trace_events.js
Expand Up @@ -79,7 +79,7 @@ class Tracing {

function createTracing(options) {
if (typeof options !== 'object' || options === null)
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

if (!ArrayIsArray(options.categories)) {
throw new ERR_INVALID_ARG_TYPE('options.categories', 'string[]',
Expand Down
2 changes: 1 addition & 1 deletion lib/vm.js
Expand Up @@ -371,7 +371,7 @@ function compileFunction(code, params, options = {}) {
if (typeof extension !== 'object') {
throw new ERR_INVALID_ARG_TYPE(
`options.contextExtensions[${i}]`,
'Object',
'object',
extension
);
}
Expand Down
11 changes: 6 additions & 5 deletions test/parallel/test-assert.js
Expand Up @@ -484,7 +484,7 @@ assert.throws(() => {
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "options" argument must be an instance of Object.' +
message: 'The "options" argument must be of type object.' +
common.invalidArgTypeHelper(input)
});
});
Expand Down Expand Up @@ -937,8 +937,9 @@ common.expectsError(
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "error" argument must be of type function or an instance of' +
" Object, Error, or RegExp. Received type string ('Error message')"
message: 'The "error" argument must be of type function or ' +
'an instance of Error, RegExp, or Object. Received type string ' +
"('Error message')"
}
);

Expand All @@ -951,8 +952,8 @@ common.expectsError(
() => assert.throws(() => {}, input),
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "error" argument must be of type function or an instance ' +
'of Object, Error, or RegExp.' +
message: 'The "error" argument must be of type function or ' +
'an instance of Error, RegExp, or Object.' +
common.invalidArgTypeHelper(input)
}
);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-constructor.js
Expand Up @@ -15,7 +15,7 @@ assert.strictEqual(typeof ChildProcess, 'function');
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be an instance of Object.' +
message: 'The "options" argument must be of type object.' +
`${common.invalidArgTypeHelper(options)}`
});
});
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-fork.js
Expand Up @@ -51,8 +51,8 @@ assert.throws(() => n.send(), {

assert.throws(() => n.send(Symbol()), {
name: 'TypeError',
message: 'The "message" argument must be one of type string, number, or ' +
'boolean or an instance of Object. Received type symbol (Symbol())',
message: 'The "message" argument must be one of type string,' +
' object, number, or boolean. Received type symbol (Symbol())',
code: 'ERR_INVALID_ARG_TYPE'
});
n.send({ hello: 'world' });
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-console-instance.js
Expand Up @@ -140,7 +140,7 @@ out.write = err.write = (d) => {};
});
},
{
message: 'The "inspectOptions" argument must be an instance of Object.' +
message: 'The "inspectOptions" argument must be of type object.' +
common.invalidArgTypeHelper(inspectOptions),
code: 'ERR_INVALID_ARG_TYPE'
}
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-crypto-key-objects.js
Expand Up @@ -59,7 +59,7 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
type: TypeError,
code: 'ERR_INVALID_ARG_TYPE',
message:
'The "handle" argument must be an instance of Object. Received type ' +
'The "handle" argument must be of type object. Received type ' +
"string ('')"
});
}
Expand Down Expand Up @@ -142,7 +142,7 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
common.expectsError(() => publicKey.export(opt), {
type: TypeError,
code: 'ERR_INVALID_ARG_TYPE',
message: /^The "options" argument must be an instance of Object/
message: /^The "options" argument must be of type object/
});
}

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-crypto-keygen.js
Expand Up @@ -615,7 +615,7 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
common.expectsError(() => generateKeyPair('rsa', common.mustNotCall()), {
type: TypeError,
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be an instance of Object. ' +
message: 'The "options" argument must be of type object. ' +
'Received undefined'
});

Expand All @@ -624,7 +624,7 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
common.expectsError(() => generateKeyPair('ed448', 0, common.mustNotCall()), {
type: TypeError,
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be an instance of Object. ' +
message: 'The "options" argument must be of type object. ' +
'Received type number (0)'
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-rmdir-recursive.js
Expand Up @@ -182,7 +182,7 @@ function removeAsync(dir) {
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: /^The "options" argument must be an instance of Object\./
message: /^The "options" argument must be of type object\./
});
});

Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-http2-createsecureserver-options.js
Expand Up @@ -15,8 +15,8 @@ invalidOptions.forEach((invalidOption) => {
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type Object. Received ' +
`type ${typeof invalidOption}`
message: 'The "options" argument must be of type object.' +
common.invalidArgTypeHelper(invalidOption)
}
);
});
Expand All @@ -28,8 +28,8 @@ invalidOptions.forEach((invalidSettingsOption) => {
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.settings" property must be of type Object. ' +
`Received type ${typeof invalidSettingsOption}`
message: 'The "options.settings" property must be of type object.' +
common.invalidArgTypeHelper(invalidSettingsOption)
}
);
});
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-http2-createserver-options.js
Expand Up @@ -15,8 +15,8 @@ invalidOptions.forEach((invalidOption) => {
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type Object. Received ' +
`type ${typeof invalidOption}`
message: 'The "options" argument must be of type object.' +
common.invalidArgTypeHelper(invalidOption)
}
);
});
Expand All @@ -28,8 +28,8 @@ invalidOptions.forEach((invalidSettingsOption) => {
{
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.settings" property must be of type Object. ' +
`Received type ${typeof invalidSettingsOption}`
message: 'The "options.settings" property must be of type object.' +
common.invalidArgTypeHelper(invalidSettingsOption)
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-misc-util.js
Expand Up @@ -35,7 +35,7 @@ common.expectsError(
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "test" argument must be an instance of Object. Received ' +
message: 'The "test" argument must be of type object. Received ' +
"type string ('foo')"
});

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-util-asserts.js
Expand Up @@ -30,7 +30,7 @@ const {
() => assertIsObject(input, 'foo', 'Object'),
{
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "foo" argument must be an instance of Object.' +
message: 'The "foo" argument must be of type object.' +
common.invalidArgTypeHelper(input)
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-path-parse-format.js
Expand Up @@ -220,7 +220,7 @@ function checkFormat(path, testCases) {
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "pathObject" argument must be an instance of Object.' +
message: 'The "pathObject" argument must be of type object.' +
common.invalidArgTypeHelper(pathObject)
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-performanceobserver.js
Expand Up @@ -48,7 +48,7 @@ assert.strictEqual(counts[NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION], 0);
{
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be an instance of Object.' +
message: 'The "options" argument must be of type object.' +
common.invalidArgTypeHelper(input)
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-process-cpuUsage.js
Expand Up @@ -38,7 +38,7 @@ assert.throws(
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "prevValue" argument must be an instance of Object. ' +
message: 'The "prevValue" argument must be of type object. ' +
'Received type number (1)'
}
);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-tls-keyengine-invalid-arg-type.js
Expand Up @@ -12,12 +12,12 @@ common.expectsError(
privateKeyIdentifier: 'key' });
},
{ code: 'ERR_INVALID_ARG_TYPE',
message: / Received type number$/ });
message: / Received type number \(0\)$/ });

common.expectsError(
() => {
tls.createSecureContext({ privateKeyEngine: 'engine',
privateKeyIdentifier: 0 });
},
{ code: 'ERR_INVALID_ARG_TYPE',
message: / Received type number$/ });
message: / Received type number \(0\)$/ });

0 comments on commit b033324

Please sign in to comment.