-
-
Notifications
You must be signed in to change notification settings - Fork 78
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 #92 from koic/add_new_performance_bind_call
[Fix #77] Add new `Performance/BindCall` cop
- Loading branch information
Showing
7 changed files
with
188 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Performance | ||
# In Ruby 2.7, `UnboundMethod#bind_call` has been added. | ||
# | ||
# This cop identifies places where `bind(obj).call(args, ...)` | ||
# can be replaced by `bind_call(obj, args, ...)`. | ||
# | ||
# The `bind_call(obj, args, ...)` method is faster than | ||
# `bind(obj).call(args, ...)`. | ||
# | ||
# @example | ||
# # bad | ||
# umethod.bind(obj).call(foo, bar) | ||
# umethod.bind(obj).(foo, bar) | ||
# | ||
# # good | ||
# umethod.bind_call(obj, foo, bar) | ||
# | ||
class BindCall < Cop | ||
include RangeHelp | ||
extend TargetRubyVersion | ||
|
||
minimum_target_ruby_version 2.7 | ||
|
||
MSG = 'Use `bind_call(%<bind_arg>s%<comma>s%<call_args>s)` ' \ | ||
'instead of `bind(%<bind_arg>s).call(%<call_args>s)`.' | ||
|
||
def_node_matcher :bind_with_call_method?, <<~PATTERN | ||
(send | ||
$(send | ||
(send nil? _) :bind | ||
$(...)) :call | ||
$...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
bind_with_call_method?(node) do |receiver, bind_arg, call_args_node| | ||
range = correction_range(receiver, node) | ||
|
||
call_args = build_call_args(call_args_node) | ||
|
||
message = message(bind_arg.source, call_args) | ||
|
||
add_offense(node, location: range, message: message) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
receiver, bind_arg, call_args_node = bind_with_call_method?(node) | ||
|
||
range = correction_range(receiver, node) | ||
|
||
call_args = build_call_args(call_args_node) | ||
call_args = ", #{call_args}" unless call_args.empty? | ||
|
||
replacement_method = "bind_call(#{bind_arg.source}#{call_args})" | ||
|
||
lambda do |corrector| | ||
corrector.replace(range, replacement_method) | ||
end | ||
end | ||
|
||
private | ||
|
||
def message(bind_arg, call_args) | ||
comma = call_args.empty? ? '' : ', ' | ||
|
||
format(MSG, bind_arg: bind_arg, comma: comma, call_args: call_args) | ||
end | ||
|
||
def correction_range(receiver, node) | ||
location_of_bind = receiver.loc.selector.begin_pos | ||
location_of_call = node.loc.end.end_pos | ||
|
||
range_between(location_of_bind, location_of_call) | ||
end | ||
|
||
def build_call_args(call_args_node) | ||
call_args_node.map(&:source).join(', ') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Performance::BindCall, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'TargetRubyVersion <= 2.6', :ruby26 do | ||
it 'does not register an offense when using `bind(obj).call(args, ...)`' do | ||
expect_no_offenses(<<~RUBY) | ||
umethod.bind(obj).call(foo, bar) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'TargetRubyVersion >= 2.7', :ruby27 do | ||
it 'registers an offense when using `bind(obj).call(args, ...)`' do | ||
expect_offense(<<~RUBY) | ||
umethod.bind(obj).call(foo, bar) | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Use `bind_call(obj, foo, bar)` instead of `bind(obj).call(foo, bar)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
umethod.bind_call(obj, foo, bar) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `bind(obj).()`' do | ||
expect_offense(<<~RUBY) | ||
umethod.bind(obj).() | ||
^^^^^^^^^^^^ Use `bind_call(obj)` instead of `bind(obj).call()`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
umethod.bind_call(obj) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when the argument of `bind` method is a string' do | ||
expect_offense(<<~RUBY) | ||
umethod.bind('obj').call(foo, bar) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `bind_call('obj', foo, bar)` instead of `bind('obj').call(foo, bar)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
umethod.bind_call('obj', foo, bar) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when a argument of `call` method is a string' do | ||
expect_offense(<<~RUBY) | ||
umethod.bind(obj).call('foo', bar) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `bind_call(obj, 'foo', bar)` instead of `bind(obj).call('foo', bar)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
umethod.bind_call(obj, 'foo', bar) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `bind_call(obj, args, ...)`' do | ||
expect_no_offenses(<<~RUBY) | ||
umethod.bind_call(obj, foo, bar) | ||
RUBY | ||
end | ||
end | ||
end |