File tree Expand file tree Collapse file tree 3 files changed +30
-3
lines changed Expand file tree Collapse file tree 3 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,17 @@ exports[`generateCodeFrames > start with position 3`] = `
139
139
"
140
140
` ;
141
141
142
+ exports [` generateCodeFrames > supports more than 1000 lines 1` ] = `
143
+ "
144
+ 1198 | // 1197
145
+ 1199 | // 1198
146
+ 1200 | // 1199
147
+ | ^
148
+ 1201 | // 1200
149
+ 1202 | // 1201
150
+ "
151
+ ` ;
152
+
142
153
exports [` generateCodeFrames > works with CRLF 1` ] = `
143
154
"
144
155
1 | import foo from './foo'
Original file line number Diff line number Diff line change @@ -288,6 +288,9 @@ foo()
288
288
// 2
289
289
// 3
290
290
` . trim ( )
291
+ const veryLongSource = Array . from ( { length : 2000 } , ( _ , i ) => `// ${ i } ` ) . join (
292
+ '\n' ,
293
+ )
291
294
292
295
const expectSnapshot = ( value : string ) => {
293
296
try {
@@ -340,6 +343,10 @@ foo()
340
343
test ( 'invalid start > end' , ( ) => {
341
344
expectSnapshot ( generateCodeFrame ( source , 2 , 0 ) )
342
345
} )
346
+
347
+ test ( 'supports more than 1000 lines' , ( ) => {
348
+ expectSnapshot ( generateCodeFrame ( veryLongSource , { line : 1200 , column : 0 } ) )
349
+ } )
343
350
} )
344
351
345
352
describe ( 'getHash' , ( ) => {
Original file line number Diff line number Diff line change @@ -511,6 +511,11 @@ export function generateCodeFrame(
511
511
end !== undefined ? posToNumber ( source , end ) : start ,
512
512
source . length ,
513
513
)
514
+ const lastPosLine =
515
+ end !== undefined
516
+ ? numberToPos ( source , end ) . line
517
+ : numberToPos ( source , start ) . line + range
518
+ const lineNumberWidth = Math . max ( 3 , String ( lastPosLine ) . length + 1 )
514
519
const lines = source . split ( splitRE )
515
520
let count = 0
516
521
const res : string [ ] = [ ]
@@ -521,7 +526,7 @@ export function generateCodeFrame(
521
526
if ( j < 0 || j >= lines . length ) continue
522
527
const line = j + 1
523
528
res . push (
524
- `${ line } ${ ' ' . repeat ( Math . max ( 3 - String ( line ) . length , 0 ) ) } | ${
529
+ `${ line } ${ ' ' . repeat ( lineNumberWidth - String ( line ) . length ) } | ${
525
530
lines [ j ]
526
531
} `,
527
532
)
@@ -533,11 +538,15 @@ export function generateCodeFrame(
533
538
1 ,
534
539
end > count ? lineLength - pad : end - start ,
535
540
)
536
- res . push ( ` | ` + ' ' . repeat ( pad ) + '^' . repeat ( length ) )
541
+ res . push (
542
+ `${ ' ' . repeat ( lineNumberWidth ) } | ` +
543
+ ' ' . repeat ( pad ) +
544
+ '^' . repeat ( length ) ,
545
+ )
537
546
} else if ( j > i ) {
538
547
if ( end > count ) {
539
548
const length = Math . max ( Math . min ( end - count , lineLength ) , 1 )
540
- res . push ( ` | ` + '^' . repeat ( length ) )
549
+ res . push ( `${ ' ' . repeat ( lineNumberWidth ) } | ` + '^' . repeat ( length ) )
541
550
}
542
551
count += lineLength + 1
543
552
}
You can’t perform that action at this time.
0 commit comments