/
prefer-regexp-exec.ts
159 lines (144 loc) · 4.49 KB
/
prefer-regexp-exec.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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import {
createRule,
getParserServices,
getStaticValue,
getTypeName,
getWrappingFixer,
} from '../util';
enum ArgumentType {
Other = 0,
String = 1 << 0,
RegExp = 1 << 1,
Both = String | RegExp,
}
export default createRule({
name: 'prefer-regexp-exec',
defaultOptions: [],
meta: {
type: 'suggestion',
fixable: 'code',
docs: {
description:
'Enforce that `RegExp#exec` is used instead of `String#match` if no global flag is provided',
category: 'Best Practices',
recommended: 'error',
requiresTypeChecking: true,
},
messages: {
regExpExecOverStringMatch: 'Use the `RegExp#exec()` method instead.',
},
schema: [],
},
create(context) {
const globalScope = context.getScope();
const parserServices = getParserServices(context);
const typeChecker = parserServices.program.getTypeChecker();
const sourceCode = context.getSourceCode();
/**
* Check if a given node type is a string.
* @param node The node type to check.
*/
function isStringType(type: ts.Type): boolean {
return getTypeName(typeChecker, type) === 'string';
}
/**
* Check if a given node type is a RegExp.
* @param node The node type to check.
*/
function isRegExpType(type: ts.Type): boolean {
return getTypeName(typeChecker, type) === 'RegExp';
}
function collectArgumentTypes(types: ts.Type[]): ArgumentType {
let result = ArgumentType.Other;
for (const type of types) {
if (isRegExpType(type)) {
result |= ArgumentType.RegExp;
} else if (isStringType(type)) {
result |= ArgumentType.String;
}
}
return result;
}
return {
"CallExpression[arguments.length=1] > MemberExpression.callee[property.name='match'][computed=false]"(
memberNode: TSESTree.MemberExpression,
): void {
const objectNode = memberNode.object;
const callNode = memberNode.parent as TSESTree.CallExpression;
const argumentNode = callNode.arguments[0];
const argumentValue = getStaticValue(argumentNode, globalScope);
if (
!isStringType(
typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(objectNode),
),
)
) {
return;
}
// Don't report regular expressions with global flag.
if (
!argumentValue ||
(argumentValue.value instanceof RegExp &&
argumentValue.value.flags.includes('g'))
) {
return;
}
if (
argumentNode.type === AST_NODE_TYPES.Literal &&
typeof argumentNode.value == 'string'
) {
const regExp = RegExp(argumentNode.value);
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode],
wrap: objectCode => `${regExp.toString()}.exec(${objectCode})`,
}),
});
}
const argumentType = typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(argumentNode),
);
const argumentTypes = collectArgumentTypes(
tsutils.unionTypeParts(argumentType),
);
switch (argumentTypes) {
case ArgumentType.RegExp:
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode, argumentNode],
wrap: (objectCode, argumentCode) =>
`${argumentCode}.exec(${objectCode})`,
}),
});
case ArgumentType.String:
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode, argumentNode],
wrap: (objectCode, argumentCode) =>
`RegExp(${argumentCode}).exec(${objectCode})`,
}),
});
}
},
};
},
});