File tree Expand file tree Collapse file tree 4 files changed +35
-3
lines changed Expand file tree Collapse file tree 4 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -34,3 +34,14 @@ index 4d2637c..99dc494 100644
34
34
@@ -10,0 +7,2 @@ if (!myFunc(true == myVar))
35
35
+if (!myFunc(true == myVar)) (myRes = false), (someotherThing = null);
36
36
+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";` ;
Original file line number Diff line number Diff line change @@ -16,3 +16,12 @@ Array [
16
16
} ,
17
17
]
18
18
` ;
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
+ ` ;
Original file line number Diff line number Diff line change 1
1
import * as child_process from "child_process" ;
2
2
import { mocked } from "ts-jest/utils" ;
3
3
import { getDiffForFile , getRangesForDiff } from "./git" ;
4
- import { hunks } from "./__fixtures__/diff" ;
4
+ import { hunks , includingOnlyRemovals } from "./__fixtures__/diff" ;
5
5
6
6
jest . mock ( "child_process" ) ;
7
7
@@ -12,6 +12,10 @@ describe("git", () => {
12
12
expect ( getRangesForDiff ( hunks ) ) . toMatchSnapshot ( ) ;
13
13
} ) ;
14
14
15
+ it ( "should work for hunks which include only-removal-ranges" , ( ) => {
16
+ expect ( getRangesForDiff ( includingOnlyRemovals ) ) . toMatchSnapshot ( ) ;
17
+ } ) ;
18
+
15
19
it ( "should get the staged diff of a file" , ( ) => {
16
20
mockedChildProcess . execSync . mockReturnValue ( Buffer . from ( hunks ) ) ;
17
21
Original file line number Diff line number Diff line change @@ -36,14 +36,22 @@ const getRangeForChangedLines = (line: string) => {
36
36
range . groups ?. linesCountDelimiter && range . groups ?. linesCount
37
37
? parseInt ( range . groups . linesCount )
38
38
: 1 ;
39
+
40
+ const hasAddedLines = linesCount !== 0 ;
39
41
const start : number = parseInt ( range . groups ?. start ) ;
40
42
const end = start + linesCount ;
41
43
42
- return new Range ( start , end ) ;
44
+ return hasAddedLines ? new Range ( start , end ) : null ;
43
45
} ;
44
46
47
+ const removeNullRanges = ( r : Range | null ) : r is Range => r !== null ;
48
+
45
49
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 ) ;
47
55
} ;
48
56
49
57
export { getDiffForFile , getRangesForDiff } ;
You can’t perform that action at this time.
0 commit comments