@@ -5,18 +5,23 @@ const range = 2
5
5
export function generateCodeFrame (
6
6
source : string ,
7
7
start : number = 0 ,
8
- end : number = source . length
8
+ end ? : number
9
9
) : string {
10
- const lines = source . split ( / \r ? \n / )
10
+ const lines = source . split ( / \n / )
11
+ . map ( x => x [ x . length - 1 ] === '\r' ? { line : x . substr ( 0 , x . length - 1 ) , increment : 2 } : { line : x , increment : 1 } )
12
+ if ( typeof end === 'undefined' ) {
13
+ end = lines . reduce ( ( s , x ) => s + x . line . length + x . increment , 0 )
14
+ }
11
15
let count = 0
12
16
const res = [ ]
13
17
for ( let i = 0 ; i < lines . length ; i ++ ) {
14
- count += lines [ i ] . length + 1
18
+ count += lines [ i ] . line . length + 1
15
19
if ( count >= start ) {
16
20
for ( let j = i - range ; j <= i + range || end > count ; j ++ ) {
17
- if ( j < 0 || j >= lines . length ) continue
18
- res . push ( `${ j + 1 } ${ repeat ( ` ` , 3 - String ( j + 1 ) . length ) } | ${ lines [ j ] } ` )
19
- const lineLength = lines [ j ] . length
21
+ if ( j < 0 ) continue
22
+ if ( j >= lines . length ) break
23
+ res . push ( `${ j + 1 } ${ repeat ( ` ` , 3 - String ( j + 1 ) . length ) } | ${ lines [ j ] . line } ` )
24
+ const lineLength = lines [ j ] . line . length
20
25
if ( j === i ) {
21
26
// push underline
22
27
const pad = start - ( count - lineLength ) + 1
@@ -27,7 +32,7 @@ export function generateCodeFrame (
27
32
const length = Math . min ( end - count , lineLength )
28
33
res . push ( ` | ` + repeat ( `^` , length ) )
29
34
}
30
- count += lineLength + 1
35
+ count += lineLength + lines [ j ] . increment
31
36
}
32
37
}
33
38
break
0 commit comments