|
| 1 | +import {JSDocTagInfo} from '../../../../libraries/typescript/lib/typescript'; |
| 2 | +import {Diagnostic} from '../../interfaces'; |
| 3 | +import {Handler} from './handler'; |
| 4 | +import {makeDiagnostic, tsutils} from '../../utils'; |
| 5 | + |
| 6 | +interface Options { |
| 7 | + filter(tags: Map<string, JSDocTagInfo>): boolean; |
| 8 | + message(signature: string): string; |
| 9 | +} |
| 10 | + |
| 11 | +const expectDeprecatedHelper = (options: Options): Handler => { |
| 12 | + return (checker, nodes) => { |
| 13 | + const diagnostics: Diagnostic[] = []; |
| 14 | + |
| 15 | + if (!nodes) { |
| 16 | + // Bail out if we don't have any nodes |
| 17 | + return diagnostics; |
| 18 | + } |
| 19 | + |
| 20 | + for (const node of nodes) { |
| 21 | + const argument = node.arguments[0]; |
| 22 | + |
| 23 | + const tags = tsutils.resolveJSDocTags(checker, argument); |
| 24 | + |
| 25 | + if (!tags || !options.filter(tags)) { |
| 26 | + // Bail out if not tags couldn't be resolved or when the node matches the filter expression |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + const message = tsutils.expressionToString(checker, argument); |
| 31 | + |
| 32 | + diagnostics.push(makeDiagnostic(node, options.message(message || '?'))); |
| 33 | + } |
| 34 | + |
| 35 | + return diagnostics; |
| 36 | + }; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Assert that the argument from the `expectDeprecated` statement is marked as `@deprecated`. |
| 41 | + * If it's not marked as `@deprecated`, an error diagnostic is returned. |
| 42 | + * |
| 43 | + * @param checker - The TypeScript type checker. |
| 44 | + * @param nodes - The `expectDeprecated` AST nodes. |
| 45 | + * @return List of diagnostics. |
| 46 | + */ |
| 47 | +export const expectDeprecated = expectDeprecatedHelper({ |
| 48 | + filter: tags => !tags.has('deprecated'), |
| 49 | + message: signature => `Expected \`${signature}\` to be marked as \`@deprecated\`` |
| 50 | +}); |
| 51 | + |
| 52 | +/** |
| 53 | + * Assert that the argument from the `expectNotDeprecated` statement is not marked as `@deprecated`. |
| 54 | + * If it's marked as `@deprecated`, an error diagnostic is returned. |
| 55 | + * |
| 56 | + * @param checker - The TypeScript type checker. |
| 57 | + * @param nodes - The `expectNotDeprecated` AST nodes. |
| 58 | + * @return List of diagnostics. |
| 59 | + */ |
| 60 | +export const expectNotDeprecated = expectDeprecatedHelper({ |
| 61 | + filter: tags => tags.has('deprecated'), |
| 62 | + message: signature => `Expected \`${signature}\` to not be marked as \`@deprecated\`` |
| 63 | +}); |
0 commit comments