/
restrict-plus-operands.ts
104 lines (93 loc) · 2.98 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
import { TSESTree } from '@typescript-eslint/typescript-estree';
import ts from 'typescript';
import * as util from '../util';
export default util.createRule({
name: 'restrict-plus-operands',
meta: {
type: 'problem',
docs: {
description:
'When adding two variables, operands must both be of type number or of type string.',
tslintRuleName: 'restrict-plus-operands',
category: 'Best Practices',
recommended: false,
},
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.",
},
schema: [],
},
defaultOptions: [],
create(context) {
const service = util.getParserServices(context);
const typeChecker = service.program.getTypeChecker();
type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid';
/**
* Helper function to get base type of node
* @param type type to be evaluated
* @returns string, number or invalid
*/
function getBaseTypeOfLiteralType(type: ts.Type): BaseLiteral {
if (type.isNumberLiteral()) {
return 'number';
}
if (type.isStringLiteral()) {
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';
}
const stringType = typeChecker.typeToString(type);
if (stringType === 'number' || stringType === 'string') {
return stringType;
}
return 'invalid';
}
/**
* Helper function to get base type of node
* @param node the node to be evaluated.
*/
function getNodeType(node: TSESTree.Node): BaseLiteral {
const tsNode = service.esTreeNodeToTSNodeMap.get(node);
const type = typeChecker.getTypeAtLocation(tsNode);
return getBaseTypeOfLiteralType(type);
}
return {
"BinaryExpression[operator='+']"(node: TSESTree.BinaryExpression) {
const leftType = getNodeType(node.left);
const rightType = getNodeType(node.right);
if (
leftType === 'invalid' ||
rightType === 'invalid' ||
leftType !== rightType
) {
if (leftType === 'string' || rightType === 'string') {
context.report({
node,
messageId: 'notStrings',
});
} else if (leftType === 'bigint' || rightType === 'bigint') {
context.report({
node,
messageId: 'notBigInts',
});
} else {
context.report({
node,
messageId: 'notNumbers',
});
}
}
},
};
},
});