|
| 1 | +--- |
| 2 | +pageClass: "rule-details" |
| 3 | +sidebarDepth: 0 |
| 4 | +title: "regexp/optimal-lookaround-quantifier" |
| 5 | +description: "disallow the alternatives of lookarounds that end with a non-constant quantifier" |
| 6 | +--- |
| 7 | +# regexp/optimal-lookaround-quantifier |
| 8 | + |
| 9 | +> disallow the alternatives of lookarounds that end with a non-constant quantifier |
| 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 | + |
| 13 | +## :book: Rule Details |
| 14 | + |
| 15 | +Non-constant quantifiers are quantifiers that describe a range (e.g. `?`, `*`, |
| 16 | +`+`, `{0,1}`, `{5,9}`, `{3,}`). They have to match some number of times (the |
| 17 | +minimum) after which further matches are optional until a certain maximum (may |
| 18 | +be infinite) is reached. |
| 19 | + |
| 20 | +It's obvious that `/ba{2}/` and `/ba{2,6}/` will match differently because of |
| 21 | +the different quantifiers of `a` but that not the case if for lookarounds. Both |
| 22 | +`/b(?=a{2})/` and `/b(?=a{2,6})/` will match strings the same way. I.e. for the |
| 23 | +input string `"baaa"`, both will create the same match arrays. The two regular |
| 24 | +expression are actually equivalent, meaning that `(?=a{2})` is equivalent to |
| 25 | +`(?=a{2,6})`. |
| 26 | + |
| 27 | +More generally, if a non-constant quantifier is an **end** of the expression |
| 28 | +tree of a **lookahead**, that quantifier can be replaced with a constant |
| 29 | +quantifier that matched the element minimum-if-the-non-constant-quantifier many |
| 30 | +times. For **lookbehinds**, the non-constant quantifier has to be at the |
| 31 | +**start** of the expression tree as lookbehinds are matched from right to left. |
| 32 | + |
| 33 | +<eslint-code-block> |
| 34 | + |
| 35 | +```js |
| 36 | +/* eslint regexp/optimal-lookaround-quantifier: "error" */ |
| 37 | + |
| 38 | +/* ✓ GOOD */ |
| 39 | +// lookaheads |
| 40 | +var foo = /\w+(?=\s*:)/; |
| 41 | + |
| 42 | +// lookbehinds |
| 43 | +var foo = /(?<=ab+)/; |
| 44 | + |
| 45 | +/* ✗ BAD */ |
| 46 | +// lookaheads |
| 47 | +var foo = /(?=ab+)/; // == /(?=ab)/ |
| 48 | +var foo = /(?=ab*)/; // == /(?=a)/ |
| 49 | +var foo = /(?!ab?)/; // == /(?!a)/ |
| 50 | +var foo = /(?!ab{6,})/; // == /(?!ab{6})/ |
| 51 | + |
| 52 | +// lookbehinds |
| 53 | +var foo = /(?<=a+b)/; // == /(?<=ab)/ |
| 54 | +var foo = /(?<!\w*\s*,)/; // == /(?<!,)/ |
| 55 | +``` |
| 56 | + |
| 57 | +</eslint-code-block> |
| 58 | + |
| 59 | +## :wrench: Options |
| 60 | + |
| 61 | +Nothing. |
| 62 | + |
| 63 | +## :heart: Compatibility |
| 64 | + |
| 65 | +This rule was taken from [eslint-plugin-clean-regex]. |
| 66 | +This rule is compatible with [clean-regex/optimal-lookaround-quantifier] rule. |
| 67 | + |
| 68 | +[eslint-plugin-clean-regex]: https://github.com/RunDevelopment/eslint-plugin-clean-regex |
| 69 | +[clean-regex/optimal-lookaround-quantifier]: https://github.com/RunDevelopment/eslint-plugin-clean-regex/blob/master/docs/rules/optimal-lookaround-quantifier.md |
| 70 | + |
| 71 | +## :mag: Implementation |
| 72 | + |
| 73 | +- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/optimal-lookaround-quantifier.ts) |
| 74 | +- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/optimal-lookaround-quantifier.ts) |
0 commit comments