Skip to content

Commit 416eafa

Browse files
Add minAlternatives option for prefer-character-class (#462)
* Add `minAlternatives` option for `prefer-character-class` * Update docs/rules/prefer-character-class.md Co-authored-by: Eric Cornelissen <[email protected]> Co-authored-by: Eric Cornelissen <[email protected]>
1 parent 201043b commit 416eafa

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

docs/rules/prefer-character-class.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,22 @@ var foo = /(\w|\d)+:/
4444

4545
## :wrench: Options
4646

47-
Nothing.
47+
```json5
48+
{
49+
"regexp/prefer-character-class": [
50+
"error",
51+
{
52+
"minAlternatives": 3
53+
}
54+
]
55+
}
56+
```
57+
58+
### `minAlternatives: integer`
59+
60+
This number controls how many character alternatives have to be present for them to be merged. By default, there need to be at least 3 alternatives.
61+
62+
Note that this option does not affect character alternatives where the characters overlap. These alternatives will always be merged to prevent excessive backtracking.
4863

4964
## :rocket: Version
5065

lib/rules/prefer-character-class.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,14 +423,28 @@ export default createRule("prefer-character-class", {
423423
recommended: true,
424424
},
425425
fixable: "code",
426-
schema: [],
426+
schema: [
427+
{
428+
type: "object",
429+
properties: {
430+
minAlternatives: {
431+
type: "integer",
432+
minimum: 2,
433+
},
434+
},
435+
additionalProperties: false,
436+
},
437+
],
427438
messages: {
428439
unexpected:
429440
"Unexpected the disjunction of single element alternatives. Use character class '[...]' instead.",
430441
},
431442
type: "suggestion", // "problem",
432443
},
433444
create(context) {
445+
const minCharacterAlternatives: number =
446+
context.options[0]?.minAlternatives ?? 3
447+
434448
/**
435449
* Create visitor
436450
*/
@@ -515,7 +529,7 @@ export default createRule("prefer-character-class", {
515529
const parsedAlts = parseRawAlts(alts, flags)
516530

517531
if (
518-
characterAltsCount >= 3 ||
532+
characterAltsCount >= minCharacterAlternatives ||
519533
containsCharacterClass(alts) ||
520534
totalIsAll(alts, regexpContext) ||
521535
findNonDisjointAlt(parsedAlts)

tests/lib/rules/prefer-character-class.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tester.run("prefer-character-class", rule as any, {
1919
String.raw`/(?:[ab]|c\b)/`,
2020
String.raw`/(?:[ab]|cd)/`,
2121
String.raw`/(?:[ab]|(c))/`,
22+
{ code: String.raw`/a|b|c|\d/`, options: [{ minAlternatives: 5 }] },
2223
],
2324
invalid: [
2425
{
@@ -285,5 +286,13 @@ tester.run("prefer-character-class", rule as any, {
285286
},
286287
],
287288
},
289+
290+
// minAlternatives option
291+
{
292+
code: String.raw`/(?:a|b)/`,
293+
output: String.raw`/(?:[ab])/`,
294+
options: [{ minAlternatives: 2 }],
295+
errors: 1,
296+
},
288297
],
289298
})

0 commit comments

Comments
 (0)