Skip to content

Commit 26a3aa7

Browse files
Add regexp/no-misleading-unicode-character rule (#332)
* Add `regexp/no-misleading-unicode-character` rule * Implemented review comment * Added `fixable` option
1 parent 192455b commit 26a3aa7

File tree

8 files changed

+663
-0
lines changed

8 files changed

+663
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
114114
| [regexp/no-escape-backspace](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-escape-backspace.html) | disallow escape backspace (`[\b]`) | :star: |
115115
| [regexp/no-invalid-regexp](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-invalid-regexp.html) | disallow invalid regular expression strings in `RegExp` constructors | :star: |
116116
| [regexp/no-lazy-ends](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-lazy-ends.html) | disallow lazy quantifiers at the end of an expression | :star: |
117+
| [regexp/no-misleading-unicode-character](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-misleading-unicode-character.html) | disallow multi-code-point characters in character classes and quantifiers | :wrench: |
117118
| [regexp/no-optional-assertion](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-optional-assertion.html) | disallow optional assertions | :star: |
118119
| [regexp/no-potentially-useless-backreference](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-potentially-useless-backreference.html) | disallow backreferences that reference a group that might not be matched | :star: |
119120
| [regexp/no-super-linear-backtracking](https://ota-meshi.github.io/eslint-plugin-regexp/rules/no-super-linear-backtracking.html) | disallow exponential and polynomial backtracking | :star::wrench: |

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The rules with the following star :star: are included in the `plugin:regexp/reco
2323
| [regexp/no-escape-backspace](./no-escape-backspace.md) | disallow escape backspace (`[\b]`) | :star: |
2424
| [regexp/no-invalid-regexp](./no-invalid-regexp.md) | disallow invalid regular expression strings in `RegExp` constructors | :star: |
2525
| [regexp/no-lazy-ends](./no-lazy-ends.md) | disallow lazy quantifiers at the end of an expression | :star: |
26+
| [regexp/no-misleading-unicode-character](./no-misleading-unicode-character.md) | disallow multi-code-point characters in character classes and quantifiers | :wrench: |
2627
| [regexp/no-optional-assertion](./no-optional-assertion.md) | disallow optional assertions | :star: |
2728
| [regexp/no-potentially-useless-backreference](./no-potentially-useless-backreference.md) | disallow backreferences that reference a group that might not be matched | :star: |
2829
| [regexp/no-super-linear-backtracking](./no-super-linear-backtracking.md) | disallow exponential and polynomial backtracking | :star::wrench: |
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
pageClass: "rule-details"
3+
sidebarDepth: 0
4+
title: "regexp/no-misleading-unicode-character"
5+
description: "disallow multi-code-point characters in character classes and quantifiers"
6+
---
7+
# regexp/no-misleading-unicode-character
8+
9+
> disallow multi-code-point characters in character classes and 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+
This rule reports misleading Unicode characters.
17+
18+
Some Unicode characters like '❇️', '🏳️‍🌈', and '👨‍👩‍👦' consist of multiple code points. This causes problems in character classes and around quantifiers. E.g.
19+
20+
```js
21+
> /^[❇️🏳️‍🌈]$/.test("🏳️‍🌈")
22+
false
23+
> /^👨‍👩‍👦{2,4}$/.test("👨‍👩‍👦👨‍👩‍👦")
24+
false
25+
```
26+
27+
This rule is inspired by the [no-misleading-character-class] rule.
28+
29+
<eslint-code-block fix>
30+
31+
```js
32+
/* eslint regexp/no-misleading-unicode-character: "error" */
33+
34+
/* ✓ GOOD */
35+
var foo = /👍+/u;
36+
var foo = /👨‍👩‍👦/;
37+
38+
/* ✗ BAD */
39+
var foo = /👍+/;
40+
var foo = /[❇️🏳️‍🌈👨‍👩‍👦]❤️/;
41+
```
42+
43+
</eslint-code-block>
44+
45+
## :wrench: Options
46+
47+
```json
48+
{
49+
"regexp/no-unused-capturing-group": ["error", {
50+
"fixable": true
51+
}]
52+
}
53+
```
54+
55+
- `fixable: true | false`
56+
57+
This option controls whether the rule is fixable. Defaults to `false`.
58+
59+
This rule is not fixable by default. Misleading Unicode characters can typically be fixed automatically by assuming that users want to treat one displayed character as one regex character. However, this assumption is not useful in all languages, so this rule provides suggestions instead of fixes by default.
60+
61+
## :books: Further reading
62+
63+
- [no-misleading-character-class]
64+
65+
[no-misleading-character-class]: https://eslint.org/docs/rules/no-misleading-character-class
66+
67+
## :mag: Implementation
68+
69+
- [Rule source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/lib/rules/no-misleading-unicode-character.ts)
70+
- [Test source](https://github.com/ota-meshi/eslint-plugin-regexp/blob/master/tests/lib/rules/no-misleading-unicode-character.ts)

0 commit comments

Comments
 (0)