Repository: ota-meshi/eslint-plugin-regexp
Pull request #583 (2023-09-09 00:42:08)

Improve `regexp/no-invalid-regexp` rule to check for unknown pattern flags.
This PR improves the `regexp/no-invalid-regexp` rule to check flags even for unknown patterns.

changeset-bot[bot]: ###  ⚠️  No Changeset found

Latest commit: 78c2b521959379d3f954e52db42c8384633e7a68

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**

<details><summary>This PR includes no changesets</summary>

  When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

</details>

[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/ota-meshi/eslint-plugin-regexp/new/no-invalid?filename=.changeset/warm-ladybugs-wonder.md&value=---%0A%22eslint-plugin-regexp%22%3A%20patch%0A---%0A%0AImprove%20%60regexp%2Fno-invalid-regexp%60%20rule%20to%20check%20for%20unknown%20pattern%20flags.%0A)



Patch:
diff --git a/.changeset/warm-ladybugs-wonder.md b/.changeset/warm-ladybugs-wonder.md
new file mode 100644
index 0000000..cebca31
--- /dev/null
+++ b/.changeset/warm-ladybugs-wonder.md
@@ -0,0 +1,5 @@
+---
+"eslint-plugin-regexp": minor
+---
+
+Improve `regexp/no-invalid-regexp` rule to check for unknown pattern flags.
diff --git a/lib/rules/no-invalid-regexp.ts b/lib/rules/no-invalid-regexp.ts
index 0c76ba2..58d6d8c 100644
--- a/lib/rules/no-invalid-regexp.ts
+++ b/lib/rules/no-invalid-regexp.ts
@@ -1,4 +1,4 @@
-import type { RegExpContextForInvalid } from "../utils"
+import type { RegExpContextForInvalid, RegExpContextForUnknown } from "../utils"
 import { createRule, defineRegexpVisitor } from "../utils"
 
 /** Returns the position of the error */
@@ -22,6 +22,8 @@ export default createRule("no-invalid-regexp", {
         schema: [],
         messages: {
             error: "{{message}}",
+            duplicateFlag: "Duplicate {{flag}} flag.",
+            uvFlag: "Regex 'u' and 'v' flags cannot be used together.",
         },
         type: "problem",
     },
@@ -56,6 +58,33 @@ export default createRule("no-invalid-regexp", {
             })
         }
 
-        return defineRegexpVisitor(context, { visitInvalid })
+        /** Checks for the combination of `u` and `v` flags */
+        function visitUnknown(regexpContext: RegExpContextForUnknown): void {
+            const { node, flags, flagsString, getFlagsLocation } = regexpContext
+
+            const flagSet = new Set<string>()
+            for (const flag of flagsString ?? "") {
+                if (flagSet.has(flag)) {
+                    context.report({
+                        node,
+                        loc: getFlagsLocation(),
+                        messageId: "duplicateFlag",
+                        data: { flag },
+                    })
+                    return
+                }
+                flagSet.add(flag)
+            }
+
+            if (flags.unicode && flags.unicodeSets) {
+                context.report({
+                    node,
+                    loc: getFlagsLocation(),
+                    messageId: "uvFlag",
+                })
+            }
+        }
+
+        return defineRegexpVisitor(context, { visitInvalid, visitUnknown })
     },
 })


Test patch:
diff --git a/tests/lib/rules/no-invalid-regexp.ts b/tests/lib/rules/no-invalid-regexp.ts
index 7b545b3..27626f4 100644
--- a/tests/lib/rules/no-invalid-regexp.ts
+++ b/tests/lib/rules/no-invalid-regexp.ts
@@ -44,5 +44,24 @@ tester.run("no-invalid-regexp", rule as any, {
                 },
             ],
         },
+
+        {
+            code: "new RegExp(pattern, 'uu');",
+            errors: [
+                {
+                    message: "Duplicate u flag.",
+                    column: 22,
+                },
+            ],
+        },
+        {
+            code: "new RegExp(pattern, 'uv');",
+            errors: [
+                {
+                    message: "Regex 'u' and 'v' flags cannot be used together.",
+                    column: 22,
+                },
+            ],
+        },
     ],
 })