UNPKG

eslint-plugin-unicorn-x

Version:
105 lines (92 loc) 2.39 kB
/** @typedef { { name?: string, names?: string[], argumentsLength?: number, minimumArguments?: number, maximumArguments?: number, allowSpreadElement?: boolean, optional?: boolean, } } CallOrNewExpressionCheckOptions */ function create(node, options, types) { if (!types.includes(node?.type)) { return false; } const minimumArguments = options?.minimumArguments ?? 0; const maximumArguments = options?.maximumArguments ?? Number.POSITIVE_INFINITY; const allowSpreadElement = options?.allowSpreadElement ?? false; const names = options?.name ? [options.name] : options?.names; const optional = options?.optional; const argumentsLength = options?.argumentsLength; if ( (optional === true && node.optional !== optional) || (optional === false && // `node.optional` can be `undefined` in some parsers node.optional) ) { return false; } if ( typeof argumentsLength === 'number' && node.arguments.length !== argumentsLength ) { return false; } if (minimumArguments !== 0 && node.arguments.length < minimumArguments) { return false; } if ( Number.isFinite(maximumArguments) && node.arguments.length > maximumArguments ) { return false; } if (!allowSpreadElement) { const maximumArgumentsLength = Number.isFinite(maximumArguments) ? maximumArguments : argumentsLength; if ( typeof maximumArgumentsLength === 'number' && node.arguments.some( (node, index) => node.type === 'SpreadElement' && index < maximumArgumentsLength, ) ) { return false; } } if ( Array.isArray(names) && names.length > 0 && (node.callee.type !== 'Identifier' || !names.includes(node.callee.name)) ) { return false; } return true; } /** @param {CallOrNewExpressionCheckOptions} [options] @returns {boolean} */ export const isCallExpression = (node, options) => create(node, options, ['CallExpression']); /** @param {CallOrNewExpressionCheckOptions} [options] @returns {boolean} */ export const isNewExpression = (node, options) => { if (typeof options?.optional === 'boolean') { throw new TypeError('Cannot check node.optional in `isNewExpression`.'); } return create(node, options, ['NewExpression']); }; /** @param {CallOrNewExpressionCheckOptions} [options] @returns {boolean} */ export const isCallOrNewExpression = (node, options) => create(node, options, ['CallExpression', 'NewExpression']);