Skip to content

Commit dc2087a

Browse files
committed
fix(sort-regexp): avoid reordering negated class alternatives
1 parent 6f073d8 commit dc2087a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

rules/sort-regexp/has-shadowing-alternatives.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export function hasShadowingAlternatives({
1313
}: {
1414
alternatives: Alternative[]
1515
}): boolean {
16+
let hasNegatedCharacterClassAlternative = alternatives.some(alternative => {
17+
let firstElement = alternative.elements.at(0)
18+
19+
return firstElement?.type === 'CharacterClass' && firstElement.negate
20+
})
21+
22+
if (hasNegatedCharacterClassAlternative) {
23+
return true
24+
}
25+
1626
let rawAlternatives = alternatives.map(alternative => alternative.raw)
1727

1828
for (let index = 0; index < rawAlternatives.length; index++) {

test/rules/sort-regexp.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,15 @@ describe('sort-regexp', () => {
567567
})
568568
})
569569

570+
it('does not reorder alternatives when negated character class overlaps literal', async () => {
571+
await valid({
572+
code: dedent`
573+
/(?:\([^)]*\)|[^;])+/
574+
`,
575+
options: [options],
576+
})
577+
})
578+
570579
it('sorts alternatives with special characters', async () => {
571580
await invalid({
572581
errors: [
@@ -1940,6 +1949,15 @@ describe('sort-regexp', () => {
19401949
})
19411950
})
19421951

1952+
it('does not reorder alternatives when negated character class overlaps literal', async () => {
1953+
await valid({
1954+
code: dedent`
1955+
/(?:\([^)]*\)|[^;])+/
1956+
`,
1957+
options: [options],
1958+
})
1959+
})
1960+
19431961
it('sorts alternatives with special characters', async () => {
19441962
await invalid({
19451963
errors: [
@@ -2858,6 +2876,15 @@ describe('sort-regexp', () => {
28582876
})
28592877
})
28602878

2879+
it('does not reorder alternatives when negated character class overlaps literal', async () => {
2880+
await valid({
2881+
code: dedent`
2882+
/(?:\([^)]*\)|[^;])+/
2883+
`,
2884+
options: [options],
2885+
})
2886+
})
2887+
28612888
it('sorts alternatives with quantifiers', async () => {
28622889
await invalid({
28632890
errors: [

0 commit comments

Comments
 (0)