-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
non-nullable-type-assertion-style.ts
118 lines (103 loc) · 3.07 KB
/
non-nullable-type-assertion-style.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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';
export default util.createRule({
name: 'non-nullable-type-assertion-style',
meta: {
docs: {
category: 'Best Practices',
description:
'Prefers a non-null assertion over explicit type cast when possible',
recommended: false,
requiresTypeChecking: true,
suggestion: true,
},
fixable: 'code',
messages: {
preferNonNullAssertion:
'Use a ! assertion to more succintly remove null and undefined from the type.',
},
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const sourceCode = context.getSourceCode();
const getTypesIfNotLoose = (node: TSESTree.Node): ts.Type[] | undefined => {
const type = checker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node),
);
if (
tsutils.isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)
) {
return undefined;
}
return tsutils.unionTypeParts(type);
};
const sameTypeWithoutNullish = (
assertedTypes: ts.Type[],
originalTypes: ts.Type[],
): boolean => {
const nonNullishOriginalTypes = originalTypes.filter(
type =>
type.flags !== ts.TypeFlags.Null &&
type.flags !== ts.TypeFlags.Undefined,
);
for (const assertedType of assertedTypes) {
if (!nonNullishOriginalTypes.includes(assertedType)) {
return false;
}
}
for (const originalType of nonNullishOriginalTypes) {
if (!assertedTypes.includes(originalType)) {
return false;
}
}
return true;
};
const isConstAssertion = (
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
): boolean => {
return (
node.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference &&
node.typeAnnotation.typeName.type === AST_NODE_TYPES.Identifier &&
node.typeAnnotation.typeName.name === 'const'
);
};
return {
'TSAsExpression, TSTypeAssertion'(
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
): void {
if (isConstAssertion(node)) {
return;
}
const originalTypes = getTypesIfNotLoose(node.expression);
if (!originalTypes) {
return;
}
const assertedTypes = getTypesIfNotLoose(node.typeAnnotation);
if (!assertedTypes) {
return;
}
if (sameTypeWithoutNullish(assertedTypes, originalTypes)) {
context.report({
fix(fixer) {
return fixer.replaceText(
node,
`${sourceCode.getText(node.expression)}!`,
);
},
messageId: 'preferNonNullAssertion',
node,
});
}
},
};
},
});