-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new MixedCaseRange cop #11561
Add new MixedCaseRange cop #11561
Conversation
found by the new Lint/UnsafeRange cop.
b538b5e
to
9fa2fac
Compare
te is defined as ts + length plus te angers the spellchecker
I guess that's fine. I also regret not having a Regexp department at this point, as there are quite a few cops specific to regexps.
Perhaps we should mention
I've been wondering about this myself (the other ranges that might be commonly used) There's also the broader point that
We'll need to support parsing Ruby 2.7 code at least until RuboCop 2 is released. Then the parser will likely target only Ruby 3+. |
The cop handles |
Indeed, there may be an opportunity here to promote better regular expression usage here as well. I'd love to suggest that people use I was thinking more about this over the weekend and wondering if a better approach would be to move this functionality into some sort of helper and make specific cops... Then we could have one simple, explicit cop for mixing the A-Z and a-z ranges (with specific, helpful messages), and if anyone comes up with a use case or likelihood for mixing any other ranges we could add another cop then (with another specific message). |
Yeah, probably this will work better in practice. |
JFYI, regexp_parser 2.7 is not Ruby 2.7 :-) Support for regexp_parser 1 and regexp_parser 2 was previously discussed on #9162 and related PRs. |
@koic My bad! Once again I misread a message. 😅 |
So, how about we agree to:
@rwstauner @koic How does this sound? |
yeah, that's basically what i was thinking. i'm sorry i haven't gotten back to this. it was a hackdays project and i've been busier than usual at work since then. I should be able to devote a little time to finish this up in a few weeks. |
Remove number range as that is considerably less likely to be used and makes writing helpful messages more difficult.
Consuming classes don't need to care that it might be a compound expression. Instead return an object with the same interface as you'd get from Regexp::Parser.
f8186bd
to
7e698fa
Compare
I reworked this a bit to abstract and simplify it and updated the PR description. |
7e698fa
to
cef3f4f
Compare
Looks good to me in its current simpler form. Sorry for the slow turnaround on this! |
Follow up rubocop/rubocop#11561. ```console $ bundle exec rake (snip) Offenses: lib/rubocop/cop/rails/time_zone.rb:58:33: W: [Correctable] Lint/MixedCaseRange: Ranges from upper to lower case ASCII letters may include unintended characters. Instead of A-z (which also includes several symbols) specify each range individually: A-Za-z and individually specify any symbols. TIMEZONE_SPECIFIER = /([A-z]|[+-]\d{2}:?\d{2})\z/.freeze ^^^ 281 files inspected, 1 offense detected, 1 offense autocorrectable ```
No worries, i was busy and slow, too. Thanks for your help! |
I saw several instances of regexp character classes like
[A-z]
which are misleading (there are several unintended symbols betweenZ
anda
), so I thought it was worth making a cop for it.After building the cop it found a few occurrences in this codebase :)
There is some extra complexity here to reconstruct octal escape sequences for regexp_parser < 2.7. If rubocop required 2.7+ that code could be simplified.
I tested the new cop with Regexp::Parser versions 2.6.2 and 2.8.0 and they both passed.
What do you think about the MSG?
Do you want me to squash all these commits down to one?
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.