-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathprefer-includes-over-regex-test.ts
More file actions
137 lines (122 loc) · 4.01 KB
/
prefer-includes-over-regex-test.ts
File metadata and controls
137 lines (122 loc) · 4.01 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
import type {Rule} from 'eslint';
import type {CallExpression, Expression, RegExpLiteral} from 'estree';
function isRegexLiteral(node: Expression): node is RegExpLiteral {
return node.type === 'Literal' && 'regex' in node && node.regex !== undefined;
}
const SPECIAL_CHARS_RE = /[\\.*+?()[\]{}|^$]/;
type Kind = 'includes' | 'startsWith' | 'endsWith' | 'equals';
function classifyRegex(pattern: string): {kind: Kind; literal: string} | null {
let body = pattern;
let hasStart = false;
let hasEnd = false;
if (body.startsWith('^')) {
hasStart = true;
body = body.slice(1);
}
// A trailing `$` is the end anchor unless it's preceded by an unescaped `\`
if (body.endsWith('$') && !body.endsWith('\\$')) {
hasEnd = true;
body = body.slice(0, -1);
}
if (body.length === 0) return null;
if (SPECIAL_CHARS_RE.test(body)) return null;
let kind: Kind;
if (hasStart && hasEnd) kind = 'equals';
else if (hasStart) kind = 'startsWith';
else if (hasEnd) kind = 'endsWith';
else kind = 'includes';
return {kind, literal: body};
}
function needsParens(node: Expression): boolean {
switch (node.type) {
case 'Identifier':
case 'Literal':
case 'MemberExpression':
case 'CallExpression':
case 'NewExpression':
case 'ThisExpression':
case 'ArrayExpression':
case 'ObjectExpression':
case 'TemplateLiteral':
case 'TaggedTemplateExpression':
return false;
default:
return true;
}
}
export const preferIncludesOverRegexTest: Rule.RuleModule = {
meta: {
type: 'suggestion',
docs: {
description:
'Prefer String.prototype.{includes,startsWith,endsWith} over equivalent regex.test() calls',
recommended: false
},
fixable: 'code',
schema: [],
messages: {
preferIncludes:
"Prefer `s.includes('{{literal}}')` over `/{{pattern}}/.test(s)`.",
preferStartsWith:
"Prefer `s.startsWith('{{literal}}')` over `/^{{literal}}/.test(s)`.",
preferEndsWith:
"Prefer `s.endsWith('{{literal}}')` over `/{{literal}}$/.test(s)`.",
preferEquals:
"Prefer `s === '{{literal}}'` over `/^{{literal}}$/.test(s)`."
}
},
create(context) {
const sourceCode = context.sourceCode;
return {
CallExpression(node: CallExpression) {
if (node.callee.type !== 'MemberExpression') return;
if (node.callee.computed) return;
if (
node.callee.property.type !== 'Identifier' ||
node.callee.property.name !== 'test'
)
return;
if (node.arguments.length !== 1) return;
const regex = node.callee.object;
if (regex.type !== 'Literal' || !isRegexLiteral(regex)) return;
if (regex.regex.flags !== '') return;
const cls = classifyRegex(regex.regex.pattern);
if (!cls) return;
const arg = node.arguments[0]!;
if (arg.type === 'SpreadElement') return;
const argText = needsParens(arg as Expression)
? `(${sourceCode.getText(arg)})`
: sourceCode.getText(arg);
const literalText = JSON.stringify(cls.literal);
let replacement: string;
let messageId: string;
switch (cls.kind) {
case 'includes':
replacement = `${argText}.includes(${literalText})`;
messageId = 'preferIncludes';
break;
case 'startsWith':
replacement = `${argText}.startsWith(${literalText})`;
messageId = 'preferStartsWith';
break;
case 'endsWith':
replacement = `${argText}.endsWith(${literalText})`;
messageId = 'preferEndsWith';
break;
case 'equals':
replacement = `${argText} === ${literalText}`;
messageId = 'preferEquals';
break;
}
context.report({
node,
messageId,
data: {literal: cls.literal, pattern: regex.regex.pattern},
fix(fixer) {
return fixer.replaceText(node, replacement);
}
});
}
};
}
};