forked from sindresorhus/eslint-plugin-unicorn
/
prefer-regexp-test.js
144 lines (125 loc) · 3.62 KB
/
prefer-regexp-test.js
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
'use strict';
const {isParenthesized, getStaticValue} = require('eslint-utils');
const {checkVueTemplate} = require('./utils/rule.js');
const {methodCallSelector} = require('./selectors/index.js');
const {isRegexLiteral, isNewExpression} = require('./ast/index.js');
const {isBooleanNode} = require('./utils/boolean.js');
const shouldAddParenthesesToMemberExpressionObject = require('./utils/should-add-parentheses-to-member-expression-object.js');
const REGEXP_EXEC = 'regexp-exec';
const STRING_MATCH = 'string-match';
const SUGGESTION = 'suggestion';
const messages = {
[REGEXP_EXEC]: 'Prefer `.test(…)` over `.exec(…)`.',
[STRING_MATCH]: 'Prefer `RegExp#test(…)` over `String#match(…)`.',
[SUGGESTION]: 'Switch to `RegExp#test(…)`.',
};
const cases = [
{
type: REGEXP_EXEC,
selector: methodCallSelector({
method: 'exec',
argumentsLength: 1,
}),
getNodes: node => ({
stringNode: node.arguments[0],
methodNode: node.callee.property,
regexpNode: node.callee.object,
}),
fix: (fixer, {methodNode}) => fixer.replaceText(methodNode, 'test'),
},
{
type: STRING_MATCH,
selector: methodCallSelector({
method: 'match',
argumentsLength: 1,
}),
getNodes: node => ({
stringNode: node.callee.object,
methodNode: node.callee.property,
regexpNode: node.arguments[0],
}),
* fix(fixer, {stringNode, methodNode, regexpNode}, sourceCode) {
yield fixer.replaceText(methodNode, 'test');
let stringText = sourceCode.getText(stringNode);
if (
!isParenthesized(regexpNode, sourceCode)
// Only `SequenceExpression` need add parentheses
&& stringNode.type === 'SequenceExpression'
) {
stringText = `(${stringText})`;
}
yield fixer.replaceText(regexpNode, stringText);
let regexpText = sourceCode.getText(regexpNode);
if (
!isParenthesized(stringNode, sourceCode)
&& shouldAddParenthesesToMemberExpressionObject(regexpNode, sourceCode)
) {
regexpText = `(${regexpText})`;
}
// The nodes that pass `isBooleanNode` cannot have an ASI problem.
yield fixer.replaceText(stringNode, regexpText);
},
},
];
const isRegExpNode = node => isRegexLiteral(node) || isNewExpression(node, {name: 'RegExp'});
const isRegExpWithoutGlobalFlag = (node, scope) => {
const staticResult = getStaticValue(node, scope);
// Don't know if there is `g` flag
if (!staticResult) {
return false;
}
const {value} = staticResult;
return (
Object.prototype.toString.call(value) === '[object RegExp]'
&& !value.global
);
};
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => Object.fromEntries(
cases.map(checkCase => [
checkCase.selector,
node => {
if (!isBooleanNode(node)) {
return;
}
const {type, getNodes, fix} = checkCase;
const nodes = getNodes(node);
const {methodNode, regexpNode} = nodes;
if (regexpNode.type === 'Literal' && !regexpNode.regex) {
return;
}
const problem = {
node: type === REGEXP_EXEC ? methodNode : node,
messageId: type,
};
const fixFunction = fixer => fix(fixer, nodes, context.getSourceCode());
if (
isRegExpNode(regexpNode)
|| isRegExpWithoutGlobalFlag(regexpNode, context.getScope())
) {
problem.fix = fixFunction;
} else {
problem.suggest = [
{
messageId: SUGGESTION,
fix: fixFunction,
},
];
}
return problem;
},
]),
);
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create: checkVueTemplate(create),
meta: {
type: 'suggestion',
docs: {
description: 'Prefer `RegExp#test()` over `String#match()` and `RegExp#exec()`.',
},
fixable: 'code',
hasSuggestions: true,
messages,
},
};