From 7a30f5e2ccc1c3fb4158f73867364e71bd09773a Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sun, 3 Aug 2025 11:31:30 +0900 Subject: [PATCH 1/2] fix(sort-character-class-elements): wrong fix for `^` --- lib/rules/sort-character-class-elements.ts | 9 +++++++ ...ort-character-class-elements.ts.eslintsnap | 26 +++++++++++++++++++ .../rules/sort-character-class-elements.ts | 2 ++ 3 files changed, 37 insertions(+) diff --git a/lib/rules/sort-character-class-elements.ts b/lib/rules/sort-character-class-elements.ts index 8ea0d6448..f2ed2cc33 100644 --- a/lib/rules/sort-character-class-elements.ts +++ b/lib/rules/sort-character-class-elements.ts @@ -296,6 +296,15 @@ function escapeRaw(node: CharacterClassElement, target: CharacterClassElement) { ) { raw = `\\${raw}` } + } else if (raw.startsWith("^")) { + const parent = target.parent as CharacterClass + const elements: ( + | UnicodeSetsCharacterClassElement + | ClassRangesCharacterClassElement + )[] = parent.elements + if (elements.indexOf(target) === 0) { + raw = `\\${raw}` + } } if (target.raw.startsWith("-")) { if (node.type === "Character" || node.type === "CharacterSet") { diff --git a/tests/lib/rules/__snapshots__/sort-character-class-elements.ts.eslintsnap b/tests/lib/rules/__snapshots__/sort-character-class-elements.ts.eslintsnap index 81d78e81a..957f5853f 100644 --- a/tests/lib/rules/__snapshots__/sort-character-class-elements.ts.eslintsnap +++ b/tests/lib/rules/__snapshots__/sort-character-class-elements.ts.eslintsnap @@ -375,3 +375,29 @@ Output: [1] Expected character class elements to be in ascending order. '\q{aa}' should be before '\q{ab}'. --- + + +Test: sort-character-class-elements >> invalid +Code: + 1 | /[~^*]/ + | ^ [1] + | ^ [2] + +Output: + 1 | /[*\^~]/ + +[1] Expected character class elements to be in ascending order. '^' should be before '~'. +[2] Expected character class elements to be in ascending order. '*' should be before '~'. +--- + + +Test: sort-character-class-elements >> invalid +Code: + 1 | /[~^]/ + | ^ [1] + +Output: + 1 | /[\^~]/ + +[1] Expected character class elements to be in ascending order. '^' should be before '~'. +--- diff --git a/tests/lib/rules/sort-character-class-elements.ts b/tests/lib/rules/sort-character-class-elements.ts index 6c40689c9..38b50e2ca 100644 --- a/tests/lib/rules/sort-character-class-elements.ts +++ b/tests/lib/rules/sort-character-class-elements.ts @@ -96,5 +96,7 @@ tester.run("sort-character-class-elements", rule as any, { String.raw`/[\q{b}\q{c}\q{a}]/v`, String.raw`/[\q{ac}\q{ab}\q{aa}]/v`, String.raw`/[\q{ab}\q{ac}\q{aa}]/v`, + String.raw`/[~^*]/`, + String.raw`/[~^]/`, ], }) From 2b9e54b082a2246f14d9963074f10f527690c993 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Sun, 3 Aug 2025 11:32:15 +0900 Subject: [PATCH 2/2] Create spicy-rats-invent.md --- .changeset/spicy-rats-invent.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spicy-rats-invent.md diff --git a/.changeset/spicy-rats-invent.md b/.changeset/spicy-rats-invent.md new file mode 100644 index 000000000..a4ddbfc3d --- /dev/null +++ b/.changeset/spicy-rats-invent.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-regexp": patch +--- + +fix(sort-character-class-elements): wrong autofix for `^`