Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: print deprecation reason correctly on ts 4.3+ #35

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}