Skip to content

Commit

Permalink
feat: add auto fixable hint
Browse files Browse the repository at this point in the history
  • Loading branch information
fengzilong committed Feb 29, 2020
1 parent 387f6ff commit eecb5c9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
21 changes: 16 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ const path = require( 'path' )
const chalk = require( 'chalk' )
const plur = require( 'plur' )
const stringWidth = require( 'string-width' )
const gradient = require( 'gradient-string' )
const { locate } = require( './locator' )

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

const lines = []
results
Expand Down Expand Up @@ -52,9 +54,13 @@ module.exports = function ( results ) {
/\B`(.*?)`\B|\B'(.*?)'\B/g,
( m, p1, p2 ) => chalk.bold( p1 || p2 )
)
message = message.replace( /\s+$/, '' )
message = message.replace( /\s+$/, '' );

const severity = ( m.fatal || m.severity === 2 || m.severity === 'error' ) ?
if ( Boolean( m.fix ) ) {
autoFixableCount = autoFixableCount + 1
}

const severity = (m.fatal || m.severity === 2 || m.severity === 'error' ) ?
'error' :
'warning'
const line = Number( m.line || 0 )
Expand All @@ -65,6 +71,7 @@ module.exports = function ( results ) {
message,
line,
column,
fixable: Boolean( m.fix ),
}
} )

Expand All @@ -79,10 +86,14 @@ module.exports = function ( results ) {
} )

if ( totalErrorCount + totalWarningCount > 0 ) {
const totalErrorMessage = chalk.red( `${ totalErrorCount } ${ plur( 'error', totalErrorCount ) }` )
const totalWarningMessage = chalk.yellow( `${ totalWarningCount } ${ plur( 'warning', totalWarningCount ) }` )
const totalErrorMessage = chalk.redBright( ` ${ chalk.bold( totalErrorCount ) } ${ plur( 'error', totalErrorCount ) }` )
const totalWarningMessage = chalk.yellowBright( ` ${ chalk.bold( totalWarningCount ) } ${ plur( 'warning', totalWarningCount ) }` )
const totalFixableMessage = chalk.greenBright( ` ${ chalk.bold( autoFixableCount ) } ${ plur( 'auto-fixable', autoFixableCount ) } ( with the \`--fix\` flag )` )
const maxLen = stringWidth( totalFixableMessage ) + 1
return `\n${ lines.join( '\n' ) }\n` +
`${ totalErrorMessage }\n${ totalWarningMessage }\n`
`${ gradient.fruit( `-`.repeat( maxLen ) ) }\n` +
`${ totalErrorMessage }\n${ totalWarningMessage }\n${ totalFixableMessage }\n` +
`${ gradient.cristal( `-`.repeat( maxLen ) ) }\n`
}

return ''
Expand Down
8 changes: 6 additions & 2 deletions lib/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ function locate( source, locations ) {
tabsToSpaces( str.slice( 0, l.column - 1 ) ).length
) + '└─ ',
severity: l.severity,
message: l.message
message: l.message,
fixable: l.fixable,
}
} ).sort( ( a, b ) => {
return a.spaceLen - b.spaceLen
Expand Down Expand Up @@ -97,9 +98,12 @@ function locate( source, locations ) {
const indentation = tabsToSpaces( formattedLines[ i ] )
const display = i => {
if ( i.severity ) {
const fixableMessage = i.fixable === true ?
chalk.dim( ' ( auto-fixable )' ) :
''
return chalk.dim( i.content ) +
`${ i.severity === 'warning' ? chalk.yellow.dim( '⚠' ) : chalk.red( '✖' ) } ` +
chalk.dim( i.message )
chalk.dim( i.message ) + fixableMessage
}

return chalk.dim( i.content )
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"chalk": "^3.0.0",
"emphasize": "^3.0.0",
"gradient-string": "^1.2.0",
"plur": "^4.0.0",
"string-width": "^4.2.0"
},
Expand Down
28 changes: 27 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
resolved "https://registry.npmjs.org/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==

"@types/tinycolor2@^1.4.0":
version "1.4.2"
resolved "https://registry.npmjs.org/@types/tinycolor2/-/tinycolor2-1.4.2.tgz#721ca5c5d1a2988b4a886e35c2ffc5735b6afbdf"
integrity sha512-PeHg/AtdW6aaIO2a+98Xj7rWY4KC1E6yOy7AFknJQ7VXUGNrMlyxDFxJo7HqLtjQms/ZhhQX52mLVW/EX3JGOw==

acorn-jsx@^5.1.0:
version "5.1.0"
resolved "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.1.0.tgz#294adb71b57398b0680015f0a38c563ee1db5384"
Expand Down Expand Up @@ -105,7 +110,7 @@ callsites@^3.0.0:
resolved "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73"
integrity sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==

chalk@^2.0.0, chalk@^2.1.0, chalk@^2.4.2:
chalk@^2.0.0, chalk@^2.1.0, chalk@^2.4.1, chalk@^2.4.2:
version "2.4.2"
resolved "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424"
integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==
Expand Down Expand Up @@ -428,6 +433,14 @@ globals@^12.1.0:
dependencies:
type-fest "^0.8.1"

gradient-string@^1.2.0:
version "1.2.0"
resolved "https://registry.npmjs.org/gradient-string/-/gradient-string-1.2.0.tgz#93f39f2c7c8dcb095608c2ccf0aac24aa315fbac"
integrity sha512-Lxog7IDMMWNjwo4O0KbdBvSewk4vW6kQe5XaLuuPCyCE65AGQ1P8YqKJa5dq8TYf/Ge31F+KjWzPR5mAJvjlAg==
dependencies:
chalk "^2.4.1"
tinygradient "^0.4.1"

has-flag@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
Expand Down Expand Up @@ -851,6 +864,19 @@ through@^2.3.6:
resolved "https://registry.npmjs.org/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=

tinycolor2@^1.0.0:
version "1.4.1"
resolved "https://registry.npmjs.org/tinycolor2/-/tinycolor2-1.4.1.tgz#f4fad333447bc0b07d4dc8e9209d8f39a8ac77e8"
integrity sha1-9PrTM0R7wLB9TcjpIJ2POaisd+g=

tinygradient@^0.4.1:
version "0.4.3"
resolved "https://registry.npmjs.org/tinygradient/-/tinygradient-0.4.3.tgz#0a8dfde56f8865deec4c435a51bd5b0c0dec59fa"
integrity sha512-tBPYQSs6eWukzzAITBSmqcOwZCKACvRa/XjPPh1mj4mnx4G3Drm51HxyCTU/TKnY8kG4hmTe5QlOh9O82aNtJQ==
dependencies:
"@types/tinycolor2" "^1.4.0"
tinycolor2 "^1.0.0"

tmp@^0.0.33:
version "0.0.33"
resolved "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz#6d34335889768d21b2bcda0aa277ced3b1bfadf9"
Expand Down

0 comments on commit eecb5c9

Please sign in to comment.