Bug: no-misleading-character-class triggers on seemingly-OK code
See original GitHub issueEnvironment
Node version: 16.9.1 npm version: 7.21.1 Local ESLint version: 7.32.0 Global ESLint version: (none) Operating System: Windows 10
What parser are you using?
Default (Espree)
What did you do?
Configuration
Just using eslint:recommended. This can be demoed on eslint.org
/[\u00B7\u0300-\u036F]/u.test("");
What did you expect to happen?
No error, if I am understanding https://eslint.org/docs/rules/no-misleading-character-class correctly. Those docs imply the error will trigger when I include in my source text a multi-code-point character, which I have not done; I’ve only used Unicode escapes.
What actually happened?
4:3 error Unexpected combined character in character class no-misleading-character-class
Participation
- I am willing to submit a pull request for this issue.
Additional comments
For context, I am trying to reproduce https://www.w3.org/TR/REC-xml/#NT-NameStartChar as a RegExp. The #xB7 | [#x0300-#x036F]
part of the grammar is what maps to my [\u00B7\u0300-\u036F]
character class.
It’s possible that this is a doc issue, as I’m actually doing something dangerous or unusual, and so the error should trigger? But if I am, it’s not clear at all from the docs what I’m doing that’s bad.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Marking as accepted because there is agreement on adding a new option for this.
Thanks for the research! Hmm.
It would be nice if there were a version of this rule that did what the documentation currently says, which is prevent actually-misleading cases where the result appears as a signal grapheme. That is, taking an example from the doc: I think
/^[❇️]$/u
is indeed misleading because it looks like one character. But/^[\u2747\uFE0F]$/u
is, in my opinion, not misleading, because it’s pretty clear that there are two possible code points, of which only one will be matched. (This is despite the fact that the two regexps are equivalent semantically.)In other words, it would be good to have a mode for this rule that exempts escape sequences.