|
| 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 | + utils, |
| 15 | +}) => { |
| 16 | + const { |
| 17 | + enableFixer = true, |
| 18 | + } = context.options[0] || {}; |
| 19 | + |
| 20 | + /** |
| 21 | + * @param {import('@es-joy/jsdoccomment').JsdocTagWithInline} tag |
| 22 | + */ |
| 23 | + const checkType = (tag) => { |
| 24 | + const potentialType = tag.type; |
| 25 | + |
| 26 | + /** @type {import('jsdoc-type-pratt-parser').RootResult} */ |
| 27 | + let parsedType; |
| 28 | + try { |
| 29 | + parsedType = parseType( |
| 30 | + /** @type {string} */ (potentialType), 'typescript', |
| 31 | + ); |
| 32 | + } catch { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + traverse(parsedType, (nde, parentNode) => { |
| 37 | + // @ts-expect-error Adding our own property for use below |
| 38 | + nde.parentNode = parentNode; |
| 39 | + }); |
| 40 | + |
| 41 | + traverse(parsedType, (nde, parentNode, property, index) => { |
| 42 | + switch (nde.type) { |
| 43 | + case 'JsdocTypeCallSignature': { |
| 44 | + const object = /** @type {import('jsdoc-type-pratt-parser').ObjectResult} */ ( |
| 45 | + parentNode |
| 46 | + ); |
| 47 | + if (typeof index === 'number' && object.elements.length === 1) { |
| 48 | + utils.reportJSDoc( |
| 49 | + 'Call signature found; function type preferred.', |
| 50 | + tag, |
| 51 | + enableFixer ? () => { |
| 52 | + const func = /** @type {import('jsdoc-type-pratt-parser').FunctionResult} */ ({ |
| 53 | + arrow: true, |
| 54 | + constructor: false, |
| 55 | + meta: /** @type {Required<import('jsdoc-type-pratt-parser').MethodSignatureResult['meta']>} */ ( |
| 56 | + nde.meta |
| 57 | + ), |
| 58 | + parameters: nde.parameters, |
| 59 | + parenthesis: true, |
| 60 | + returnType: nde.returnType, |
| 61 | + type: 'JsdocTypeFunction', |
| 62 | + typeParameters: nde.typeParameters, |
| 63 | + }); |
| 64 | + |
| 65 | + if (property && 'parentNode' in object && object.parentNode) { |
| 66 | + if (typeof object.parentNode === 'object' && |
| 67 | + 'elements' in object.parentNode && |
| 68 | + Array.isArray(object.parentNode.elements) |
| 69 | + ) { |
| 70 | + const idx = object.parentNode.elements.indexOf(object); |
| 71 | + object.parentNode.elements[idx] = func; |
| 72 | + /* c8 ignore next 6 -- Guard */ |
| 73 | + } else { |
| 74 | + throw new Error( |
| 75 | + // @ts-expect-error Ok |
| 76 | + `Rule currently unable to handle type ${object.parentNode.type}`, |
| 77 | + ); |
| 78 | + } |
| 79 | + } else { |
| 80 | + parsedType = func; |
| 81 | + } |
| 82 | + |
| 83 | + rewireByParsedType(jsdoc, tag, parsedType, indent); |
| 84 | + } : null, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + }; |
| 93 | + |
| 94 | + const tags = utils.filterTags(({ |
| 95 | + tag, |
| 96 | + }) => { |
| 97 | + return Boolean(tag !== 'import' && utils.tagMightHaveTypePosition(tag)); |
| 98 | + }); |
| 99 | + |
| 100 | + for (const tag of tags) { |
| 101 | + if (tag.type) { |
| 102 | + checkType(tag); |
| 103 | + } |
| 104 | + } |
| 105 | +}, { |
| 106 | + iterateAllJsdocs: true, |
| 107 | + meta: { |
| 108 | + docs: { |
| 109 | + description: '', |
| 110 | + url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/ts-prefer-function-type.md#repos-sticky-header', |
| 111 | + }, |
| 112 | + fixable: 'code', |
| 113 | + schema: [ |
| 114 | + { |
| 115 | + additionalProperties: false, |
| 116 | + properties: { |
| 117 | + enableFixer: { |
| 118 | + description: 'Whether to enable the fixer or not', |
| 119 | + type: 'boolean', |
| 120 | + }, |
| 121 | + }, |
| 122 | + type: 'object', |
| 123 | + }, |
| 124 | + ], |
| 125 | + type: 'suggestion', |
| 126 | + }, |
| 127 | +}); |
0 commit comments