-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprefer-regex-test.ts
More file actions
193 lines (163 loc) · 4.47 KB
/
prefer-regex-test.ts
File metadata and controls
193 lines (163 loc) · 4.47 KB
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
import type {TSESLint, TSESTree} from '@typescript-eslint/utils';
import {tryGetTypedParserServices} from '../utils/typescript.js';
import {isInBooleanContext} from '../utils/ast.js';
type MessageIds = 'preferTest';
function isRegExpLiteral(node: TSESTree.Node): node is TSESTree.Literal {
return (
node.type === 'Literal' &&
'regex' in node &&
node.regex !== undefined &&
node.regex !== null
);
}
/**
* Checks if a node is a `new RegExp(...)`
*/
function isRegExpConstructor(node: TSESTree.Node): boolean {
if (node.type !== 'NewExpression') {
return false;
}
const {callee} = node;
// new RegExp()
if (callee.type === 'Identifier' && callee.name === 'RegExp') {
return true;
}
// new window.RegExp() or new globalThis.RegExp()
if (
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
(callee.object.name === 'window' || callee.object.name === 'globalThis') &&
callee.property.type === 'Identifier' &&
callee.property.name === 'RegExp' &&
!callee.computed
) {
return true;
}
return false;
}
/**
* Checks if a node is a RegExp (literal or constructor)
*/
function isRegExp(node: TSESTree.Node | null | undefined): boolean {
return (
node !== null &&
node !== undefined &&
(isRegExpLiteral(node) || isRegExpConstructor(node))
);
}
/**
* Checks if a node resolves to a RegExp using TypeScript types (when available)
*/
function isRegExpByType(
node: TSESTree.Node,
context: TSESLint.RuleContext<MessageIds, []>
): boolean {
const services = tryGetTypedParserServices(context);
if (!services) {
return false;
}
const type = services.getTypeAtLocation(node);
if (!type) {
return false;
}
const checker = services.program.getTypeChecker();
const typeString = checker.typeToString(type);
return typeString === 'RegExp';
}
/**
* Checks if a node resolves to a RegExp (literal, constructor, or by type)
*/
function resolvesToRegExp(
node: TSESTree.Node,
context: TSESLint.RuleContext<MessageIds, []>
): boolean {
if (isRegExpByType(node, context)) {
return true;
}
if (isRegExp(node)) {
return true;
}
if (node.type !== 'Identifier') {
return false;
}
const scope = context.sourceCode.getScope(node);
const variable = scope.references.find(
(ref) => ref.identifier === node
)?.resolved;
if (!variable) {
return false;
}
for (const def of variable.defs) {
if (def.type === 'Variable' && def.node.type === 'VariableDeclarator') {
const init = def.node.init;
if (isRegExp(init)) {
return true;
}
}
}
return false;
}
export const preferRegexTest: TSESLint.RuleModule<MessageIds, []> = {
meta: {
type: 'suggestion',
docs: {
description:
'prefer `RegExp.test()` over `String.match()` and `RegExp.exec()` when only checking for match existence'
},
fixable: 'code',
messages: {
preferTest:
'Prefer `{{regex}}.test({{string}})` over `{{original}}` for boolean checks'
},
schema: []
},
defaultOptions: [],
create(context) {
return {
CallExpression(node: TSESTree.CallExpression) {
if (!isInBooleanContext(node)) {
return;
}
const {callee} = node;
if (callee.type !== 'MemberExpression') {
return;
}
const property = callee.property;
if (property.type !== 'Identifier' || node.arguments.length !== 1) {
return;
}
let regexNode: TSESTree.Node;
let stringNode: TSESTree.Node;
if (property.name === 'match') {
// str.match(regex)
stringNode = callee.object;
regexNode = node.arguments[0]!;
} else if (property.name === 'exec') {
// regex.exec(str)
regexNode = callee.object;
stringNode = node.arguments[0]!;
} else {
return;
}
if (!resolvesToRegExp(regexNode, context)) {
return;
}
const sourceCode = context.sourceCode;
const regexText = sourceCode.getText(regexNode);
const stringText = sourceCode.getText(stringNode);
context.report({
node,
messageId: 'preferTest',
data: {
regex: regexText,
string: stringText,
original: sourceCode.getText(node)
},
fix(fixer) {
return fixer.replaceText(node, `${regexText}.test(${stringText})`);
}
});
}
};
}
};