forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-deprecated.js
More file actions
217 lines (193 loc) · 6.64 KB
/
template-no-deprecated.js
File metadata and controls
217 lines (193 loc) · 6.64 KB
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
'use strict';
// ts.SymbolFlags.Alias = 2097152 (1 << 21).
// Hardcoded to avoid adding a direct `typescript` dependency. This value has
// been stable since TypeScript was open-sourced (~2014) but is not formally
// guaranteed. If it ever changes, this rule will need to require the user's
// installed TypeScript and read ts.SymbolFlags.Alias at runtime.
const TS_ALIAS_FLAG = 2_097_152;
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description:
'disallow using deprecated Glimmer components, helpers, and modifiers in templates',
category: 'Ember Octane',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-deprecated.md',
},
schema: [],
messages: {
deprecated: '`{{name}}` is deprecated.',
deprecatedWithReason: '`{{name}}` is deprecated. {{reason}}',
},
},
create(context) {
const services = context.sourceCode.parserServices ?? context.parserServices;
if (!services?.program) {
return {};
}
const checker = services.program.getTypeChecker();
const sourceCode = context.sourceCode;
// Cache component class symbol → Args object type (null = no Args) per lint run.
const argsTypeCache = new Map();
function getComponentArgsType(classSymbol) {
if (argsTypeCache.has(classSymbol)) {
return argsTypeCache.get(classSymbol);
}
let result = null;
try {
const declaredType = checker.getDeclaredTypeOfSymbol(classSymbol);
const baseTypes = checker.getBaseTypes(declaredType);
outer: for (const base of baseTypes) {
for (const arg of checker.getTypeArguments(base) ?? []) {
const argsSymbol = arg.getProperty('Args');
if (argsSymbol) {
result = checker.getTypeOfSymbol(argsSymbol);
break outer;
}
}
}
} catch {
result = null;
}
argsTypeCache.set(classSymbol, result);
return result;
}
function getJsDocDeprecation(symbol) {
let jsDocTags;
try {
jsDocTags = symbol?.getJsDocTags(checker);
} catch {
// workaround for https://github.com/microsoft/TypeScript/issues/60024
return undefined;
}
const tag = jsDocTags?.find((t) => t.name === 'deprecated');
if (!tag) {
return undefined;
}
const displayParts = tag.text;
return displayParts ? displayParts.map((p) => p.text).join('') : '';
}
function searchForDeprecationInAliasesChain(symbol, checkAliasedSymbol) {
// eslint-disable-next-line no-bitwise
if (!symbol || !(symbol.flags & TS_ALIAS_FLAG)) {
return checkAliasedSymbol ? getJsDocDeprecation(symbol) : undefined;
}
const targetSymbol = checker.getAliasedSymbol(symbol);
let current = symbol;
// eslint-disable-next-line no-bitwise
while (current.flags & TS_ALIAS_FLAG) {
const reason = getJsDocDeprecation(current);
if (reason !== undefined) {
return reason;
}
const immediateAliasedSymbol =
current.getDeclarations() && checker.getImmediateAliasedSymbol(current);
if (!immediateAliasedSymbol) {
break;
}
current = immediateAliasedSymbol;
if (checkAliasedSymbol && current === targetSymbol) {
return getJsDocDeprecation(current);
}
}
return undefined;
}
function checkDeprecatedIdentifier(identifierNode, scope) {
const ref = scope.references.find((v) => v.identifier === identifierNode);
const variable = ref?.resolved;
const def = variable?.defs[0];
if (!def || def.type !== 'ImportBinding') {
return;
}
const tsNode = services.esTreeNodeToTSNodeMap.get(def.node);
if (!tsNode) {
return;
}
// ImportClause and ImportSpecifier require .name for getSymbolAtLocation
const tsIdentifier = tsNode.name ?? tsNode;
const symbol = checker.getSymbolAtLocation(tsIdentifier);
if (!symbol) {
return;
}
const reason = searchForDeprecationInAliasesChain(symbol, true);
if (reason === undefined) {
return;
}
if (reason === '') {
context.report({
node: identifierNode,
messageId: 'deprecated',
data: { name: identifierNode.name },
});
} else {
context.report({
node: identifierNode,
messageId: 'deprecatedWithReason',
data: { name: identifierNode.name, reason },
});
}
}
return {
GlimmerPathExpression(node) {
checkDeprecatedIdentifier(node.head, sourceCode.getScope(node));
},
GlimmerElementNode(node) {
// GlimmerElementNode is in its own scope; get the outer scope
const scope = sourceCode.getScope(node.parent);
checkDeprecatedIdentifier(node.parts[0], scope);
},
GlimmerAttrNode(node) {
if (!node.name.startsWith('@')) {
return;
}
// Resolve the component import binding from the parent element
const elementNode = node.parent;
const scope = sourceCode.getScope(elementNode.parent);
const ref = scope.references.find((v) => v.identifier === elementNode.parts[0]);
const def = ref?.resolved?.defs[0];
if (!def || def.type !== 'ImportBinding') {
return;
}
const tsNode = services.esTreeNodeToTSNodeMap.get(def.node);
if (!tsNode) {
return;
}
const tsIdentifier = tsNode.name ?? tsNode;
const importSymbol = checker.getSymbolAtLocation(tsIdentifier);
if (!importSymbol) {
return;
}
// Resolve alias to the class symbol
// eslint-disable-next-line no-bitwise
const classSymbol =
importSymbol.flags & TS_ALIAS_FLAG
? checker.getAliasedSymbol(importSymbol)
: importSymbol;
const argsType = getComponentArgsType(classSymbol);
if (!argsType) {
return;
}
const argName = node.name.slice(1); // strip leading '@'
const argSymbol = argsType.getProperty(argName);
const reason = getJsDocDeprecation(argSymbol);
if (reason === undefined) {
return;
}
if (reason === '') {
context.report({
node,
messageId: 'deprecated',
data: { name: node.name },
});
} else {
context.report({
node,
messageId: 'deprecatedWithReason',
data: { name: node.name, reason },
});
}
},
};
},
};