-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
no-deprecated.ts
111 lines (106 loc) · 3.27 KB
/
no-deprecated.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
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/eslint-plugin-etc
*/
import { TSESTree as es } from "@typescript-eslint/experimental-utils";
import { getParent, getParserServices } from "eslint-etc";
import * as ts from "typescript";
import { findTaggedNames } from "../tag";
import { getTags, isDeclaration } from "../tslint-tag";
import { ruleCreator } from "../utils";
const deprecatedNamesByProgram = new WeakMap<ts.Program, Set<string>>();
const defaultOptions: readonly {
ignored?: Record<string, string>;
}[] = [];
const rule = ruleCreator({
defaultOptions,
meta: {
docs: {
description: "Forbids the use of deprecated APIs.",
recommended: false,
},
fixable: undefined,
hasSuggestions: false,
messages: {
forbidden: `"{{name}}" is deprecated: {{comment}}`,
},
schema: [
{
properties: {
ignored: { type: "object" },
},
type: "object",
},
],
type: "problem",
},
name: "no-deprecated",
create: (context, unused: typeof defaultOptions) => {
const [{ ignored = {} } = {}] = context.options;
const ignoredNameRegExps: RegExp[] = [];
const ignoredPathRegExps: RegExp[] = [];
Object.entries(ignored).forEach(([key, value]) => {
switch (value) {
case "name":
ignoredNameRegExps.push(new RegExp(key));
break;
case "path":
ignoredPathRegExps.push(new RegExp(key));
break;
default:
break;
}
});
const { esTreeNodeToTSNodeMap, program } = getParserServices(context);
const typeChecker = program.getTypeChecker();
const getPath = (identifier: ts.Identifier) => {
const type = typeChecker.getTypeAtLocation(identifier);
return typeChecker.getFullyQualifiedName(type.symbol);
};
let deprecatedNames = deprecatedNamesByProgram.get(program);
if (!deprecatedNames) {
deprecatedNames = findTaggedNames("deprecated", program);
deprecatedNamesByProgram.set(program, deprecatedNames);
}
return {
Identifier: (node: es.Identifier) => {
switch (getParent(node)?.type) {
case "ExportSpecifier":
case "ImportDefaultSpecifier":
case "ImportNamespaceSpecifier":
case "ImportSpecifier":
return;
default:
break;
}
const identifier = esTreeNodeToTSNodeMap.get(node) as ts.Identifier;
if (!deprecatedNames?.has(identifier.text)) {
return;
}
if (isDeclaration(identifier)) {
return;
}
if (
ignoredNameRegExps.some((regExp) => regExp.test(identifier.text)) ||
ignoredPathRegExps.some((regExp) => regExp.test(getPath(identifier)))
) {
return;
}
const tags = getTags("deprecated", identifier, typeChecker);
if (tags.length > 0) {
for (const tag of tags) {
context.report({
data: {
comment: tag.trim().replace(/[\n\r\s\t]+/g, " "),
name: identifier.text,
},
messageId: "forbidden",
node,
});
}
}
},
};
},
});
export = rule;