Skip to content
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

Fix a false positive for Style/CaseEquality cop #8526

Merged

Conversation

koic
Copy link
Member

@koic koic commented Aug 12, 2020

Follow #8322 (comment)

Fix a false positive for Style/CaseEquality cop when the receiver is not a camel cased constant.

The following constant should not be replaced with is_a?.
What kind of object is actually assigned depends on the constant. e.g.:

REGEXP_CONSTANT === something #=> does not register an offense.

This PR will only consider camel-cased constant as class name. e.g.:

Array === something #=> auto-correct to `something.is_a?(Array)`

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@@ -33,11 +33,14 @@ class CaseEquality < Base
extend AutoCorrector

MSG = 'Avoid the use of the case equality operator `===`.'
CAMEL_CASE = /[A-Z]+[a-z]+/.freeze
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this pattern should be more specific, otherwise SomeNamespace::REGXP would pass when it shouldn't. I'd suggest something like \A(?=.*[a-z])(?:[A-Z][a-z]*|::)+\z instead, but I'm not sure if that's good enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regular expression is based on the following:
https://github.com/rubocop-hq/rubocop/blob/v0.89.1/lib/rubocop/cop/style/inverse_methods.rb#L45

But the suggested regexp seems more better. I updated this PR. Thank you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that the last component of a constant name is all that matters, this pattern could work better: (\A|::)(?=.*[a-z])([A-Z][a-z0-9]*)+\z. (I also took digits into account)

In any case, class names like URI::HTTP and Addressable::URI cannot be handled nicely unless you keep a known class name list.

Copy link
Contributor

@marcandre marcandre Aug 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just do lhs.children.last.match? /[[:lower:]]/. The last child is the name of the constant without its scope. parser did all this nice work for us, let's avoid checking the source whenever we can.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should introduce a ConstNode and provide class_const?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this... rubocop/rubocop-ast#99

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcandre Thank you for creating the new API. I'd like to replace it with class_name? when new RuboCop AST is released.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koic I added it as module_name? and aliased it to class_name? 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this PR with module_name?. This PR requires the next RuboCop AST release (0.4.0?) due to its dependency on the unreleased API.

@koic koic force-pushed the fix_false_positive_for_style_case_equality branch 2 times, most recently from 94c1a9b to a65594f Compare August 17, 2020 08:11
@koic koic force-pushed the fix_false_positive_for_style_case_equality branch from a65594f to bfac3d7 Compare August 26, 2020 15:38
Follow rubocop#8322 (comment)

Fix a false positive for `Style/CaseEquality` cop when
the receiver is not a camel cased constant.

The following constant should not be replaced with `is_a?`.
What kind of object is actually assigned depends on the constant. e.g.:

```ruby
REGEXP_CONSTANT === something #=> does not register an offense.
```

This PR will only consider camel-cased constant as class name. e.g.:

```ruby
Array === something #=> auto-correct to `something.is_a?(Array)`
```
@koic koic force-pushed the fix_false_positive_for_style_case_equality branch from bfac3d7 to 252fe51 Compare September 14, 2020 01:33
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_runtime_dependency('regexp_parser', '>= 1.7')
s.add_runtime_dependency('rexml')
s.add_runtime_dependency('rubocop-ast', '>= 0.3.0', '< 1.0')
s.add_runtime_dependency('rubocop-ast', '>= 0.4.0', '< 1.0')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuboCop AST 0.4.0 has been released and I updated this one.
https://rubygems.org/gems/rubocop-ast/versions/0.4.0

@koic koic merged commit 612c176 into rubocop:master Sep 14, 2020
@koic koic deleted the fix_false_positive_for_style_case_equality branch September 14, 2020 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants