File tree 2 files changed +22
-5
lines changed
2 files changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,14 @@ export function parsePatch(uniDiff, options = {}) {
7
7
let index = { } ;
8
8
list . push ( index ) ;
9
9
10
+ // Ignore any leading junk
11
+ while ( i < diffstr . length ) {
12
+ if ( ( / ^ I n d e x : / . test ( diffstr [ i ] ) ) || ( / ^ @ @ / . test ( diffstr [ i ] ) ) ) {
13
+ break ;
14
+ }
15
+ i ++ ;
16
+ }
17
+
10
18
let header = ( / ^ I n d e x : ( .* ) / . exec ( diffstr [ i ] ) ) ;
11
19
if ( header ) {
12
20
index . index = header [ 1 ] ;
@@ -31,10 +39,11 @@ export function parsePatch(uniDiff, options = {}) {
31
39
break ;
32
40
} else if ( / ^ @ @ / . test ( diffstr [ i ] ) ) {
33
41
index . hunks . push ( parseHunk ( ) ) ;
34
- } else if ( ! diffstr [ i ] ) {
35
- i ++ ;
36
- } else {
42
+ } else if ( diffstr [ i ] && options . strict ) {
43
+ // Ignore unexpected content unless in strict mode
37
44
throw new Error ( 'Unknown line ' + ( i + 1 ) + ' ' + JSON . stringify ( diffstr [ i ] ) ) ;
45
+ } else {
46
+ i ++ ;
38
47
}
39
48
}
40
49
}
Original file line number Diff line number Diff line change @@ -238,9 +238,17 @@ Index: test2
238
238
parsePatch ( `@@ -1,4 +1 @@` , { strict : true } ) ;
239
239
} ) . to [ 'throw' ] ( 'Removed line count did not match for hunk at line 1' ) ;
240
240
} ) ;
241
- it ( 'should throw on invalid input' , function ( ) {
241
+
242
+ it ( 'should not throw on invalid input' , function ( ) {
243
+ expect ( parsePatch ( 'blit\nblat\nIndex: foo\nfoo' ) )
244
+ . to . eql ( [ {
245
+ hunks : [ ] ,
246
+ index : 'foo'
247
+ } ] ) ;
248
+ } ) ;
249
+ it ( 'should throw on invalid input in strict mode' , function ( ) {
242
250
expect ( function ( ) {
243
- parsePatch ( 'Index: foo\nfoo' ) ;
251
+ parsePatch ( 'Index: foo\nfoo' , { strict : true } ) ;
244
252
} ) . to [ 'throw' ] ( / U n k n o w n l i n e 2 " f o o " / ) ;
245
253
} ) ;
246
254
} ) ;
You can’t perform that action at this time.
0 commit comments