Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
fix: print deprecation reason correctly on ts 4.3+
Browse files Browse the repository at this point in the history
  • Loading branch information
gluxon committed Dec 14, 2021
1 parent a03446f commit e3c3c47
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/rules/deprecation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/utils/stringifyJSDocTagInfoText.ts
Original file line number Diff line number Diff line change
@@ -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';
}

0 comments on commit e3c3c47

Please sign in to comment.