diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 7fd31c9dc6b4ea..cfad8c014eb801 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -35,7 +35,6 @@ const { const { signals } = internalBinding('constants').os; /** - * @callback isInt32 * @param {number} value * @returns {boolean} */ @@ -44,7 +43,6 @@ function isInt32(value) { } /** - * @callback isUint32 * @param {number} value * @returns {boolean} */ @@ -179,7 +177,7 @@ function validateNumber(value, name, min = undefined, max) { throw new ERR_INVALID_ARG_TYPE(name, 'number', value); if ((min != null && value < min) || (max != null && value > max) || - ((min != null || max != null) && NumberIsNaN(value))) { + ((min != null || max != null) && NumberIsNaN(value))) { throw new ERR_OUT_OF_RANGE( name, `${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`, @@ -230,7 +228,11 @@ function getOwnPropertyValueOrDefault(options, key, defaultValue) { * @callback validateObject * @param {*} value * @param {string} name - * @param {{allowArray: boolean=, allowFunction: boolean=, nullable: boolean=}} [options] + * @param {{ + * allowArray: boolean=, + * allowFunction: boolean=, + * nullable: boolean= + * }} [options] */ /** @type {validateObject} */ @@ -240,10 +242,10 @@ const validateObject = hideStackFrames( const allowFunction = getOwnPropertyValueOrDefault(options, 'allowFunction', false); const nullable = getOwnPropertyValueOrDefault(options, 'nullable', false); if ((!nullable && value === null) || - (!allowArray && ArrayIsArray(value)) || - (typeof value !== 'object' && ( - !allowFunction || typeof value !== 'function' - ))) { + (!allowArray && ArrayIsArray(value)) || + (typeof value !== 'object' && ( + !allowFunction || typeof value !== 'function' + ))) { throw new ERR_INVALID_ARG_TYPE(name, 'Object', value); } }); @@ -253,7 +255,7 @@ const validateObject = hideStackFrames( * @param {*} value * @param {string} name * @param {number} [minLength] - * @returns {asserts value is unknown[]} + * @returns {asserts value is any[]} */ /** @type {validateArray} */ @@ -270,24 +272,23 @@ const validateArray = hideStackFrames((value, name, minLength = 0) => { }); /** - * @callback validateSignalName * @param {*} signal * @param {string} name * @returns {asserts signal is keyof signals} */ - -/** @type {validateSignalName} */ function validateSignalName(signal, name = 'signal') { validateString(signal, name); if (signals[signal] === undefined) { if (signals[StringPrototypeToUpperCase(signal)] !== undefined) { throw new ERR_UNKNOWN_SIGNAL(signal + - ' (signals must use all capital letters)'); + ' (signals must use all capital letters)'); } throw new ERR_UNKNOWN_SIGNAL(signal); } + + return true; } /** @@ -299,29 +300,28 @@ function validateSignalName(signal, name = 'signal') { /** @type {validateBuffer} */ const validateBuffer = hideStackFrames((buffer, name = 'buffer') => { if (!isArrayBufferView(buffer)) { - throw new ERR_INVALID_ARG_TYPE(name, ['Buffer', 'TypedArray', 'DataView'], buffer); + throw new ERR_INVALID_ARG_TYPE(name, + ['Buffer', 'TypedArray', 'DataView'], + buffer); } }); /** - * @callback validateEncoding * @param {string} data * @param {string} encoding */ - -/** @type {validateEncoding} */ function validateEncoding(data, encoding) { const normalizedEncoding = normalizeEncoding(encoding); const length = data.length; if (normalizedEncoding === 'hex' && length % 2 !== 0) { - throw new ERR_INVALID_ARG_VALUE('encoding', encoding, `is invalid for data of length ${length}`); + throw new ERR_INVALID_ARG_VALUE('encoding', encoding, + `is invalid for data of length ${length}`); } } /** - * @callback validatePort - * @description Check that the port number is not NaN when coerced to a number, + * Check that the port number is not NaN when coerced to a number, * is an integer and that it falls within the legal range of port numbers. * @param {*} port * @param {string} [name='Port'] @@ -330,10 +330,10 @@ function validateEncoding(data, encoding) { */ function validatePort(port, name = 'Port', allowZero = true) { if ((typeof port !== 'number' && typeof port !== 'string') || - (typeof port === 'string' && StringPrototypeTrim(port).length === 0) || - +port !== (+port >>> 0) || - port > 0xFFFF || - (port === 0 && !allowZero)) { + (typeof port === 'string' && StringPrototypeTrim(port).length === 0) || + +port !== (+port >>> 0) || + port > 0xFFFF || + (port === 0 && !allowZero)) { throw new ERR_SOCKET_BAD_PORT(name, port, allowZero); } return port | 0; @@ -341,14 +341,16 @@ function validatePort(port, name = 'Port', allowZero = true) { /** * @callback validateAbortSignal - * @param {string} signal + * @param {string=} signal * @param {string} name */ + +/** @type {validateAbortSignal} */ const validateAbortSignal = hideStackFrames((signal, name) => { if (signal !== undefined && - (signal === null || - typeof signal !== 'object' || - !('aborted' in signal))) { + (signal === null || + typeof signal !== 'object' || + !('aborted' in signal))) { throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal); } }); @@ -393,14 +395,11 @@ const validateUndefined = hideStackFrames((value, name) => { }); /** - * @callback validateUnion * @template T * @param {T} value * @param {string} name * @param {T[]} union */ - -/** @type {validateUnion} */ function validateUnion(value, name, union) { if (!ArrayPrototypeIncludes(union, value)) { throw new ERR_INVALID_ARG_TYPE(name, `('${ArrayPrototypeJoin(union, '|')}')`, value);