/
no-unnecessary-type-arguments.ts
185 lines (165 loc) · 5.61 KB
/
no-unnecessary-type-arguments.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
import { TSESTree } from '@typescript-eslint/utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';
import { findFirstResult } from '../util';
type ParameterCapableTSNode =
| ts.TaggedTemplateExpression
| ts.ImportTypeNode
| ts.CallExpression
| ts.NewExpression
| ts.TypeReferenceNode
| ts.ExpressionWithTypeArguments
| ts.JsxOpeningElement
| ts.JsxSelfClosingElement;
type MessageIds = 'unnecessaryTypeParameter';
export default util.createRule<[], MessageIds>({
name: 'no-unnecessary-type-arguments',
meta: {
docs: {
description:
'Enforces that type arguments will not be used if not required',
recommended: false,
requiresTypeChecking: true,
},
fixable: 'code',
messages: {
unnecessaryTypeParameter:
'This is the default value for this type parameter, so it can be omitted.',
},
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
function getTypeForComparison(type: ts.Type): {
type: ts.Type;
typeArguments: readonly ts.Type[];
} {
if (util.isTypeReferenceType(type)) {
return {
type: type.target,
typeArguments: util.getTypeArguments(type, checker),
};
}
return {
type,
typeArguments: [],
};
}
function checkTSArgsAndParameters(
esParameters: TSESTree.TSTypeParameterInstantiation,
typeParameters: readonly ts.TypeParameterDeclaration[],
): void {
// Just check the last one. Must specify previous type parameters if the last one is specified.
const i = esParameters.params.length - 1;
const arg = esParameters.params[i];
const param = typeParameters[i];
if (!param?.default) {
return;
}
// TODO: would like checker.areTypesEquivalent. https://github.com/Microsoft/TypeScript/issues/13502
const defaultType = checker.getTypeAtLocation(param.default);
const argTsNode = parserServices.esTreeNodeToTSNodeMap.get(arg);
const argType = checker.getTypeAtLocation(argTsNode);
// this check should handle some of the most simple cases of like strings, numbers, etc
if (defaultType !== argType) {
// For more complex types (like aliases to generic object types) - TS won't always create a
// global shared type object for the type - so we need to resort to manually comparing the
// reference type and the passed type arguments.
// Also - in case there are aliases - we need to resolve them before we do checks
const defaultTypeResolved = getTypeForComparison(defaultType);
const argTypeResolved = getTypeForComparison(argType);
if (
// ensure the resolved type AND all the parameters are the same
defaultTypeResolved.type !== argTypeResolved.type ||
defaultTypeResolved.typeArguments.length !==
argTypeResolved.typeArguments.length ||
defaultTypeResolved.typeArguments.some(
(t, i) => t !== argTypeResolved.typeArguments[i],
)
) {
return;
}
}
context.report({
node: arg,
messageId: 'unnecessaryTypeParameter',
fix: fixer =>
fixer.removeRange(
i === 0
? esParameters.range
: [esParameters.params[i - 1].range[1], arg.range[1]],
),
});
}
return {
TSTypeParameterInstantiation(node): void {
const expression = parserServices.esTreeNodeToTSNodeMap.get(node);
const typeParameters = getTypeParametersFromNode(expression, checker);
if (typeParameters) {
checkTSArgsAndParameters(node, typeParameters);
}
},
};
},
});
function getTypeParametersFromNode(
node: ParameterCapableTSNode,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
if (ts.isExpressionWithTypeArguments(node)) {
return getTypeParametersFromType(node.expression, checker);
}
if (ts.isTypeReferenceNode(node)) {
return getTypeParametersFromType(node.typeName, checker);
}
if (ts.isCallExpression(node) || ts.isNewExpression(node)) {
return getTypeParametersFromCall(node, checker);
}
return undefined;
}
function getTypeParametersFromType(
type: ts.EntityName | ts.Expression | ts.ClassDeclaration,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
const symAtLocation = checker.getSymbolAtLocation(type);
if (!symAtLocation) {
return undefined;
}
const sym = getAliasedSymbol(symAtLocation, checker);
const declarations = sym.getDeclarations();
if (!declarations) {
return undefined;
}
return findFirstResult(declarations, decl =>
ts.isClassLike(decl) ||
ts.isTypeAliasDeclaration(decl) ||
ts.isInterfaceDeclaration(decl)
? decl.typeParameters
: undefined,
);
}
function getTypeParametersFromCall(
node: ts.CallExpression | ts.NewExpression,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
const sig = checker.getResolvedSignature(node);
const sigDecl = sig?.getDeclaration();
if (!sigDecl) {
return ts.isNewExpression(node)
? getTypeParametersFromType(node.expression, checker)
: undefined;
}
return sigDecl.typeParameters;
}
function getAliasedSymbol(
symbol: ts.Symbol,
checker: ts.TypeChecker,
): ts.Symbol {
return tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.Alias)
? checker.getAliasedSymbol(symbol)
: symbol;
}