Skip to content

Commit ab22777

Browse files
sonimadhuridanielleadams
authored andcommitted
tools: refactor deprecated format in no-unescaped-regexp-dot
PR-URL: #44763 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 4efaf42 commit ab22777

File tree

1 file changed

+98
-95
lines changed

1 file changed

+98
-95
lines changed

tools/eslint-rules/no-unescaped-regexp-dot.js

+98-95
Original file line numberDiff line numberDiff line change
@@ -8,123 +8,126 @@
88
// Rule Definition
99
//------------------------------------------------------------------------------
1010

11-
module.exports = function(context) {
12-
const sourceCode = context.getSourceCode();
13-
const regexpStack = [];
14-
let regexpBuffer = [];
15-
let inRegExp = false;
11+
module.exports = {
12+
create(context) {
13+
const sourceCode = context.getSourceCode();
14+
const regexpStack = [];
15+
let regexpBuffer = [];
16+
let inRegExp = false;
1617

17-
function report(node, startOffset) {
18-
const indexOfDot = sourceCode.getIndexFromLoc(node.loc.start) + startOffset;
19-
context.report({
20-
node,
21-
loc: sourceCode.getLocFromIndex(indexOfDot),
22-
message: 'Unescaped dot character in regular expression'
23-
});
24-
}
18+
function report(node, startOffset) {
19+
const indexOfDot = sourceCode.getIndexFromLoc(node.loc.start) + startOffset;
20+
context.report({
21+
node,
22+
loc: sourceCode.getLocFromIndex(indexOfDot),
23+
message: 'Unescaped dot character in regular expression'
24+
});
25+
}
26+
const allowedModifiers = ['+', '*', '?', '{'];
2527

26-
const allowedModifiers = ['+', '*', '?', '{'];
27-
function checkRegExp(nodes) {
28-
let escaping = false;
29-
let inCharClass = false;
30-
for (let n = 0; n < nodes.length; ++n) {
31-
const pair = nodes[n];
32-
const node = pair[0];
33-
const str = pair[1];
34-
for (let i = 0; i < str.length; ++i) {
35-
switch (str[i]) {
36-
case '[':
37-
if (!escaping)
38-
inCharClass = true;
39-
else
40-
escaping = false;
41-
break;
42-
case ']':
43-
if (!escaping) {
44-
if (inCharClass)
45-
inCharClass = false;
46-
} else {
47-
escaping = false;
48-
}
49-
break;
50-
case '\\':
51-
escaping = !escaping;
52-
break;
53-
case '.':
54-
if (!escaping) {
55-
if (!inCharClass &&
28+
function checkRegExp(nodes) {
29+
let escaping = false;
30+
let inCharClass = false;
31+
for (let n = 0; n < nodes.length; ++n) {
32+
const pair = nodes[n];
33+
const node = pair[0];
34+
const str = pair[1];
35+
for (let i = 0; i < str.length; ++i) {
36+
switch (str[i]) {
37+
case '[':
38+
if (!escaping)
39+
inCharClass = true;
40+
else
41+
escaping = false;
42+
break;
43+
case ']':
44+
if (!escaping) {
45+
if (inCharClass)
46+
inCharClass = false;
47+
} else {
48+
escaping = false;
49+
}
50+
break;
51+
case '\\':
52+
escaping = !escaping;
53+
break;
54+
case '.':
55+
if (!escaping) {
56+
if (!inCharClass &&
5657
((i + 1) === str.length ||
5758
allowedModifiers.indexOf(str[i + 1]) === -1)) {
58-
report(node, i);
59+
report(node, i);
60+
}
61+
} else {
62+
escaping = false;
5963
}
60-
} else {
61-
escaping = false;
62-
}
63-
break;
64-
default:
65-
if (escaping)
66-
escaping = false;
64+
break;
65+
default:
66+
if (escaping)
67+
escaping = false;
68+
}
6769
}
6870
}
6971
}
70-
}
7172

72-
function checkRegExpStart(node) {
73-
if (node.callee && node.callee.name === 'RegExp') {
74-
if (inRegExp) {
75-
regexpStack.push(regexpBuffer);
76-
regexpBuffer = [];
73+
function checkRegExpStart(node) {
74+
if (node.callee && node.callee.name === 'RegExp') {
75+
if (inRegExp) {
76+
regexpStack.push(regexpBuffer);
77+
regexpBuffer = [];
78+
}
79+
inRegExp = true;
7780
}
78-
inRegExp = true;
7981
}
80-
}
8182

82-
function checkRegExpEnd(node) {
83-
if (node.callee && node.callee.name === 'RegExp') {
84-
checkRegExp(regexpBuffer);
85-
if (regexpStack.length) {
86-
regexpBuffer = regexpStack.pop();
87-
} else {
88-
inRegExp = false;
89-
regexpBuffer = [];
83+
function checkRegExpEnd(node) {
84+
if (node.callee && node.callee.name === 'RegExp') {
85+
checkRegExp(regexpBuffer);
86+
if (regexpStack.length) {
87+
regexpBuffer = regexpStack.pop();
88+
} else {
89+
inRegExp = false;
90+
regexpBuffer = [];
91+
}
9092
}
9193
}
92-
}
9394

94-
function checkLiteral(node) {
95-
const isTemplate = (node.type === 'TemplateLiteral' && node.quasis &&
95+
function checkLiteral(node) {
96+
const isTemplate = (node.type === 'TemplateLiteral' && node.quasis &&
9697
node.quasis.length);
97-
if (inRegExp &&
98+
if (inRegExp &&
9899
(isTemplate || (typeof node.value === 'string' && node.value.length))) {
99-
let p = node.parent;
100-
while (p && p.type === 'BinaryExpression') {
101-
p = p.parent;
102-
}
103-
if (p && (p.type === 'NewExpression' || p.type === 'CallExpression') &&
100+
let p = node.parent;
101+
while (p && p.type === 'BinaryExpression') {
102+
p = p.parent;
103+
}
104+
if (p && (p.type === 'NewExpression' || p.type === 'CallExpression') &&
104105
p.callee && p.callee.type === 'Identifier' &&
105106
p.callee.name === 'RegExp') {
106-
if (isTemplate) {
107-
const quasis = node.quasis;
108-
for (let i = 0; i < quasis.length; ++i) {
109-
const el = quasis[i];
110-
if (el.type === 'TemplateElement' && el.value && el.value.cooked)
111-
regexpBuffer.push([el, el.value.cooked]);
107+
if (isTemplate) {
108+
const quasis = node.quasis;
109+
for (let i = 0; i < quasis.length; ++i) {
110+
const el = quasis[i];
111+
if (el.type === 'TemplateElement' && el.value && el.value.cooked)
112+
regexpBuffer.push([el, el.value.cooked]);
113+
}
114+
} else {
115+
regexpBuffer.push([node, node.value]);
112116
}
113-
} else {
114-
regexpBuffer.push([node, node.value]);
115117
}
118+
} else if (node.regex) {
119+
checkRegExp([[node, node.regex.pattern]]);
116120
}
117-
} else if (node.regex) {
118-
checkRegExp([[node, node.regex.pattern]]);
119121
}
120-
}
121122

122-
return {
123-
'TemplateLiteral': checkLiteral,
124-
'Literal': checkLiteral,
125-
'CallExpression': checkRegExpStart,
126-
'NewExpression': checkRegExpStart,
127-
'CallExpression:exit': checkRegExpEnd,
128-
'NewExpression:exit': checkRegExpEnd
129-
};
123+
124+
return {
125+
'TemplateLiteral': checkLiteral,
126+
'Literal': checkLiteral,
127+
'CallExpression': checkRegExpStart,
128+
'NewExpression': checkRegExpStart,
129+
'CallExpression:exit': checkRegExpEnd,
130+
'NewExpression:exit': checkRegExpEnd
131+
};
132+
}
130133
};

0 commit comments

Comments
 (0)