-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
InternalAffairs/MethodNameEqual
cop
This PR adds new `InternalAffairs/MethodNameEqual` cop. ```ruby # bad node.method_name == :do_something # good node.method?(:do_something) ``` This cop is an internal cop and will not be added to a changelog entry.
- Loading branch information
Showing
17 changed files
with
113 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module InternalAffairs | ||
# Checks that method names are checked using `method?` method. | ||
# | ||
# @example | ||
# # bad | ||
# node.method_name == :do_something | ||
# | ||
# # good | ||
# node.method?(:do_something) | ||
# | ||
class MethodNameEqual < Cop | ||
include RangeHelp | ||
|
||
MSG = 'Use `method?(%<method_name>s)` instead of ' \ | ||
'`method_name == %<method_name>s`.' | ||
|
||
def_node_matcher :method_name?, <<~PATTERN | ||
(send | ||
$(send | ||
(...) :method_name) :== | ||
$...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
method_name?(node) do |method_name_node, method_name_arg| | ||
message = format(MSG, method_name: method_name_arg.first.source) | ||
|
||
range = range(method_name_node, node) | ||
|
||
add_offense(node, location: range, message: message) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
method_name?(node) do |method_name_node, method_name_arg| | ||
corrector.replace( | ||
range(method_name_node, node), | ||
"method?(#{method_name_arg.first.source})" | ||
) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def range(method_name_node, node) | ||
range_between( | ||
method_name_node.loc.selector.begin_pos, node.source_range.end_pos | ||
) | ||
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
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
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
35 changes: 35 additions & 0 deletions
35
spec/rubocop/cop/internal_affairs/method_name_equal_spec.rb
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::InternalAffairs::MethodNameEqual do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense when using `#method == :do_something`' do | ||
expect_offense(<<~RUBY) | ||
node.method_name == :do_something | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `method?(:do_something)` instead of `method_name == :do_something`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
node.method?(:do_something) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `#method == other_node.do_something`' do | ||
expect_offense(<<~RUBY) | ||
node.method_name == other_node.do_something | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `method?(other_node.do_something)` instead of `method_name == other_node.do_something`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
node.method?(other_node.do_something) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `#method?`' do | ||
expect_no_offenses(<<~RUBY) | ||
node.method?(:do_something) | ||
RUBY | ||
end | ||
end |