@putout/plugin-regexp
Version:
πPutout plugin helps with regexp
252 lines (171 loc) β’ 6.26 kB
Markdown
# @putout/plugin-regexp [![NPM version][NPMIMGURL]][NPMURL]
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-regexp.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-regexp"npm"
> Regular expressions are patterns used to match character combinations in strings.
>
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)
π[**Putout**](https://github.com/coderaiser/putout) plugin helps with **Regular Expressions**.
## Install
```
npm i @putout/plugin-regexp -D
```
## Rules
- β
[apply-character-class](#apply-character-class);
- β
[apply-ends-with](#apply-ends-with);
- β
[apply-global-regexp-to-replace-al](#apply-global-regexp-to-replace-all);
- β
[apply-literal-notation](#apply-literal-notation);
- β
[apply-starts-with](#apply-starts-with);
- β
[convert-replace-to-replace-all](#convert-replace-to-replace-all);
- β
[convert-to-string](#convert-to-string);
- β
[optimize](#optimize);
- β
[remove-useless-group](#remove-useless-group);
- β
[remove-useless-regexp](#remove-useless-regexp);
- β
[remove-duplicates-from-character-class](#remove-duplicates-from-character-class);
## Config
```json
{
"rules": {
"regexp/apply-character-class": "on",
"regexp/apply-global-regexp-to-replace-all": "on",
"regexp/apply-literal-notation": "on",
"regexp/apply-starts-with": "on",
"regexp/apply-ends-with": "on",
"regexp/optimize": "on",
"regexp/convert-to-string": "on",
"regexp/convert-replace-to-replace-all": "on",
"regexp/remove-useless-group": "on",
"regexp/remove-useless-regexp": "on",
"regexp/remove-duplicates-from-character-class": "on"
}
}
```
## optimize
### β Example of incorrect code
```js
const a = /(ab|ab)/;
```
### β
Example of correct code
```js
const a = /(ab)/;
```
## apply-character-class
Checkout in:
- π[Putout Editor](https://putout.cloudcmd.io/#/gist/af6302761114152dee215625ff411825/fb2a0589b29d80a7c76fb05eb9ace66511a5e8bb).
- [AST Explorer](https://astexplorer.net/#/gist/86288d07002d1151c7eac529c0b85f1e/ad57c586193f19e56d07b1d18725ec969388dde2).
### β Example of incorrect code
```js
/\)|\(/g;
```
### β
Example of correct code
```js
/[)(]/g;
```
## apply-global-regexp-to-replace-all
> `Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument`
>
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Requires_global_RegExp)
Checkout in π[**Putout Editor**](https://putout.cloudcmd.io/#/gist/8c759e3c8d19d660ddb01bda04d75c8b/f1d0f3a6d491f034308f5f3a375900b0a620a3b3).
### β Example of incorrect code
```js
's'.replaceAll(/hello/, 's');
'abc'.matchAll(/./);
```
### β
Example of correct code
```js
's'.replaceAll(/hello/g, 's');
'abc'.matchAll(/./g);
```
## apply-literal-notation
### β Example of incorrect code
```js
const a = new RegExp('hello', 'i');
```
### β
Example of correct code
```js
const a = /hello/i;
```
## apply-starts-with
> The `startsWith()` method determines whether a string begins with the characters of a specified string, returning `true` or `false` as appropriate.
>
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith)
**RegExp** is overkill for such a simple task as determining that string located at the beginning. Check it out in π [**Putout Editor**](https://putout.cloudcmd.io/#/gist/79e3e41c3491fb8c45fb03580e42ef20/89971d2d6528ee78df4c2a0d6271179560f76cc1).
### β Example of incorrect code
```js
/^hello/.test(a);
```
### β
Example of correct code
```js
a.startsWith('hello');
```
### Comparison
Linter | Rule | Fix
--------|-------|------------|
π **Putout**| [`regexp/apply-starts-with`](https://github.com/coderaiser/putout/tree/master/packages/plugin-regexp#apply-starts-with)| β
π¦ **TypeScript ESLint** | [`prefer-string-starts-ends-with`](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/prefer-string-starts-ends-with.md#prefer-string-starts-ends-with) | β
## apply-ends-with
> The `startsWith()` method determines whether a string ends with the characters of a specified string, returning `true` or `false` as appropriate.
>
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith)
**RegExp** is overkill for such a simple task as determining that string located at the end.
### β Example of incorrect code
```js
/hello$/.test(a);
```
### β
Example of correct code
```js
a.endsWith('hello');
```
### Comparison
Linter | Rule | Fix
--------|-------|------------|
π **Putout**| [`regexp/apply-ends-with`](https://github.com/coderaiser/putout/tree/master/packages/plugin-regexp#apply-ends-with)| β
π¦ **TypeScript ESLint** | [`prefer-string-starts-ends-with`](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/prefer-string-starts-ends-with.md#prefer-string-starts-ends-with) | β
## convert-to-string
### β Example of incorrect code
```js
'hello'.replace(/hello/, 'world');
```
### β
Example of correct code
```js
'hello'.replace('hello', 'world');
```
## convert-replace-to-replace-all
Simplify code according to [string-replace-all](https://github.com/tc39/proposal-string-replaceall).
### β Example of incorrect code
```js
'hello'.replace(/hello/g, 'world');
```
### β
Example of correct code
```js
'hello'.replaceAll('hello', 'world');
```
## remove-useless-group
### β Example of incorrect code
```js
/(hello)/.test(str);
```
### β
Example of correct code
```js
/hello/.test(str);
```
## remove-useless-regexp
### β Example of incorrect code
```js
const a = /^\.hello$/.test(str);
```
### β
Example of correct code
```js
const a = str === '.hello';
```
## remove-duplicates-from-character-class
Checkout in [AST Explorer](https://astexplorer.net/#/gist/634783e99f6a9432e375ac8ad96647d9/a889735af932bd9f88f953596bec80d3a714415f).
### β Example of incorrect code
```js
/[aaabb]/.test(str);
```
### β
Example of correct code
```js
/[ab]/.test(str);
```
## License
MIT