1 # Working with Custom Formatters
3 While ESLint has some built-in formatters available to format the linting results, it's also possible to create and distribute your own custom formatters. You can include custom formatters in your project directly or create an npm package to distribute them separately.
5 Each formatter is just a function that receives a `results` object and returns a string. For example, the following is how the `json` built-in formatter is implemented:
8 //my-awesome-formatter.js
9 module.exports = function(results, context) {
10 return JSON.stringify(results, null, 2);
14 Formatter can also be an async function (from ESLint v8.4.0), the following shows a simple example:
17 //my-awesome-formatter.js
18 module.exports = async function(results) {
19 const formatted = await asyncTask();
24 To run ESLint with this formatter, you can use the `-f` (or `--format`) command line flag:
27 eslint -f ./my-awesome-formatter.js src/
30 In order to use a local file as a custom formatter, you must begin the filename with a dot (such as `./my-awesome-formatter.js` or `../formatters/my-awesome-formatter.js`).
32 ## Packaging the Custom Formatter
34 Custom formatters can also be distributed through npm packages. To do so, create an npm package with a name in the format of `eslint-formatter-*`, where `*` is the name of your formatter (such as `eslint-formatter-awesome`). Projects should then install the package and can use the custom formatter with the `-f` (or `--format`) flag like this:
37 eslint -f awesome src/
40 Because ESLint knows to look for packages beginning with `eslint-formatter-` when the specified formatter doesn't begin with a dot, there is no need to type `eslint-formatter-` when using a packaged custom formatter.
42 Tips for `package.json`:
44 * The `main` entry should be the JavaScript file implementing your custom formatter.
45 * Add these `keywords` to help users find your formatter:
47 * `"eslint-formatter"`
50 See all [formatters on npm](https://www.npmjs.com/search?q=eslint-formatter);
52 ## The `results` Argument
54 The `results` object passed into a formatter is an array of objects containing the lint results for individual files. Here's some example output:
59 filePath: "/path/to/a/file.js",
64 message: "Expected { after 'if' condition.",
67 nodeType: "IfStatement"
70 ruleId: "no-process-exit",
72 message: "Don't use process.exit(); throw an error instead.",
75 nodeType: "CallExpression"
81 fixableWarningCount: 0,
83 "var err = doStuff();\nif (err) console.log('failed tests: ' + err);\nprocess.exit(1);\n"
86 filePath: "/path/to/Gruntfile.js",
91 fixableWarningCount: 0
96 ### The `result` Object
98 <!-- This section is copied from the "Node.js API" page. Changes to this section should
99 also be manually applied to that page. -->
101 Each object in the `results` array is a `result` object. Each `result` object contains the path of the file that was linted and information about linting issues that were encountered. Here are the properties available on each `result` object:
103 * **filePath**: The absolute path to the file that was linted.
104 * **messages**: An array of `message` objects. See below for more info about messages.
105 * **errorCount**: The number of errors for the given file.
106 * **warningCount**: The number of warnings for the given file.
107 * **source**: The source code for the given file. This property is omitted if this file has no errors/warnings or if the `output` property is present.
108 * **output**: The source code for the given file with as many fixes applied as possible. This property is omitted if no fix is available.
110 ### The `message` Object
112 Each `message` object contains information about the ESLint rule that was triggered by some source code. The properties available on each `message` object are:
114 * **ruleId**: the ID of the rule that produced the error or warning.
115 * **severity**: the severity of the failure, `1` for warnings and `2` for errors.
116 * **message**: the human readable description of the error.
117 * **line**: the line where the issue is located.
118 * **column**: the column where the issue is located.
119 * **nodeType**: the type of the node in the [AST](https://github.com/estree/estree/blob/master/spec.md#node-objects)
121 ## The `context` Argument
123 The formatter function receives an object as the second argument. The object has two properties:
125 * `cwd` ... The current working directory. This value comes from the `cwd` constructor option of the [ESLint](nodejs-api.md#-new-eslintoptions) class.
126 * `rulesMeta` ... The `meta` property values of rules. See the [Working with Rules](working-with-rules.md) page for more information about rules.
128 For example, here's what the object would look like if one rule, `no-extra-semi`, had been run:
137 description: "disallow unnecessary semicolons",
139 url: "https://eslint.org/docs/rules/no-extra-semi"
144 unexpected: "Unnecessary semicolon."
151 **Note:** if a linting is executed by deprecated `CLIEngine` class, the `context` argument may be a different value because it is up to the API users. Please check whether the `context` argument is an expected value or not if you want to support legacy environments.
155 ### Summary formatter
157 A formatter that only cares about the total count of errors and warnings will look like this:
160 module.exports = function(results, context) {
161 // accumulate the errors and warnings
162 var summary = results.reduce(
163 function(seq, current) {
164 seq.errors += current.errorCount;
165 seq.warnings += current.warningCount;
168 { errors: 0, warnings: 0 }
171 if (summary.errors > 0 || summary.warnings > 0) {
185 Running `eslint` with the previous custom formatter,
188 eslint -f ./my-awesome-formatter.js src/
191 Will produce the following output:
194 Errors: 2, Warnings: 4
197 ### Detailed formatter
199 A more complex report will look something like this:
202 module.exports = function(results, context) {
203 var results = results || [];
205 var summary = results.reduce(
206 function(seq, current) {
207 current.messages.forEach(function(msg) {
209 filePath: current.filePath,
211 ruleUrl: context.rulesMeta[msg.ruleId].docs.url,
212 message: msg.message,
217 if (msg.severity === 1) {
218 logMessage.type = "warning";
219 seq.warnings.push(logMessage);
221 if (msg.severity === 2) {
222 logMessage.type = "error";
223 seq.errors.push(logMessage);
234 if (summary.errors.length > 0 || summary.warnings.length > 0) {
235 var lines = summary.errors
236 .concat(summary.warnings)
242 msg.ruleId + (msg.ruleUrl ? " (" + msg.ruleUrl + ")" : "") +
258 So running `eslint` with this custom formatter:
261 eslint -f ./my-awesome-formatter.js src/
267 error space-infix-ops (https://eslint.org/docs/rules/space-infix-ops)
268 src/configs/bundler.js:6:8
269 error semi (https://eslint.org/docs/rules/semi)
270 src/configs/bundler.js:6:10
271 warning no-unused-vars (https://eslint.org/docs/rules/no-unused-vars)
272 src/configs/bundler.js:5:6
273 warning no-unused-vars (https://eslint.org/docs/rules/no-unused-vars)
274 src/configs/bundler.js:6:6
275 warning no-shadow (https://eslint.org/docs/rules/no-shadow)
276 src/configs/bundler.js:65:32
277 warning no-unused-vars (https://eslint.org/docs/rules/no-unused-vars)
278 src/configs/clean.js:3:6
281 ## Passing Arguments to Formatters
283 While custom formatter do not receive arguments in addition to the results object, it is possible to pass additional data into formatters.
285 ## Using Environment Variables
287 Custom formatters have access to environment variables and so can change their behavior based on environment variable data. Here's an example that uses a `AF_SKIP_WARNINGS` environment variable to determine whether or not to show warnings in the results:
290 module.exports = function(results) {
291 var skipWarnings = process.env.AF_SKIP_WARNINGS === "true"; //af stands for awesome-formatter
293 var results = results || [];
294 var summary = results.reduce(
295 function(seq, current) {
296 current.messages.forEach(function(msg) {
298 filePath: current.filePath,
300 message: msg.message,
305 if (msg.severity === 1) {
306 logMessage.type = "warning";
307 seq.warnings.push(logMessage);
309 if (msg.severity === 2) {
310 logMessage.type = "error";
311 seq.errors.push(logMessage);
322 if (summary.errors.length > 0 || summary.warnings.length > 0) {
323 var warnings = !skipWarnings ? summary.warnings : []; // skip the warnings in that case
325 var lines = summary.errors
348 You would run ESLint with this custom formatter and an environment variable set like this:
351 AF_SKIP_WARNINGS=true eslint -f ./my-awesome-formatter.js src/
357 error space-infix-ops
358 src/configs/bundler.js:6:8
361 src/configs/bundler.js:6:10
364 ### Complex Argument Passing
366 If you find the custom formatter pattern doesn't provide enough options for the way you'd like to format ESLint results, the best option is to use ESLint's built-in [JSON formatter](https://eslint.org/docs/user-guide/formatters/) and pipe the output to a second program. For example:
369 eslint -f json src/ | your-program-that-reads-JSON --option
372 In this example, the `your-program-that-reads-json` program can accept the raw JSON of ESLint results and process it before outputting its own format of the results. You can pass as many command line arguments to that program as are necessary to customize the output.
374 ## Note: Formatting for Terminals
376 Modern terminals like [iTerm2](https://www.iterm2.com/) or [Guake](http://guake-project.org/) expect a specific results format to automatically open filenames when they are clicked. Most terminals support this format for that purpose: