@@ -22,6 +22,8 @@ export interface Options {
2222 linesAbove ?: number ;
2323 /** The number of lines to show below the error. default: 3 */
2424 linesBelow ?: number ;
25+ /** The line number corresponding to the first line in `rawLines`. default: 1 */
26+ startLine ?: number ;
2527 /**
2628 * Forcibly syntax highlight the code as JavaScript (for non-terminals);
2729 * overrides highlightCode.
@@ -53,6 +55,7 @@ function getMarkerLines(
5355 loc : NodeLocation ,
5456 source : string [ ] ,
5557 opts : Options ,
58+ startLineBaseZero : number ,
5659) : {
5760 start : number ;
5861 end : number ;
@@ -68,9 +71,9 @@ function getMarkerLines(
6871 ...loc . end ,
6972 } ;
7073 const { linesAbove = 2 , linesBelow = 3 } = opts || { } ;
71- const startLine = startLoc . line ;
74+ const startLine = startLoc . line - startLineBaseZero ;
7275 const startColumn = startLoc . column ;
73- const endLine = endLoc . line ;
76+ const endLine = endLoc . line - startLineBaseZero ;
7477 const endColumn = endLoc . column ;
7578
7679 let start = Math . max ( startLine - ( linesAbove + 1 ) , 0 ) ;
@@ -127,13 +130,19 @@ export function codeFrameColumns(
127130) : string {
128131 const shouldHighlight =
129132 opts . forceColor || ( isColorSupported ( ) && opts . highlightCode ) ;
133+ const startLineBaseZero = ( opts . startLine || 1 ) - 1 ;
130134 const defs = getDefs ( shouldHighlight ) ;
131135
132136 const lines = rawLines . split ( NEWLINE ) ;
133- const { start, end, markerLines } = getMarkerLines ( loc , lines , opts ) ;
137+ const { start, end, markerLines } = getMarkerLines (
138+ loc ,
139+ lines ,
140+ opts ,
141+ startLineBaseZero ,
142+ ) ;
134143 const hasColumns = loc . start && typeof loc . start . column === "number" ;
135144
136- const numberMaxWidth = String ( end ) . length ;
145+ const numberMaxWidth = String ( end + startLineBaseZero ) . length ;
137146
138147 const highlightedLines = shouldHighlight ? highlight ( rawLines ) : rawLines ;
139148
@@ -142,7 +151,9 @@ export function codeFrameColumns(
142151 . slice ( start , end )
143152 . map ( ( line , index ) => {
144153 const number = start + 1 + index ;
145- const paddedNumber = ` ${ number } ` . slice ( - numberMaxWidth ) ;
154+ const paddedNumber = ` ${ number + startLineBaseZero } ` . slice (
155+ - numberMaxWidth ,
156+ ) ;
146157 const gutter = ` ${ paddedNumber } |` ;
147158 const hasMarker = markerLines [ number ] ;
148159 const lastMarkerLine = ! markerLines [ number + 1 ] ;
0 commit comments