1+ // oxlint-disable max-depth
12import { styleText } from 'node:util'
23import type { Report } from '../program'
34import type { CliArguments } from '../arguments'
45
6+ // Re-indent because tabs in the terminal tend to be bigger than usual
7+ function indent ( line ?: string ) : string {
8+ return ( line || '' ) . replace ( / ^ \t + / , ( tabs ) => ' ' . repeat ( tabs . length * 4 ) )
9+ }
10+
511export function print ( { report, context } : Report , params : CliArguments ) {
612 if ( report . min_line_coverage . ok ) {
713 console . log ( `${ styleText ( [ 'bold' , 'green' ] , 'Success' ) } : total line coverage is ${ ( report . min_line_coverage . actual * 100 ) . toFixed ( 2 ) } %` )
@@ -14,13 +20,15 @@ export function print({ report, context }: Report, params: CliArguments) {
1420 }
1521
1622 if ( report . min_file_line_coverage . expected !== undefined ) {
17- let { expected, ok } = report . min_file_line_coverage
23+ let { expected, actual , ok } = report . min_file_line_coverage
1824 if ( ok ) {
1925 console . log ( `${ styleText ( [ 'bold' , 'green' ] , 'Success' ) } : all files pass minimum line coverage of ${ expected * 100 } %` )
2026 } else {
2127 let num_files_failed = context . coverage . coverage_per_stylesheet . filter ( ( sheet ) => sheet . line_coverage_ratio < expected ! ) . length
2228 console . error (
23- `${ styleText ( [ 'bold' , 'red' ] , 'Failed' ) } : ${ num_files_failed } files do not meet the minimum line coverage of ${ expected * 100 } %` ,
29+ `${ styleText ( [ 'bold' , 'red' ] , 'Failed' ) } : ${ num_files_failed } files do not meet the minimum line coverage of ${
30+ expected * 100
31+ } % (minimum coverage was ${ ( actual * 100 ) . toFixed ( 2 ) } %)`,
2432 )
2533 if ( params [ 'show-uncovered' ] === 'none' ) {
2634 console . log ( ` Hint: set --show-uncovered=violations to see which files didn't pass` )
@@ -48,33 +56,40 @@ export function print({ report, context }: Report, params: CliArguments) {
4856 console . log ( styleText ( 'dim' , '─' . repeat ( terminal_width ) ) )
4957 console . log ( sheet . url )
5058 console . log ( `Coverage: ${ ( sheet . line_coverage_ratio * 100 ) . toFixed ( 2 ) } %, ${ sheet . covered_lines } /${ sheet . total_lines } lines covered` )
59+
5160 if ( min_file_line_coverage && min_file_line_coverage !== 0 && sheet . line_coverage_ratio < min_file_line_coverage ) {
5261 let lines_to_cover = min_file_line_coverage * sheet . total_lines - sheet . covered_lines
53- console . log ( `💡 Cover ${ Math . ceil ( lines_to_cover ) } more lines to meet the file threshold of ${ min_file_line_coverage * 100 } %` )
62+ console . log ( `Tip: cover ${ Math . ceil ( lines_to_cover ) } more lines to meet the file threshold of ${ min_file_line_coverage * 100 } %` )
5463 }
5564 console . log ( styleText ( 'dim' , '─' . repeat ( terminal_width ) ) )
5665
5766 let lines = sheet . text . split ( '\n' )
5867 let line_coverage = sheet . line_coverage
5968
6069 for ( let i = 0 ; i < lines . length ; i ++ ) {
61- if ( line_coverage [ i ] === 0 ) {
62- // Rewind cursor N lines to render N previous lines
63- for ( let j = i - NUM_LEADING_LINES ; j < i ; j ++ ) {
64- console . log ( styleText ( 'dim' , line_number ( j ) ) , styleText ( 'dim' , lines [ j ] || '' ) )
65- }
66- // Render uncovered lines while increasing cursor until reaching next covered block
67- while ( line_coverage [ i ] === 0 ) {
68- console . log ( styleText ( 'red' , line_number ( i , false ) ) , lines [ i ] )
69- i ++
70- }
71- // Forward cursor N lines to render N trailing lines
72- for ( let end = i + NUM_TRAILING_LINES ; i < end && i < lines . length ; i ++ ) {
73- console . log ( styleText ( 'dim' , line_number ( i ) ) , styleText ( 'dim' , lines [ i ] ! ) )
70+ if ( line_coverage [ i ] === 1 ) continue
71+
72+ // Rewind cursor N lines to render N previous lines
73+ for ( let j = i - NUM_LEADING_LINES ; j < i ; j ++ ) {
74+ // Make sure that we don't try to start before line 0
75+ if ( j >= 0 ) {
76+ console . log ( styleText ( 'dim' , line_number ( j ) ) , styleText ( 'dim' , indent ( lines [ j ] ) ) )
7477 }
75- // Show empty line between blocks
76- console . log ( )
7778 }
79+
80+ // Render uncovered lines while increasing cursor until reaching next covered block
81+ while ( line_coverage [ i ] === 0 ) {
82+ console . log ( styleText ( 'red' , line_number ( i , false ) ) , indent ( lines [ i ] ) )
83+ i ++
84+ }
85+
86+ // Forward cursor N lines to render N trailing lines
87+ for ( let end = i + NUM_TRAILING_LINES ; i < end && i < lines . length ; i ++ ) {
88+ console . log ( styleText ( 'dim' , line_number ( i ) ) , styleText ( 'dim' , indent ( lines [ i ] ) ) )
89+ }
90+
91+ // Show empty line between blocks
92+ console . log ( )
7893 }
7994 }
8095 }
0 commit comments