Module: RuboCop::Cop::SurroundingSpace

Overview

Common functionality for checking and correcting surrounding whitespace.

Constant Summary collapse

NO_SPACE_COMMAND =
'Do not use'.freeze
SPACE_COMMAND =
'Use'.freeze

Instance Method Summary collapse

Instance Method Details

#index_of_first_token(node) ⇒ Object



27
28
29
30
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 27

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

#index_of_last_token(node) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 32

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_corrector(corrector, left_token, right_token) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 72

def no_space_corrector(corrector, left_token, right_token)
  if left_token.space_after?
    range = side_space_range(range: left_token.pos, side: :right)
    corrector.remove(range)
  end
  return unless right_token.space_before?
  range = side_space_range(range: right_token.pos, side: :left)
  corrector.remove(range)
end

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

rubocop:disable Metrics/ParameterLists



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 59

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

#side_space_range(range:, side:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 10

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_corrector(corrector, left_token, right_token) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 95

def space_corrector(corrector, left_token, right_token)
  unless left_token.space_after?
    corrector.insert_after(left_token.pos, ' ')
  end
  return if right_token.space_before?
  corrector.insert_before(right_token.pos, ' ')
end

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

rubocop:disable Metrics/ParameterLists



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 82

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_tableObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 41

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

#tokens(node) ⇒ Object



52
53
54
55
56
57
# File 'lib/rubocop/cop/mixin/surrounding_space.rb', line 52

def tokens(node)
  processed_source.tokens.select do |token|
    token.end_pos <= node.source_range.end_pos &&
      token.begin_pos >= node.source_range.begin_pos
  end
end