-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathdeprecation.ts
215 lines (192 loc) · 6.56 KB
/
deprecation.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
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2021 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// https://jira.sonarsource.com/browse/RSPEC-1874
import { Rule } from 'eslint';
import { TSESTree } from '@typescript-eslint/experimental-utils';
import * as estree from 'estree';
import { isRequiredParserServices, RequiredParserServices } from '../utils';
import * as ts from 'typescript';
import { getParent } from 'eslint-plugin-sonarjs/lib/utils/nodes';
export const rule: Rule.RuleModule = {
create(context: Rule.RuleContext) {
const services = context.parserServices;
if (!isRequiredParserServices(services)) {
return {};
}
return {
Identifier: (node: estree.Node) => {
const parent = getParent(context);
if (isShortHandProperty(parent) && parent.key === node) {
// to not report twice
return;
}
const id = node as estree.Identifier;
const insideImportExport = context.getAncestors().some(anc => anc.type.includes('Import'));
if (insideImportExport || isDeclaration(id, context)) {
return;
}
const deprecation = getDeprecation(id, services, context);
if (deprecation) {
context.report({
node,
message: `'${id.name}' is deprecated. ${deprecation.reason}`,
});
}
},
};
},
};
function isDeclaration(id: estree.Identifier, context: Rule.RuleContext) {
const parent = getParent(context);
if (isShortHandProperty(parent) && parent.value === id) {
return false;
}
const variable = context.getScope().variables.find(v => v.name === id.name);
if (variable) {
return variable.defs.some(def => def.name === id);
}
const declarationTypes = [
'ClassProperty',
'TSPropertySignature',
'TSDeclareFunction',
'FunctionDeclaration',
'MethodDefinition',
'TSMethodSignature',
];
return parent && declarationTypes.includes(parent.type);
}
function getDeprecation(
id: estree.Identifier,
services: RequiredParserServices,
context: Rule.RuleContext,
) {
const tc = services.program.getTypeChecker();
const callExpression = getCallExpression(context, id);
if (callExpression) {
const tsCallExpression = services.esTreeNodeToTSNodeMap.get(callExpression as TSESTree.Node);
const signature = tc.getResolvedSignature(tsCallExpression as ts.CallLikeExpression);
if (signature) {
const deprecation = getJsDocDeprecation(signature.getJsDocTags());
if (deprecation) {
return deprecation;
}
}
}
const symbol = getSymbol(id, services, context, tc);
if (!symbol) {
return undefined;
}
if (callExpression && isFunction(symbol)) {
return undefined;
}
return getJsDocDeprecation(symbol.getJsDocTags());
}
function getSymbol(
id: estree.Identifier,
services: RequiredParserServices,
context: Rule.RuleContext,
tc: ts.TypeChecker,
) {
let symbol: ts.Symbol | undefined;
const tsId = services.esTreeNodeToTSNodeMap.get(id as TSESTree.Node) as ts.Identifier;
const parent = services.esTreeNodeToTSNodeMap.get(getParent(context) as TSESTree.Node) as ts.Node;
if (parent.kind === ts.SyntaxKind.BindingElement) {
symbol = tc.getTypeAtLocation(parent.parent).getProperty(tsId.text);
} else if (
(isPropertyAssignment(parent) && parent.name === tsId) ||
(isShorthandPropertyAssignment(parent) && parent.name === tsId)
) {
try {
symbol = tc.getPropertySymbolOfDestructuringAssignment(tsId);
} catch (e) {
// do nothing, we are in object literal, not destructuring
// no obvious easy way to check that in advance
}
} else {
symbol = tc.getSymbolAtLocation(tsId);
}
if (symbol && (symbol.flags & ts.SymbolFlags.Alias) !== 0) {
return tc.getAliasedSymbol(symbol);
}
return symbol;
}
function getCallExpression(
context: Rule.RuleContext,
id: estree.Node,
): estree.CallExpression | estree.TaggedTemplateExpression | undefined {
const ancestors = context.getAncestors();
let callee = id;
let parent = ancestors.length > 0 ? ancestors[ancestors.length - 1] : undefined;
if (parent && parent.type === 'MemberExpression' && parent.property === id) {
callee = parent;
parent = ancestors.length > 1 ? ancestors[ancestors.length - 2] : undefined;
}
if (isCallExpression(parent, callee)) {
return parent;
}
}
function isCallExpression(
node: estree.Node | undefined,
callee: estree.Node,
): node is estree.CallExpression | estree.TaggedTemplateExpression {
if (node) {
if (node.type === 'NewExpression' || node.type === 'CallExpression') {
return node.callee === callee;
} else if (node.type === 'TaggedTemplateExpression') {
return node.tag === callee;
}
}
return false;
}
function getJsDocDeprecation(tags: ts.JSDocTagInfo[]) {
for (const tag of tags) {
if (tag.name === 'deprecated') {
return tag.text ? { reason: tag.text } : new Deprecation();
}
}
return undefined;
}
function isFunction(symbol: ts.Symbol) {
const { declarations } = symbol;
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;
}
}
function isPropertyAssignment(node: ts.Node): node is ts.PropertyAssignment {
return node.kind === ts.SyntaxKind.PropertyAssignment;
}
function isShorthandPropertyAssignment(node: ts.Node): node is ts.ShorthandPropertyAssignment {
return node.kind === ts.SyntaxKind.ShorthandPropertyAssignment;
}
function isShortHandProperty(parent: estree.Node | undefined): parent is estree.Property {
return !!parent && parent.type === 'Property' && parent.shorthand;
}
class Deprecation {
reason = '';
}