-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathbetter-regex.js
146 lines (126 loc) · 3.11 KB
/
better-regex.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
145
146
import cleanRegexp from 'clean-regexp';
import regexpTree from 'regexp-tree';
import escapeString from './utils/escape-string.js';
import {isStringLiteral, isNewExpression, isRegexLiteral} from './ast/index.js';
const MESSAGE_ID = 'better-regex';
const MESSAGE_ID_PARSE_ERROR = 'better-regex/parse-error';
const messages = {
[MESSAGE_ID]: '{{original}} can be optimized to {{optimized}}.',
[MESSAGE_ID_PARSE_ERROR]: 'Problem parsing {{original}}: {{error}}',
};
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const {sortCharacterClasses} = context.options[0] || {};
const ignoreList = [];
if (sortCharacterClasses === false) {
ignoreList.push('charClassClassrangesMerge');
}
return {
Literal(node) {
if (!isRegexLiteral(node)) {
return;
}
const {raw: original, regex} = node;
// Regular Expressions with `u` and `v` flag are not well handled by `regexp-tree`
// https://github.com/DmitrySoshnikov/regexp-tree/issues/162
if (regex.flags.includes('u') || regex.flags.includes('v')) {
return;
}
let optimized = original;
try {
optimized = regexpTree.optimize(original, undefined, {blacklist: ignoreList}).toString();
} catch (error) {
return {
node,
messageId: MESSAGE_ID_PARSE_ERROR,
data: {
original,
error: error.message,
},
};
}
if (original === optimized) {
return;
}
const problem = {
node,
messageId: MESSAGE_ID,
data: {
original,
optimized,
},
};
if (
node.parent.type === 'MemberExpression'
&& node.parent.object === node
&& !node.parent.optional
&& !node.parent.computed
&& node.parent.property.type === 'Identifier'
&& (
node.parent.property.name === 'toString'
|| node.parent.property.name === 'source'
)
) {
return problem;
}
return Object.assign(problem, {
fix: fixer => fixer.replaceText(node, optimized),
});
},
NewExpression(node) {
if (!isNewExpression(node, {name: 'RegExp', minimumArguments: 1})) {
return;
}
const [patternNode, flagsNode] = node.arguments;
if (!isStringLiteral(patternNode)) {
return;
}
const oldPattern = patternNode.value;
const flags = isStringLiteral(flagsNode)
? flagsNode.value
: '';
const newPattern = cleanRegexp(oldPattern, flags);
if (oldPattern !== newPattern) {
return {
node,
messageId: MESSAGE_ID,
data: {
original: oldPattern,
optimized: newPattern,
},
fix: fixer => fixer.replaceText(
patternNode,
escapeString(newPattern, patternNode.raw.charAt(0)),
),
};
}
},
};
};
const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
sortCharacterClasses: {
type: 'boolean',
},
},
},
];
/** @type {import('eslint').Rule.RuleModule} */
const config = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Improve regexes by making them shorter, consistent, and safer.',
recommended: false,
},
fixable: 'code',
schema,
defaultOptions: [{sortCharacterClasses: true}],
messages,
},
};
export default config;