Skip to content

Commit 971b90a

Browse files
committed
test(sort-regexp): improve tests
1 parent 8ccd66d commit 971b90a

File tree

1 file changed

+243
-6
lines changed

1 file changed

+243
-6
lines changed

test/rules/sort-regexp.test.ts

Lines changed: 243 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,105 @@ describe('sort-regexp', () => {
11811181
options: [options],
11821182
})
11831183
})
1184+
1185+
it('keeps hyphen at the end when it is a literal character', async () => {
1186+
await valid({
1187+
code: dedent`
1188+
/[abc-]/
1189+
`,
1190+
options: [options],
1191+
})
1192+
})
1193+
1194+
it('keeps hyphen at the beginning when it is a literal character', async () => {
1195+
await invalid({
1196+
errors: [
1197+
{
1198+
messageId: 'unexpectedRegExpOrder',
1199+
data: { right: 'a', left: '-' },
1200+
},
1201+
],
1202+
output: dedent`
1203+
/[abc-]/
1204+
`,
1205+
code: dedent`
1206+
/[-abc]/
1207+
`,
1208+
options: [options],
1209+
})
1210+
})
1211+
1212+
it('does not move hyphen from edge to middle', async () => {
1213+
await invalid({
1214+
errors: [
1215+
{
1216+
messageId: 'unexpectedRegExpOrder',
1217+
data: { right: 'a', left: 'z' },
1218+
},
1219+
],
1220+
output: dedent`
1221+
/[axz-]/
1222+
`,
1223+
code: dedent`
1224+
/[zxa-]/
1225+
`,
1226+
options: [options],
1227+
})
1228+
})
1229+
1230+
it('sorts character class with escaped hyphen normally', async () => {
1231+
await invalid({
1232+
errors: [
1233+
{
1234+
messageId: 'unexpectedRegExpOrder',
1235+
data: { right: 'a', left: 'z' },
1236+
},
1237+
],
1238+
output: dedent(String.raw`
1239+
/[axz\-]/
1240+
`),
1241+
code: dedent(String.raw`
1242+
/[z\-xa]/
1243+
`),
1244+
options: [options],
1245+
})
1246+
})
1247+
1248+
it('does not create accidental range from literal hyphen', async () => {
1249+
await invalid({
1250+
errors: [
1251+
{
1252+
messageId: 'unexpectedRegExpOrder',
1253+
data: { right: 'a', left: 'z' },
1254+
},
1255+
],
1256+
output: dedent`
1257+
/[az-]/
1258+
`,
1259+
code: dedent`
1260+
/[za-]/
1261+
`,
1262+
options: [options],
1263+
})
1264+
})
1265+
1266+
it('preserves range when hyphen is part of range', async () => {
1267+
await invalid({
1268+
errors: [
1269+
{
1270+
messageId: 'unexpectedRegExpOrder',
1271+
data: { right: '0-9', left: 'z' },
1272+
},
1273+
],
1274+
output: dedent`
1275+
/[0-9a-fz]/
1276+
`,
1277+
code: dedent`
1278+
/[z0-9a-f]/
1279+
`,
1280+
options: [options],
1281+
})
1282+
})
11841283
})
11851284

11861285
describe('natural', () => {
@@ -1405,11 +1504,6 @@ describe('sort-regexp', () => {
14051504
})
14061505

14071506
it('sorts character classes', async () => {
1408-
let customOptions = {
1409-
...options,
1410-
type: 'natural',
1411-
} as const
1412-
14131507
await invalid({
14141508
errors: [
14151509
{
@@ -1423,7 +1517,7 @@ describe('sort-regexp', () => {
14231517
code: dedent`
14241518
/[312]/
14251519
`,
1426-
options: [customOptions],
1520+
options: [options],
14271521
})
14281522
})
14291523

@@ -2358,6 +2452,105 @@ describe('sort-regexp', () => {
23582452
options: [options],
23592453
})
23602454
})
2455+
2456+
it('keeps hyphen at the end when it is a literal character', async () => {
2457+
await valid({
2458+
code: dedent`
2459+
/[abc-]/
2460+
`,
2461+
options: [options],
2462+
})
2463+
})
2464+
2465+
it('keeps hyphen at the beginning when it is a literal character', async () => {
2466+
await invalid({
2467+
errors: [
2468+
{
2469+
messageId: 'unexpectedRegExpOrder',
2470+
data: { right: 'a', left: '-' },
2471+
},
2472+
],
2473+
output: dedent`
2474+
/[abc-]/
2475+
`,
2476+
code: dedent`
2477+
/[-abc]/
2478+
`,
2479+
options: [options],
2480+
})
2481+
})
2482+
2483+
it('does not move hyphen from edge to middle', async () => {
2484+
await invalid({
2485+
errors: [
2486+
{
2487+
messageId: 'unexpectedRegExpOrder',
2488+
data: { right: 'a', left: 'z' },
2489+
},
2490+
],
2491+
output: dedent`
2492+
/[axz-]/
2493+
`,
2494+
code: dedent`
2495+
/[zxa-]/
2496+
`,
2497+
options: [options],
2498+
})
2499+
})
2500+
2501+
it('sorts character class with escaped hyphen normally', async () => {
2502+
await invalid({
2503+
errors: [
2504+
{
2505+
messageId: 'unexpectedRegExpOrder',
2506+
data: { right: 'a', left: 'z' },
2507+
},
2508+
],
2509+
output: dedent(String.raw`
2510+
/[axz\-]/
2511+
`),
2512+
code: dedent(String.raw`
2513+
/[z\-xa]/
2514+
`),
2515+
options: [options],
2516+
})
2517+
})
2518+
2519+
it('does not create accidental range from literal hyphen', async () => {
2520+
await invalid({
2521+
errors: [
2522+
{
2523+
messageId: 'unexpectedRegExpOrder',
2524+
data: { right: 'a', left: 'z' },
2525+
},
2526+
],
2527+
output: dedent`
2528+
/[az-]/
2529+
`,
2530+
code: dedent`
2531+
/[za-]/
2532+
`,
2533+
options: [options],
2534+
})
2535+
})
2536+
2537+
it('preserves range when hyphen is part of range', async () => {
2538+
await invalid({
2539+
errors: [
2540+
{
2541+
messageId: 'unexpectedRegExpOrder',
2542+
data: { right: '0-9', left: 'z' },
2543+
},
2544+
],
2545+
output: dedent`
2546+
/[0-9a-fz]/
2547+
`,
2548+
code: dedent`
2549+
/[z0-9a-f]/
2550+
`,
2551+
options: [options],
2552+
})
2553+
})
23612554
})
23622555

23632556
describe('line-length', () => {
@@ -2787,5 +2980,49 @@ describe('sort-regexp', () => {
27872980
validateRuleJsonSchema(rule.meta.schema),
27882981
).resolves.not.toThrow()
27892982
})
2983+
2984+
it('does not report when rule is disabled for entire file', async () => {
2985+
await valid({
2986+
code: dedent`
2987+
/* eslint-disable rule-to-test/sort-regexp */
2988+
/pattern/igmus
2989+
`,
2990+
})
2991+
})
2992+
2993+
it('does not report when rule is disabled for next line', async () => {
2994+
await valid({
2995+
code: dedent`
2996+
// eslint-disable-next-line rule-to-test/sort-regexp
2997+
/pattern/igmus
2998+
`,
2999+
})
3000+
})
3001+
3002+
it('does not report when rule is disabled inline', async () => {
3003+
await valid({
3004+
code: dedent`
3005+
/pattern/igmus // eslint-disable-line rule-to-test/sort-regexp
3006+
`,
3007+
})
3008+
})
3009+
3010+
it('does not report for alternatives when disabled', async () => {
3011+
await valid({
3012+
code: dedent`
3013+
/* eslint-disable rule-to-test/sort-regexp */
3014+
/(c|b|a)/
3015+
`,
3016+
})
3017+
})
3018+
3019+
it('does not report for character classes when disabled', async () => {
3020+
await valid({
3021+
code: dedent`
3022+
// eslint-disable-next-line rule-to-test/sort-regexp
3023+
/[zxa]/
3024+
`,
3025+
})
3026+
})
27903027
})
27913028
})

0 commit comments

Comments
 (0)