Skip to content

Commit ec93d26

Browse files
committed
Tweak the offense message for Style/ClassEqualityComparison
1 parent 4733d70 commit ec93d26

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

lib/rubocop/cop/style/class_equality_comparison.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ClassEqualityComparison < Base
2121
include IgnoredMethods
2222
extend AutoCorrector
2323

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

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

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

3838
class_comparison_candidate?(node) do |receiver_node, class_node|
3939
range = offense_range(receiver_node, node)
40+
class_name = class_name(class_node, node)
4041

41-
add_offense(range) do |corrector|
42-
class_name = class_node.source
43-
class_name = class_name.delete('"').delete("'") if node.children.first.method?(:name)
44-
42+
add_offense(range, message: format(MSG, class_name: class_name)) do |corrector|
4543
corrector.replace(range, "instance_of?(#{class_name})")
4644
end
4745
end
4846
end
4947

5048
private
5149

50+
def class_name(class_node, node)
51+
if node.children.first.method?(:name)
52+
class_node.source.delete('"').delete("'")
53+
else
54+
class_node.source
55+
end
56+
end
57+
5258
def offense_range(receiver_node, node)
5359
range_between(receiver_node.loc.selector.begin_pos, node.source_range.end_pos)
5460
end

spec/rubocop/cop/style/class_equality_comparison_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
it 'registers an offense and corrects when comparing class using `==` for equality' do
99
expect_offense(<<~RUBY)
1010
var.class == Date
11-
^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
11+
^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
1212
RUBY
1313

1414
expect_correction(<<~RUBY)
@@ -19,7 +19,7 @@
1919
it 'registers an offense and corrects when comparing class using `equal?` for equality' do
2020
expect_offense(<<~RUBY)
2121
var.class.equal?(Date)
22-
^^^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
22+
^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
2323
RUBY
2424

2525
expect_correction(<<~RUBY)
@@ -30,7 +30,7 @@
3030
it 'registers an offense and corrects when comparing class using `eql?` for equality' do
3131
expect_offense(<<~RUBY)
3232
var.class.eql?(Date)
33-
^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
33+
^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
3434
RUBY
3535

3636
expect_correction(<<~RUBY)
@@ -41,7 +41,7 @@
4141
it 'registers an offense and corrects when comparing single quoted class name for equality' do
4242
expect_offense(<<~RUBY)
4343
var.class.name == 'Date'
44-
^^^^^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
44+
^^^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
4545
RUBY
4646

4747
expect_correction(<<~RUBY)
@@ -52,7 +52,7 @@
5252
it 'registers an offense and corrects when comparing double quoted class name for equality' do
5353
expect_offense(<<~RUBY)
5454
var.class.name == "Date"
55-
^^^^^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes.
55+
^^^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
5656
RUBY
5757

5858
expect_correction(<<~RUBY)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy