From 4b117fbc812588804645fc2d20dd7ed5480e617c Mon Sep 17 00:00:00 2001 From: Voltrex Date: Thu, 19 Aug 2021 22:03:59 +0430 Subject: [PATCH] console: use validators for consistency The usage of more validators could clean up validation and keep consistency. PR-URL: https://github.com/nodejs/node/pull/39812 Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/internal/console/constructor.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 04567423f78674..92c6d7293350ed 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -42,12 +42,15 @@ const { isStackOverflowError, codes: { ERR_CONSOLE_WRITABLE_STREAM, - ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_INCOMPATIBLE_OPTION_PAIR, }, } = require('internal/errors'); -const { validateInteger } = require('internal/validators'); +const { + validateArray, + validateInteger, + validateObject, +} = require('internal/validators'); const { previewEntries } = internalBinding('util'); const { Buffer: { isBuffer } } = require('buffer'); const { @@ -132,18 +135,15 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { 0, kMaxGroupIndentation); } - if (typeof inspectOptions === 'object' && inspectOptions !== null) { + if (inspectOptions !== undefined) { + validateObject(inspectOptions, 'options.inspectOptions'); + if (inspectOptions.colors !== undefined && options.colorMode !== undefined) { throw new ERR_INCOMPATIBLE_OPTION_PAIR( 'options.inspectOptions.color', 'colorMode'); } optionsMap.set(this, inspectOptions); - } else if (inspectOptions !== undefined) { - throw new ERR_INVALID_ARG_TYPE( - 'options.inspectOptions', - 'object', - inspectOptions); } // Bind the prototype functions to this Console instance @@ -479,8 +479,8 @@ const consoleMethods = { // https://console.spec.whatwg.org/#table table(tabularData, properties) { - if (properties !== undefined && !ArrayIsArray(properties)) - throw new ERR_INVALID_ARG_TYPE('properties', 'Array', properties); + if (properties !== undefined) + validateArray(properties, 'properties'); if (tabularData === null || typeof tabularData !== 'object') return this.log(tabularData);