Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Style/ClassEqualityComparison
cop
- Loading branch information
Showing
7 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop enforces the use of `Object#instance_of?` instead of class comparison | ||
# for equality. | ||
# | ||
# @example | ||
# # bad | ||
# var.class == Date | ||
# var.class.equal?(Date) | ||
# var.class.eql?(Date) | ||
# var.class.name == 'Date' | ||
# | ||
# # good | ||
# var.instance_of?(Date) | ||
# | ||
class ClassEqualityComparison < Base | ||
include RangeHelp | ||
include IgnoredMethods | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `Object.instance_of?` instead of comparing classes.' | ||
|
||
RESTRICT_ON_SEND = %i[== equal? eql?].freeze | ||
|
||
def_node_matcher :class_comparison_candidate?, <<~PATTERN | ||
(send | ||
{$(send _ :class) (send $(send _ :class) :name)} | ||
{:== :equal? :eql?} $_) | ||
PATTERN | ||
|
||
def on_send(node) | ||
def_node = node.each_ancestor(:def, :defs).first | ||
return if def_node && ignored_method?(def_node.method_name) | ||
|
||
class_comparison_candidate?(node) do |receiver_node, class_node| | ||
range = range_between(receiver_node.loc.selector.begin_pos, node.source_range.end_pos) | ||
|
||
add_offense(range) do |corrector| | ||
corrector.replace(range, "instance_of?(#{class_node.source})") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::ClassEqualityComparison, :config do | ||
let(:cop_config) do | ||
{ 'IgnoredMethods' => [] } | ||
end | ||
|
||
it 'registers an offense and corrects when comparing class for equality' do | ||
expect_offense(<<~RUBY) | ||
var.class == Date | ||
^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes. | ||
var.class.equal?(Date) | ||
^^^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes. | ||
var.class.eql?(Date) | ||
^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when comparing class name for equality' do | ||
expect_offense(<<~RUBY) | ||
var.class.name == "Date" | ||
^^^^^^^^^^^^^^^^^^^^ Use `Object.instance_of?` instead of comparing classes. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `instance_of?`' do | ||
expect_no_offenses(<<~RUBY) | ||
var.instance_of?(Date) | ||
RUBY | ||
end | ||
|
||
context 'when IgnoredMethods is specified' do | ||
let(:cop_config) do | ||
{ 'IgnoredMethods' => ['=='] } | ||
end | ||
|
||
it 'does not register an offense when comparing class for equality' do | ||
expect_no_offenses(<<~RUBY) | ||
def ==(other) | ||
self.class == other.class && | ||
name == other.name | ||
end | ||
RUBY | ||
end | ||
end | ||
end |