module RuboCop::Cop::SurroundingSpace

Common functionality for checking and correcting surrounding whitespace.

Constants

NO_SPACE_COMMAND
SPACE_COMMAND

Private Instance Methods

empty_brackets?(left_bracket_token, right_bracket_token) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 121
def empty_brackets?(left_bracket_token, right_bracket_token)
  left_index = processed_source.tokens.index(left_bracket_token)
  right_index = processed_source.tokens.index(right_bracket_token)
  right_index && left_index == right_index - 1
end
empty_offense(node, range, message, command) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 116
def empty_offense(node, range, message, command)
  add_offense(node, location: range,
                    message: format(message, command: command))
end
empty_offenses(node, left, right, message) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 106
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) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 84
def extra_space?(token, side)
  return false unless token

  if side == :left
    String(token.space_after?) == ' '
  else
    String(token.space_before?) == ' '
  end
end
index_of_first_token(node) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 31
def index_of_first_token(node)
  range = node.source_range
  token_table[range.line][range.column]
end
index_of_last_token(node) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 36
def index_of_last_token(node)
  range = node.source_range
  table_row = token_table[range.last_line]
  (0...range.last_column).reverse_each do |c|
    ix = table_row[c]
    return ix if ix
  end
end
no_space_between?(left_bracket_token, right_bracket_token) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 139
def no_space_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) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 56
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

  space_offense(node, right_token, :left, message, NO_SPACE_COMMAND)
end
offending_empty_no_space?(config, left_token, right_token) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 131
def offending_empty_no_space?(config, left_token, right_token)
  config == 'no_space' && !no_space_between?(left_token, right_token)
end
offending_empty_space?(config, left_token, right_token) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 127
def offending_empty_space?(config, left_token, right_token)
  config == 'space' && !space_between?(left_token, right_token)
end
reposition(src, pos, step) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 94
def reposition(src, pos, step)
  offset = step == -1 ? -1 : 0
  pos += step while src[pos + offset] =~ /[ \t]/
  pos.negative? ? 0 : pos
end
side_space_range(range:, side:) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 14
def side_space_range(range:, side:)
  buffer = @processed_source.buffer
  src = buffer.source

  begin_pos = range.begin_pos
  end_pos = range.end_pos
  if side == :left
    begin_pos = reposition(src, begin_pos, -1)
    end_pos -= 1
  end
  if side == :right
    begin_pos += 1
    end_pos = reposition(src, end_pos, 1)
  end
  Parser::Source::Range.new(buffer, begin_pos, end_pos)
end
space_between?(left_bracket_token, right_bracket_token) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 135
def space_between?(left_bracket_token, right_bracket_token)
  left_bracket_token.end_pos + 1 == right_bracket_token.begin_pos
end
space_offense(node, token, side, message, command) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 100
def space_offense(node, token, side, message, command)
  range = side_space_range(range: token.pos, side: side)
  add_offense(node, location: range,
                    message: format(message, command: command))
end
space_offenses(node, left_token, right_token, message, start_ok: false, end_ok: false) click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 70
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

  space_offense(node, right_token, :none, message, SPACE_COMMAND)
end
token_table() click to toggle source
# File lib/rubocop/cop/mixin/surrounding_space.rb, line 45
def token_table
  @token_table ||= begin
    table = {}
    @processed_source.tokens.each_with_index do |t, ix|
      table[t.line] ||= {}
      table[t.line][t.column] = ix
    end
    table
  end
end