lib/rubocop/cop/style/class_equality_comparison.rb in rubocop-1.48.1 vs lib/rubocop/cop/style/class_equality_comparison.rb in rubocop-1.49.0
- old
+ new
@@ -25,16 +25,20 @@
# # bad
# var.class == Date
# var.class.equal?(Date)
# var.class.eql?(Date)
# var.class.name == 'Date'
+ # var.class.to_s == 'Date'
+ # var.class.inspect == 'Date'
#
# @example AllowedMethods: [`==`]
# # good
# var.instance_of?(Date)
# var.class == Date
# var.class.name == 'Date'
+ # var.class.to_s == 'Date'
+ # var.class.inspect == 'Date'
#
# # bad
# var.class.equal?(Date)
# var.class.eql?(Date)
#
@@ -45,35 +49,40 @@
# # bad
# var.class == Date
# var.class.equal?(Date)
# var.class.eql?(Date)
# var.class.name == 'Date'
+ # var.class.to_s == 'Date'
+ # var.class.inspect == 'Date'
#
# @example AllowedPatterns: ['eq']
# # good
# var.instance_of?(Date)
# var.class.equal?(Date)
# var.class.eql?(Date)
#
# # bad
# var.class == Date
# var.class.name == 'Date'
+ # var.class.to_s == 'Date'
+ # var.class.inspect == 'Date'
#
class ClassEqualityComparison < Base
include RangeHelp
include AllowedMethods
include AllowedPattern
extend AutoCorrector
MSG = 'Use `instance_of?(%<class_name>s)` instead of comparing classes.'
RESTRICT_ON_SEND = %i[== equal? eql?].freeze
+ CLASS_NAME_METHODS = %i[name to_s inspect].freeze
# @!method class_comparison_candidate?(node)
def_node_matcher :class_comparison_candidate?, <<~PATTERN
(send
- {$(send _ :class) (send $(send _ :class) :name)}
+ {$(send _ :class) (send $(send _ :class) #class_name_method?)}
{:== :equal? :eql?} $_)
PATTERN
def on_send(node)
def_node = node.each_ancestor(:def, :defs).first
@@ -92,20 +101,24 @@
end
private
def class_name(class_node, node)
- if node.children.first.method?(:name)
+ if class_name_method?(node.children.first.method_name)
return class_node.receiver.source if class_node.receiver
if class_node.str_type?
value = class_node.source.delete('"').delete("'")
value.prepend('::') if class_node.each_ancestor(:class, :module).any?
return value
end
end
class_node.source
+ end
+
+ def class_name_method?(method_name)
+ CLASS_NAME_METHODS.include?(method_name)
end
def offense_range(receiver_node, node)
range_between(receiver_node.loc.selector.begin_pos, node.source_range.end_pos)
end