Skip to content

Commit

Permalink
Tweak the offense message for Style/ClassEqualityComparison
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Oct 9, 2020
1 parent 4733d70 commit ec93d26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
16 changes: 11 additions & 5 deletions lib/rubocop/cop/style/class_equality_comparison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ClassEqualityComparison < Base
include IgnoredMethods
extend AutoCorrector

MSG = 'Use `Object.instance_of?` instead of comparing classes.'
MSG = 'Use `instance_of?(%<class_name>s)` instead of comparing classes.'

RESTRICT_ON_SEND = %i[== equal? eql?].freeze

Expand All @@ -37,18 +37,24 @@ def on_send(node)

class_comparison_candidate?(node) do |receiver_node, class_node|
range = offense_range(receiver_node, node)
class_name = class_name(class_node, node)

add_offense(range) do |corrector|
class_name = class_node.source
class_name = class_name.delete('"').delete("'") if node.children.first.method?(:name)

add_offense(range, message: format(MSG, class_name: class_name)) do |corrector|
corrector.replace(range, "instance_of?(#{class_name})")
end
end
end

private

def class_name(class_node, node)
if node.children.first.method?(:name)
class_node.source.delete('"').delete("'")
else
class_node.source
end
end

def offense_range(receiver_node, node)
range_between(receiver_node.loc.selector.begin_pos, node.source_range.end_pos)
end
Expand Down
10 changes: 5 additions & 5 deletions spec/rubocop/cop/style/class_equality_comparison_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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.
^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -19,7 +19,7 @@
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.
^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -30,7 +30,7 @@
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.
^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -41,7 +41,7 @@
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.
^^^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -52,7 +52,7 @@
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.
^^^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
Expand Down

0 comments on commit ec93d26

Please sign in to comment.