Skip to content

Commit

Permalink
Merge pull request #14 from CatanaCorp/ec-line-shifed
Browse files Browse the repository at this point in the history
Fix the `previous_line_number_is_now` method returning a wrong value:
  • Loading branch information
Edouard-chin authored Sep 2, 2024
2 parents 85ce9c8 + 21cc029 commit b1ca720
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/github_diff_parser/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def symlink_source
#
# @return [Integer]
def previous_line_number_is_now(line_number)
return line_number unless line_shifted?(line_number)
return line_number if line_unchanged?(line_number)

applicable_hunk = last_applicable_hunk_for_line(line_number)
line = applicable_hunk.find_previous_line(line_number)
Expand Down Expand Up @@ -196,16 +196,13 @@ def revert(current_content)

private

# Check if a line was shifted. A line is considered shifted if its number is superior to the first hunk's start
# range.
#
# @param line_number [Integer]
#
# @return [Boolean]
def line_shifted?(line_number)
def line_unchanged?(line_number)
first_hunk = hunks.first

line_number > first_hunk.new_file_start_line
line_number < first_hunk.new_file_start_line
end

# Find the last hunk that shifts the line. We need the last because we know it's the one that will shift the line
Expand Down
10 changes: 10 additions & 0 deletions test/data/first_line_shifted.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
diff --git a/my_file.rb b/my_file.rb
index 479ddd7700..80acf01257 100644
--- a/my_file.rb
+++ b/my_file.rb
@@ -1,2 +1,4 @@
+# typed: false
+# frozen_string_literal: true
# Blablabla
# Example
# Something something
8 changes: 8 additions & 0 deletions test/previous_line_number_is_now_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ def test_multiple_hunks

assert_equal(68, current_number)
end

def test_when_first_line_is_shifted
parsed_diffs = GithubDiffParser.parse(read_diff("first_line_shifted"))

current_number = parsed_diffs.first.previous_line_number_is_now(1)

assert_equal(3, current_number)
end
end

0 comments on commit b1ca720

Please sign in to comment.