Skip to content

Commit 437b8ab

Browse files
Add prefer-lookaround rule (#324)
* Add prefer-lookaround rule * Update lib/rules/prefer-lookaround.ts Co-authored-by: Michael Schmidt <[email protected]> * fix prefer-lookaround * Update prefer-lookaround * Update prefer-lookaround * Add check to if it consume character * Improve `prefer-lookaround` (#328) * fix autofix of end capturing group Co-authored-by: Michael Schmidt <[email protected]>
1 parent 6e07030 commit 437b8ab

File tree

9 files changed

+1209
-35
lines changed

9 files changed

+1209
-35
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
167167
| [regexp/no-useless-non-capturing-group](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-non-capturing-group.html) | disallow unnecessary Non-capturing group | :star::wrench: |
168168
| [regexp/prefer-character-class](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-character-class.html) | enforce using character class | :star::wrench: |
169169
| [regexp/prefer-d](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-d.html) | enforce using `\d` | :star::wrench: |
170+
| [regexp/prefer-lookaround](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-lookaround.html) | prefer lookarounds over capturing group that do not replace | :wrench: |
170171
| [regexp/prefer-named-backreference](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-named-backreference.html) | enforce using named backreferences | :wrench: |
171172
| [regexp/prefer-plus-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-plus-quantifier.html) | enforce using `+` quantifier | :star::wrench: |
172173
| [regexp/prefer-question-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-question-quantifier.html) | enforce using `?` quantifier | :star::wrench: |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
7676
| [regexp/no-useless-non-capturing-group](./no-useless-non-capturing-group.md) | disallow unnecessary Non-capturing group | :star::wrench: |
7777
| [regexp/prefer-character-class](./prefer-character-class.md) | enforce using character class | :star::wrench: |
7878
| [regexp/prefer-d](./prefer-d.md) | enforce using `\d` | :star::wrench: |
79+
| [regexp/prefer-lookaround](./prefer-lookaround.md) | prefer lookarounds over capturing group that do not replace | :wrench: |
7980
| [regexp/prefer-named-backreference](./prefer-named-backreference.md) | enforce using named backreferences | :wrench: |
8081
| [regexp/prefer-plus-quantifier](./prefer-plus-quantifier.md) | enforce using `+` quantifier | :star::wrench: |
8182
| [regexp/prefer-question-quantifier](./prefer-question-quantifier.md) | enforce using `?` quantifier | :star::wrench: |

docs/rules/prefer-lookaround.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "regexp/prefer-lookaround"
5+
description: "prefer lookarounds over capturing group that do not replace"
6+
---
7+
# regexp/prefer-lookaround
8+
9+
> prefer lookarounds over capturing group that do not replace
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+
This rule reports string replacement using capturing groups that can be replaced with lookaround assertions.
17+
18+
<eslint-code-block fix>
19+
20+
```js
21+
/* eslint regexp/prefer-lookaround: "error" */
22+
23+
/* ✓ GOOD */
24+
var str = 'JavaScript'.replace(/Java(?=Script)/g, 'Type')
25+
26+
/* ✗ BAD */
27+
var str = 'JavaScript'.replace(/Java(Script)/g, 'Type$1')
28+
```
29+
30+
</eslint-code-block>
31+
32+
## :wrench: Options
33+
34+
```json
35+
{
36+
"regexp/prefer-lookaround": ["error", {
37+
"strictTypes": true
38+
}]
39+
}
40+
```
41+
42+
- `strictTypes` ... If `true`, strictly check the type of object to determine if the regex instance was used in `replace()` and `replaceAll()`. Default is `true`.
43+
This option is always on when using TypeScript.
44+
45+
## :mag: Implementation
46+
47+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/prefer-lookaround.ts)
48+
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/prefer-lookaround.ts)

0 commit comments

Comments
 (0)