File tree Expand file tree Collapse file tree 2 files changed +16
-10
lines changed Expand file tree Collapse file tree 2 files changed +16
-10
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ class ClassEqualityComparison < Base
21
21
include IgnoredMethods
22
22
extend AutoCorrector
23
23
24
- MSG = 'Use `Object. instance_of?` instead of comparing classes.'
24
+ MSG = 'Use `instance_of?(%<class_name>s) ` instead of comparing classes.'
25
25
26
26
RESTRICT_ON_SEND = %i[ == equal? eql? ] . freeze
27
27
@@ -37,18 +37,24 @@ def on_send(node)
37
37
38
38
class_comparison_candidate? ( node ) do |receiver_node , class_node |
39
39
range = offense_range ( receiver_node , node )
40
+ class_name = class_name ( class_node , node )
40
41
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 |
45
43
corrector . replace ( range , "instance_of?(#{ class_name } )" )
46
44
end
47
45
end
48
46
end
49
47
50
48
private
51
49
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
+
52
58
def offense_range ( receiver_node , node )
53
59
range_between ( receiver_node . loc . selector . begin_pos , node . source_range . end_pos )
54
60
end
Original file line number Diff line number Diff line change 8
8
it 'registers an offense and corrects when comparing class using `==` for equality' do
9
9
expect_offense ( <<~RUBY )
10
10
var.class == Date
11
- ^^^^^^^^^^^^^ Use `Object. instance_of?` instead of comparing classes.
11
+ ^^^^^^^^^^^^^ Use `instance_of?(Date) ` instead of comparing classes.
12
12
RUBY
13
13
14
14
expect_correction ( <<~RUBY )
19
19
it 'registers an offense and corrects when comparing class using `equal?` for equality' do
20
20
expect_offense ( <<~RUBY )
21
21
var.class.equal?(Date)
22
- ^^^^^^^^^^^^^^^^^^ Use `Object. instance_of?` instead of comparing classes.
22
+ ^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date) ` instead of comparing classes.
23
23
RUBY
24
24
25
25
expect_correction ( <<~RUBY )
30
30
it 'registers an offense and corrects when comparing class using `eql?` for equality' do
31
31
expect_offense ( <<~RUBY )
32
32
var.class.eql?(Date)
33
- ^^^^^^^^^^^^^^^^ Use `Object. instance_of?` instead of comparing classes.
33
+ ^^^^^^^^^^^^^^^^ Use `instance_of?(Date) ` instead of comparing classes.
34
34
RUBY
35
35
36
36
expect_correction ( <<~RUBY )
41
41
it 'registers an offense and corrects when comparing single quoted class name for equality' do
42
42
expect_offense ( <<~RUBY )
43
43
var.class.name == 'Date'
44
- ^^^^^^^^^^^^^^^^^^^^ Use `Object. instance_of?` instead of comparing classes.
44
+ ^^^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date) ` instead of comparing classes.
45
45
RUBY
46
46
47
47
expect_correction ( <<~RUBY )
52
52
it 'registers an offense and corrects when comparing double quoted class name for equality' do
53
53
expect_offense ( <<~RUBY )
54
54
var.class.name == "Date"
55
- ^^^^^^^^^^^^^^^^^^^^ Use `Object. instance_of?` instead of comparing classes.
55
+ ^^^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date) ` instead of comparing classes.
56
56
RUBY
57
57
58
58
expect_correction ( <<~RUBY )
You can’t perform that action at this time.
0 commit comments