Skip to content

Commit 944cebf

Browse files
committed
fix(sort-regexp): avoid reordering when leading negated class is greedy
1 parent dc2087a commit 944cebf

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ export function hasShadowingAlternatives({
1616
let hasNegatedCharacterClassAlternative = alternatives.some(alternative => {
1717
let firstElement = alternative.elements.at(0)
1818

19-
return firstElement?.type === 'CharacterClass' && firstElement.negate
19+
if (!firstElement) {
20+
return false
21+
}
22+
23+
if (
24+
firstElement.type === 'Quantifier' &&
25+
firstElement.element.type === 'CharacterClass'
26+
) {
27+
return firstElement.element.negate
28+
}
29+
30+
return firstElement.type === 'CharacterClass' && firstElement.negate
2031
})
2132

2233
if (hasNegatedCharacterClassAlternative) {

test/rules/sort-regexp.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,15 @@ describe('sort-regexp', () => {
576576
})
577577
})
578578

579+
it('does not reorder @import alternatives with greedy negated class', async () => {
580+
await valid({
581+
code: dedent(String.raw`
582+
/@import(?:\s*url\([^)]*\).*?|[^;]*);/
583+
`),
584+
options: [options],
585+
})
586+
})
587+
579588
it('sorts alternatives with special characters', async () => {
580589
await invalid({
581590
errors: [
@@ -1958,6 +1967,15 @@ describe('sort-regexp', () => {
19581967
})
19591968
})
19601969

1970+
it('does not reorder @import alternatives with greedy negated class', async () => {
1971+
await valid({
1972+
code: dedent(String.raw`
1973+
/@import(?:\s*url\([^)]*\).*?|[^;]*);/
1974+
`),
1975+
options: [options],
1976+
})
1977+
})
1978+
19611979
it('sorts alternatives with special characters', async () => {
19621980
await invalid({
19631981
errors: [
@@ -2885,6 +2903,15 @@ describe('sort-regexp', () => {
28852903
})
28862904
})
28872905

2906+
it('does not reorder @import alternatives with greedy negated class', async () => {
2907+
await valid({
2908+
code: dedent(String.raw`
2909+
/@import(?:\s*url\([^)]*\).*?|[^;]*);/
2910+
`),
2911+
options: [options],
2912+
})
2913+
})
2914+
28882915
it('sorts alternatives with quantifiers', async () => {
28892916
await invalid({
28902917
errors: [

0 commit comments

Comments
 (0)