Skip to content

Commit

Permalink
feat: colorize error symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzilong committed Feb 28, 2020
1 parent 4aa5f91 commit 0e6e9de
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 57 deletions.
5 changes: 5 additions & 0 deletions fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ const content = fs.readFileSync( 'index.md', 'utf8' )
console.log( content )

const { level } = this

// Parsing error
function test() {
import( './a' ).then( () => {} )
}
117 changes: 65 additions & 52 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,82 @@ const stringWidth = require( 'string-width' )
const { locate } = require( './locator' )

module.exports = function ( results ) {
let totalErrorCount = 0
let totalWarningCount = 0

const lines = []
results.forEach( result => {
const {
filePath,
source,
output,
errorCount,
warningCount,
messages = []
} = result
results
.forEach( result => {
const {
filePath,
source,
output,
errorCount,
warningCount,
messages = []
} = result

totalErrorCount = totalErrorCount + errorCount
totalWarningCount = totalWarningCount + warningCount

const errorSummary = errorCount > 0 ?
chalk.red.bold( errorCount + ' ' + plur( 'error', errorCount ) ) :
''
const warningSummary = warningCount > 0 ?
chalk.yellow.bold( warningCount + ' ' + plur( 'warning', warningCount ) ) :
''
const errorSummary = errorCount > 0 ?
chalk.red.bold( errorCount + ' ' + plur( 'error', errorCount ) ) :
''
const warningSummary = warningCount > 0 ?
chalk.yellow.bold(
`${ warningCount } ${ plur( 'warning', warningCount ) }`
) :
''

const relativePath = path.relative( '.', filePath )
const basename = path.basename( relativePath )
const dirname = path.dirname( relativePath ) + path.sep
let header = '\n ' + chalk.dim( dirname ) + chalk.dim.bold( basename ) +
` 🔥${ errorCount > 0 ? ' ' : '' }` +
[ errorSummary, warningSummary ].join( ' ' ) +
`🔥\n`
const relativePath = path.relative( '.', filePath )
const basename = path.basename( relativePath )
const dirname = path.dirname( relativePath ) + path.sep
let header = '\n ' + chalk.dim( dirname ) + chalk.dim.bold( basename ) +
` 🔥${ errorCount > 0 ? ' ' : '' }` +
[ errorSummary, warningSummary ].join( ' ' ) +
`🔥\n`

const divider = chalk.dim( '-'.repeat( stringWidth( header ) ) )
header = divider + header + divider
const divider = chalk.dim( '-'.repeat( stringWidth( header ) ) )
header = divider + header + divider

lines.push( header )
lines.push( header )

const locations = messages.map( m => {
let { message } = m
const locations = messages.map( m => {
let { message } = m

message = message.replace(
/\B`(.*?)`\B|\B'(.*?)'\B/g,
( m, p1, p2 ) => chalk.bold( p1 || p2 )
)
message = message.replace( /\s+$/, '' )
message = message.replace(
/\B`(.*?)`\B|\B'(.*?)'\B/g,
( m, p1, p2 ) => chalk.bold( p1 || p2 )
)
message = message.replace( /\s+$/, '' )

const severity = ( m.fatal || m.severity === 2 || m.severity === 'error' ) ?
'error' :
'warning'
const line = Number( m.line || 0 )
const column = Number( m.column || 0 )
const severity = ( m.fatal || m.severity === 2 || m.severity === 'error' ) ?
'error' :
'warning'
const line = Number( m.line || 0 )
const column = Number( m.column || 0 )

return {
severity,
message,
line,
column,
return {
severity,
message,
line,
column,
}
} )

if ( source || output ) {
try {
const codeframe = locate( source || output, locations )
lines.push( codeframe )
} catch ( e ) {
console.log( e )
}
}
} )

if ( source || output ) {
try {
const codeframe = locate( source || output, locations )
lines.push( codeframe )
} catch ( e ) {
console.log( e )
}
}
} )
if ( totalErrorCount + totalWarningCount > 0 ) {
return '\n' + lines.join( '\n' )
}

return '\n' + lines.join( '\n' )
return ''
}
24 changes: 19 additions & 5 deletions lib/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function locate( source, locations ) {
number: chalk.green,
literal: chalk.green,
'class': chalk.yellow, // eslint-disable-line
'function': chalk.yellow, // eslint-disable-line
'function': chalk.reset.white, // eslint-disable-line
params: chalk.reset.white,
} ).value.split( '\n' )

return lines
Expand All @@ -40,8 +41,9 @@ function locate( source, locations ) {
content: spaces(
digits + 2 +
tabsToSpaces( str.slice( 0, l.column - 1 ) ).length
) + '└─ ' + ( l.severity === 'warning' ? '⚠' : '✖' ) + ' ' +
l.message
) + '└─ ',
severity: l.severity,
message: l.message
}
} ).sort( ( a, b ) => {
return a.spaceLen - b.spaceLen
Expand Down Expand Up @@ -91,8 +93,20 @@ function locate( source, locations ) {
content: ''
} )

return chalk.bgBlack( `${ chalk.dim( lineNum + ':' ) } ${ tabsToSpaces( formattedLines[ i ] ) } ` ) +
`\n${ indicators.map( i => chalk.dim( i.content ) ).join( '\n' ) }`
const lineNo = chalk.dim( lineNum + ':' )
const indentation = tabsToSpaces( formattedLines[ i ] )
const display = i => {
if ( i.severity ) {
return chalk.dim( i.content ) +
`${ i.severity === 'warning' ? chalk.yellow.dim( '⚠' ) : chalk.red( '✖' ) } ` +
chalk.dim( i.message )
}

return chalk.dim( i.content )
}

return chalk.bgBlack( `${ lineNo } ${ indentation } ` ) +
`\n${ indicators.map( display ).join( '\n' ) }`
}

return ''
Expand Down
Binary file modified media/snapshot.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0e6e9de

Please sign in to comment.