123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::SurroundingSpace

Overview

Common functionality for checking and correcting surrounding whitespace.

Constant Summary

RangeHelp - Included

BYTE_ORDER_MARK, NOT_GIVEN

Instance Method Summary

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

Instance Method Details

#empty_brackets?(left_bracket_token, right_bracket_token, tokens: processed_source.tokens) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 110

def empty_brackets?(left_bracket_token, right_bracket_token, tokens: processed_source.tokens)
  left_index = tokens.index(left_bracket_token)
  right_index = tokens.index(right_bracket_token)
  right_index && left_index == right_index - 1
end

#empty_offense(node, range, message, command) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 104

def empty_offense(node, range, message, command)
  add_offense(range, message: format(message, command: command)) do |corrector|
    autocorrect(corrector, node)
  end
end

#empty_offenses(node, left, right, message) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 94

def empty_offenses(node, left, right, message)
  range = range_between(left.begin_pos, right.end_pos)
  if offending_empty_space?(empty_config, left, right)
    empty_offense(node, range, message, 'Use one')
  end
  return unless offending_empty_no_space?(empty_config, left, right)

  empty_offense(node, range, message, 'Do not use')
end

#extra_space?(token, side) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 68

def extra_space?(token, side)
  return false unless token

  if side == :left
    SINGLE_SPACE_REGEXP.match?(String(token.space_after?))
  else
    SINGLE_SPACE_REGEXP.match?(String(token.space_before?))
  end
end

#no_character_between?(left_bracket_token, right_bracket_token) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 129

def no_character_between?(left_bracket_token, right_bracket_token)
  left_bracket_token.end_pos == right_bracket_token.begin_pos
end

#no_space_offenses(node, left_token, right_token, message, start_ok: false, end_ok: false) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 38

def no_space_offenses(node, # rubocop:disable Metrics/ParameterLists
                      left_token,
                      right_token,
                      message,
                      start_ok: false,
                      end_ok: false)
  if extra_space?(left_token, :left) && !start_ok
    space_offense(node, left_token, :right, message, NO_SPACE_COMMAND)
  end
  return if (!extra_space?(right_token, :right) || end_ok) ||
            (autocorrect_with_disable_uncorrectable? && !start_ok)

  space_offense(node, right_token, :left, message, NO_SPACE_COMMAND)
end

#offending_empty_no_space?(config, left_token, right_token) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 120

def offending_empty_no_space?(config, left_token, right_token)
  config == 'no_space' && !no_character_between?(left_token, right_token)
end

#offending_empty_space?(config, left_token, right_token) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 116

def offending_empty_space?(config, left_token, right_token)
  config == 'space' && !space_between?(left_token, right_token)
end

#on_new_investigation (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 33

def on_new_investigation
  @token_table = nil
  super
end

#reposition(src, pos, step, include_newlines: false) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 78

def reposition(src, pos, step, include_newlines: false)
  offset = step == -1 ? -1 : 0
  pos += step while SINGLE_SPACE_REGEXP.match?(src[pos + offset]) ||
                    (include_newlines && src[pos + offset] == "\n")
  pos.negative? ? 0 : pos
end

#side_space_range(range:, side:, include_newlines: false) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 16

def side_space_range(range:, side:, include_newlines: false)
  buffer = processed_source.buffer
  src = buffer.source

  begin_pos = range.begin_pos
  end_pos = range.end_pos
  if side == :left
    end_pos = begin_pos
    begin_pos = reposition(src, begin_pos, -1, include_newlines: include_newlines)
  end
  if side == :right
    begin_pos = end_pos
    end_pos = reposition(src, end_pos, 1, include_newlines: include_newlines)
  end
  Parser::Source::Range.new(buffer, begin_pos, end_pos)
end

#space_between?(left_bracket_token, right_bracket_token) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 124

def space_between?(left_bracket_token, right_bracket_token)
  left_bracket_token.end_pos + 1 == right_bracket_token.begin_pos &&
    processed_source.buffer.source[left_bracket_token.end_pos] == ' '
end

#space_offense(node, token, side, message, command) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 85

def space_offense(node, token, side, message, command)
  range = side_space_range(range: token.pos, side: side)
  add_offense(range, message: format(message, command: command)) do |corrector|
    autocorrect(corrector, node) unless ignored_node?(node)

    ignore_node(node)
  end
end

#space_offenses(node, left_token, right_token, message, start_ok: false, end_ok: false) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 53

def space_offenses(node, # rubocop:disable Metrics/ParameterLists
                   left_token,
                   right_token,
                   message,
                   start_ok: false,
                   end_ok: false)
  unless extra_space?(left_token, :left) || start_ok
    space_offense(node, left_token, :none, message, SPACE_COMMAND)
  end
  return if (extra_space?(right_token, :right) || end_ok) ||
            (autocorrect_with_disable_uncorrectable? && !start_ok)

  space_offense(node, right_token, :none, message, SPACE_COMMAND)
end