Repository: eslint/eslint
File: lib/rules/no-invalid-regexp.js (JavaScript)

Code before:
         * @param {string|null} flagsToCheck The RegExp invalid flags.
         * @param {string} allFlags all valid and allowed flags.
         * @returns {string|null} The syntax error.
         */
        function validateRegExpFlags(flags, flagsToCheck, allFlags) {
            const duplicateFlags = [];

            if (typeof flagsToCheck === "string") {
                for (const flag of flagsToCheck) {
                    if (allFlags.includes(flag)) {
                        duplicateFlags.push(flag);
                    }
                }
            }

            /*
             * `regexpp` checks the combination of `u` and `v` flags when parsing `Pattern` according to `ecma262`,
             * but this rule may check only the flag when the pattern is unidentifiable, so check it here.
             * https://tc39.es/ecma262/multipage/text-processing.html#sec-parsepattern
             */
            if (flags && flags.includes("u") && flags.includes("v")) {
                return "Regex 'u' and 'v' flags cannot be used together";
            }

            if (duplicateFlags.length > 0) {
                return `Duplicate flags supplied to RegExp constructor '${duplicateFlags.join("")}'`;
            }

            if (!flagsToCheck) {
                return null;
            }

            return `Invalid flags supplied to RegExp constructor '${flagsToCheck}'`;
        }

        return {
            "CallExpression, NewExpression"(node) {
                if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp") {
                    return;
                }

                const flags = getFlags(node);
                let flagsToCheck = flags;
                const allFlags = allowedFlags.length > 0 ? validFlags.split("").concat(allowedFlags) : validFlags.split("");

                if (flags) {
                    allFlags.forEach(flag => {
                        flagsToCheck = flagsToCheck.replace(flag, "");
                    });
                }


Review comment (refactor):
Could we reformat this message? It reads a bit strange to me.
```suggestion
                return `Duplicate flags ('${duplicateFlags.join("")}') supplied to RegExp constructor.`;
```

Code after:
         * @param {string|null} flagsToCheck The RegExp invalid flags.
         * @param {string} allFlags all valid and allowed flags.
         * @returns {string|null} The syntax error.
         */
        function validateRegExpFlags(flags, flagsToCheck, allFlags) {
            const duplicateFlags = [];

            if (typeof flagsToCheck === "string") {
                for (const flag of flagsToCheck) {
                    if (allFlags.includes(flag)) {
                        duplicateFlags.push(flag);
                    }
                }
            }

            /*
             * `regexpp` checks the combination of `u` and `v` flags when parsing `Pattern` according to `ecma262`,
             * but this rule may check only the flag when the pattern is unidentifiable, so check it here.
             * https://tc39.es/ecma262/multipage/text-processing.html#sec-parsepattern
             */
            if (flags && flags.includes("u") && flags.includes("v")) {
                return "Regex 'u' and 'v' flags cannot be used together";
            }

            if (duplicateFlags.length > 0) {
                return `Duplicate flags ('${duplicateFlags.join("")}') supplied to RegExp constructor`;
            }

            if (!flagsToCheck) {
                return null;
            }

            return `Invalid flags supplied to RegExp constructor '${flagsToCheck}'`;
        }

        return {
            "CallExpression, NewExpression"(node) {
                if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp") {
                    return;
                }

                const flags = getFlags(node);
                let flagsToCheck = flags;
                const allFlags = allowedFlags.length > 0 ? validFlags.split("").concat(allowedFlags) : validFlags.split("");

                if (flags) {
                    allFlags.forEach(flag => {
                        flagsToCheck = flagsToCheck.replace(flag, "");
                    });
                }