forked from typescript-eslint/typescript-eslint
/
base-to-string.ts
130 lines (113 loc) · 3.37 KB
/
base-to-string.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
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import * as util from '../util';
enum Usefulness {
Always,
Never = 'will',
Sometimes = 'may',
}
export default util.createRule({
name: 'base-to-string',
meta: {
docs: {
description:
'Requires that `.toString()` is only called on objects which provide useful information when stringified',
category: 'Best Practices',
recommended: false,
requiresTypeChecking: true,
},
messages: {
baseToString:
"'{{name}} {{certainty}} evaluate to '[Object object]' when stringified.",
},
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const typeChecker = parserServices.program.getTypeChecker();
function checkExpression(node: TSESTree.Expression, type?: ts.Type): void {
if (node.type === AST_NODE_TYPES.Literal) {
return;
}
const certainty = collectToStringCertainty(
type ??
typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node),
),
);
if (certainty === Usefulness.Always) {
return;
}
context.report({
data: {
certainty,
name: context.getSourceCode().getText(node),
},
messageId: 'baseToString',
node,
});
}
function collectToStringCertainty(type: ts.Type): Usefulness {
const toString = typeChecker.getPropertyOfType(type, 'toString');
if (toString === undefined || toString.declarations.length === 0) {
return Usefulness.Always;
}
if (
toString.declarations.every(
({ parent }) =>
!ts.isInterfaceDeclaration(parent) || parent.name.text !== 'Object',
)
) {
return Usefulness.Always;
}
if (!type.isUnion()) {
return Usefulness.Never;
}
for (const subType of type.types) {
if (collectToStringCertainty(subType) !== Usefulness.Never) {
return Usefulness.Sometimes;
}
}
return Usefulness.Never;
}
return {
BinaryExpression(node: TSESTree.BinaryExpression): void {
const leftType = typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node.left),
);
const rightType = typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node.right),
);
if (util.getTypeName(typeChecker, leftType) === 'string') {
checkExpression(node.right, rightType);
} else if (util.getTypeName(typeChecker, rightType) === 'string') {
checkExpression(node.left, leftType);
}
},
CallExpression(node: TSESTree.CallExpression): void {
const { callee } = node;
if (callee.type !== AST_NODE_TYPES.MemberExpression) {
return;
}
const { property } = callee;
if (
property.type !== AST_NODE_TYPES.Identifier ||
property.name !== 'toString'
) {
return;
}
checkExpression(callee.object);
},
TemplateLiteral(node: TSESTree.TemplateLiteral): void {
for (const expression of node.expressions) {
checkExpression(expression);
}
},
};
},
});