-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom eslint formatter #2138
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,6 +166,43 @@ compiler.plugin('done', function(stats) { | |
}); | ||
``` | ||
|
||
#### `formatter(results: Object): string` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe "This is our custom ESLint formatter that integrates well with Create React App console output. You can remove it and use the default one instead." |
||
This is an eslint formatter that takes the result generated from eslint and formats the output string | ||
|
||
```js | ||
const formatter = require('react-dev-utils/formatter'); | ||
const webpack = require('webpack'); | ||
|
||
module: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not very clear this is Webpack config. I'd prefer that we show a specific ESLint rule alone, with a comment like // ESLint loader in your webpack config:
// ... |
||
strictExportPresence: true, | ||
rules: [ | ||
{ parser: { requireEnsure: false } }, | ||
{ | ||
test: /\.(js|jsx)$/, | ||
enforce: 'pre', | ||
use: [ | ||
{ | ||
// @remove-on-eject-begin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nobody knows what |
||
// Point ESLint to our predefined config. | ||
options: { | ||
formatter, | ||
baseConfig: { | ||
extends: ['react-app'], | ||
}, | ||
ignore: false, | ||
useEslintrc: false, | ||
}, | ||
// @remove-on-eject-end | ||
loader: 'eslint-loader', | ||
}, | ||
], | ||
include: paths.appSrc, | ||
} | ||
] | ||
} | ||
``` | ||
|
||
#### `getProcessForPort(port: number): string` | ||
|
||
Finds the currently running process on `port`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'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'; | ||
|
||
results.forEach(result => { | ||
let messages = result.messages; | ||
if (messages.length === 0) { | ||
return ''; | ||
} | ||
|
||
let thereAreErrors = false; | ||
messages = messages.map(message => { | ||
let messageType; | ||
if (isError(message)) { | ||
messageType = 'error'; | ||
thereAreErrors = true; | ||
} else { | ||
messageType = 'warn'; | ||
} | ||
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 (thereAreErrors) { | ||
messages = messages.filter(m => m[2] === 'error'); | ||
} | ||
// add color to messageTypes | ||
messages = messages.map(m => { | ||
m[2] = m[2] === 'error' ? chalk.red(m[2]) : chalk.yellow(m[2]); | ||
return m; | ||
}); | ||
let outputTable = table(messages, { | ||
align: ['l', 'l', 'l'], | ||
stringLength(str) { | ||
return chalk.stripColor(str).length; | ||
}, | ||
}); | ||
output += `${outputTable}\n\n`; | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
module.exports = formatter; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make it
eslintFormatter
instead?