Skip to content

Commit

Permalink
[Fix #8875] Fix incorrect autocorrect for `Style/ClassEqualityCompari…
Browse files Browse the repository at this point in the history
…son`

Fixes #8875.

Fix an incorrect auto-correct for `Style/ClassEqualityComparison` when
comparing string class name.
  • Loading branch information
koic committed Oct 9, 2020
1 parent 272771b commit 4733d70
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [#8862](https://github.com/rubocop-hq/rubocop/issues/8862): Fix an error for `Lint/AmbiguousRegexpLiteral` when using regexp without method calls in nested structure. ([@koic][])
* [#8872](https://github.com/rubocop-hq/rubocop/issues/8872): Fix an error for `Metrics/ClassLength` when multiple assignments to constants. ([@koic][])
* [#8871](https://github.com/rubocop-hq/rubocop/issues/8871): Fix a false positive for `Style/RedundantBegin` when using `begin` for method argument or part of conditions. ([@koic][])
* [#8875](https://github.com/rubocop-hq/rubocop/issues/8875): Fix an incorrect auto-correct for `Style/ClassEqualityComparison` when comparing class name. ([@koic][])

## 0.93.0 (2020-10-08)

Expand Down
13 changes: 11 additions & 2 deletions lib/rubocop/cop/style/class_equality_comparison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ def on_send(node)
return if def_node && ignored_method?(def_node.method_name)

class_comparison_candidate?(node) do |receiver_node, class_node|
range = range_between(receiver_node.loc.selector.begin_pos, node.source_range.end_pos)
range = offense_range(receiver_node, node)

add_offense(range) do |corrector|
corrector.replace(range, "instance_of?(#{class_node.source})")
class_name = class_node.source
class_name = class_name.delete('"').delete("'") if node.children.first.method?(:name)

corrector.replace(range, "instance_of?(#{class_name})")
end
end
end

private

def offense_range(receiver_node, node)
range_between(receiver_node.loc.selector.begin_pos, node.source_range.end_pos)
end
end
end
end
Expand Down
41 changes: 39 additions & 2 deletions spec/rubocop/cop/style/class_equality_comparison_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,59 @@
{ 'IgnoredMethods' => [] }
end

it 'registers an offense and corrects when comparing class for equality' do
it 'registers an offense and corrects when comparing class using `==` for equality' do
expect_offense(<<~RUBY)
var.class == Date
^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
var.instance_of?(Date)
RUBY
end

it 'registers an offense and corrects when comparing class using `equal?` for equality' do
expect_offense(<<~RUBY)
var.class.equal?(Date)
^^^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
var.instance_of?(Date)
RUBY
end

it 'registers an offense and corrects when comparing class using `eql?` for equality' do
expect_offense(<<~RUBY)
var.class.eql?(Date)
^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
var.instance_of?(Date)
RUBY
end

it 'registers an offense and corrects when comparing single quoted class name for equality' do
expect_offense(<<~RUBY)
var.class.name == 'Date'
^^^^^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
var.instance_of?(Date)
RUBY
end

it 'registers an offense and corrects when comparing class name for equality' do
it 'registers an offense and corrects when comparing double quoted class name for equality' do
expect_offense(<<~RUBY)
var.class.name == "Date"
^^^^^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
var.instance_of?(Date)
RUBY
end

it 'does not register an offense when using `instance_of?`' do
Expand Down

0 comments on commit 4733d70

Please sign in to comment.