123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Layout::SpaceAroundBlockParameters

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::AutoCorrector, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/layout/space_around_block_parameters.rb

Overview

Checks the spacing inside and after block parameters pipes. Line breaks inside parameter pipes are checked by Layout/MultilineBlockLayout and not by this cop.

Examples:

EnforcedStyleInsidePipes: no_space (default)

# bad
{}.each { | x,  y |puts x }
->( x,  y ) { puts x }

# good
{}.each { |x, y| puts x }
->(x, y) { puts x }

EnforcedStyleInsidePipes: space

# bad
{}.each { |x,  y| puts x }
->(x,  y) { puts x }

# good
{}.each { | x, y | puts x }
->( x, y ) { puts x }

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

::RuboCop::Cop::RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

Class Attribute Summary

::RuboCop::Cop::AutoCorrector - Extended

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns an url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::RangeHelp - Included

#add_range, #column_offset_between,
#contents_range

A range containing only the contents of a literal with delimiters (e.g.

#directions,
#effective_column

Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value.

#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

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

Instance Method Details

#check_after_closing_pipe(arguments) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 65

def check_after_closing_pipe(arguments)
  _opening_pipe, closing_pipe = pipes(arguments)
  block = arguments.parent

  check_space(closing_pipe.end_pos, block.body.source_range.begin_pos,
              closing_pipe, 'after closing `|`')
end

#check_arg(arg) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 125

def check_arg(arg)
  arg.children.each { |a| check_arg(a) } if arg.mlhs_type?

  expr = arg.source_range
  check_no_space(
    range_with_surrounding_space(expr, side: :left).begin_pos,
    expr.begin_pos - 1,
    'Extra space before'
  )
end

#check_closing_pipe_space(arguments, closing_pipe) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 103

def check_closing_pipe_space(arguments, closing_pipe)
  args = arguments.children

  last         = args.last.source_range
  last_end_pos = last_end_pos_inside_pipes(arguments, last)

  check_space(last_end_pos, closing_pipe.begin_pos, last, 'after last block parameter')
  check_no_space(last_end_pos + 1, closing_pipe.begin_pos, 'Extra space after last')
end

#check_each_arg(args) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 121

def check_each_arg(args)
  args.children.each { |arg| check_arg(arg) }
end

#check_inside_pipes(arguments) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 56

def check_inside_pipes(arguments)
  case style
  when :no_space
    check_no_space_style_inside_pipes(arguments)
  when :space
    check_space_style_inside_pipes(arguments)
  end
end

#check_no_space(space_begin_pos, space_end_pos, msg) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 150

def check_no_space(space_begin_pos, space_end_pos, msg)
  return if space_begin_pos >= space_end_pos

  range = range_between(space_begin_pos, space_end_pos)
  return if range.source.include?("\n")

  message = "#{msg} block parameter detected."
  add_offense(range, message: message) { |corrector| corrector.remove(range) }
end

#check_no_space_style_inside_pipes(arguments) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 73

def check_no_space_style_inside_pipes(arguments)
  args = arguments.children
  opening_pipe, closing_pipe = pipes(arguments)

  first = args.first.source_range
  last = args.last.source_range

  check_no_space(opening_pipe.end_pos, first.begin_pos, 'Space before first')
  check_no_space(last_end_pos_inside_pipes(arguments, last),
                 closing_pipe.begin_pos, 'Space after last')
end

#check_opening_pipe_space(arguments, opening_pipe) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 92

def check_opening_pipe_space(arguments, opening_pipe)
  args = arguments.children

  first_arg = args.first
  range = first_arg.source_range

  check_space(opening_pipe.end_pos, range.begin_pos, range,
              'before first block parameter', first_arg)
  check_no_space(opening_pipe.end_pos, range.begin_pos - 1, 'Extra space before first')
end

#check_space(space_begin_pos, space_end_pos, range, msg, node = nil) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 136

def check_space(space_begin_pos, space_end_pos, range, msg, node = nil)
  return if space_begin_pos != space_end_pos

  target = node || range
  message = "Space #{msg} missing."
  add_offense(target, message: message) do |corrector|
    if node
      corrector.insert_before(node, ' ')
    else
      corrector.insert_after(target, ' ')
    end
  end
end

#check_space_style_inside_pipes(arguments) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 85

def check_space_style_inside_pipes(arguments)
  opening_pipe, closing_pipe = pipes(arguments)

  check_opening_pipe_space(arguments, opening_pipe)
  check_closing_pipe_space(arguments, closing_pipe)
end

#last_end_pos_inside_pipes(arguments, range) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 113

def last_end_pos_inside_pipes(arguments, range)
  pos = range.end_pos
  num = pos - arguments.source_range.begin_pos
  trailing_comma_index = arguments.source[num..].index(',')

  trailing_comma_index ? pos + trailing_comma_index + 1 : pos
end

#on_block(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 32

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  arguments = node.arguments

  return unless node.arguments? && pipes?(arguments)

  check_inside_pipes(arguments)
  check_after_closing_pipe(arguments) if node.body
  check_each_arg(arguments)
end

#pipes(arguments) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 44

def pipes(arguments)
  [arguments.loc.begin, arguments.loc.end]
end

#pipes?(arguments) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 48

def pipes?(arguments)
  pipes(arguments).none?(&:nil?)
end

#style_parameter_name (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/layout/space_around_block_parameters.rb', line 52

def style_parameter_name
  'EnforcedStyleInsidePipes'
end