Class: RuboCop::Cop::Style::ItBlockParameter

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRubyVersion
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/style/it_block_parameter.rb

Overview

Checks for blocks with one argument where ‘it` block parameter can be used.

It provides three ‘EnforcedStyle` options:

  1. ‘only_numbered_parameters` (default) … Detects only numbered block parameters.

  2. ‘always` … Always uses the `it` block parameter.

  3. ‘disallow` … Disallows the `it` block parameter.

A single numbered parameter is detected when ‘only_numbered_parameters` or `always`.

Examples:

EnforcedStyle: only_numbered_parameters (default)

# bad
block { do_something(_1) }

# good
block { do_something(it) }
block { |named_param| do_something(named_param) }

EnforcedStyle: always

# bad
block { do_something(_1) }
block { |named_param| do_something(named_param) }

# good
block { do_something(it) }

EnforcedStyle: disallow

# bad
block { do_something(it) }

# good
block { do_something(_1) }
block { |named_param| do_something(named_param) }

Constant Summary collapse

MSG_USE_IT_BLOCK_PARAMETER =
'Use `it` block parameter.'
MSG_AVOID_IT_BLOCK_PARAMETER =
'Avoid using `it` block parameter.'

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 TargetRubyVersion

maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

Methods included from AutoCorrector

support_autocorrect?

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #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

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_block(node) ⇒ Object

[View source]

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubocop/cop/style/it_block_parameter.rb', line 50

def on_block(node)
  return unless style == :always
  return unless node.arguments.one?

  # `restarg`, `kwrestarg`, `blockarg` nodes can return early.
  return unless node.first_argument.arg_type?

  variables = find_block_variables(node, node.first_argument.source)

  variables.each do |variable|
    add_offense(variable, message: MSG_USE_IT_BLOCK_PARAMETER) do |corrector|
      corrector.remove(node.arguments)
      corrector.replace(variable, 'it')
    end
  end
end

#on_itblock(node) ⇒ Object

[View source]

80
81
82
83
84
85
86
87
88
# File 'lib/rubocop/cop/style/it_block_parameter.rb', line 80

def on_itblock(node)
  return unless style == :disallow

  variables = find_block_variables(node, 'it')

  variables.each do |variable|
    add_offense(variable, message: MSG_AVOID_IT_BLOCK_PARAMETER)
  end
end

#on_numblock(node) ⇒ Object

[View source]

67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubocop/cop/style/it_block_parameter.rb', line 67

def on_numblock(node)
  return if style == :disallow
  return unless node.children[1] == 1

  variables = find_block_variables(node, '_1')

  variables.each do |variable|
    add_offense(variable, message: MSG_USE_IT_BLOCK_PARAMETER) do |corrector|
      corrector.replace(variable, 'it')
    end
  end
end