Class: RuboCop::Cop::Style::ExplicitBlockArgument

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/style/explicit_block_argument.rb

Overview

This cop enforces the use of explicit block argument to avoid writing block literal that just passes its arguments to another block.

NOTE: This cop only registers an offense if the block args match the yield args exactly.

Examples:

# bad
def with_tmp_dir
  Dir.mktmpdir do |tmp_dir|
    Dir.chdir(tmp_dir) { |dir| yield dir } # block just passes arguments
  end
end

# bad
def nine_times
  9.times { yield }
end

# good
def with_tmp_dir(&block)
  Dir.mktmpdir do |tmp_dir|
    Dir.chdir(tmp_dir, &block)
  end
end

with_tmp_dir do |dir|
  puts "dir is accessible as a parameter and pwd is set: #{dir}"
end

# good
def nine_times(&block)
  9.times(&block)
end

Constant Summary collapse

MSG =
'Consider using explicit block argument in the '\
"surrounding method's signature over `yield`."

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

#initialize(config = nil, options = nil) ⇒ ExplicitBlockArgument

Returns a new instance of ExplicitBlockArgument.


53
54
55
56
# File 'lib/rubocop/cop/style/explicit_block_argument.rb', line 53

def initialize(config = nil, options = nil)
  super
  @def_nodes = Set.new
end

Instance Method Details

#on_yield(node) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubocop/cop/style/explicit_block_argument.rb', line 58

def on_yield(node)
  block_node = node.parent

  yielding_block?(block_node) do |send_node, block_args, yield_args|
    return unless yielding_arguments?(block_args, yield_args)

    def_node = block_node.each_ancestor(:def, :defs).first
    # if `yield` is being called outside of a method context, ignore
    # this is not a valid ruby pattern, but can happen in haml or erb,
    # so this can cause crashes in haml_lint
    return unless def_node

    add_offense(block_node) do |corrector|
      corrector.remove(block_body_range(block_node, send_node))

      add_block_argument(send_node, corrector)
      add_block_argument(def_node, corrector) if @def_nodes.add?(def_node)
    end
  end
end

#yielding_block?(node) ⇒ Object


49
50
51
# File 'lib/rubocop/cop/style/explicit_block_argument.rb', line 49

def_node_matcher :yielding_block?, <<~PATTERN
  (block $_ (args $...) (yield $...))
PATTERN