Skip to content

Commit 97653b7

Browse files
Add option for capturing groups to regexp/optimal-quantifier-concatenation (#452)
* Add option for capturing groups to `regexp/optimal-quantifier-concatenation` * Fixed tests * FIxed typo * Removed extra empty line
1 parent 60c62a7 commit 97653b7

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

docs/rules/optimal-quantifier-concatenation.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,36 @@ var foo = /\w+(?:(a)|b)*/;
4343

4444
## :wrench: Options
4545

46-
Nothing.
46+
```json5
47+
{
48+
"regexp/optimal-quantifier-concatenation": [
49+
"error",
50+
{
51+
"capturingGroups": "report"
52+
}
53+
]
54+
}
55+
```
56+
57+
### `capturingGroups`
58+
59+
The type of concatenation this rule reports might be intentional around capturing groups. This option allows you turn of false unfixable reports around capturing groups.
60+
61+
- `capturingGroups: "report"` (_default_)
62+
63+
Concatenations around quantifiers will be reported.
64+
65+
- `capturingGroups: "ignore"`
66+
67+
Concatenations around quantifiers will not be reported.
68+
69+
If this option is used, it is recommend to have the [regexp/no-super-linear-backtracking] rule enabled to protect against ReDoS.
70+
71+
## :books: Further reading
72+
73+
- [regexp/no-super-linear-backtracking]
74+
75+
[regexp/no-super-linear-backtracking]: ./no-super-linear-backtracking.md
4776

4877
## :rocket: Version
4978

lib/rules/optimal-quantifier-concatenation.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ function getLoc(
499499
})
500500
}
501501

502+
const enum CapturingGroupReporting {
503+
ignore = "ignore",
504+
report = "report",
505+
}
506+
502507
export default createRule("optimal-quantifier-concatenation", {
503508
meta: {
504509
docs: {
@@ -508,7 +513,17 @@ export default createRule("optimal-quantifier-concatenation", {
508513
recommended: true,
509514
},
510515
fixable: "code",
511-
schema: [],
516+
schema: [
517+
{
518+
type: "object",
519+
properties: {
520+
capturingGroups: {
521+
enum: ["ignore", "report"],
522+
},
523+
},
524+
additionalProperties: false,
525+
},
526+
],
512527
messages: {
513528
combine:
514529
"{{left}} and {{right}} can be combined into one quantifier {{fix}}.{{cap}}",
@@ -526,6 +541,10 @@ export default createRule("optimal-quantifier-concatenation", {
526541
type: "suggestion",
527542
},
528543
create(context) {
544+
const cgReporting: CapturingGroupReporting =
545+
context.options[0]?.capturingGroups ??
546+
CapturingGroupReporting.report
547+
529548
/**
530549
* Creates a visitor
531550
*/
@@ -548,6 +567,12 @@ export default createRule("optimal-quantifier-concatenation", {
548567

549568
const involvesCapturingGroup =
550569
hasCapturingGroup(left) || hasCapturingGroup(right)
570+
if (
571+
involvesCapturingGroup &&
572+
cgReporting === CapturingGroupReporting.ignore
573+
) {
574+
continue
575+
}
551576

552577
const cap = involvesCapturingGroup
553578
? " This cannot be fixed automatically because it might change or remove a capturing group."

tests/lib/rules/optimal-quantifier-concatenation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ tester.run("optimal-quantifier-concatenation", rule as any, {
1919
String.raw`/\d+(?:\w+|-\d+)/`,
2020
String.raw`/aa?/`,
2121
String.raw`/\w?\w/`,
22+
{
23+
code: String.raw`/(\d)\d+/`,
24+
options: [{ capturingGroups: "ignore" }],
25+
},
2226
],
2327
invalid: [
2428
{

0 commit comments

Comments
 (0)