Skip to content

Commit a08cdea

Browse files
anonrigjuanarbol
authored andcommitted
typings: add JSDoc for internal/validators
PR-URL: #44181 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 70662f4 commit a08cdea

File tree

2 files changed

+153
-7
lines changed

2 files changed

+153
-7
lines changed

lib/internal/errors.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,9 @@ function makeNodeErrorWithCode(Base, key) {
392392

393393
/**
394394
* This function removes unnecessary frames from Node.js core errors.
395-
* @template {(...args: any[]) => any} T
396-
* @type {(fn: T) => T}
395+
* @template {(...args: unknown[]) => unknown} T
396+
* @param {T} fn
397+
* @returns {T}
397398
*/
398399
function hideStackFrames(fn) {
399400
// We rename the functions that will be hidden to cut off the stacktrace

lib/internal/validators.js

+150-5
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,18 @@ const {
3535
} = require('internal/util/types');
3636
const { signals } = internalBinding('constants').os;
3737

38+
/**
39+
* @param {*} value
40+
* @returns {boolean}
41+
*/
3842
function isInt32(value) {
3943
return value === (value | 0);
4044
}
4145

46+
/**
47+
* @param {*} value
48+
* @returns {boolean}
49+
*/
4250
function isUint32(value) {
4351
return value === (value >>> 0);
4452
}
@@ -71,6 +79,16 @@ function parseFileMode(value, name, def) {
7179
return value;
7280
}
7381

82+
/**
83+
* @callback validateInteger
84+
* @param {*} value
85+
* @param {string} name
86+
* @param {number} [min]
87+
* @param {number} [max]
88+
* @returns {asserts value is number}
89+
*/
90+
91+
/** @type {validateInteger} */
7492
const validateInteger = hideStackFrames(
7593
(value, name, min = NumberMIN_SAFE_INTEGER, max = NumberMAX_SAFE_INTEGER) => {
7694
if (typeof value !== 'number')
@@ -82,6 +100,16 @@ const validateInteger = hideStackFrames(
82100
}
83101
);
84102

103+
/**
104+
* @callback validateInt32
105+
* @param {*} value
106+
* @param {string} name
107+
* @param {number} [min]
108+
* @param {number} [max]
109+
* @returns {asserts value is number}
110+
*/
111+
112+
/** @type {validateInt32} */
85113
const validateInt32 = hideStackFrames(
86114
(value, name, min = -2147483648, max = 2147483647) => {
87115
// The defaults for min and max correspond to the limits of 32-bit integers.
@@ -97,7 +125,16 @@ const validateInt32 = hideStackFrames(
97125
}
98126
);
99127

100-
const validateUint32 = hideStackFrames((value, name, positive) => {
128+
/**
129+
* @callback validateUint32
130+
* @param {*} value
131+
* @param {string} name
132+
* @param {number|boolean} [positive=false]
133+
* @returns {asserts value is number}
134+
*/
135+
136+
/** @type {validateUint32} */
137+
const validateUint32 = hideStackFrames((value, name, positive = false) => {
101138
if (typeof value !== 'number') {
102139
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
103140
}
@@ -112,11 +149,29 @@ const validateUint32 = hideStackFrames((value, name, positive) => {
112149
}
113150
});
114151

152+
/**
153+
* @callback validateString
154+
* @param {*} value
155+
* @param {string} name
156+
* @returns {asserts value is string}
157+
*/
158+
159+
/** @type {validateString} */
115160
function validateString(value, name) {
116161
if (typeof value !== 'string')
117162
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
118163
}
119164

165+
/**
166+
* @callback validateNumber
167+
* @param {*} value
168+
* @param {string} name
169+
* @param {number} [min]
170+
* @param {number} [max]
171+
* @returns {asserts value is number}
172+
*/
173+
174+
/** @type {validateNumber} */
120175
function validateNumber(value, name, min = undefined, max) {
121176
if (typeof value !== 'number')
122177
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
@@ -130,6 +185,15 @@ function validateNumber(value, name, min = undefined, max) {
130185
}
131186
}
132187

188+
/**
189+
* @callback validateOneOf
190+
* @template T
191+
* @param {T} value
192+
* @param {string} name
193+
* @param {T[]} oneOf
194+
*/
195+
196+
/** @type {validateOneOf} */
133197
const validateOneOf = hideStackFrames((value, name, oneOf) => {
134198
if (!ArrayPrototypeIncludes(oneOf, value)) {
135199
const allowed = ArrayPrototypeJoin(
@@ -141,6 +205,14 @@ const validateOneOf = hideStackFrames((value, name, oneOf) => {
141205
}
142206
});
143207

208+
/**
209+
* @callback validateBoolean
210+
* @param {*} value
211+
* @param {string} name
212+
* @returns {asserts value is boolean}
213+
*/
214+
215+
/** @type {validateBoolean} */
144216
function validateBoolean(value, name) {
145217
if (typeof value !== 'boolean')
146218
throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value);
@@ -153,16 +225,19 @@ function getOwnPropertyValueOrDefault(options, key, defaultValue) {
153225
}
154226

155227
/**
156-
* @param {unknown} value
228+
* @callback validateObject
229+
* @param {*} value
157230
* @param {string} name
158231
* @param {{
159232
* allowArray?: boolean,
160233
* allowFunction?: boolean,
161234
* nullable?: boolean
162235
* }} [options]
163236
*/
237+
238+
/** @type {validateObject} */
164239
const validateObject = hideStackFrames(
165-
(value, name, options) => {
240+
(value, name, options = null) => {
166241
const allowArray = getOwnPropertyValueOrDefault(options, 'allowArray', false);
167242
const allowFunction = getOwnPropertyValueOrDefault(options, 'allowFunction', false);
168243
const nullable = getOwnPropertyValueOrDefault(options, 'nullable', false);
@@ -175,6 +250,15 @@ const validateObject = hideStackFrames(
175250
}
176251
});
177252

253+
/**
254+
* @callback validateArray
255+
* @param {*} value
256+
* @param {string} name
257+
* @param {number} [minLength]
258+
* @returns {asserts value is any[]}
259+
*/
260+
261+
/** @type {validateArray} */
178262
const validateArray = hideStackFrames((value, name, minLength = 0) => {
179263
if (!ArrayIsArray(value)) {
180264
throw new ERR_INVALID_ARG_TYPE(name, 'Array', value);
@@ -185,6 +269,12 @@ const validateArray = hideStackFrames((value, name, minLength = 0) => {
185269
}
186270
});
187271

272+
// eslint-disable-next-line jsdoc/require-returns-check
273+
/**
274+
* @param {*} signal
275+
* @param {string} [name='signal']
276+
* @returns {asserts signal is keyof signals}
277+
*/
188278
function validateSignalName(signal, name = 'signal') {
189279
validateString(signal, name);
190280

@@ -198,6 +288,14 @@ function validateSignalName(signal, name = 'signal') {
198288
}
199289
}
200290

291+
/**
292+
* @callback validateBuffer
293+
* @param {*} buffer
294+
* @param {string} [name='buffer']
295+
* @returns {asserts buffer is ArrayBufferView}
296+
*/
297+
298+
/** @type {validateBuffer} */
201299
const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
202300
if (!isArrayBufferView(buffer)) {
203301
throw new ERR_INVALID_ARG_TYPE(name,
@@ -206,6 +304,10 @@ const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
206304
}
207305
});
208306

307+
/**
308+
* @param {string} data
309+
* @param {string} encoding
310+
*/
209311
function validateEncoding(data, encoding) {
210312
const normalizedEncoding = normalizeEncoding(encoding);
211313
const length = data.length;
@@ -216,8 +318,14 @@ function validateEncoding(data, encoding) {
216318
}
217319
}
218320

219-
// Check that the port number is not NaN when coerced to a number,
220-
// is an integer and that it falls within the legal range of port numbers.
321+
/**
322+
* Check that the port number is not NaN when coerced to a number,
323+
* is an integer and that it falls within the legal range of port numbers.
324+
* @param {*} port
325+
* @param {string} [name='Port']
326+
* @param {boolean} [allowZero=true]
327+
* @returns {number}
328+
*/
221329
function validatePort(port, name = 'Port', allowZero = true) {
222330
if ((typeof port !== 'number' && typeof port !== 'string') ||
223331
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
@@ -234,6 +342,13 @@ const validateCallback = hideStackFrames((callback) => {
234342
throw new ERR_INVALID_CALLBACK(callback);
235343
});
236344

345+
/**
346+
* @callback validateAbortSignal
347+
* @param {*} signal
348+
* @param {string} name
349+
*/
350+
351+
/** @type {validateAbortSignal} */
237352
const validateAbortSignal = hideStackFrames((signal, name) => {
238353
if (signal !== undefined &&
239354
(signal === null ||
@@ -243,21 +358,51 @@ const validateAbortSignal = hideStackFrames((signal, name) => {
243358
}
244359
});
245360

361+
/**
362+
* @callback validateFunction
363+
* @param {*} value
364+
* @param {string} name
365+
* @returns {asserts value is Function}
366+
*/
367+
368+
/** @type {validateFunction} */
246369
const validateFunction = hideStackFrames((value, name) => {
247370
if (typeof value !== 'function')
248371
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
249372
});
250373

374+
/**
375+
* @callback validatePlainFunction
376+
* @param {*} value
377+
* @param {string} name
378+
* @returns {asserts value is Function}
379+
*/
380+
381+
/** @type {validatePlainFunction} */
251382
const validatePlainFunction = hideStackFrames((value, name) => {
252383
if (typeof value !== 'function' || isAsyncFunction(value))
253384
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
254385
});
255386

387+
/**
388+
* @callback validateUndefined
389+
* @param {*} value
390+
* @param {string} name
391+
* @returns {asserts value is undefined}
392+
*/
393+
394+
/** @type {validateUndefined} */
256395
const validateUndefined = hideStackFrames((value, name) => {
257396
if (value !== undefined)
258397
throw new ERR_INVALID_ARG_TYPE(name, 'undefined', value);
259398
});
260399

400+
/**
401+
* @template T
402+
* @param {T} value
403+
* @param {string} name
404+
* @param {T[]} union
405+
*/
261406
function validateUnion(value, name, union) {
262407
if (!ArrayPrototypeIncludes(union, value)) {
263408
throw new ERR_INVALID_ARG_TYPE(name, `('${ArrayPrototypeJoin(union, '|')}')`, value);

0 commit comments

Comments
 (0)