ota-meshi / eslint-plugin-regexp

ESLint plugin for finding regex mistakes and style guide violations.

Home Page:https://ota-meshi.github.io/eslint-plugin-regexp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Report 2 single-character alternative groups with regexp/prefer-character-class

ericcornelissen opened this issue · comments

Description

I noticed that the regexp/prefer-character-class rule does not report on groups with 2 single-character alternatives, e.g. (?:a|b). I would expect it do this, I didn't spot any justification for not reporting these in the docs (if there is, please let me know 🙂)

Alternatively (and/or to avoid breaking compatibility), a solution could be to add a numeric option to the rule to configure the minimum number of single-character alternatives for which the rule should report. The default for this option would then probably be 3 (same as the current behavior), and could be configured to 2 (or less) if desired.

I added an option for this in #462.

I didn't spot any justification

Mostly reducing noise. I test all rules on Prism's 2k+ regexes, and there are a lot of regexes with long disjunctions that just so happen to contain 2 single-character alternatives. Merging those doesn't really help readability (and sometimes even reduces it). So I chose 3 as the minimum so that the rule doesn't make unnecessary changes.

2 (or less)

There won't be a less. There have to be at least 2 alternatives so that we can merge them into a single character class.

There won't be a less. There have to be at least 2 alternatives so that we can merge them into a single character class.

Of course, you're right. By "or less" I meant that if the option is set to, e.g., 0, the rule still works and reports on all groups with n > 0 - logically starting at 2 in practice - single-character alternatives. That being said, requiring the option to be bigger than 2 also makes sense to avoid potential confusion.

Not necessarily. If I set it to minAlternatvies: 1, I would actually expect the rule to transform /a|bc/ to /[a]|bc/. Of course, this makes no sense from a practical point of view, it is something this setting would allow strictly speaking.

However, there's also a more practical reason to require at least 2: the implementation already makes optimizations with the assumption that minAlternatives is >= 2.