-
-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add custom eslint formatter * Add formatter docs * Update formatter docs * Slightly tweak it * Update README.md
- Loading branch information
Showing
7 changed files
with
120 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
const table = require('text-table'); | ||
|
||
function isError(message) { | ||
if (message.fatal || message.severity === 2) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function formatter(results) { | ||
let output = '\n'; | ||
|
||
let hasErrors = false; | ||
let hasWarnings = false; | ||
|
||
results.forEach(result => { | ||
let messages = result.messages; | ||
if (messages.length === 0) { | ||
return; | ||
} | ||
|
||
let hasErrors = false; | ||
messages = messages.map(message => { | ||
let messageType; | ||
if (isError(message)) { | ||
messageType = 'error'; | ||
hasErrors = true; | ||
} else { | ||
messageType = 'warn'; | ||
hasWarnings = true; | ||
} | ||
|
||
let line = message.line || 0; | ||
let column = message.column || 0; | ||
let position = chalk.dim(`${line}:${column}`); | ||
return [ | ||
'', | ||
position, | ||
messageType, | ||
message.message.replace(/\.$/, ''), | ||
chalk.dim(message.ruleId || ''), | ||
]; | ||
}); | ||
|
||
// if there are error messages, we want to show only errors | ||
if (hasErrors) { | ||
messages = messages.filter(m => m[2] === 'error'); | ||
} | ||
|
||
// add color to messageTypes | ||
messages.forEach(m => { | ||
m[2] = m[2] === 'error' ? chalk.red(m[2]) : chalk.yellow(m[2]); | ||
}); | ||
|
||
let outputTable = table(messages, { | ||
align: ['l', 'l', 'l'], | ||
stringLength(str) { | ||
return chalk.stripColor(str).length; | ||
}, | ||
}); | ||
|
||
output += `${outputTable}\n\n`; | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
module.exports = formatter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters