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

Support autocorrect for `Style/CaseEquality` cop #8322

Merged
merged 1 commit into from Jul 18, 2020

Conversation

@fatkodima
Copy link
Contributor

@fatkodima fatkodima commented Jul 13, 2020

No description provided.

@tejasbubane
Copy link
Contributor

@tejasbubane tejasbubane commented Jul 13, 2020

@fatkodima Since rubocop 0.88 is released, you should update VersionChanged & move changelog under master.

@fatkodima fatkodima force-pushed the fatkodima:case_equality-autocorrection branch from ea3bd14 to dfee211 Jul 13, 2020
@fatkodima
Copy link
Contributor Author

@fatkodima fatkodima commented Jul 13, 2020

Updated.

@fatkodima fatkodima force-pushed the fatkodima:case_equality-autocorrection branch from dfee211 to c6b2f86 Jul 13, 2020
@@ -29,13 +29,20 @@ module Style
# (1..100).include?(7)
# some_string =~ /something/
#
class CaseEquality < Cop
class CaseEquality < Base

This comment has been minimized.

@tejasbubane

tejasbubane Jul 14, 2020
Contributor

I think this should not be changed:

Suggested change
class CaseEquality < Base
class CaseEquality < Cop

This comment has been minimized.

@koic

koic Jul 14, 2020
Member

The auto-correction has been added by the new API, I think it is okay to extend Base. Rather, changing the inheritance to Cop will fail tests.

This comment has been minimized.

@tejasbubane

tejasbubane Jul 14, 2020
Contributor

@koic I might be a little confused between Base and Cop. I see most cops inherit from Cop, do not extend AutoCorrector and have separate method autocorrect. Which of these is the preferred/new approach?

This comment has been minimized.

@marcandre

marcandre Jul 14, 2020
Contributor

Inheriting from Base is preferred/new approach. Here's the doc: https://docs.rubocop.org/rubocop/v1_upgrade_notes.html

@bbatsov bbatsov merged commit 994fe7c into rubocop-hq:master Jul 18, 2020
26 checks passed
26 checks passed
windows 2.4
Details
windows 2.5
Details
windows 2.6
Details
windows 2.7
Details
windows mingw
Details
ci/circleci: cc-setup Your tests passed on CircleCI!
Details
ci/circleci: cc-upload-coverage Your tests passed on CircleCI!
Details
ci/circleci: documentation-checks Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-rubocop Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-spec Your tests passed on CircleCI!
Details
child = lhs.children.first
"#{lhs.source}.include?(#{rhs.source})" if child&.range_type?
when :const
"#{rhs.source}.is_a?(#{lhs.source})"

This comment has been minimized.

@knu

knu Aug 11, 2020
Contributor

Not all constants are class/module instances. Regexps are often bound to a constant, to be used with ===.

I think a better bet is to replace the expression only if the LHS constant is CamelCased.

This comment has been minimized.

@koic

koic Aug 12, 2020
Member

Thank you for your feedback. I've opened the PR #8526.

def replacement(lhs, rhs)
case lhs.type
when :regexp
"#{rhs.source} =~ #{lhs.source}"

This comment has been minimized.

@knu

knu Aug 11, 2020
Contributor

/regexp/ === object and object =~ /regexp/ are not interchangeable. The former returns true or false whereas the latter returns an index or nil.

This comment has been minimized.

@koic

koic Aug 12, 2020
Member

Exactly! I've opened the PR #8527.

koic added a commit to koic/rubocop that referenced this pull request Aug 12, 2020
Follow rubocop-hq#8322 (comment)

Fix a false positive for `Style/CaseEquality` cop when
the receiver is a non cameml 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 added a commit to koic/rubocop that referenced this pull request Aug 12, 2020
Follow rubocop-hq#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 mentioned this pull request Aug 12, 2020
7 of 8 tasks complete
koic added a commit to koic/rubocop that referenced this pull request Aug 12, 2020
Follow rubocop-hq#8322 (comment)

This PR fixes an incorrect auto-correction for `Style/CaseEquality` cop
when comparing with `===` against a regular expression receiver.

This PR auto-correct `===` with `match?` to maintain compatibility of
returning a boolean value.
koic added a commit to koic/rubocop that referenced this pull request Aug 12, 2020
Follow rubocop-hq#8322 (comment)

This PR fixes an incorrect auto-correction for `Style/CaseEquality` cop
when comparing with `===` against a regular expression receiver.

This PR auto-correct `===` with `match?` to keep compatibility of
returning a boolean value.
koic added a commit to koic/rubocop that referenced this pull request Aug 12, 2020
Follow rubocop-hq#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 added a commit to koic/rubocop that referenced this pull request Aug 12, 2020
Follow rubocop-hq#8322 (comment)

This PR prevents an incorrect auto-correction for `Style/CaseEquality` cop
when comparing with `===` against a regular expression receiver.

OTOH, the automatic correction from `a === b` to `a.match?(b)` needs to
consider `Regexp.last_match?`, `$~`, `$1`, and etc.
This correction is expected to be supported by `Performance/Regexp` cop.
See: rubocop-hq/rubocop-performance#152
koic added a commit to koic/rubocop that referenced this pull request Aug 17, 2020
Follow rubocop-hq#8322 (comment)

This PR prevents an incorrect auto-correction for `Style/CaseEquality` cop
when comparing with `===` against a regular expression receiver.

OTOH, the automatic correction from `a === b` to `a.match?(b)` needs to
consider `Regexp.last_match?`, `$~`, `$1`, and etc.
This correction is expected to be supported by `Performance/Regexp` cop.
See: rubocop-hq/rubocop-performance#152
koic added a commit to koic/rubocop that referenced this pull request Aug 17, 2020
Follow rubocop-hq#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 added a commit to koic/rubocop that referenced this pull request Aug 26, 2020
Follow rubocop-hq#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 added a commit to koic/rubocop that referenced this pull request Sep 14, 2020
Follow rubocop-hq#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)`
```
blackst0ne added a commit to blackst0ne/rubocop that referenced this pull request Sep 20, 2020
Follow rubocop-hq#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)`
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants
You can’t perform that action at this time.