Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorMadhuri <sonimadhuri12@gmail.com>2022-09-28 00:26:54 +0300
committerJuan José Arboleda <soyjuanarbol@gmail.com>2022-10-11 22:45:29 +0300
commit006d7f1f2adcbf85e12c55251ecbc29b3e91927b (patch)
tree9024089106d80a8d3b63b91390b1637b78858e35 /tools
parent969a12be4b6783c02f42634928d285a207c47e5d (diff)
tools: refactor deprecated format in no-unescaped-regexp-dot
PR-URL: https://github.com/nodejs/node/pull/44763 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/eslint-rules/no-unescaped-regexp-dot.js193
1 files changed, 98 insertions, 95 deletions
diff --git a/tools/eslint-rules/no-unescaped-regexp-dot.js b/tools/eslint-rules/no-unescaped-regexp-dot.js
index e9349713bfa..554eb9bdfb6 100644
--- a/tools/eslint-rules/no-unescaped-regexp-dot.js
+++ b/tools/eslint-rules/no-unescaped-regexp-dot.js
@@ -8,123 +8,126 @@
// Rule Definition
//------------------------------------------------------------------------------
-module.exports = function(context) {
- const sourceCode = context.getSourceCode();
- const regexpStack = [];
- let regexpBuffer = [];
- let inRegExp = false;
+module.exports = {
+ create(context) {
+ const sourceCode = context.getSourceCode();
+ const regexpStack = [];
+ let regexpBuffer = [];
+ let inRegExp = false;
- function report(node, startOffset) {
- const indexOfDot = sourceCode.getIndexFromLoc(node.loc.start) + startOffset;
- context.report({
- node,
- loc: sourceCode.getLocFromIndex(indexOfDot),
- message: 'Unescaped dot character in regular expression'
- });
- }
+ function report(node, startOffset) {
+ const indexOfDot = sourceCode.getIndexFromLoc(node.loc.start) + startOffset;
+ context.report({
+ node,
+ loc: sourceCode.getLocFromIndex(indexOfDot),
+ message: 'Unescaped dot character in regular expression'
+ });
+ }
+ const allowedModifiers = ['+', '*', '?', '{'];
- const allowedModifiers = ['+', '*', '?', '{'];
- function checkRegExp(nodes) {
- let escaping = false;
- let inCharClass = false;
- for (let n = 0; n < nodes.length; ++n) {
- const pair = nodes[n];
- const node = pair[0];
- const str = pair[1];
- for (let i = 0; i < str.length; ++i) {
- switch (str[i]) {
- case '[':
- if (!escaping)
- inCharClass = true;
- else
- escaping = false;
- break;
- case ']':
- if (!escaping) {
- if (inCharClass)
- inCharClass = false;
- } else {
- escaping = false;
- }
- break;
- case '\\':
- escaping = !escaping;
- break;
- case '.':
- if (!escaping) {
- if (!inCharClass &&
+ function checkRegExp(nodes) {
+ let escaping = false;
+ let inCharClass = false;
+ for (let n = 0; n < nodes.length; ++n) {
+ const pair = nodes[n];
+ const node = pair[0];
+ const str = pair[1];
+ for (let i = 0; i < str.length; ++i) {
+ switch (str[i]) {
+ case '[':
+ if (!escaping)
+ inCharClass = true;
+ else
+ escaping = false;
+ break;
+ case ']':
+ if (!escaping) {
+ if (inCharClass)
+ inCharClass = false;
+ } else {
+ escaping = false;
+ }
+ break;
+ case '\\':
+ escaping = !escaping;
+ break;
+ case '.':
+ if (!escaping) {
+ if (!inCharClass &&
((i + 1) === str.length ||
allowedModifiers.indexOf(str[i + 1]) === -1)) {
- report(node, i);
+ report(node, i);
+ }
+ } else {
+ escaping = false;
}
- } else {
- escaping = false;
- }
- break;
- default:
- if (escaping)
- escaping = false;
+ break;
+ default:
+ if (escaping)
+ escaping = false;
+ }
}
}
}
- }
- function checkRegExpStart(node) {
- if (node.callee && node.callee.name === 'RegExp') {
- if (inRegExp) {
- regexpStack.push(regexpBuffer);
- regexpBuffer = [];
+ function checkRegExpStart(node) {
+ if (node.callee && node.callee.name === 'RegExp') {
+ if (inRegExp) {
+ regexpStack.push(regexpBuffer);
+ regexpBuffer = [];
+ }
+ inRegExp = true;
}
- inRegExp = true;
}
- }
- function checkRegExpEnd(node) {
- if (node.callee && node.callee.name === 'RegExp') {
- checkRegExp(regexpBuffer);
- if (regexpStack.length) {
- regexpBuffer = regexpStack.pop();
- } else {
- inRegExp = false;
- regexpBuffer = [];
+ function checkRegExpEnd(node) {
+ if (node.callee && node.callee.name === 'RegExp') {
+ checkRegExp(regexpBuffer);
+ if (regexpStack.length) {
+ regexpBuffer = regexpStack.pop();
+ } else {
+ inRegExp = false;
+ regexpBuffer = [];
+ }
}
}
- }
- function checkLiteral(node) {
- const isTemplate = (node.type === 'TemplateLiteral' && node.quasis &&
+ function checkLiteral(node) {
+ const isTemplate = (node.type === 'TemplateLiteral' && node.quasis &&
node.quasis.length);
- if (inRegExp &&
+ if (inRegExp &&
(isTemplate || (typeof node.value === 'string' && node.value.length))) {
- let p = node.parent;
- while (p && p.type === 'BinaryExpression') {
- p = p.parent;
- }
- if (p && (p.type === 'NewExpression' || p.type === 'CallExpression') &&
+ let p = node.parent;
+ while (p && p.type === 'BinaryExpression') {
+ p = p.parent;
+ }
+ if (p && (p.type === 'NewExpression' || p.type === 'CallExpression') &&
p.callee && p.callee.type === 'Identifier' &&
p.callee.name === 'RegExp') {
- if (isTemplate) {
- const quasis = node.quasis;
- for (let i = 0; i < quasis.length; ++i) {
- const el = quasis[i];
- if (el.type === 'TemplateElement' && el.value && el.value.cooked)
- regexpBuffer.push([el, el.value.cooked]);
+ if (isTemplate) {
+ const quasis = node.quasis;
+ for (let i = 0; i < quasis.length; ++i) {
+ const el = quasis[i];
+ if (el.type === 'TemplateElement' && el.value && el.value.cooked)
+ regexpBuffer.push([el, el.value.cooked]);
+ }
+ } else {
+ regexpBuffer.push([node, node.value]);
}
- } else {
- regexpBuffer.push([node, node.value]);
}
+ } else if (node.regex) {
+ checkRegExp([[node, node.regex.pattern]]);
}
- } else if (node.regex) {
- checkRegExp([[node, node.regex.pattern]]);
}
- }
- return {
- 'TemplateLiteral': checkLiteral,
- 'Literal': checkLiteral,
- 'CallExpression': checkRegExpStart,
- 'NewExpression': checkRegExpStart,
- 'CallExpression:exit': checkRegExpEnd,
- 'NewExpression:exit': checkRegExpEnd
- };
+
+ return {
+ 'TemplateLiteral': checkLiteral,
+ 'Literal': checkLiteral,
+ 'CallExpression': checkRegExpStart,
+ 'NewExpression': checkRegExpStart,
+ 'CallExpression:exit': checkRegExpEnd,
+ 'NewExpression:exit': checkRegExpEnd
+ };
+ }
};