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.

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

Constants included from Util

Util::LITERAL_REGEX

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, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

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

add_parentheses, args_begin, args_end, begins_its_line?, comment_line?, comment_lines?, double_quotes_required?, escape_string, first_part_of_call_chain, indent, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, trim_string_interporation_escape_character

Methods included from PathUtil

absolute?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, relative_path, smart_path

Constructor Details

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

Returns a new instance of ExplicitBlockArgument.


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

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

Instance Method Details

#on_yield(node) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubocop/cop/style/explicit_block_argument.rb', line 54

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