From e3c3c47885c44cd48dddf744868102fcf3dff6e7 Mon Sep 17 00:00:00 2001 From: Brandon Cheng Date: Tue, 14 Dec 2021 10:59:28 -0500 Subject: [PATCH] fix: print deprecation reason correctly on ts 4.3+ --- src/rules/deprecation.ts | 3 ++- src/utils/stringifyJSDocTagInfoText.ts | 28 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/utils/stringifyJSDocTagInfoText.ts diff --git a/src/rules/deprecation.ts b/src/rules/deprecation.ts index 47dd7cc..01cca04 100644 --- a/src/rules/deprecation.ts +++ b/src/rules/deprecation.ts @@ -20,6 +20,7 @@ import { } from '@typescript-eslint/experimental-utils'; import { isReassignmentTarget } from 'tsutils'; import * as ts from 'typescript'; +import { stringifyJSDocTagInfoText } from '../utils/stringifyJSDocTagInfoText'; const createRule = ESLintUtils.RuleCreator( () => 'https://github.com/gund/eslint-plugin-deprecation', @@ -288,7 +289,7 @@ function isCallExpression( function getJsDocDeprecation(tags: ts.JSDocTagInfo[]) { for (const tag of tags) { if (tag.name === 'deprecated') { - return { reason: tag.text || '' }; + return { reason: stringifyJSDocTagInfoText(tag) }; } } return undefined; diff --git a/src/utils/stringifyJSDocTagInfoText.ts b/src/utils/stringifyJSDocTagInfoText.ts new file mode 100644 index 0000000..afa3532 --- /dev/null +++ b/src/utils/stringifyJSDocTagInfoText.ts @@ -0,0 +1,28 @@ +import * as ts from 'typescript'; + +/** + * Stringifies the text within a JSDocTagInfo AST node with compatibility for + * pre/post TypeScript 4.3 API changes. + */ +export function stringifyJSDocTagInfoText(tag: ts.JSDocTagInfo): string { + return isJSDocTagInfo4Point2AndBefore(tag) + ? tag.text ?? '' + : ts.displayPartsToString(tag.text); +} + +/** + * Copied from TypeScript 4.2. + * https://github.com/microsoft/TypeScript/blob/fb6c8392681f50a305236a7d662123a69827061f/lib/protocol.d.ts#L2820-L2823 + * + * The `text` field was changed from `string` to `SymbolDisplayPart[]` in 4.3. + */ +interface JSDocTagInfo4Point2AndBefore { + name: string; + text?: string; +} + +function isJSDocTagInfo4Point2AndBefore( + tag: ts.JSDocTagInfo | JSDocTagInfo4Point2AndBefore, +): tag is JSDocTagInfo4Point2AndBefore { + return typeof tag.text === 'string'; +}