-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
restrict-template-expressions.ts
177 lines (158 loc) · 4.42 KB
/
restrict-template-expressions.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
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as ts from 'typescript';
import * as util from '../util';
type Options = [
{
allowAny?: boolean;
allowBoolean?: boolean;
allowNullish?: boolean;
allowNumber?: boolean;
allowRegExp?: boolean;
allowNever?: boolean;
},
];
type MessageId = 'invalidType';
export default util.createRule<Options, MessageId>({
name: 'restrict-template-expressions',
meta: {
type: 'problem',
docs: {
description:
'Enforce template literal expressions to be of `string` type',
recommended: 'recommended',
requiresTypeChecking: true,
},
messages: {
invalidType: 'Invalid type "{{type}}" of template literal expression.',
},
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
allowAny: {
description:
'Whether to allow `any` typed values in template expressions.',
type: 'boolean',
},
allowBoolean: {
description:
'Whether to allow `boolean` typed values in template expressions.',
type: 'boolean',
},
allowNullish: {
description:
'Whether to allow `nullish` typed values in template expressions.',
type: 'boolean',
},
allowNumber: {
description:
'Whether to allow `number` typed values in template expressions.',
type: 'boolean',
},
allowRegExp: {
description:
'Whether to allow `regexp` typed values in template expressions.',
type: 'boolean',
},
allowNever: {
description:
'Whether to allow `never` typed values in template expressions.',
type: 'boolean',
},
},
},
],
},
defaultOptions: [
{
allowAny: true,
allowBoolean: true,
allowNullish: true,
allowNumber: true,
allowRegExp: true,
},
],
create(context, [options]) {
const services = util.getParserServices(context);
const checker = services.program.getTypeChecker();
function isUnderlyingTypePrimitive(type: ts.Type): boolean {
if (util.isTypeFlagSet(type, ts.TypeFlags.StringLike)) {
return true;
}
if (
options.allowNumber &&
util.isTypeFlagSet(
type,
ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike,
)
) {
return true;
}
if (
options.allowBoolean &&
util.isTypeFlagSet(type, ts.TypeFlags.BooleanLike)
) {
return true;
}
if (options.allowAny && util.isTypeAnyType(type)) {
return true;
}
if (options.allowRegExp && util.getTypeName(checker, type) === 'RegExp') {
return true;
}
if (
options.allowNullish &&
util.isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined)
) {
return true;
}
if (options.allowNever && util.isTypeNeverType(type)) {
return true;
}
return false;
}
return {
TemplateLiteral(node: TSESTree.TemplateLiteral): void {
// don't check tagged template literals
if (node.parent.type === AST_NODE_TYPES.TaggedTemplateExpression) {
return;
}
for (const expression of node.expressions) {
const expressionType = util.getConstrainedTypeAtLocation(
services,
expression,
);
if (
!isInnerUnionOrIntersectionConformingTo(
expressionType,
isUnderlyingTypePrimitive,
)
) {
context.report({
node: expression,
messageId: 'invalidType',
data: { type: checker.typeToString(expressionType) },
});
}
}
},
};
function isInnerUnionOrIntersectionConformingTo(
type: ts.Type,
predicate: (underlyingType: ts.Type) => boolean,
): boolean {
return rec(type);
function rec(innerType: ts.Type): boolean {
if (innerType.isUnion()) {
return innerType.types.every(rec);
}
if (innerType.isIntersection()) {
return innerType.types.some(rec);
}
return predicate(innerType);
}
}
},
});