UNPKG

eslint-plugin-jsdoc

Version:
244 lines (220 loc) • 6.75 kB
import iterateJsdoc, { parseComment, } from '../iterateJsdoc.js'; import { getClosureTemplateTagDefaults, getTags, } from '../jsdocUtils.js'; import { getJSDocComment, parse as parseType, traverse, tryParse as tryParseType, } from '@es-joy/jsdoccomment'; import { parse as espree, } from 'espree'; export default iterateJsdoc(({ jsdoc, node, report, settings, sourceCode, utils, }) => { const { mode, } = settings; const tgName = /** @type {string} */ (utils.getPreferredTagName({ tagName: 'template', })); if (!tgName) { return; } const templateTags = utils.getTags(tgName); const usedNames = new Set(); /** * @param {string} potentialType */ const checkForUsedTypes = (potentialType) => { let parsedType; try { parsedType = mode === 'permissive' ? tryParseType(/** @type {string} */ (potentialType), undefined, { computedPropertyParser: espree, }) : parseType(/** @type {string} */ (potentialType), mode, { computedPropertyParser: espree, }); } catch { return; } traverse(parsedType, (nde, parentNode) => { const { type, value, } = /** @type {import('jsdoc-type-pratt-parser').NameResult} */ (nde); if (type === 'JsdocTypeName') { usedNames.add(value); // An unquoted index of an indexed access type, e.g., `K` in `T[K]` } else if ( type === 'JsdocTypeProperty' && parentNode?.type === 'JsdocTypeNamePath' && parentNode.pathType === 'property-brackets' && !(/** @type {import('jsdoc-type-pratt-parser').PropertyResult} */ (nde).meta.quote) ) { usedNames.add(value); } }); }; const checkParamsAndReturnsTags = (jsdc = jsdoc) => { const paramName = /** @type {string} */ (utils.getPreferredTagName({ tagName: 'param', })); const paramTags = getTags(jsdc, paramName); for (const paramTag of paramTags) { checkForUsedTypes(paramTag.type); } const returnsName = /** @type {string} */ (utils.getPreferredTagName({ tagName: 'returns', })); const returnsTags = getTags(jsdc, returnsName); for (const returnsTag of returnsTags) { checkForUsedTypes(returnsTag.type); } }; const checkTemplateTags = () => { // A template name may be used only within the constraint or default of // another template, e.g., `T` in `@template {keyof T} K` or // `@template [U=T[]]` for (const tag of templateTags) { checkForUsedTypes(tag.type); for (const defaultType of getClosureTemplateTagDefaults(tag)) { checkForUsedTypes(defaultType); } } for (const tag of templateTags) { const names = utils.parseClosureTemplateTag(tag); for (const nme of names) { if (!usedNames.has(nme)) { report(`@${tgName} ${nme} not in use`, null, tag); } } } }; /** * @param {import('@typescript-eslint/types').TSESTree.FunctionDeclaration| * import('@typescript-eslint/types').TSESTree.ClassDeclaration| * import('@typescript-eslint/types').TSESTree.TSInterfaceDeclaration| * import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} aliasDeclaration * @param {boolean} [checkParamsAndReturns] */ const checkParameters = (aliasDeclaration, checkParamsAndReturns) => { /* c8 ignore next -- Guard */ const { params, } = aliasDeclaration.typeParameters ?? { params: [], }; for (const { name: { name, }, } of params) { usedNames.add(name); } if (checkParamsAndReturns) { checkParamsAndReturnsTags(); } else if (aliasDeclaration.type === 'ClassDeclaration') { /* c8 ignore next -- TS */ for (const nde of aliasDeclaration?.body?.body ?? []) { const commentNode = getJSDocComment(sourceCode, nde, settings); if (!commentNode) { continue; } const innerJsdoc = parseComment(commentNode, ''); checkParamsAndReturnsTags(innerJsdoc); const typeName = /** @type {string} */ (utils.getPreferredTagName({ tagName: 'type', })); const typeTags = getTags(innerJsdoc, typeName); for (const typeTag of typeTags) { checkForUsedTypes(typeTag.type); } } } // Template names may be used only within the heritage clause types of a // class/interface, e.g. `@template T` used in `@augments {Base<T>}`. for (const heritageTagName of [ 'augments', 'extends', 'implements', ]) { for (const heritageTag of getTags(jsdoc, heritageTagName)) { checkForUsedTypes(heritageTag.type); } } checkTemplateTags(); }; const handleTypeAliases = () => { const nde = /** @type {import('@typescript-eslint/types').TSESTree.Node} */ ( node ); if (!nde) { return; } switch (nde.type) { case 'ClassDeclaration': case 'TSInterfaceDeclaration': case 'TSTypeAliasDeclaration': checkParameters(nde); break; case 'ExportDefaultDeclaration': case 'ExportNamedDeclaration': switch (nde.declaration?.type) { case 'ClassDeclaration': case 'TSInterfaceDeclaration': case 'TSTypeAliasDeclaration': checkParameters(nde.declaration); break; case 'FunctionDeclaration': checkParameters(nde.declaration, true); break; } break; case 'FunctionDeclaration': checkParameters(nde, true); break; } }; const callbackTags = utils.getTags('callback'); const functionTags = utils.getTags('function'); if (callbackTags.length || functionTags.length) { checkParamsAndReturnsTags(); checkTemplateTags(); return; } const typedefTags = utils.getTags('typedef'); if (!typedefTags.length || typedefTags.length >= 2) { handleTypeAliases(); return; } const potentialTypedefType = typedefTags[0].type; checkForUsedTypes(potentialTypedefType); const propertyName = /** @type {string} */ (utils.getPreferredTagName({ tagName: 'property', })); const propertyTags = utils.getTags(propertyName); for (const propertyTag of propertyTags) { checkForUsedTypes(propertyTag.type); } checkTemplateTags(); }, { iterateAllJsdocs: true, meta: { docs: { description: 'Checks that any `@template` names are actually used in the connected `@typedef` or type alias.', url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/check-template-names.md#repos-sticky-header', }, schema: [], type: 'suggestion', }, });