console: improve inspectOptions validation
This commit adds stricter type checking to the inspectOptions option to the Console constructor. PR-URL: https://github.com/nodejs/node/pull/25090 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
67f95825e5
commit
bf36f0755c
@ -91,13 +91,15 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
|
|||||||
if (typeof colorMode !== 'boolean' && colorMode !== 'auto')
|
if (typeof colorMode !== 'boolean' && colorMode !== 'auto')
|
||||||
throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode);
|
throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode);
|
||||||
|
|
||||||
if (inspectOptions) {
|
if (typeof inspectOptions === 'object' && inspectOptions !== null) {
|
||||||
if (inspectOptions.colors !== undefined &&
|
if (inspectOptions.colors !== undefined &&
|
||||||
options.colorMode !== undefined) {
|
options.colorMode !== undefined) {
|
||||||
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
|
throw new ERR_INCOMPATIBLE_OPTION_PAIR(
|
||||||
'inspectOptions.color', 'colorMode');
|
'inspectOptions.color', 'colorMode');
|
||||||
}
|
}
|
||||||
optionsMap.set(this, inspectOptions);
|
optionsMap.set(this, inspectOptions);
|
||||||
|
} else if (inspectOptions !== undefined) {
|
||||||
|
throw new ERR_INVALID_ARG_TYPE('inspectOptions', 'object', inspectOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind the prototype functions to this Console instance
|
// Bind the prototype functions to this Console instance
|
||||||
|
@ -128,3 +128,21 @@ out.write = err.write = (d) => {};
|
|||||||
assert.throws(() => c2.warn('foo'), /^Error: err$/);
|
assert.throws(() => c2.warn('foo'), /^Error: err$/);
|
||||||
assert.throws(() => c2.dir('foo'), /^Error: out$/);
|
assert.throws(() => c2.dir('foo'), /^Error: out$/);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Console constructor throws if inspectOptions is not an object.
|
||||||
|
[null, true, false, 'foo', 5, Symbol()].forEach((inspectOptions) => {
|
||||||
|
assert.throws(
|
||||||
|
() => {
|
||||||
|
new Console({
|
||||||
|
stdout: out,
|
||||||
|
stderr: err,
|
||||||
|
inspectOptions
|
||||||
|
});
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: 'The "inspectOptions" argument must be of type object.' +
|
||||||
|
` Received type ${typeof inspectOptions}`,
|
||||||
|
code: 'ERR_INVALID_ARG_TYPE'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user