/
no-unnecessary-type-assertion.ts
167 lines (152 loc) · 4.54 KB
/
no-unnecessary-type-assertion.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
import { TSESTree } from '@typescript-eslint/util';
import * as tsutils from 'tsutils';
import ts from 'typescript';
import * as util from '../util';
type Options = [
{
typesToIgnore?: string[];
}
];
type MessageIds = 'unnecessaryAssertion';
export default util.createRule<Options, MessageIds>({
name: 'no-unnecessary-type-assertion',
meta: {
docs: {
description:
'Warns if a type assertion does not change the type of an expression',
category: 'Best Practices',
recommended: false,
tslintRuleName: 'no-unnecessary-type-assertion',
},
fixable: 'code',
messages: {
unnecessaryAssertion:
'This assertion is unnecessary since it does not change the type of the expression.',
},
schema: [
{
type: 'object',
properties: {
typesToIgnore: {
type: 'array',
items: {
type: 'string',
},
},
},
},
],
type: 'suggestion',
},
defaultOptions: [{}],
create(context, [options]) {
const sourceCode = context.getSourceCode();
const parserServices = util.getParserServices(context);
/**
* Sometimes tuple types don't have ObjectFlags.Tuple set, like when they're being matched against an inferred type.
* So, in addition, check if there are integer properties 0..n and no other numeric keys
*/
function couldBeTupleType(type: ts.ObjectType): boolean {
const properties = type.getProperties();
if (properties.length === 0) {
return false;
}
let i = 0;
for (; i < properties.length; ++i) {
const name = properties[i].name;
if (String(i) !== name) {
if (i === 0) {
// if there are no integer properties, this is not a tuple
return false;
}
break;
}
}
for (; i < properties.length; ++i) {
if (String(+properties[i].name) === properties[i].name) {
return false; // if there are any other numeric properties, this is not a tuple
}
}
return true;
}
function checkNonNullAssertion(
node: TSESTree.Node,
checker: ts.TypeChecker,
): void {
const originalNode = parserServices.esTreeNodeToTSNodeMap.get<
ts.NonNullExpression
>(node);
const type = checker.getTypeAtLocation(originalNode.expression);
if (type === checker.getNonNullableType(type)) {
context.report({
node,
messageId: 'unnecessaryAssertion',
fix(fixer) {
return fixer.removeRange([
originalNode.expression.end,
originalNode.end,
]);
},
});
}
}
function verifyCast(
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
checker: ts.TypeChecker,
): void {
if (
options &&
options.typesToIgnore &&
options.typesToIgnore.indexOf(
sourceCode.getText(node.typeAnnotation),
) !== -1
) {
return;
}
const originalNode = parserServices.esTreeNodeToTSNodeMap.get<
ts.AssertionExpression
>(node);
const castType = checker.getTypeAtLocation(originalNode);
if (
tsutils.isTypeFlagSet(castType, ts.TypeFlags.Literal) ||
(tsutils.isObjectType(castType) &&
(tsutils.isObjectFlagSet(castType, ts.ObjectFlags.Tuple) ||
couldBeTupleType(castType)))
) {
// It's not always safe to remove a cast to a literal type or tuple
// type, as those types are sometimes widened without the cast.
return;
}
const uncastType = checker.getTypeAtLocation(originalNode.expression);
if (uncastType === castType) {
context.report({
node,
messageId: 'unnecessaryAssertion',
fix(fixer) {
return originalNode.kind === ts.SyntaxKind.TypeAssertionExpression
? fixer.removeRange([
originalNode.getStart(),
originalNode.expression.getStart(),
])
: fixer.removeRange([
originalNode.expression.end,
originalNode.end,
]);
},
});
}
}
const checker = parserServices.program.getTypeChecker();
return {
TSNonNullExpression(node) {
checkNonNullAssertion(node, checker);
},
TSTypeAssertion(node) {
verifyCast(node, checker);
},
TSAsExpression(node) {
verifyCast(node, checker);
},
};
},
});