Repository: ota-meshi/eslint-plugin-regexp
Pull request #592 (2023-09-10 06:06:32)

Add support for v flag to `regexp/unicode-escape` and `regexp/prefer-unicode-codepoint-escapes` rules


related to https://github.com/ota-meshi/eslint-plugin-regexp/issues/545

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

Latest commit: 80d950b60aa65f512fd96b39187bf438def5ff65

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/unicode-escape?filename=.changeset/large-dingos-allow.md&value=---%0A%22eslint-plugin-regexp%22%3A%20patch%0A---%0A%0AAdd%20support%20for%20v%20flag%20to%20%60regexp%2Funicode-escape%60%20and%20%60regexp%2Fprefer-unicode-codepoint-escapes%60%20rules%0A)



Patch:
diff --git a/.changeset/early-guests-hug.md b/.changeset/early-guests-hug.md
new file mode 100644
index 0000000..c55d43f
--- /dev/null
+++ b/.changeset/early-guests-hug.md
@@ -0,0 +1,5 @@
+---
+"eslint-plugin-regexp": minor
+---
+
+Add support for v flag to `regexp/prefer-unicode-codepoint-escapes` rule
diff --git a/.changeset/large-dingos-allow.md b/.changeset/large-dingos-allow.md
new file mode 100644
index 0000000..6918607
--- /dev/null
+++ b/.changeset/large-dingos-allow.md
@@ -0,0 +1,5 @@
+---
+"eslint-plugin-regexp": minor
+---
+
+Add support for v flag to `regexp/unicode-escape` rule
diff --git a/lib/rules/prefer-unicode-codepoint-escapes.ts b/lib/rules/prefer-unicode-codepoint-escapes.ts
index c26f209..777d221 100644
--- a/lib/rules/prefer-unicode-codepoint-escapes.ts
+++ b/lib/rules/prefer-unicode-codepoint-escapes.ts
@@ -23,7 +23,7 @@ export default createRule("prefer-unicode-codepoint-escapes", {
         ): RegExpVisitor.Handlers {
             const { node, flags, getRegexpLocation, fixReplaceNode } =
                 regexpContext
-            if (!flags.unicode) {
+            if (!flags.unicode && !flags.unicodeSets) {
                 return {}
             }
             return {
diff --git a/lib/rules/unicode-escape.ts b/lib/rules/unicode-escape.ts
index adf8389..49455d9 100644
--- a/lib/rules/unicode-escape.ts
+++ b/lib/rules/unicode-escape.ts
@@ -88,7 +88,7 @@ export default createRule("unicode-escape", {
             regexpContext: RegExpContext,
         ): RegExpVisitor.Handlers {
             const { flags } = regexpContext
-            if (!flags.unicode) {
+            if (!flags.unicode && !flags.unicodeSets) {
                 return {}
             }
             return {


Test patch:
diff --git a/tests/lib/rules/prefer-unicode-codepoint-escapes.ts b/tests/lib/rules/prefer-unicode-codepoint-escapes.ts
index 9a87ad9..b3e68aa 100644
--- a/tests/lib/rules/prefer-unicode-codepoint-escapes.ts
+++ b/tests/lib/rules/prefer-unicode-codepoint-escapes.ts
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/prefer-unicode-codepoint-escapes"
 
 const tester = new RuleTester({
     parserOptions: {
-        ecmaVersion: 2020,
+        ecmaVersion: "latest",
         sourceType: "module",
     },
 })
@@ -15,6 +15,8 @@ tester.run("prefer-unicode-codepoint-escapes", rule as any, {
         String.raw`/[\ud83d\ude00]/`,
         String.raw`/\u{1f600}/u`,
         String.raw`/😀/u`,
+        String.raw`/\u{1f600}/v`,
+        String.raw`/😀/v`,
     ],
     invalid: [
         {
@@ -75,5 +77,19 @@ tester.run("prefer-unicode-codepoint-escapes", rule as any, {
                 "Use Unicode codepoint escapes instead of Unicode escapes using surrogate pairs.",
             ],
         },
+        {
+            code: String.raw`/\ud83d\ude00/v`,
+            output: String.raw`/\u{1f600}/v`,
+            errors: [
+                {
+                    message:
+                        "Use Unicode codepoint escapes instead of Unicode escapes using surrogate pairs.",
+                    line: 1,
+                    column: 2,
+                    endLine: 1,
+                    endColumn: 14,
+                },
+            ],
+        },
     ],
 })
diff --git a/tests/lib/rules/unicode-escape.ts b/tests/lib/rules/unicode-escape.ts
index c04b81c..2e8c76d 100644
--- a/tests/lib/rules/unicode-escape.ts
+++ b/tests/lib/rules/unicode-escape.ts
@@ -3,7 +3,7 @@ import rule from "../../../lib/rules/unicode-escape"
 
 const tester = new RuleTester({
     parserOptions: {
-        ecmaVersion: 2020,
+        ecmaVersion: "latest",
         sourceType: "module",
     },
 })
@@ -19,6 +19,14 @@ tester.run("unicode-escape", rule as any, {
             code: String.raw`/a \x0a \cM \0 \u0100 \u00ff \ud83d\ude00 \u{1f600}/u`,
             options: ["unicodeEscape"],
         },
+        {
+            code: String.raw`/a \x0a \cM \0 \u{ff} \u{100} \ud83d\ude00 \u{1f600}/v`,
+            options: ["unicodeCodePointEscape"],
+        },
+        {
+            code: String.raw`/a \x0a \cM \0 \u0100 \u00ff \ud83d\ude00 \u{1f600}/v`,
+            options: ["unicodeEscape"],
+        },
 
         // no u flag
         {
@@ -77,5 +85,39 @@ tester.run("unicode-escape", rule as any, {
                 },
             ],
         },
+        {
+            code: String.raw`/a \x0a \cM \0 \u0100 \u00ff \ud83d\ude00 \u{1f600}/v`,
+            output: String.raw`/a \x0a \cM \0 \u{100} \u{ff} \ud83d\ude00 \u{1f600}/v`,
+            options: ["unicodeCodePointEscape"],
+            errors: [
+                {
+                    message:
+                        "Expected unicode code point escape ('\\u{100}'), but unicode escape ('\\u0100') is used.",
+                    column: 16,
+                },
+                {
+                    message:
+                        "Expected unicode code point escape ('\\u{ff}'), but unicode escape ('\\u00ff') is used.",
+                    column: 23,
+                },
+            ],
+        },
+        {
+            code: String.raw`/a \x0a \cM \0 \u{ff} \u{100} \ud83d\ude00 \u{1f600}/v`,
+            output: String.raw`/a \x0a \cM \0 \u00ff \u0100 \ud83d\ude00 \u{1f600}/v`,
+            options: ["unicodeEscape"],
+            errors: [
+                {
+                    message:
+                        "Expected unicode escape ('\\u00ff'), but unicode code point escape ('\\u{ff}') is used.",
+                    column: 16,
+                },
+                {
+                    message:
+                        "Expected unicode escape ('\\u0100'), but unicode code point escape ('\\u{100}') is used.",
+                    column: 23,
+                },
+            ],
+        },
     ],
 })