Skip to content

Commit

Permalink
Merge pull request #10850 from r7kamura/feature/class-equality-compar…
Browse files Browse the repository at this point in the history
…ison-module

Fix `Style/ClassEqualityComparison` autocorrection within module
  • Loading branch information
koic committed Aug 1, 2022
2 parents ad31e36 + cc0e22a commit e9553ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_fix_styleclassequalitycomparison.md
@@ -0,0 +1 @@
* [#10850](https://github.com/rubocop/rubocop/pull/10850): Fix `Style/ClassEqualityComparison` autocorrection within module. ([@r7kamura][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/class_equality_comparison.rb
Expand Up @@ -97,7 +97,9 @@ def class_name(class_node, node)
if node.children.first.method?(:name)
return class_node.receiver.source if class_node.receiver

class_node.source.delete('"').delete("'")
value = class_node.source.delete('"').delete("'")
value.prepend('::') if class_node.each_ancestor(:class, :module).any?
value
else
class_node.source
end
Expand Down
27 changes: 27 additions & 0 deletions spec/rubocop/cop/style/class_equality_comparison_spec.rb
Expand Up @@ -100,4 +100,31 @@ def equal?(other)
RUBY
end
end

context 'with String comparison in module' do
it 'registers and corrects an offense' do
expect_offense(<<~RUBY)
module Foo
def bar?(value)
bar.class.name == 'Bar'
^^^^^^^^^^^^^^^^^^^ Use `instance_of?(::Bar)` instead of comparing classes.
end
class Bar
end
end
RUBY

expect_correction(<<~RUBY)
module Foo
def bar?(value)
bar.instance_of?(::Bar)
end
class Bar
end
end
RUBY
end
end
end

0 comments on commit e9553ad

Please sign in to comment.