/
restrict-plus-operands.ts
199 lines (175 loc) · 5.06 KB
/
restrict-plus-operands.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { TSESTree } from '@typescript-eslint/utils';
import * as ts from 'typescript';
import * as util from '../util';
type Options = [
{
checkCompoundAssignments?: boolean;
allowAny?: boolean;
},
];
type MessageIds =
| 'notNumbers'
| 'notStrings'
| 'notBigInts'
| 'notValidAnys'
| 'notValidTypes';
export default util.createRule<Options, MessageIds>({
name: 'restrict-plus-operands',
meta: {
type: 'problem',
docs: {
description:
'When adding two variables, operands must both be of type number or of type string',
recommended: 'error',
requiresTypeChecking: true,
},
messages: {
notNumbers:
"Operands of '+' operation must either be both strings or both numbers.",
notStrings:
"Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.",
notBigInts: "Operands of '+' operation must be both bigints.",
notValidAnys:
"Operands of '+' operation with any is possible only with string, number, bigint or any",
notValidTypes:
"Operands of '+' operation must either be one of string, number, bigint or any (if allowed by option)",
},
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
checkCompoundAssignments: {
type: 'boolean',
},
allowAny: {
type: 'boolean',
},
},
},
],
},
defaultOptions: [
{
checkCompoundAssignments: false,
allowAny: false,
},
],
create(context, [{ checkCompoundAssignments, allowAny }]) {
const service = util.getParserServices(context);
const typeChecker = service.program.getTypeChecker();
type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid' | 'any';
/**
* Helper function to get base type of node
*/
function getBaseTypeOfLiteralType(type: ts.Type): BaseLiteral {
if (type.isNumberLiteral()) {
return 'number';
}
if (
type.isStringLiteral() ||
util.isTypeFlagSet(type, ts.TypeFlags.TemplateLiteral)
) {
return 'string';
}
// is BigIntLiteral
if (type.flags & ts.TypeFlags.BigIntLiteral) {
return 'bigint';
}
if (type.isUnion()) {
const types = type.types.map(getBaseTypeOfLiteralType);
return types.every(value => value === types[0]) ? types[0] : 'invalid';
}
if (type.isIntersection()) {
const types = type.types.map(getBaseTypeOfLiteralType);
if (types.some(value => value === 'string')) {
return 'string';
}
if (types.some(value => value === 'number')) {
return 'number';
}
if (types.some(value => value === 'bigint')) {
return 'bigint';
}
return 'invalid';
}
const stringType = typeChecker.typeToString(type);
if (
stringType === 'number' ||
stringType === 'string' ||
stringType === 'bigint' ||
stringType === 'any'
) {
return stringType;
}
return 'invalid';
}
/**
* Helper function to get base type of node
* @param node the node to be evaluated.
*/
function getNodeType(
node: TSESTree.Expression | TSESTree.PrivateIdentifier,
): BaseLiteral {
const tsNode = service.esTreeNodeToTSNodeMap.get(node);
const type = util.getConstrainedTypeAtLocation(typeChecker, tsNode);
return getBaseTypeOfLiteralType(type);
}
function checkPlusOperands(
node: TSESTree.BinaryExpression | TSESTree.AssignmentExpression,
): void {
const leftType = getNodeType(node.left);
const rightType = getNodeType(node.right);
if (leftType === rightType) {
if (leftType === 'invalid') {
context.report({
node,
messageId: 'notValidTypes',
});
}
if (!allowAny && leftType === 'any') {
context.report({
node,
messageId: 'notValidAnys',
});
}
return;
}
if (leftType === 'any' || rightType === 'any') {
if (!allowAny || leftType === 'invalid' || rightType === 'invalid') {
context.report({
node,
messageId: 'notValidAnys',
});
}
return;
}
if (leftType === 'string' || rightType === 'string') {
return context.report({
node,
messageId: 'notStrings',
});
}
if (leftType === 'bigint' || rightType === 'bigint') {
return context.report({
node,
messageId: 'notBigInts',
});
}
if (leftType === 'number' || rightType === 'number') {
return context.report({
node,
messageId: 'notNumbers',
});
}
}
return {
"BinaryExpression[operator='+']": checkPlusOperands,
...(checkCompoundAssignments && {
"AssignmentExpression[operator='+=']"(node): void {
checkPlusOperands(node);
},
}),
};
},
});