Custom Formatters
Custom formatters allow you to control how ESLint displays linting results. Whether you need JSON for CI/CD integration, HTML reports for dashboards, or custom formats for specific tools, formatters make it possible.When to Create a Formatter
Create a custom formatter when you need to:- Generate reports for CI/CD systems
- Create HTML dashboards or visualizations
- Integrate with IDEs or editors
- Export to specific file formats (XML, CSV, JUnit)
- Apply custom styling or branding
- Aggregate results across multiple runs
Built-in Formatters:ESLint includes several built-in formatters:
stylish, compact, json, junit, html, and more. Use these as references for creating your own.Formatter Structure
A formatter is a function that receives results and returns a string:Synchronous Formatter
my-formatter.js
Async Formatter
my-async-formatter.js
The Results Parameter
Theresults parameter is an array of LintResult objects:
LintResult Properties
Absolute path to the linted file
Array of lint messages for this file
Number of errors (severity 2)
Number of warnings (severity 1)
Number of errors that can be auto-fixed
Number of warnings that can be auto-fixed
Source code of the file (may be omitted)
Messages that were suppressed by inline comments
LintMessage Properties
ID of the rule that generated the message
Message severity: 0 (off), 1 (warn), 2 (error)
Human-readable error message
1-based line number where the issue occurs
1-based column number where the issue occurs
1-based line number where the issue ends
1-based column number where the issue ends
Auto-fix information (if available)
Alternative fixes that require manual approval
The Context Parameter
Thecontext object provides additional information:
Current working directory
Whether color output was requested (
--color or --no-color)Present if
--max-warnings was exceededmaxWarnings: The limit that was setfoundWarnings: Actual number of warnings
Metadata for all rules that were run
Example: Summary Formatter
Display only error and warning counts:Example: Detailed Formatter
Include file paths and rule information:Example: JSON Formatter
Format as JSON for tool integration:Example: HTML Report
Generate an HTML dashboard:Example: JUnit XML
Generate JUnit-compatible XML for CI systems:Using Environment Variables
Customize formatter behavior with environment variables:Terminal-Friendly Output
Modern terminals support clickable links:Color Support
Add colors when requested:Creating Your Formatter
1
Create Formatter File
my-formatter.js
2
Test Locally
3
Add Features
Enhance with:
- Summary statistics
- Color support
- Rule metadata
- Links to documentation
4
Package for Distribution
Create an npm package (optional):
package.json
Using a Custom Formatter
Local File
npm Package
Packaging a Formatter
1
Create Package Structure
2
Configure package.json
3
Implement Formatter
index.js
4
Publish
Testing Formatters
Advanced Patterns
Piping to External Tools
For complex formatting, use ESLint’s JSON output:Async Formatters
Perform asynchronous operations:Stateful Formatters
Maintain state across runs (use with caution):Best Practices
Error Handling
Real-World Examples
Learn from ESLint’s built-in formatters:stylish
Default formatter with colors and summary
compact
One line per message for easy parsing
json
JSON output for tool integration
html
HTML report generator
junit
JUnit XML for CI systems
checkstyle
Checkstyle XML format
Community Formatters
Explore formatters on npm:eslint-formatter-table- Table formateslint-formatter-gitlab- GitLab code quality formateslint-formatter-pretty- Enhanced stylish formattereslint-formatter-summary- Aggregated summary
Next Steps
Create a Plugin
Bundle formatter with rules and configs
Built-in Formatters
Explore all built-in formatters