-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
non-nullable-type-assertion-style.ts
132 lines (115 loc) · 3.57 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tools from 'ts-api-utils';
import * as ts from 'typescript';
import * as util from '../util';
export default util.createRule({
name: 'non-nullable-type-assertion-style',
meta: {
docs: {
description: 'Enforce non-null assertions over explicit type casts',
recommended: 'strict',
requiresTypeChecking: true,
},
fixable: 'code',
messages: {
preferNonNullAssertion:
'Use a ! assertion to more succinctly remove null and undefined from the type.',
},
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
const services = util.getParserServices(context);
const sourceCode = context.getSourceCode();
const getTypesIfNotLoose = (node: TSESTree.Node): ts.Type[] | undefined => {
const type = services.getTypeAtLocation(node);
if (tools.isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return undefined;
}
return tools.unionTypeParts(type);
};
const couldBeNullish = (type: ts.Type): boolean => {
if (type.flags & ts.TypeFlags.TypeParameter) {
const constraint = type.getConstraint();
return constraint == null || couldBeNullish(constraint);
} else if (tools.isUnionType(type)) {
for (const part of type.types) {
if (couldBeNullish(part)) {
return true;
}
}
return false;
} else {
return (
(type.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0
);
}
};
const sameTypeWithoutNullish = (
assertedTypes: ts.Type[],
originalTypes: ts.Type[],
): boolean => {
const nonNullishOriginalTypes = originalTypes.filter(
type =>
(type.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) === 0,
);
if (nonNullishOriginalTypes.length === originalTypes.length) {
return false;
}
for (const assertedType of assertedTypes) {
if (
couldBeNullish(assertedType) ||
!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,
});
}
},
};
},
});