|
| 1 | +import iterateJsdoc from '../iterateJsdoc.js'; |
| 2 | +import { |
| 3 | + rewireByParsedType, |
| 4 | +} from '../jsdocUtils.js'; |
| 5 | +import { |
| 6 | + parse as parseType, |
| 7 | + traverse, |
| 8 | +} from '@es-joy/jsdoccomment'; |
| 9 | + |
| 10 | +export default iterateJsdoc(({ |
| 11 | + context, |
| 12 | + indent, |
| 13 | + jsdoc, |
| 14 | + settings, |
| 15 | + utils, |
| 16 | +}) => { |
| 17 | + if (settings.mode !== 'typescript') { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const { |
| 22 | + enableFixer = true, |
| 23 | + } = context.options[0] ?? {}; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param {import('@es-joy/jsdoccomment').JsdocTagWithInline} tag |
| 27 | + */ |
| 28 | + const checkType = (tag) => { |
| 29 | + const potentialType = tag.type; |
| 30 | + /** @type {import('jsdoc-type-pratt-parser').RootResult} */ |
| 31 | + let parsedType; |
| 32 | + try { |
| 33 | + parsedType = parseType( |
| 34 | + /** @type {string} */ (potentialType), 'typescript', |
| 35 | + ); |
| 36 | + } catch { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + traverse(parsedType, (nde, parentNode, property, index) => { |
| 41 | + switch (nde.type) { |
| 42 | + case 'JsdocTypeTemplateLiteral': { |
| 43 | + const stringInterpolationIndex = nde.interpolations.findIndex((interpolation) => { |
| 44 | + return interpolation.type === 'JsdocTypeStringValue'; |
| 45 | + }); |
| 46 | + if (stringInterpolationIndex > -1) { |
| 47 | + utils.reportJSDoc( |
| 48 | + 'Found an unnecessary string literal within a template.', |
| 49 | + tag, |
| 50 | + enableFixer ? () => { |
| 51 | + nde.literals.splice( |
| 52 | + stringInterpolationIndex, |
| 53 | + 2, |
| 54 | + nde.literals[stringInterpolationIndex] + |
| 55 | + /** @type {import('jsdoc-type-pratt-parser').StringValueResult} */ |
| 56 | + (nde.interpolations[stringInterpolationIndex]).value + |
| 57 | + nde.literals[stringInterpolationIndex + 1], |
| 58 | + ); |
| 59 | + |
| 60 | + nde.interpolations.splice( |
| 61 | + stringInterpolationIndex, 1, |
| 62 | + ); |
| 63 | + |
| 64 | + rewireByParsedType(jsdoc, tag, parsedType, indent); |
| 65 | + } : null, |
| 66 | + ); |
| 67 | + } else if (nde.literals.length === 2 && nde.literals[0] === '' && |
| 68 | + nde.literals[1] === '' |
| 69 | + ) { |
| 70 | + utils.reportJSDoc( |
| 71 | + 'Found a lone template expression within a template.', |
| 72 | + tag, |
| 73 | + enableFixer ? () => { |
| 74 | + const interpolation = nde.interpolations[0]; |
| 75 | + |
| 76 | + if (parentNode && property) { |
| 77 | + if (typeof index === 'number') { |
| 78 | + // @ts-expect-error Safe |
| 79 | + parentNode[property][index] = interpolation; |
| 80 | + } else { |
| 81 | + // @ts-expect-error Safe |
| 82 | + parentNode[property] = interpolation; |
| 83 | + } |
| 84 | + } else { |
| 85 | + parsedType = interpolation; |
| 86 | + } |
| 87 | + |
| 88 | + rewireByParsedType(jsdoc, tag, parsedType, indent); |
| 89 | + } : null, |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + }; |
| 96 | + |
| 97 | + const tags = utils.filterTags(({ |
| 98 | + tag, |
| 99 | + }) => { |
| 100 | + return Boolean(tag !== 'import' && utils.tagMightHaveTypePosition(tag)); |
| 101 | + }); |
| 102 | + |
| 103 | + for (const tag of tags) { |
| 104 | + if (tag.type) { |
| 105 | + checkType(tag); |
| 106 | + } |
| 107 | + } |
| 108 | +}, { |
| 109 | + iterateAllJsdocs: true, |
| 110 | + meta: { |
| 111 | + docs: { |
| 112 | + description: 'Catches unnecessary template expressions such as string expressions within a template literal.', |
| 113 | + url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/ts-no-unnecessary-template-expression.md#repos-sticky-header', |
| 114 | + }, |
| 115 | + fixable: 'code', |
| 116 | + schema: [ |
| 117 | + { |
| 118 | + additionalProperties: false, |
| 119 | + properties: { |
| 120 | + enableFixer: { |
| 121 | + description: 'Whether to enable the fixer. Defaults to `true`.', |
| 122 | + type: 'boolean', |
| 123 | + }, |
| 124 | + }, |
| 125 | + type: 'object', |
| 126 | + }, |
| 127 | + ], |
| 128 | + type: 'suggestion', |
| 129 | + }, |
| 130 | +}); |
0 commit comments