Module: Rubocop::Cop::Style::SurroundingSpace

Included in:
SpaceAroundBraces, SpaceAroundEqualsInParameterDefault, SpaceAroundOperators, SpaceInside, SpaceInsideHashLiteralBraces
Defined in:
lib/rubocop/cop/style/surrounding_space.rb

Overview

Common functionality for checking surrounding space.

Instance Method Summary collapse

Instance Method Details

#build_token_table(tokens) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 36

def build_token_table(tokens)
  table = {}
  tokens.each_with_index do |t, ix|
    table[[t.pos.line, t.pos.column]] = ix
  end
  table
end

#index_of_first_token(node, tokens) ⇒ Object



21
22
23
24
25
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 21

def index_of_first_token(node, tokens)
  @token_table ||= build_token_table(tokens)
  b = node.loc.expression.begin
  @token_table[[b.line, b.column]]
end

#index_of_last_token(node, tokens) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 27

def index_of_last_token(node, tokens)
  @token_table ||= build_token_table(tokens)
  e = node.loc.expression.end
  (0...e.column).to_a.reverse.find do |c|
    ix = @token_table[[e.line, c]]
    return ix if ix
  end
end

#space_between?(t1, t2) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 10

def space_between?(t1, t2)
  char_preceding_2nd_token =
    @source[t2.pos.line - 1][t2.pos.column - 1]
  if char_preceding_2nd_token == '+' && t1.type != :tPLUS
    # Special case. A unary plus is not present in the tokens.
    char_preceding_2nd_token =
      @source[t2.pos.line - 1][t2.pos.column - 2]
  end
  t2.pos.line > t1.pos.line || char_preceding_2nd_token == ' '
end