Skip to content

Commit 49681e7

Browse files
committed
process: refactor emitWarning
Remove a couple of obsolete checks by refactoring the code with else. Also remove the possibility of an undefined warning. That is neither documented nor does it result in a usable warning. PR-URL: #20726 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 43ee4d6 commit 49681e7

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

lib/internal/process/warning.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function setupProcessWarnings() {
105105
// process.emitWarning(str[, type[, code]][, ctor])
106106
// process.emitWarning(str[, options])
107107
process.emitWarning = function(warning, type, code, ctor, now) {
108-
var detail;
108+
let detail;
109109
if (type !== null && typeof type === 'object' && !Array.isArray(type)) {
110110
ctor = type.ctor;
111111
code = type.code;
@@ -117,23 +117,23 @@ function setupProcessWarnings() {
117117
code = undefined;
118118
type = 'Warning';
119119
}
120+
if (type !== undefined && typeof type !== 'string') {
121+
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
122+
}
120123
if (typeof code === 'function') {
121124
ctor = code;
122125
code = undefined;
123-
}
124-
if (code !== undefined && typeof code !== 'string')
126+
} else if (code !== undefined && typeof code !== 'string') {
125127
throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
126-
if (type !== undefined && typeof type !== 'string')
127-
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
128-
if (warning === undefined || typeof warning === 'string') {
128+
}
129+
if (typeof warning === 'string') {
129130
// eslint-disable-next-line no-restricted-syntax
130131
warning = new Error(warning);
131132
warning.name = String(type || 'Warning');
132133
if (code !== undefined) warning.code = code;
133134
if (detail !== undefined) warning.detail = detail;
134135
Error.captureStackTrace(warning, ctor || process.emitWarning);
135-
}
136-
if (!(warning instanceof Error)) {
136+
} else if (!(warning instanceof Error)) {
137137
throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
138138
}
139139
if (warning.name === 'DeprecationWarning') {

test/parallel/test-process-emitwarning.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class CustomWarning extends Error {
4343
[testMsg, { type: testType, code: testCode, detail: [] }],
4444
[testMsg, { type: testType, code: testCode, detail: null }],
4545
[testMsg, { type: testType, code: testCode, detail: 1 }]
46-
].forEach((i) => {
47-
process.emitWarning.apply(null, i);
46+
].forEach((args) => {
47+
process.emitWarning(...args);
4848
});
4949

5050
const warningNoToString = new CustomWarning();
@@ -57,18 +57,25 @@ warningThrowToString.toString = function() {
5757
};
5858
process.emitWarning(warningThrowToString);
5959

60-
const expectedError =
61-
common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }, 11);
62-
6360
// TypeError is thrown on invalid input
64-
assert.throws(() => process.emitWarning(1), expectedError);
65-
assert.throws(() => process.emitWarning({}), expectedError);
66-
assert.throws(() => process.emitWarning(true), expectedError);
67-
assert.throws(() => process.emitWarning([]), expectedError);
68-
assert.throws(() => process.emitWarning('', '', {}), expectedError);
69-
assert.throws(() => process.emitWarning('', 1), expectedError);
70-
assert.throws(() => process.emitWarning('', '', 1), expectedError);
71-
assert.throws(() => process.emitWarning('', true), expectedError);
72-
assert.throws(() => process.emitWarning('', '', true), expectedError);
73-
assert.throws(() => process.emitWarning('', []), expectedError);
74-
assert.throws(() => process.emitWarning('', '', []), expectedError);
61+
[
62+
[1],
63+
[{}],
64+
[true],
65+
[[]],
66+
['', '', {}],
67+
['', 1],
68+
['', '', 1],
69+
['', true],
70+
['', '', true],
71+
['', []],
72+
['', '', []],
73+
[],
74+
[undefined, 'foo', 'bar'],
75+
[undefined]
76+
].forEach((args) => {
77+
common.expectsError(
78+
() => process.emitWarning(...args),
79+
{ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }
80+
);
81+
});

0 commit comments

Comments
 (0)