Skip to content

Commit 065a44f

Browse files
Add optimal-quantifier-concatenation rule (#207)
* Add `optimal-quantifier-concatenation` rule * Improved detection
1 parent 696308d commit 065a44f

File tree

6 files changed

+722
-0
lines changed

6 files changed

+722
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
139139
| [regexp/no-useless-two-nums-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-two-nums-quantifier.html) | disallow unnecessary `{n,m}` quantifier | :star::wrench: |
140140
| [regexp/no-zero-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-zero-quantifier.html) | disallow quantifiers with a maximum of zero | |
141141
| [regexp/optimal-lookaround-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/optimal-lookaround-quantifier.html) | disallow the alternatives of lookarounds that end with a non-constant quantifier | |
142+
| [regexp/optimal-quantifier-concatenation](https://ota-meshi.github.io/eslint-plugin-regexp/rules/optimal-quantifier-concatenation.html) | require optimal quantifiers for concatenated quantifiers | :wrench: |
142143
| [regexp/prefer-escape-replacement-dollar-char](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-escape-replacement-dollar-char.html) | enforces escape of replacement `$` character (`$$`). | |
143144
| [regexp/prefer-predefined-assertion](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-predefined-assertion.html) | prefer predefined assertion over equivalent lookarounds | :wrench: |
144145
| [regexp/prefer-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-quantifier.html) | enforce using quantifier | :wrench: |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
5353
| [regexp/no-useless-two-nums-quantifier](./no-useless-two-nums-quantifier.md) | disallow unnecessary `{n,m}` quantifier | :star::wrench: |
5454
| [regexp/no-zero-quantifier](./no-zero-quantifier.md) | disallow quantifiers with a maximum of zero | |
5555
| [regexp/optimal-lookaround-quantifier](./optimal-lookaround-quantifier.md) | disallow the alternatives of lookarounds that end with a non-constant quantifier | |
56+
| [regexp/optimal-quantifier-concatenation](./optimal-quantifier-concatenation.md) | require optimal quantifiers for concatenated quantifiers | :wrench: |
5657
| [regexp/prefer-escape-replacement-dollar-char](./prefer-escape-replacement-dollar-char.md) | enforces escape of replacement `$` character (`$$`). | |
5758
| [regexp/prefer-predefined-assertion](./prefer-predefined-assertion.md) | prefer predefined assertion over equivalent lookarounds | :wrench: |
5859
| [regexp/prefer-quantifier](./prefer-quantifier.md) | enforce using quantifier | :wrench: |
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "regexp/optimal-quantifier-concatenation"
5+
description: "require optimal quantifiers for concatenated quantifiers"
6+
---
7+
# regexp/optimal-quantifier-concatenation
8+
9+
> require optimal quantifiers for concatenated quantifiers
10+
11+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
13+
14+
## :book: Rule Details
15+
16+
If two quantified characters, character classes, or characters are concatenated, the quantifiers can be optimized if either of the characters elements is a subset of the other.
17+
18+
Let's take `\d+\w+` as an example. <br>
19+
This can be optimized to the equivalent pattern `\d\w+`. Not only is the optimized pattern simpler, it is also faster because the first pattern might take up to _O(n^2)_ steps to fail while the optimized pattern will fail after at most _O(n)_ steps. Generally, the optimized pattern will take less backtracking steps to fail.
20+
21+
Choosing optimal quantifiers does not only make your patterns simpler but also faster and most robust against ReDoS attacks.
22+
23+
<eslint-code-block fix>
24+
25+
```js
26+
/* eslint regexp/optimal-quantifier-concatenation: "error" */
27+
28+
/* ✓ GOOD */
29+
var foo = /\w+\d{4}/;
30+
var foo = /\w{3,5}\d*/;
31+
var foo = /a+b+c+d+[abc]+/;
32+
var foo = /a\w*/;
33+
34+
/* ✗ BAD */
35+
var foo = /\w+\d+/;
36+
var foo = /\w+\d?/;
37+
var foo = /\w?\w/;
38+
var foo = /[ab]*(?:a|b)/;
39+
var foo = /\w+(?:(a)|b)*/;
40+
```
41+
42+
</eslint-code-block>
43+
44+
## :wrench: Options
45+
46+
Nothing.
47+
48+
## :mag: Implementation
49+
50+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/optimal-quantifier-concatenation.ts)
51+
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/optimal-quantifier-concatenation.ts)

0 commit comments

Comments
 (0)