Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minAlternatives option for prefer-character-class #462

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion docs/rules/prefer-character-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,22 @@ var foo = /(\w|\d)+:/

## :wrench: Options

Nothing.
```json5
{
"regexp/prefer-character-class": [
"error",
{
"minAlternatives": 3
}
]
}
```

### `minAlternatives: integer`

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.

Note that this option does not affect character alternatives where the characters overlap. These alternatives will always be merged to prevent excessive backtracking.

## :rocket: Version

Expand Down
18 changes: 16 additions & 2 deletions lib/rules/prefer-character-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,28 @@ export default createRule("prefer-character-class", {
recommended: true,
},
fixable: "code",
schema: [],
schema: [
{
type: "object",
properties: {
minAlternatives: {
type: "integer",
minimum: 2,
},
},
additionalProperties: false,
},
],
messages: {
unexpected:
"Unexpected the disjunction of single element alternatives. Use character class '[...]' instead.",
},
type: "suggestion", // "problem",
},
create(context) {
const minCharacterAlternatives: number =
context.options[0]?.minAlternatives ?? 3

/**
* Create visitor
*/
Expand Down Expand Up @@ -515,7 +529,7 @@ export default createRule("prefer-character-class", {
const parsedAlts = parseRawAlts(alts, flags)

if (
characterAltsCount >= 3 ||
characterAltsCount >= minCharacterAlternatives ||
containsCharacterClass(alts) ||
totalIsAll(alts, regexpContext) ||
findNonDisjointAlt(parsedAlts)
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/prefer-character-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tester.run("prefer-character-class", rule as any, {
String.raw`/(?:[ab]|c\b)/`,
String.raw`/(?:[ab]|cd)/`,
String.raw`/(?:[ab]|(c))/`,
{ code: String.raw`/a|b|c|\d/`, options: [{ minAlternatives: 5 }] },
],
invalid: [
{
Expand Down Expand Up @@ -285,5 +286,13 @@ tester.run("prefer-character-class", rule as any, {
},
],
},

// minAlternatives option
{
code: String.raw`/(?:a|b)/`,
output: String.raw`/(?:[ab])/`,
options: [{ minAlternatives: 2 }],
errors: 1,
},
],
})