Class: RuboCop::Git::Patch

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/git/patch.rb

Overview

Constant Summary collapse

RANGE_INFORMATION_LINE =
/^@@ .+\+(?<line_number>\d+),/
MODIFIED_LINE =
/^\+(?!\+|\+)/
NOT_REMOVED_LINE =
/^[^-]/
PATCH_INFO_LINE =
/\+([0-9,]+)/

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Patch

Returns a new instance of Patch.



9
10
11
12
# File 'lib/rubocop/git/patch.rb', line 9

def initialize(body)
  @body = body || ''
  @changes = []
end

Instance Method Details

#additionsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubocop/git/patch.rb', line 14

def additions
  line_number = 0

  lines.each_with_index.inject(@changes) do |additions, (content, patch_position)|
    case content
    when RANGE_INFORMATION_LINE
      line_number = Regexp.last_match[:line_number].to_i
    when MODIFIED_LINE
      additions << Line.new(content, line_number, patch_position)
      line_number += 1
    when NOT_REMOVED_LINE
      line_number += 1
    end

    additions
  end
end

#additions_mapObject

maps out additions line numbers to indicate start and end of code changes

[5,7], [11,11]

indicates changes from line 5, 6, 7 and then

another one at 11



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubocop/git/patch.rb', line 35

def additions_map
  if @changes.empty?
    self.additions
  end

  map = []
  starting_line = ending_line = 0

  @changes.each do |addition|
    if starting_line == 0
      starting_line = ending_line = addition.line_number
    elsif addition.line_number == ( ending_line + 1 )
      ending_line = addition.line_number
    else # this row is not part of the last rows "group"
      map.push([starting_line, ending_line])
      starting_line = ending_line = addition.line_number
    end
  end
  map.push([starting_line, ending_line])
  map
end