Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10600 from nobuyo/add-new-internal-affairs-method…
…-name-end-with-cop Add new `InternalAffairs/MethodNameEndWith` cop
- Loading branch information
Showing
9 changed files
with
196 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module InternalAffairs | ||
# This cop checks potentially usage of method identifier predicates | ||
# defined in rubocop-ast instead of `method_name.end_with?`. | ||
# | ||
# @example | ||
# # bad | ||
# node.method_name.to_s.end_with?('=') | ||
# | ||
# # good | ||
# node.assignment_method? | ||
# | ||
# # bad | ||
# node.method_name.to_s.end_with?('?') | ||
# | ||
# # good | ||
# node.predicate_method? | ||
# | ||
# # bad | ||
# node.method_name.to_s.end_with?('!') | ||
# | ||
# # good | ||
# node.bang_method? | ||
# | ||
class MethodNameEndWith < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `%<method_name>s` instead of `%<method_suffix>s`.' | ||
SUGGEST_METHOD_FOR_SUFFIX = { | ||
'=' => 'assignment_method?', | ||
'!' => 'bang_method?', | ||
'?' => 'predicate_method?' | ||
}.freeze | ||
|
||
# @!method method_name_end_with?(node) | ||
def_node_matcher :method_name_end_with?, <<~PATTERN | ||
{ | ||
(call | ||
(call | ||
$(... :method_name) :to_s) :end_with? | ||
$(str {"=" "?" "!"})) | ||
(call | ||
$(... :method_name) :end_with? | ||
$(str {"=" "?" "!"})) | ||
} | ||
PATTERN | ||
|
||
def on_send(node) | ||
method_name_end_with?(node) do |method_name_node, end_with_arg| | ||
range = range(method_name_node, node) | ||
message = format( | ||
MSG, | ||
method_name: SUGGEST_METHOD_FOR_SUFFIX[end_with_arg.value], | ||
method_suffix: range.source | ||
) | ||
|
||
add_offense(node, message: message) | ||
end | ||
end | ||
alias on_csend on_send | ||
|
||
private | ||
|
||
def range(method_name_node, node) | ||
range = if method_name_node.call_type? | ||
method_name_node.loc.selector | ||
else | ||
method_name_node.source_range | ||
end | ||
|
||
range_between(range.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
96 changes: 96 additions & 0 deletions
96
spec/rubocop/cop/internal_affairs/method_name_end_with_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,96 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::InternalAffairs::MethodNameEndWith, :config do | ||
it 'registers an offense if there is potentially usage of `assignment_method?`' do | ||
expect_offense(<<~RUBY) | ||
node.method_name.to_s.end_with?('=') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `assignment_method?` instead of `method_name.to_s.end_with?('=')`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if `method_name` is a variable and there is potentially usage of `assignment_method?`' do | ||
expect_offense(<<~RUBY) | ||
def assignment_method?(method_name) | ||
method_name.to_s.end_with?('=') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `assignment_method?` instead of `method_name.to_s.end_with?('=')`. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offense if there is potentially usage of `predicate_method?`' do | ||
expect_offense(<<~RUBY) | ||
node.method_name.to_s.end_with?('?') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `predicate_method?` instead of `method_name.to_s.end_with?('?')`. | ||
RUBY | ||
end | ||
|
||
it 'registers offense if there is potentially usage of `bang_method?`' do | ||
expect_offense(<<~RUBY) | ||
node.method_name.to_s.end_with?('!') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `bang_method?` instead of `method_name.to_s.end_with?('!')`. | ||
RUBY | ||
end | ||
|
||
it 'registers offense if there is potentially usage of `bang_method?` with safe navigation operator' do | ||
expect_offense(<<~RUBY) | ||
node.method_name&.to_s&.end_with?('!') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `bang_method?` instead of `method_name&.to_s&.end_with?('!')`. | ||
RUBY | ||
end | ||
|
||
it 'does not register offense if argument for end_with? is some other string' do | ||
expect_no_offenses(<<~RUBY) | ||
node.method_name.to_s.end_with?('_foo') | ||
RUBY | ||
end | ||
|
||
context 'Ruby >= 2.7', :ruby27 do | ||
it 'registers an offense if method_name is symbol' do | ||
expect_offense(<<~RUBY) | ||
node.method_name.end_with?('=') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `assignment_method?` instead of `method_name.end_with?('=')`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense if method_name is symbol with safe navigation operator' do | ||
expect_offense(<<~RUBY) | ||
node&.method_name&.end_with?('=') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `assignment_method?` instead of `method_name&.end_with?('=')`. | ||
RUBY | ||
end | ||
|
||
it 'registers offense if argument for Symbol#end_with? is \'?\'' do | ||
expect_offense(<<~RUBY) | ||
node.method_name.end_with?('?') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `predicate_method?` instead of `method_name.end_with?('?')`. | ||
RUBY | ||
end | ||
|
||
it 'registers offense if argument for Symbol#end_with? is \'?\' with safe navigation operator' do | ||
expect_offense(<<~RUBY) | ||
node.method_name&.end_with?('?') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `predicate_method?` instead of `method_name&.end_with?('?')`. | ||
RUBY | ||
end | ||
|
||
it 'registers offense if argument for Symbol#end_with? is \'!\'' do | ||
expect_offense(<<~RUBY) | ||
node.method_name.end_with?('!') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `bang_method?` instead of `method_name.end_with?('!')`. | ||
RUBY | ||
end | ||
|
||
it 'registers offense if argument for Symbol#end_with? is \'!\' with safe navigation operator' do | ||
expect_offense(<<~RUBY) | ||
node.method_name&.end_with?('!') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `bang_method?` instead of `method_name&.end_with?('!')`. | ||
RUBY | ||
end | ||
|
||
it 'does not register offense if argument for Symbol#end_with? is some other string' do | ||
expect_no_offenses(<<~RUBY) | ||
node.method_name.end_with?('_foo') | ||
RUBY | ||
end | ||
end | ||
end |