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 four ‘EnforcedStyle` options:

  1. ‘allow_single_line` (default) … Always uses the `it` block parameter in a single line.

  2. ‘only_numbered_parameters` … Detects only numbered block parameters.

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

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

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

Examples:

EnforcedStyle: allow_single_line (default)

# bad block do do_something(it) end block { do_something(_1) } # good block { do_something(it) } block { |named_param| do_something(named_param) }

EnforcedStyle: only_numbered_parameters

# 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_PARAMETER =
'Use `it` block parameter.'
MSG_AVOID_IT_PARAMETER =
'Avoid using `it` block parameter.'
MSG_AVOID_IT_PARAMETER_MULTILINE =
'Avoid using `it` block parameter for multi-line blocks.'

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

 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
# File 'lib/rubocop/cop/style/it_block_parameter.rb', line 64 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_PARAMETER) do |corrector| corrector.remove(node.arguments) corrector.replace(variable, 'it') end end end

#on_itblock(node) ⇒ Object

 94 95 96 97 98 99 100 101 102 103 104 105 106 107
# File 'lib/rubocop/cop/style/it_block_parameter.rb', line 94 def on_itblock(node) case style when :allow_single_line return if node.single_line? add_offense(node, message: MSG_AVOID_IT_PARAMETER_MULTILINE) when :disallow variables = find_block_variables(node, 'it') variables.each do |variable| add_offense(variable, message: MSG_AVOID_IT_PARAMETER) end end end

#on_numblock(node) ⇒ Object

 81 82 83 84 85 86 87 88 89 90 91 92
# File 'lib/rubocop/cop/style/it_block_parameter.rb', line 81 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_PARAMETER) do |corrector| corrector.replace(variable, 'it') end end end