]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/lib/rules/no-invalid-regexp.js
import 8.41.0 source
[pve-eslint.git] / eslint / lib / rules / no-invalid-regexp.js
index 0f1d9c7bedc696183e2ac66a4a3b3ad2eab64c2c..9a35743d122f9af62fea6e19dff92bb9094135e0 100644 (file)
@@ -8,7 +8,7 @@
 // Requirements
 //------------------------------------------------------------------------------
 
-const RegExpValidator = require("regexpp").RegExpValidator;
+const RegExpValidator = require("@eslint-community/regexpp").RegExpValidator;
 const validator = new RegExpValidator();
 const validFlags = /[dgimsuy]/gu;
 const undefined1 = void 0;
@@ -25,7 +25,7 @@ module.exports = {
         docs: {
             description: "Disallow invalid regular expression strings in `RegExp` constructors",
             recommended: true,
-            url: "https://eslint.org/docs/rules/no-invalid-regexp"
+            url: "https://eslint.org/docs/latest/rules/no-invalid-regexp"
         },
 
         schema: [{
@@ -59,6 +59,20 @@ module.exports = {
             }
         }
 
+        /**
+         * Reports error with the provided message.
+         * @param {ASTNode} node The node holding the invalid RegExp
+         * @param {string} message The message to report.
+         * @returns {void}
+         */
+        function report(node, message) {
+            context.report({
+                node,
+                messageId: "regexMessage",
+                data: { message }
+            });
+        }
+
         /**
          * Check if node is a string
          * @param {ASTNode} node node to evaluate
@@ -108,10 +122,13 @@ module.exports = {
 
         /**
          * Check syntax error in a given flags.
-         * @param {string} flags The RegExp flags to validate.
+         * @param {string|null} flags The RegExp flags to validate.
          * @returns {string|null} The syntax error.
          */
         function validateRegExpFlags(flags) {
+            if (!flags) {
+                return null;
+            }
             try {
                 validator.validateFlags(flags);
                 return null;
@@ -122,34 +139,39 @@ module.exports = {
 
         return {
             "CallExpression, NewExpression"(node) {
-                if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp" || !isString(node.arguments[0])) {
+                if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp") {
                     return;
                 }
-                const pattern = node.arguments[0].value;
+
                 let flags = getFlags(node);
 
                 if (flags && allowedFlags) {
                     flags = flags.replace(allowedFlags, "");
                 }
 
-                const message =
-                    (
-                        flags && validateRegExpFlags(flags)
-                    ) ||
-                    (
+                let message = validateRegExpFlags(flags);
+
+                if (message) {
+                    report(node, message);
+                    return;
+                }
+
+                if (!isString(node.arguments[0])) {
+                    return;
+                }
+
+                const pattern = node.arguments[0].value;
+
+                message = (
 
-                        // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
-                        flags === null
-                            ? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
-                            : validateRegExpPattern(pattern, flags.includes("u"))
-                    );
+                    // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
+                    flags === null
+                        ? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
+                        : validateRegExpPattern(pattern, flags.includes("u"))
+                );
 
                 if (message) {
-                    context.report({
-                        node,
-                        messageId: "regexMessage",
-                        data: { message }
-                    });
+                    report(node, message);
                 }
             }
         };