Module: Rubocop::Cop::AutocorrectAlignment

Overview

This module does auto-correction of nodes that should just be moved to the left or to the right, amount being determined by the instance variable @column_delta.

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 23

def autocorrect(node)
  # We can't use the instance variable inside the lambda. That would
  # just give each lambda the same reference and they would all get
  # the last value of @column_delta. A local variable fixes the
  # problem.
  column_delta = @column_delta

  @corrections << lambda do |corrector|
    expr = node.loc.expression
    each_line(expr) do |line_begin_pos, line|
      range = calculate_range(expr, line_begin_pos, column_delta)
      if column_delta > 0
        unless range.source == "\n"
          corrector.insert_before(range, ' ' * column_delta)
        end
      else
        remove(range, corrector) if range.source =~ /^[ \t]+$/
      end
    end
  end
end

#calculate_range(expr, line_begin_pos, column_delta) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 45

def calculate_range(expr, line_begin_pos, column_delta)
  starts_with_space = expr.source_buffer.source[line_begin_pos] =~ / /
  pos_to_remove = if column_delta > 0 || starts_with_space
                    line_begin_pos
                  else
                    line_begin_pos - column_delta.abs
                  end
  Parser::Source::Range.new(expr.source_buffer, pos_to_remove,
                            pos_to_remove + column_delta.abs)
end

#check_alignment(items, base_column = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 9

def check_alignment(items, base_column = nil)
  base_column ||= items.first.loc.column unless items.empty?
  items.each_cons(2) do |prev, current|
    if current.loc.line > prev.loc.line && start_of_line?(current.loc)
      @column_delta = base_column - current.loc.column
      add_offense(current, :expression) if @column_delta != 0
    end
  end
end

#each_line(expr) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 69

def each_line(expr)
  offset = 0
  expr.source.each_line do |line|
    line_begin_pos = expr.begin_pos + offset
    yield line_begin_pos, line
    offset += line.length
  end
end

#remove(range, corrector) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 56

def remove(range, corrector)
  original_stderr = $stderr
  $stderr = StringIO.new # Avoid error messages on console
  corrector.remove(range)
rescue RuntimeError
  range = Parser::Source::Range.new(range.source_buffer,
                                    range.begin_pos + 1,
                                    range.end_pos + 1)
  retry if range.source =~ /^ +$/
ensure
  $stderr = original_stderr
end

#start_of_line?(loc) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rubocop/cop/mixin/autocorrect_alignment.rb', line 19

def start_of_line?(loc)
  loc.expression.source_line[0...loc.column] =~ /^\s*$/
end