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

Add option to allow case equality when the receiver is a constant #7861

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### New features

* [#7850](https://github.com/rubocop-hq/rubocop/issues/7850): Make it possible to enable/disable pending cops. ([@koic][])
* [#7861](https://github.com/rubocop-hq/rubocop/issues/7861): Make it to allow `Style/CaseEquality` when the receiver is a constant. ([@rafaelfranca][])

### Bug fixes

Expand Down Expand Up @@ -4438,3 +4439,4 @@
[@dmolesUC]: https://github.com/dmolesUC
[@yuritomanek]: https://github.com/yuritomanek
[@egze]: https://github.com/egze
[@rafaelfranca]: https://github.com/rafaelfranca
9 changes: 9 additions & 0 deletions config/default.yml
Expand Up @@ -2417,6 +2417,15 @@ Style/CaseEquality:
StyleGuide: '#no-case-equality'
Enabled: true
VersionAdded: '0.9'
# If AllowOnConstant is enabled, the cop will ignore violations when the receiver of
# the case equality operator is a constant.
#
# # bad
# /string/ === "string"
#
# # good
# String === "string"
AllowOnConstant: false

Style/CharacterLiteral:
Description: 'Checks for uses of character literals.'
Expand Down
25 changes: 24 additions & 1 deletion lib/rubocop/cop/style/case_equality.rb
Expand Up @@ -16,14 +16,37 @@ module Style
# (1..100).include?(7)
# some_string =~ /something/
#
# @example AllowOnConstant
# # Style/CaseEquality:
# # AllowOnConstant: true
#
# # bad
# (1..100) === 7
# /something/ === some_string
#
# # good
# Array === something
# (1..100).include?(7)
# some_string =~ /something/
#
class CaseEquality < Cop
MSG = 'Avoid the use of the case equality operator `===`.'

def_node_matcher :case_equality?, '(send _ :=== _)'
def_node_matcher :case_equality?, '(send #const? :=== _)'

def on_send(node)
case_equality?(node) { add_offense(node, location: :selector) }
end

private

def const?(node)
if cop_config.fetch('AllowOnConstant', false)
!node.const_type?
else
true
end
end
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions manual/cops_style.md
Expand Up @@ -557,6 +557,27 @@ something.is_a?(Array)
(1..100).include?(7)
some_string =~ /something/
```
#### AllowOnConstant

```ruby
# Style/CaseEquality:
# AllowOnConstant: true

# bad
(1..100) === 7
/something/ === some_string

# good
Array === something
(1..100).include?(7)
some_string =~ /something/
```

### Configurable attributes

Name | Default value | Configurable values
--- | --- | ---
AllowOnConstant | `false` | Boolean

### References

Expand Down
27 changes: 26 additions & 1 deletion spec/rubocop/cop/style/case_equality_spec.rb
@@ -1,12 +1,37 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Style::CaseEquality do
subject(:cop) { described_class.new }
subject(:cop) { described_class.new(config) }

let(:config) { RuboCop::Config.new }

it 'registers an offense for ===' do
expect_offense(<<~RUBY)
Array === var
^^^ Avoid the use of the case equality operator `===`.
RUBY
end

context 'when constant checks are allowed' do
let(:config) do
RuboCop::Config.new(
'Style/CaseEquality' => {
'AllowOnConstant' => true
}
)
end

it 'does not register an offense for === when the receiver is a constant' do
expect_no_offenses(<<~RUBY)
Array === var
RUBY
end

it 'registers an offense for === when the receiver is not a constant' do
expect_offense(<<~RUBY)
/OMG/ === "OMG"
^^^ Avoid the use of the case equality operator `===`.
RUBY
end
end
end