Module: RuboCop::Cop::SurroundingSpace

Overview

Common functionality for checking surrounding space.

Instance Method Summary collapse

Instance Method Details

#index_of_first_token(node) ⇒ Object



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

def index_of_first_token(node)
  range = node.source_range
  token_table[range.line][range.column]
end

#index_of_last_token(node) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 19

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

#space_between?(t1, _t2) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 7

def space_between?(t1, _t2)
  # Check if the range between the tokens starts with a space. It can
  # contain other characters, e.g. a unary plus, but it must start with
  # space.
  t1.pos.source_buffer.source.match(/\G\s/, t1.pos.end_pos)
end

#token_tableObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 28

def token_table
  @token_table ||= begin
    table = {}
    @processed_source.tokens.each_with_index do |t, ix|
      table[t.pos.line] ||= {}
      table[t.pos.line][t.pos.column] = ix
    end
    table
  end
end