Skip to content

Commit 02ebe7c

Browse files
Increase range threshold in regexp/prefer-range (#360)
* Increase range threshold in `regexp/prefer-range` * Updated documentation
1 parent 2bd0af5 commit 02ebe7c

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

docs/rules/prefer-range.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ This rule is aimed to use ranges instead of multiple adjacent characters in char
2222
/* eslint regexp/prefer-range: "error" */
2323

2424
/* ✓ GOOD */
25-
var foo = /[a-c]/
25+
var foo = /[0-9]/
2626
var foo = /[a-f]/
2727

2828
/* ✗ BAD */
29-
var foo = /[abc]/
29+
var foo = /[123456]/
3030
var foo = /[a-cd-f]/
3131
```
3232

@@ -58,15 +58,15 @@ It allows all values that the [allowedCharacterRanges] setting allows.
5858
/* eslint regexp/prefer-range: ["error", { "target": "alphanumeric" }] */
5959

6060
/* ✓ GOOD */
61-
var foo = /[a-c]/
61+
var foo = /[0-9]/
6262
var foo = /[a-f]/
6363
var foo = /[!-$]/
6464
var foo = /[!"#$]/
6565
var foo = /[😀-😄]/u
6666
var foo = /[😀😁😂😃😄]/u
6767

6868
/* ✗ BAD */
69-
var foo = /[abc]/
69+
var foo = /[123456]/
7070
var foo = /[a-cd-f]/
7171
```
7272

@@ -80,13 +80,13 @@ var foo = /[a-cd-f]/
8080
/* eslint regexp/prefer-range: ["error", { "target": "all" }] */
8181

8282
/* ✓ GOOD */
83-
var foo = /[a-c]/
83+
var foo = /[0-9]/
8484
var foo = /[a-f]/
8585
var foo = /[!-$]/
8686
var foo = /[😀-😄]/u
8787

8888
/* ✗ BAD */
89-
var foo = /[abc]/
89+
var foo = /[123456]/
9090
var foo = /[a-cd-f]/
9191
var foo = /[!"#$]/
9292
var foo = /[😀😁😂😃😄]/u
@@ -102,14 +102,14 @@ var foo = /[😀😁😂😃😄]/u
102102
/* eslint regexp/prefer-range: ["error", { "target": [ "alphanumeric", "😀-😏" ] }] */
103103

104104
/* ✓ GOOD */
105-
var foo = /[a-c]/
105+
var foo = /[0-9]/
106106
var foo = /[a-f]/
107107
var foo = /[!-$]/
108108
var foo = /[!"#$]/
109109
var foo = /[😀-😄]/u
110110

111111
/* ✗ BAD */
112-
var foo = /[abc]/
112+
var foo = /[123456]/
113113
var foo = /[a-cd-f]/
114114
var foo = /[😀😁😂😃😄]/u
115115
```

lib/rules/prefer-range.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,8 @@ export default createRule("prefer-range", {
148148
}
149149

150150
for (const group of groups) {
151-
if (
152-
group.max.value - group.min.value > 1 &&
153-
group.nodes.length > 1
154-
) {
151+
const charCount = group.max.value - group.min.value + 1
152+
if (charCount >= 4 && group.nodes.length > 1) {
155153
const newText = `${group.min.raw}-${group.max.raw}`
156154
const ranges = getReportRanges(group.nodes)
157155
if (!ranges) {

tests/lib/rules/prefer-range.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ tester.run("prefer-range", rule as any, {
1212
valid: [
1313
`/[a]/`,
1414
`/[ab]/`,
15-
`/[a-c]/`,
15+
`/[abc]/`,
1616
`/[a-b]/`,
17+
`/[a-c]/`,
18+
`/[a-d]/`,
1719
`/[0-9]/`,
1820
`/[A-Z]/`,
1921
`/[a-zA-ZZ-a]/`,
@@ -59,25 +61,25 @@ tester.run("prefer-range", rule as any, {
5961
],
6062
invalid: [
6163
{
62-
code: `/[abc]/`,
63-
output: `/[a-c]/`,
64+
code: `/[abcd]/`,
65+
output: `/[a-d]/`,
6466
errors: [
6567
{
6668
message:
67-
"Unexpected multiple adjacent characters. Use 'a-c' instead.",
69+
"Unexpected multiple adjacent characters. Use 'a-d' instead.",
6870
line: 1,
6971
column: 3,
7072
endLine: 1,
71-
endColumn: 6,
73+
endColumn: 7,
7274
},
7375
],
7476
},
7577
{
76-
code: `/[ABC abc]/`,
77-
output: `/[A-C a-c]/`,
78+
code: `/[ABCD abcd]/`,
79+
output: `/[A-D a-d]/`,
7880
errors: [
79-
"Unexpected multiple adjacent characters. Use 'A-C' instead.",
80-
"Unexpected multiple adjacent characters. Use 'a-c' instead.",
81+
"Unexpected multiple adjacent characters. Use 'A-D' instead.",
82+
"Unexpected multiple adjacent characters. Use 'a-d' instead.",
8183
],
8284
},
8385
{

0 commit comments

Comments
 (0)