forked from typescript-eslint/typescript-eslint
/
restrict-template-expressions.ts
134 lines (118 loc) · 3.29 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
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import * as util from '../util';
type Options = [
{
allowNullable?: boolean;
allowNumber?: boolean;
allowBoolean?: 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',
category: 'Best Practices',
recommended: false,
requiresTypeChecking: true,
},
messages: {
invalidType: 'Invalid type of template literal expression.',
},
schema: [
{
type: 'object',
properties: {
allowBoolean: { type: 'boolean' },
allowNullable: { type: 'boolean' },
allowNumber: { type: 'boolean' },
},
},
],
},
defaultOptions: [{}],
create(context, [options]) {
const service = util.getParserServices(context);
const typeChecker = service.program.getTypeChecker();
function isUnderlyingTypePrimitive(type: ts.Type): boolean {
if (util.isTypeFlagSet(type, ts.TypeFlags.StringLike)) {
return true;
}
if (
util.isTypeFlagSet(
type,
ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike,
) &&
options.allowNumber
) {
return true;
}
if (
util.isTypeFlagSet(type, ts.TypeFlags.BooleanLike) &&
options.allowBoolean
) {
return true;
}
if (
util.isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined) &&
options.allowNullable
) {
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) {
if (
!isUnderlyingExpressionTypeConfirmingTo(
expression,
isUnderlyingTypePrimitive,
)
) {
context.report({
node: expression,
messageId: 'invalidType',
});
}
}
},
};
function isUnderlyingExpressionTypeConfirmingTo(
expression: TSESTree.Expression,
predicate: (underlyingType: ts.Type) => boolean,
): boolean {
const expressionType = getExpressionNodeType(expression);
return rec(
// "Extracts" generic constraint, indexed access and conditional types:
typeChecker.getBaseConstraintOfType(expressionType) ?? expressionType,
);
function rec(type: ts.Type): boolean {
if (type.isUnion()) {
return type.types.every(rec);
}
if (type.isIntersection()) {
return type.types.some(rec);
}
return predicate(type);
}
}
/**
* Helper function to extract the TS type of an TSESTree expression.
*/
function getExpressionNodeType(node: TSESTree.Expression): ts.Type {
const tsNode = service.esTreeNodeToTSNodeMap.get(node);
return typeChecker.getTypeAtLocation(tsNode);
}
},
});