Skip to content

Commit

Permalink
Add regexp/no-invisible-character rule and `regexp/no-useless-two-n…
Browse files Browse the repository at this point in the history
…ums-quantifier` rule and refactor (#2)
  • Loading branch information
ota-meshi authored Jul 7, 2020
1 parent 7136b07 commit ca97441
Show file tree
Hide file tree
Showing 44 changed files with 948 additions and 213 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ module.exports = {

This plugin provides one config:

- `plugin:regexp/recommended` ... This is the recommended configuration for this plugin.
- `plugin:regexp/recommended` ... This is the recommended configuration for this plugin.
See [lib/configs/recommended.ts](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/configs/recommended.ts) for details.

<!--USAGE_SECTION_END-->

Expand All @@ -84,8 +85,10 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
| [regexp/no-empty-group](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-empty-group.html) | disallow empty group | :star: |
| [regexp/no-empty-lookarounds-assertion](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-empty-lookarounds-assertion.html) | disallow empty lookahead assertion or empty lookbehind assertion | :star: |
| [regexp/no-escape-backspace](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-escape-backspace.html) | disallow escape backspace (`[\b]`) | :star: |
| [regexp/no-invisible-character](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-invisible-character.html) | disallow invisible raw character | :star::wrench: |
| [regexp/no-octal](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-octal.html) | disallow octal escape sequence | :star: |
| [regexp/no-useless-exactly-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-useless-exactly-quantifier.html) | disallow unnecessary exactly quantifier | :star: |
| [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: |
| [regexp/prefer-d](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-d.html) | enforce using `\d` | :star::wrench: |
| [regexp/prefer-plus-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-plus-quantifier.html) | enforce using `+` quantifier | :star::wrench: |
| [regexp/prefer-question-quantifier](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-question-quantifier.html) | enforce using `?` quantifier | :star::wrench: |
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
| [regexp/no-empty-group](./no-empty-group.md) | disallow empty group | :star: |
| [regexp/no-empty-lookarounds-assertion](./no-empty-lookarounds-assertion.md) | disallow empty lookahead assertion or empty lookbehind assertion | :star: |
| [regexp/no-escape-backspace](./no-escape-backspace.md) | disallow escape backspace (`[\b]`) | :star: |
| [regexp/no-invisible-character](./no-invisible-character.md) | disallow invisible raw character | :star::wrench: |
| [regexp/no-octal](./no-octal.md) | disallow octal escape sequence | :star: |
| [regexp/no-useless-exactly-quantifier](./no-useless-exactly-quantifier.md) | disallow unnecessary exactly quantifier | :star: |
| [regexp/no-useless-two-nums-quantifier](./no-useless-two-nums-quantifier.md) | disallow unnecessary `{n,m}` quantifier | :star: |
| [regexp/prefer-d](./prefer-d.md) | enforce using `\d` | :star::wrench: |
| [regexp/prefer-plus-quantifier](./prefer-plus-quantifier.md) | enforce using `+` quantifier | :star::wrench: |
| [regexp/prefer-question-quantifier](./prefer-question-quantifier.md) | enforce using `?` quantifier | :star::wrench: |
Expand Down
24 changes: 12 additions & 12 deletions docs/rules/match-any.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ e.g. `[\s\S]`, `[^]`, `/./s` (dotAll) and more.
/* eslint regexp/match-any: "error" */

/* ✓ GOOD */
var foo = /[\s\S]/
var foo = /./s
var foo = /[\s\S]/;
var foo = /./s;

/* ✗ BAD */
var foo = /[\S\s]/
var foo = /[^]/
var foo = /[\d\D]/
var foo = /[\w\W]/
var foo = /[\S\s]/;
var foo = /[^]/;
var foo = /[\d\D]/;
var foo = /[\w\W]/;
```

</eslint-code-block>
Expand All @@ -55,14 +55,14 @@ var foo = /[\w\W]/
/* eslint regexp/match-any: ["error", { "allows": ["[^]"] }] */

/* ✓ GOOD */
var foo = /[^]/
var foo = /[^]/;

/* ✗ BAD */
var foo = /[\s\S]/
var foo = /[\S\s]/
var foo = /./s
var foo = /[\d\D]/
var foo = /[\w\W]/
var foo = /[\s\S]/;
var foo = /[\S\s]/;
var foo = /./s;
var foo = /[\d\D]/;
var foo = /[\w\W]/;
```

</eslint-code-block>
Expand Down
14 changes: 7 additions & 7 deletions docs/rules/no-assertion-capturing-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ This rule reports capturing group that captures assertions.
/* eslint regexp/no-assertion-capturing-group: "error" */

/* ✓ GOOD */
var foo = /(a)/
var foo = /a(?:\b)/
var foo = /a(?:$)/
var foo = /(?:^)a/
var foo = /(a)/;
var foo = /a(?:\b)/;
var foo = /a(?:$)/;
var foo = /(?:^)a/;

/* ✗ BAD */
var foo = /a(\b)/
var foo = /a($)/
var foo = /(^)a/
var foo = /a(\b)/;
var foo = /a($)/;
var foo = /(^)a/;
```

</eslint-code-block>
Expand Down
14 changes: 7 additions & 7 deletions docs/rules/no-dupe-characters-character-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: "disallow duplicate characters in the RegExp character class"
Because multiple same character classes in regular expressions only one is useful, they might be typing mistakes.

```js
var foo = /\\(\\)/
var foo = /\\(\\)/;
```

## :book: Rule Details
Expand All @@ -26,18 +26,18 @@ This rule disallows duplicate characters in the RegExp character class.
/* eslint regexp/no-dupe-characters-character-class: "error" */

/* ✓ GOOD */
var foo = /[\(\)]/
var foo = /[\(\)]/;

var foo = /[a-z\s]/
var foo = /[a-z\s]/;

var foo = /[\w]/
var foo = /[\w]/;

/* ✗ BAD */
var foo = /[\\(\\)]/
var foo = /[\\(\\)]/;
// ^^ ^^ "\\" are duplicated
var foo = /[a-z\\s]/
var foo = /[a-z\\s]/;
// ^^^ ^ "s" are duplicated
var foo = /[\w0-9]/
var foo = /[\w0-9]/;
// ^^^^^ "0-9" are duplicated
```

Expand Down
12 changes: 6 additions & 6 deletions docs/rules/no-empty-group.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ This rule reports empty groups.
/* eslint regexp/no-empty-group: "error" */

/* ✓ GOOD */
var foo = /(a)/
var foo = /(?:a)/
var foo = /(a)/;
var foo = /(?:a)/;

/* ✗ BAD */
// capturing group
var foo = /()/
var foo = /(|)/
var foo = /()/;
var foo = /(|)/;
// non-capturing group
var foo = /(?:)/
var foo = /(?:|)/
var foo = /(?:)/;
var foo = /(?:|)/;
```

</eslint-code-block>
Expand Down
16 changes: 8 additions & 8 deletions docs/rules/no-empty-lookarounds-assertion.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ This rule reports empty lookahead assertion or empty lookbehind assertion.
/* eslint regexp/no-empty-lookarounds-assertion: "error" */

/* ✓ GOOD */
var foo = /x(?=y)/
var foo = /x(?!y)/
var foo = /(?<=y)x/
var foo = /(?<!y)x/
var foo = /x(?=y)/;
var foo = /x(?!y)/;
var foo = /(?<=y)x/;
var foo = /(?<!y)x/;

/* ✗ BAD */
var foo = /x(?=)/
var foo = /x(?!)/
var foo = /(?<=)x/
var foo = /(?<!)x/
var foo = /x(?=)/;
var foo = /x(?!)/;
var foo = /(?<=)x/;
var foo = /(?<!)x/;
```

</eslint-code-block>
Expand Down
10 changes: 5 additions & 5 deletions docs/rules/no-escape-backspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ The word boundaries (`\b`) and the escape backspace (`[\b]`) are indistinguishab
/* eslint regexp/no-escape-backspace: "error" */

/* ✓ GOOD */
var foo = /\b/
var foo = /\u0008/
var foo = /\cH/
var foo = /\x08/
var foo = /\b/;
var foo = /\u0008/;
var foo = /\cH/;
var foo = /\x08/;

/* ✗ BAD */
var foo = /[\b]/
var foo = /[\b]/;
```

</eslint-code-block>
Expand Down
46 changes: 46 additions & 0 deletions docs/rules/no-invisible-character.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "regexp/no-invisible-character"
description: "disallow invisible raw character"
---
# regexp/no-invisible-character

> disallow invisible raw character
- :gear: This rule is included in `"plugin:regexp/recommended"`.
- :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.

## :book: Rule Details

This rule disallows using invisible characters other than SPACE (`U+0020`) without using escapes.

<eslint-code-block fix>

```js
/* eslint regexp/no-invisible-character: "error" */

/* ✓ GOOD */
var foo = /\t/;
var foo = /\v/;
var foo = /\f/;
var foo = /\u3000/;
var foo = / /; // SPACE (`U+0020`)

/* ✗ BAD */
var foo = / /;
var foo = / /;
var foo = / /;
var foo = / /;
```

</eslint-code-block>

## :wrench: Options

Nothing.

## Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/no-invisible-character.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/no-invisible-character.js)
6 changes: 3 additions & 3 deletions docs/rules/no-octal.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ This rule reports octal escape.
/* eslint regexp/no-octal: "error" */

/* ✓ GOOD */
var foo = /\0/
var foo = /=/
var foo = /\0/;
var foo = /=/;

/* ✗ BAD */
var foo = /\075/
var foo = /\075/;
```

</eslint-code-block>
Expand Down
6 changes: 3 additions & 3 deletions docs/rules/no-useless-exactly-quantifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ This rule reports `{0}` or `{1}` quantifiers.
/* eslint regexp/no-useless-exactly-quantifier: "error" */

/* ✓ GOOD */
var foo = /a/
var foo = /a/;

/* ✗ BAD */
var foo = /a{1}/
var foo = /a{0}/
var foo = /a{1}/;
var foo = /a{0}/;
```

</eslint-code-block>
Expand Down
44 changes: 44 additions & 0 deletions docs/rules/no-useless-two-nums-quantifier.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "regexp/no-useless-two-nums-quantifier"
description: "disallow unnecessary `{n,m}` quantifier"
---
# regexp/no-useless-two-nums-quantifier

> disallow unnecessary `{n,m}` quantifier
- :gear: This rule is included in `"plugin:regexp/recommended"`.

## :book: Rule Details

This rule reports unnecessary `{n,m}` quantifiers.

<eslint-code-block >

```js
/* eslint regexp/no-useless-two-nums-quantifier: "error" */

/* ✓ GOOD */
var foo = /a{0,1}/;
var foo = /a{1,5}/;
var foo = /a{1,}/;
var foo = /a{2}/;


/* ✗ BAD */
var foo = /a{0,0}/;
var foo = /a{1,1}/;
var foo = /a{2,2}/;
```

</eslint-code-block>

## :wrench: Options

Nothing.

## Implementation

- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/no-useless-two-nums-quantifier.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/no-useless-two-nums-quantifier.js)
8 changes: 4 additions & 4 deletions docs/rules/prefer-d.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ This rule is aimed at using `\d` instead of `[0-9]` in regular expressions.
/* eslint regexp/prefer-d: "error" */

/* ✓ GOOD */
var foo = /\d/
var foo = /\D/
var foo = /\d/;
var foo = /\D/;

/* ✗ BAD */
var foo = /[0-9]/
var foo = /[^0-9]/
var foo = /[0-9]/;
var foo = /[^0-9]/;
```

</eslint-code-block>
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/prefer-plus-quantifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ This rule is aimed at using `+` quantifier instead of `{1,}` in regular expressi
/* eslint regexp/prefer-plus-quantifier: "error" */

/* ✓ GOOD */
var foo = /a+/
var foo = /a+/;

/* ✗ BAD */
var foo = /a{1,}/
var foo = /a{1,}/;
```

</eslint-code-block>
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/prefer-question-quantifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ This rule is aimed at using `?` quantifier instead of `{0,1}` in regular express
/* eslint regexp/prefer-question-quantifier: "error" */

/* ✓ GOOD */
var foo = /a?/
var foo = /a?/;

/* ✗ BAD */
var foo = /a{0,1}/
var foo = /a{0,1}/;
```

</eslint-code-block>
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/prefer-star-quantifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This rule is aimed at using `*` quantifier instead of `{0,}` in regular expressi
var foo = /a*/

/* ✗ BAD */
var foo = /a{0,}/
var foo = /a{0,}/;
```

</eslint-code-block>
Expand Down
4 changes: 2 additions & 2 deletions docs/rules/prefer-t.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ This rule is aimed at using `\t` in regular expressions.
/* eslint regexp/prefer-t: "error" */

/* ✓ GOOD */
var foo = /\t/
var foo = /\t/;

/* ✗ BAD */
var foo = /\u0009/
var foo = /\u0009/;
```

</eslint-code-block>
Expand Down
Loading

0 comments on commit ca97441

Please sign in to comment.