@@ -460,10 +460,14 @@ export function pad(source: string, n = 2): string {
460460 return lines . map ( ( l ) => ` ` . repeat ( n ) + l ) . join ( `\n` )
461461}
462462
463- export function posToNumber (
464- source : string ,
465- pos : number | { line : number ; column : number } ,
466- ) : number {
463+ type Pos = {
464+ /** 1-based */
465+ line : number
466+ /** 0-based */
467+ column : number
468+ }
469+
470+ export function posToNumber ( source : string , pos : number | Pos ) : number {
467471 if ( typeof pos === 'number' ) return pos
468472 const lines = source . split ( splitRE )
469473 const { line, column } = pos
@@ -474,10 +478,7 @@ export function posToNumber(
474478 return start + column
475479}
476480
477- export function numberToPos (
478- source : string ,
479- offset : number | { line : number ; column : number } ,
480- ) : { line : number ; column : number } {
481+ export function numberToPos ( source : string , offset : number | Pos ) : Pos {
481482 if ( typeof offset !== 'number' ) return offset
482483 if ( offset > source . length ) {
483484 throw new Error (
@@ -501,16 +502,16 @@ export function numberToPos(
501502
502503export function generateCodeFrame (
503504 source : string ,
504- start : number | { line : number ; column : number } = 0 ,
505- end ?: number ,
505+ start : number | Pos = 0 ,
506+ end ?: number | Pos ,
506507) : string {
507508 start = posToNumber ( source , start )
508- end = end || start
509+ end = end !== undefined ? posToNumber ( source , end ) : start
509510 const lines = source . split ( splitRE )
510511 let count = 0
511512 const res : string [ ] = [ ]
512513 for ( let i = 0 ; i < lines . length ; i ++ ) {
513- count += lines [ i ] . length + 1
514+ count += lines [ i ] . length
514515 if ( count >= start ) {
515516 for ( let j = i - range ; j <= i + range || end > count ; j ++ ) {
516517 if ( j < 0 || j >= lines . length ) continue
@@ -523,7 +524,7 @@ export function generateCodeFrame(
523524 const lineLength = lines [ j ] . length
524525 if ( j === i ) {
525526 // push underline
526- const pad = Math . max ( start - ( count - lineLength ) + 1 , 0 )
527+ const pad = Math . max ( start - ( count - lineLength ) , 0 )
527528 const length = Math . max (
528529 1 ,
529530 end > count ? lineLength - pad : end - start ,
@@ -539,6 +540,7 @@ export function generateCodeFrame(
539540 }
540541 break
541542 }
543+ count ++
542544 }
543545 return res . join ( '\n' )
544546}
0 commit comments