diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index e367cf1f58bd9d..131d479474f248 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -105,7 +105,7 @@ function setupProcessWarnings() { // process.emitWarning(str[, type[, code]][, ctor]) // process.emitWarning(str[, options]) process.emitWarning = function(warning, type, code, ctor, now) { - var detail; + let detail; if (type !== null && typeof type === 'object' && !Array.isArray(type)) { ctor = type.ctor; code = type.code; @@ -117,23 +117,23 @@ function setupProcessWarnings() { code = undefined; type = 'Warning'; } + if (type !== undefined && typeof type !== 'string') { + throw new ERR_INVALID_ARG_TYPE('type', 'string', type); + } if (typeof code === 'function') { ctor = code; code = undefined; - } - if (code !== undefined && typeof code !== 'string') + } else if (code !== undefined && typeof code !== 'string') { throw new ERR_INVALID_ARG_TYPE('code', 'string', code); - if (type !== undefined && typeof type !== 'string') - throw new ERR_INVALID_ARG_TYPE('type', 'string', type); - if (warning === undefined || typeof warning === 'string') { + } + if (typeof warning === 'string') { // eslint-disable-next-line no-restricted-syntax warning = new Error(warning); warning.name = String(type || 'Warning'); if (code !== undefined) warning.code = code; if (detail !== undefined) warning.detail = detail; Error.captureStackTrace(warning, ctor || process.emitWarning); - } - if (!(warning instanceof Error)) { + } else if (!(warning instanceof Error)) { throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning); } if (warning.name === 'DeprecationWarning') { diff --git a/test/parallel/test-process-emitwarning.js b/test/parallel/test-process-emitwarning.js index e07fcd253c4128..e51d391a7bd1ea 100644 --- a/test/parallel/test-process-emitwarning.js +++ b/test/parallel/test-process-emitwarning.js @@ -43,8 +43,8 @@ class CustomWarning extends Error { [testMsg, { type: testType, code: testCode, detail: [] }], [testMsg, { type: testType, code: testCode, detail: null }], [testMsg, { type: testType, code: testCode, detail: 1 }] -].forEach((i) => { - process.emitWarning.apply(null, i); +].forEach((args) => { + process.emitWarning(...args); }); const warningNoToString = new CustomWarning(); @@ -57,18 +57,25 @@ warningThrowToString.toString = function() { }; process.emitWarning(warningThrowToString); -const expectedError = - common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }, 11); - // TypeError is thrown on invalid input -assert.throws(() => process.emitWarning(1), expectedError); -assert.throws(() => process.emitWarning({}), expectedError); -assert.throws(() => process.emitWarning(true), expectedError); -assert.throws(() => process.emitWarning([]), expectedError); -assert.throws(() => process.emitWarning('', '', {}), expectedError); -assert.throws(() => process.emitWarning('', 1), expectedError); -assert.throws(() => process.emitWarning('', '', 1), expectedError); -assert.throws(() => process.emitWarning('', true), expectedError); -assert.throws(() => process.emitWarning('', '', true), expectedError); -assert.throws(() => process.emitWarning('', []), expectedError); -assert.throws(() => process.emitWarning('', '', []), expectedError); +[ + [1], + [{}], + [true], + [[]], + ['', '', {}], + ['', 1], + ['', '', 1], + ['', true], + ['', '', true], + ['', []], + ['', '', []], + [], + [undefined, 'foo', 'bar'], + [undefined] +].forEach((args) => { + common.expectsError( + () => process.emitWarning(...args), + { code: 'ERR_INVALID_ARG_TYPE', type: TypeError } + ); +});