-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
no-base-to-string.ts
190 lines (166 loc) · 5.16 KB
/
no-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
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
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';
enum Usefulness {
Always,
Never = 'will',
Sometimes = 'may',
}
type Options = [
{
ignoredTypeNames?: string[];
},
];
type MessageIds = 'baseToString';
export default util.createRule<Options, MessageIds>({
name: 'no-base-to-string',
meta: {
docs: {
description:
'Require `.toString()` to only be called on objects which provide useful information when stringified',
recommended: 'strict',
requiresTypeChecking: true,
},
messages: {
baseToString:
"'{{name}}' {{certainty}} evaluate to '[object Object]' when stringified.",
},
schema: [
{
type: 'object',
properties: {
ignoredTypeNames: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
],
type: 'suggestion',
},
defaultOptions: [
{
ignoredTypeNames: ['Error', 'RegExp', 'URL', 'URLSearchParams'],
},
],
create(context, [option]) {
const parserServices = util.getParserServices(context);
const typeChecker = parserServices.program.getTypeChecker();
const ignoredTypeNames = option.ignoredTypeNames ?? [];
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');
const declarations = toString?.getDeclarations();
if (!toString || !declarations || declarations.length === 0) {
return Usefulness.Always;
}
// Patch for old version TypeScript, the Boolean type definition missing toString()
if (
type.flags & ts.TypeFlags.Boolean ||
type.flags & ts.TypeFlags.BooleanLiteral
) {
return Usefulness.Always;
}
if (ignoredTypeNames.includes(util.getTypeName(typeChecker, type))) {
return Usefulness.Always;
}
if (
declarations.every(
({ parent }) =>
!ts.isInterfaceDeclaration(parent) || parent.name.text !== 'Object',
)
) {
return Usefulness.Always;
}
if (type.isIntersection()) {
for (const subType of type.types) {
const subtypeUsefulness = collectToStringCertainty(subType);
if (subtypeUsefulness === Usefulness.Always) {
return Usefulness.Always;
}
}
return Usefulness.Never;
}
if (!type.isUnion()) {
return Usefulness.Never;
}
let allSubtypesUseful = true;
let someSubtypeUseful = false;
for (const subType of type.types) {
const subtypeUsefulness = collectToStringCertainty(subType);
if (subtypeUsefulness !== Usefulness.Always && allSubtypesUseful) {
allSubtypesUseful = false;
}
if (subtypeUsefulness !== Usefulness.Never && !someSubtypeUseful) {
someSubtypeUseful = true;
}
}
if (allSubtypesUseful && someSubtypeUseful) {
return Usefulness.Always;
}
if (someSubtypeUseful) {
return Usefulness.Sometimes;
}
return Usefulness.Never;
}
return {
'AssignmentExpression[operator = "+="], BinaryExpression[operator = "+"]'(
node: TSESTree.AssignmentExpression | 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' &&
node.left.type !== AST_NODE_TYPES.PrivateIdentifier
) {
checkExpression(node.left, leftType);
}
},
'CallExpression > MemberExpression.callee > Identifier[name = "toString"].property'(
node: TSESTree.Expression,
): void {
const memberExpr = node.parent as TSESTree.MemberExpression;
checkExpression(memberExpr.object);
},
TemplateLiteral(node: TSESTree.TemplateLiteral): void {
if (node.parent.type === AST_NODE_TYPES.TaggedTemplateExpression) {
return;
}
for (const expression of node.expressions) {
checkExpression(expression);
}
},
};
},
});