Skip to content

Commit a072d14

Browse files
authored
fix(sort-character-class-elements): wrong fix for ^ (#859)
* fix(sort-character-class-elements): wrong fix for `^` * Create spicy-rats-invent.md
1 parent 8b4056b commit a072d14

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

.changeset/spicy-rats-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-regexp": patch
3+
---
4+
5+
fix(sort-character-class-elements): wrong autofix for `^`

lib/rules/sort-character-class-elements.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,15 @@ function escapeRaw(node: CharacterClassElement, target: CharacterClassElement) {
296296
) {
297297
raw = `\\${raw}`
298298
}
299+
} else if (raw.startsWith("^")) {
300+
const parent = target.parent as CharacterClass
301+
const elements: (
302+
| UnicodeSetsCharacterClassElement
303+
| ClassRangesCharacterClassElement
304+
)[] = parent.elements
305+
if (elements.indexOf(target) === 0) {
306+
raw = `\\${raw}`
307+
}
299308
}
300309
if (target.raw.startsWith("-")) {
301310
if (node.type === "Character" || node.type === "CharacterSet") {

tests/lib/rules/__snapshots__/sort-character-class-elements.ts.eslintsnap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,3 +375,29 @@ Output:
375375

376376
[1] Expected character class elements to be in ascending order. '\q{aa}' should be before '\q{ab}'.
377377
---
378+
379+
380+
Test: sort-character-class-elements >> invalid
381+
Code:
382+
1 | /[~^*]/
383+
| ^ [1]
384+
| ^ [2]
385+
386+
Output:
387+
1 | /[*\^~]/
388+
389+
[1] Expected character class elements to be in ascending order. '^' should be before '~'.
390+
[2] Expected character class elements to be in ascending order. '*' should be before '~'.
391+
---
392+
393+
394+
Test: sort-character-class-elements >> invalid
395+
Code:
396+
1 | /[~^]/
397+
| ^ [1]
398+
399+
Output:
400+
1 | /[\^~]/
401+
402+
[1] Expected character class elements to be in ascending order. '^' should be before '~'.
403+
---

tests/lib/rules/sort-character-class-elements.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,7 @@ tester.run("sort-character-class-elements", rule as any, {
9696
String.raw`/[\q{b}\q{c}\q{a}]/v`,
9797
String.raw`/[\q{ac}\q{ab}\q{aa}]/v`,
9898
String.raw`/[\q{ab}\q{ac}\q{aa}]/v`,
99+
String.raw`/[~^*]/`,
100+
String.raw`/[~^]/`,
99101
],
100102
})

0 commit comments

Comments
 (0)