/
prefer-regexp-exec.ts
135 lines (123 loc) · 3.92 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
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
createRule,
getParserServices,
getStaticValue,
getTypeName,
getWrappingFixer,
} from '../util';
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 is a string.
* @param node The node to check.
*/
function isStringType(node: TSESTree.Expression): boolean {
const objectType = typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node),
);
return getTypeName(typeChecker, objectType) === 'string';
}
/**
* Check if a given node is a RegExp.
* @param node The node to check.
*/
function isRegExpType(node: TSESTree.Expression): boolean {
const objectType = typeChecker.getTypeAtLocation(
parserServices.esTreeNodeToTSNodeMap.get(node),
);
return getTypeName(typeChecker, objectType) === 'RegExp';
}
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(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})`,
}),
});
}
if (isRegExpType(argumentNode)) {
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode, argumentNode],
wrap: (objectCode, argumentCode) =>
`${argumentCode}.exec(${objectCode})`,
}),
});
}
if (isStringType(argumentNode)) {
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
fix: getWrappingFixer({
sourceCode,
node: callNode,
innerNode: [objectNode, argumentNode],
wrap: (objectCode, argumentCode) =>
`RegExp(${argumentCode}).exec(${objectCode})`,
}),
});
}
return context.report({
node: memberNode.property,
messageId: 'regExpExecOverStringMatch',
});
},
};
},
});