|
| 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