Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: improve hideStackFrames intellisense #44181

Merged
merged 11 commits into from
Aug 17, 2022
5 changes: 3 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,9 @@ function makeNodeErrorWithCode(Base, key) {

/**
* This function removes unnecessary frames from Node.js core errors.
* @template {(...args: any[]) => any} T
* @type {(fn: T) => T}
* @template {(...args: unknown[]) => unknown} T
* @param {T} fn
* @returns {T}
*/
function hideStackFrames(fn) {
// We rename the functions that will be hidden to cut off the stacktrace
Expand Down
38 changes: 33 additions & 5 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ function parseFileMode(value, name, def) {
return value;
}

/**
* @type {(function(unknown, string, number=, number=): void)}
*/
anonrig marked this conversation as resolved.
Show resolved Hide resolved
const validateInteger = hideStackFrames(
(value, name, min = NumberMIN_SAFE_INTEGER, max = NumberMAX_SAFE_INTEGER) => {
if (typeof value !== 'number')
Expand All @@ -81,6 +84,9 @@ const validateInteger = hideStackFrames(
}
);

/**
* @type {(function(unknown, string, number=, number=): void)}
*/
const validateInt32 = hideStackFrames(
(value, name, min = -2147483648, max = 2147483647) => {
// The defaults for min and max correspond to the limits of 32-bit integers.
Expand All @@ -96,7 +102,10 @@ const validateInt32 = hideStackFrames(
}
);

const validateUint32 = hideStackFrames((value, name, positive) => {
/**
* @type {(function(unknown, string, boolean=): void)}
*/
const validateUint32 = hideStackFrames((value, name, positive = false) => {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
Expand Down Expand Up @@ -129,6 +138,9 @@ function validateNumber(value, name, min = undefined, max) {
}
}

/**
* @type {(function(unknown, string, unknown[]): void)}
*/
const validateOneOf = hideStackFrames((value, name, oneOf) => {
if (!ArrayPrototypeIncludes(oneOf, value)) {
const allowed = ArrayPrototypeJoin(
Expand All @@ -152,13 +164,11 @@ function getOwnPropertyValueOrDefault(options, key, defaultValue) {
}

/**
* @param {unknown} value
* @param {string} name
* @param {{
* @type {(function(unknown, string, {
* allowArray?: boolean,
* allowFunction?: boolean,
* nullable?: boolean
* }} [options]
* }): void)}
*/
const validateObject = hideStackFrames(
(value, name, options) => {
Expand All @@ -174,6 +184,9 @@ const validateObject = hideStackFrames(
}
});

/**
* @type {(function(unknown, string, number=): void)}
*/
const validateArray = hideStackFrames((value, name, minLength = 0) => {
if (!ArrayIsArray(value)) {
throw new ERR_INVALID_ARG_TYPE(name, 'Array', value);
Expand All @@ -197,6 +210,9 @@ function validateSignalName(signal, name = 'signal') {
}
}

/**
* @type {(function(unknown, string=): void)}
*/
const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
if (!isArrayBufferView(buffer)) {
throw new ERR_INVALID_ARG_TYPE(name,
Expand Down Expand Up @@ -228,6 +244,9 @@ function validatePort(port, name = 'Port', allowZero = true) {
return port | 0;
}

/**
* @type {(function(unknown, string): void)}
*/
anonrig marked this conversation as resolved.
Show resolved Hide resolved
const validateAbortSignal = hideStackFrames((signal, name) => {
if (signal !== undefined &&
(signal === null ||
Expand All @@ -237,16 +256,25 @@ const validateAbortSignal = hideStackFrames((signal, name) => {
}
});

/**
* @type {(function(unknown, string): void)}
*/
const validateFunction = hideStackFrames((value, name) => {
if (typeof value !== 'function')
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
});

/**
* @type {(function(unknown, string): void)}
*/
const validatePlainFunction = hideStackFrames((value, name) => {
if (typeof value !== 'function' || isAsyncFunction(value))
throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
});

/**
* @type {(function(unknown, string): void)}
*/
const validateUndefined = hideStackFrames((value, name) => {
if (value !== undefined)
throw new ERR_INVALID_ARG_TYPE(name, 'undefined', value);
Expand Down