Class: RuboCop::Cop::Style::ItBlockParameter
- 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:
-
‘only_numbered_parameters` (default) … Detects only numbered block parameters.
-
‘always` … Always uses the `it` block parameter.
-
‘disallow` … Disallows the `it` block parameter.
A single numbered parameter is detected when ‘only_numbered_parameters` or `always`.
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
Instance Attribute Summary
Attributes inherited from Base
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
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
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
permalink #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 |
permalink #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 |
permalink #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 |