Skip to content

Commit 247da93

Browse files
committed
Fix ranges containing only removals
1 parent b1df648 commit 247da93

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/__fixtures__/diff.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@ index 4d2637c..99dc494 100644
3434
@@ -10,0 +7,2 @@ if (!myFunc(true == myVar))
3535
+if (!myFunc(true == myVar)) (myRes = false), (someotherThing = null);
3636
+unrelated = true;`;
37+
38+
export const includingOnlyRemovals = `diff --git a/dirty.js b/dirty.js
39+
index cb3c131..874b8f9 100644
40+
--- a/dirty.js
41+
+++ b/dirty.js
42+
@@ -1,0 +2,2 @@ if (new Date().getTime()) console.log("curly");
43+
+if (new Date().getTime()) console.log("curly");
44+
+if (new Date().getTime()) console.log("curly");
45+
@@ -17,2 +16,0 @@ import { a } from "../components/a";
46+
-import { b } from "../context/b";
47+
-import { c } from "../context/c";`;

src/__snapshots__/git.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ Array [
1616
},
1717
]
1818
`;
19+
20+
exports[`git should work for hunks which include only-removal-ranges 1`] = `
21+
Array [
22+
Range {
23+
"exclusiveUpperBound": 4,
24+
"inclusiveLowerBound": 2,
25+
},
26+
]
27+
`;

src/git.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as child_process from "child_process";
22
import { mocked } from "ts-jest/utils";
33
import { getDiffForFile, getRangesForDiff } from "./git";
4-
import { hunks } from "./__fixtures__/diff";
4+
import { hunks, includingOnlyRemovals } from "./__fixtures__/diff";
55

66
jest.mock("child_process");
77

@@ -12,6 +12,10 @@ describe("git", () => {
1212
expect(getRangesForDiff(hunks)).toMatchSnapshot();
1313
});
1414

15+
it("should work for hunks which include only-removal-ranges", () => {
16+
expect(getRangesForDiff(includingOnlyRemovals)).toMatchSnapshot();
17+
});
18+
1519
it("should get the staged diff of a file", () => {
1620
mockedChildProcess.execSync.mockReturnValue(Buffer.from(hunks));
1721

src/git.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,22 @@ const getRangeForChangedLines = (line: string) => {
3636
range.groups?.linesCountDelimiter && range.groups?.linesCount
3737
? parseInt(range.groups.linesCount)
3838
: 1;
39+
40+
const hasAddedLines = linesCount !== 0;
3941
const start: number = parseInt(range.groups?.start);
4042
const end = start + linesCount;
4143

42-
return new Range(start, end);
44+
return hasAddedLines ? new Range(start, end) : null;
4345
};
4446

47+
const removeNullRanges = (r: Range | null): r is Range => r !== null;
48+
4549
const getRangesForDiff = (diff: string): Range[] => {
46-
return diff.split("\n").filter(isHunkHeader).map(getRangeForChangedLines);
50+
return diff
51+
.split("\n")
52+
.filter(isHunkHeader)
53+
.map(getRangeForChangedLines)
54+
.filter(removeNullRanges);
4755
};
4856

4957
export { getDiffForFile, getRangesForDiff };

0 commit comments

Comments
 (0)