This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
/
deprecationRule.ts
259 lines (242 loc) · 9.01 KB
/
deprecationRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* @license
* Copyright 2018 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
getDeclarationOfBindingElement,
getJsDoc,
isBindingElement,
isCallExpression,
isIdentifier,
isNewExpression,
isPropertyAccessExpression,
isPropertyAssignment,
isReassignmentTarget,
isShorthandPropertyAssignment,
isSymbolFlagSet,
isTaggedTemplateExpression,
isVariableDeclaration,
isVariableDeclarationList,
} from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
export class Rule extends Lint.Rules.TypedRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "deprecation",
description: "Warns when deprecated APIs are used.",
descriptionDetails: Lint.Utils.dedent`Any usage of an identifier
with the @deprecated JSDoc annotation will trigger a warning.
See http://usejsdoc.org/tags-deprecated.html`,
rationale: "Deprecated APIs should be avoided, and usage updated.",
optionsDescription: "",
options: null,
optionExamples: [true],
type: "maintainability",
typescriptOnly: false,
requiresTypeInfo: true,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING(name: string, message: string) {
return `${name} is deprecated${message === "" ? "." : `: ${message.trim()}`}`;
}
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, undefined, program.getTypeChecker());
}
}
function walk(ctx: Lint.WalkContext, tc: ts.TypeChecker) {
return ts.forEachChild(ctx.sourceFile, function cb(node): void {
if (isIdentifier(node)) {
if (!isDeclaration(node)) {
const deprecation = getDeprecation(node, tc);
if (deprecation !== undefined) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING(node.text, deprecation));
}
}
} else {
switch (node.kind) {
case ts.SyntaxKind.ImportDeclaration:
case ts.SyntaxKind.ImportEqualsDeclaration:
case ts.SyntaxKind.ExportDeclaration:
case ts.SyntaxKind.ExportAssignment:
return;
}
return ts.forEachChild(node, cb);
}
});
}
function isDeclaration(identifier: ts.Identifier): boolean {
const parent = identifier.parent;
switch (parent.kind) {
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ClassExpression:
case ts.SyntaxKind.InterfaceDeclaration:
case ts.SyntaxKind.TypeParameter:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.LabeledStatement:
case ts.SyntaxKind.JsxAttribute:
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.MethodSignature:
case ts.SyntaxKind.PropertySignature:
case ts.SyntaxKind.TypeAliasDeclaration:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.EnumDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
return true;
case ts.SyntaxKind.VariableDeclaration:
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.EnumMember:
case ts.SyntaxKind.ImportEqualsDeclaration:
return (parent as ts.NamedDeclaration).name === identifier;
case ts.SyntaxKind.PropertyAssignment:
return (
(parent as ts.PropertyAssignment).name === identifier &&
!isReassignmentTarget(identifier.parent.parent as ts.ObjectLiteralExpression)
);
case ts.SyntaxKind.BindingElement:
// return true for `b` in `const {a: b} = obj"`
return (
(parent as ts.BindingElement).name === identifier &&
(parent as ts.BindingElement).propertyName !== undefined
);
default:
return false;
}
}
function getCallExpresion(node: ts.Expression): ts.CallLikeExpression | undefined {
let parent = node.parent;
if (isPropertyAccessExpression(parent) && parent.name === node) {
node = parent;
parent = node.parent;
}
return isTaggedTemplateExpression(parent) ||
((isCallExpression(parent) || isNewExpression(parent)) && parent.expression === node)
? parent
: undefined;
}
function getDeprecation(node: ts.Identifier, tc: ts.TypeChecker): string | undefined {
const callExpression = getCallExpresion(node);
if (callExpression !== undefined) {
const result = getSignatureDeprecation(tc.getResolvedSignature(callExpression));
if (result !== undefined) {
return result;
}
}
let symbol: ts.Symbol | undefined;
const parent = node.parent;
if (parent.kind === ts.SyntaxKind.BindingElement) {
symbol = tc.getTypeAtLocation(parent.parent).getProperty(node.text);
} else if (
(isPropertyAssignment(parent) && parent.name === node) ||
(isShorthandPropertyAssignment(parent) &&
parent.name === node &&
isReassignmentTarget(node))
) {
symbol = tc.getPropertySymbolOfDestructuringAssignment(node);
} else {
symbol = tc.getSymbolAtLocation(node);
}
if (symbol !== undefined && isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)) {
symbol = tc.getAliasedSymbol(symbol);
}
if (
symbol === undefined ||
// if this is a CallExpression and the declaration is a function or method,
// stop here to avoid collecting JsDoc of all overload signatures
(callExpression !== undefined && isFunctionOrMethod(symbol.declarations))
) {
return undefined;
}
return getSymbolDeprecation(symbol);
}
function findDeprecationTag(tags: ts.JSDocTagInfo[]): string | undefined {
for (const tag of tags) {
if (tag.name === "deprecated") {
return tag.text === undefined ? "" : tag.text;
}
}
return undefined;
}
function getSymbolDeprecation(symbol: ts.Symbol): string | undefined {
if (symbol.getJsDocTags !== undefined) {
return findDeprecationTag(symbol.getJsDocTags());
}
// for compatibility with typescript@<2.3.0
return getDeprecationFromDeclarations(symbol.declarations);
}
function getSignatureDeprecation(signature?: ts.Signature): string | undefined {
if (signature === undefined) {
return undefined;
}
if (signature.getJsDocTags !== undefined) {
return findDeprecationTag(signature.getJsDocTags());
}
// for compatibility with typescript@<2.3.0
return signature.declaration === undefined
? undefined
: getDeprecationFromDeclaration(signature.declaration);
}
function getDeprecationFromDeclarations(declarations?: ts.Declaration[]): string | undefined {
if (declarations === undefined) {
return undefined;
}
let declaration: ts.Node;
for (declaration of declarations) {
if (isBindingElement(declaration)) {
declaration = getDeclarationOfBindingElement(declaration);
}
if (isVariableDeclaration(declaration)) {
declaration = declaration.parent;
}
if (isVariableDeclarationList(declaration)) {
declaration = declaration.parent;
}
const result = getDeprecationFromDeclaration(declaration);
if (result !== undefined) {
return result;
}
}
return undefined;
}
function getDeprecationFromDeclaration(declaration: ts.Node): string | undefined {
for (const comment of getJsDoc(declaration)) {
if (comment.tags === undefined) {
continue;
}
for (const tag of comment.tags) {
if (tag.tagName.text === "deprecated") {
return tag.comment === undefined ? "" : tag.comment;
}
}
}
return undefined;
}
function isFunctionOrMethod(declarations?: ts.Declaration[]) {
if (declarations === undefined || declarations.length === 0) {
return false;
}
switch (declarations[0].kind) {
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.MethodSignature:
return true;
default:
return false;
}
}