Class: RuboCop::Cop::Performance::RedundantBlockCall
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/redundant_block_call.rb |
Overview
Identifies the use of a &block
parameter and block.call
where yield
would do just as well.
Constant Summary
-
CLOSE_PAREN =
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 31')'
-
MSG =
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 28'Use `yield` instead of `%<argname>s.call`.'
-
OPEN_PAREN =
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 30'('
-
SPACE =
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 32' '
-
YIELD =
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 29'yield'
Instance Method Summary
- #on_def(node) (also: #on_defs)
-
#on_defs(node)
Alias for #on_def.
- #args_include_block_pass?(blockcall) ⇒ Boolean private
-
#autocorrect(corrector, node)
private
offenses are registered on the
block.call
nodes. - #calls_to_report(argname, body) private
- #shadowed_block_argument?(body, block_argument_of_method_signature) ⇒ Boolean private
Instance Method Details
#args_include_block_pass?(blockcall) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 99
def args_include_block_pass?(blockcall) _receiver, _call, *args = *blockcall args.any?(&:block_pass_type?) end
#autocorrect(corrector, node) (private)
offenses are registered on the block.call
nodes
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 65
def autocorrect(corrector, node) _receiver, _method, *args = *node new_source = String.new(YIELD) unless args.empty? new_source += if parentheses?(node) OPEN_PAREN else SPACE end new_source << args.map(&:source).join(', ') end new_source << CLOSE_PAREN if parentheses?(node) && !args.empty? corrector.replace(node, new_source) end
#calls_to_report(argname, body) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 83
def calls_to_report(argname, body) return [] if blockarg_assigned?(body, argname) || shadowed_block_argument?(body, argname) blockarg_calls(body, argname).map do |call| return [] if args_include_block_pass?(call) call end end
#on_def(node) Also known as: #on_defs
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 47
def on_def(node) blockarg_def(node) do |argname, body| next unless body calls_to_report(argname, body).each do |blockcall| next if blockcall.block_literal? add_offense(blockcall, message: format(MSG, argname: argname)) do |corrector| autocorrect(corrector, blockcall) end end end end
#on_defs(node)
Alias for #on_def.
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 60
alias on_defs on_def
#shadowed_block_argument?(body, block_argument_of_method_signature) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/performance/redundant_block_call.rb', line 93
def shadowed_block_argument?(body, block_argument_of_method_signature) return false unless body.block_type? body.arguments.map(&:source).include?(block_argument_of_method_signature.to_s) end