Skip to content

Commit 5756c8b

Browse files
authored
Fix crash in regexp/order-in-character-class rule (#52)
1 parent c555276 commit 5756c8b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/rules/order-in-character-class.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function isNeedEscape(
271271
}
272272
const parent = target.parent as CharacterClass
273273
const prev = parent.elements[parent.elements.indexOf(target) - 1]
274-
if (prev.type !== "Character" && prev.type !== "CharacterSet") {
274+
if (!prev || (prev.type !== "Character" && prev.type !== "CharacterSet")) {
275275
return false
276276
}
277277
return true

tests/lib/rules/order-in-character-class.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,12 @@ tester.run("order-in-character-class", rule as any, {
178178
"Expected character class elements to be in ascending order. '-' should be before 'a-b'.",
179179
],
180180
},
181+
{
182+
code: String.raw`/[a-b-]/u`,
183+
output: String.raw`/[-a-b]/u`,
184+
errors: [
185+
"Expected character class elements to be in ascending order. '-' should be before 'a-b'.",
186+
],
187+
},
181188
],
182189
})

0 commit comments

Comments
 (0)