Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Style/ClassEqualityComparison autocorrection within module #10850

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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