ESLint Formatter Pretty provides a visually enhanced formatter for ESLint that transforms standard lint output into a more readable and user-friendly format. It features pretty output with color-coding and symbols, automatically sorts results by severity (errors first, then warnings), stylizes inline code blocks in error messages, and includes interactive features like command-clickable rule IDs that open documentation and clickable file headers for editor navigation in supported terminals.
npm install --save-dev eslint-formatter-pretty@5 (for ESLint compatibility)Note: Version 6 is not compatible with ESLint CLI due to limitations. Use version 5 for ESLint integration.
ES Modules:
import eslintFormatterPretty from "eslint-formatter-pretty";CommonJS:
const eslintFormatterPretty = require("eslint-formatter-pretty");eslint --format=pretty file.jsimport eslintFormatterPretty from "eslint-formatter-pretty";
// ESLint results from running ESLint programmatically
const results = [
{
filePath: "./src/index.js",
errorCount: 1,
warningCount: 1,
messages: [
{
line: 10,
column: 5,
severity: 2, // error
message: "Unexpected console statement.",
ruleId: "no-console"
},
{
line: 15,
column: 8,
severity: 1, // warning
message: "Variable 'unused' is defined but never used.",
ruleId: "no-unused-vars"
}
]
}
];
// Format the results
const formatted = eslintFormatterPretty(results);
console.log(formatted);XO (Default formatter):
// No configuration needed - it's the default formattergrunt-eslint:
grunt.initConfig({
eslint: {
target: ['file.js'],
options: {
format: 'pretty'
}
}
});gulp-eslint:
import gulp from 'gulp';
import eslint from 'gulp-eslint';
export const lint = (
gulp.src('file.js')
.pipe(eslint())
.pipe(eslint.format('pretty'))
);webpack eslint-loader:
import eslintFormatterPretty from 'eslint-formatter-pretty';
export default {
entry: ['file.js'],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader',
options: {
formatter: eslintFormatterPretty
}
}
]
}
};The main export is a function that formats ESLint results into a pretty, user-friendly format.
/**
* Pretty formatter for ESLint that transforms standard lint output into readable format
* @param {LintResult[]} results - Lint result for individual files
* @param {LintResultData} [data] - Extended information related to analysis results
* @returns {string} The formatted output string
*/
function eslintFormatterPretty(results, data);Parameters:
results (Array): Array of ESLint LintResult objects containing lint results for individual filesdata (Object, optional): Extended information related to the analysis results, particularly:
rulesMeta: Object containing metadata for rules, used to generate documentation URLsReturns:
String containing the formatted output with colors, symbols, and interactive elements. Returns empty string if no errors or warnings are found.
Key Features:
All type definitions are re-exported from ESLint for convenience:
// Re-exported from ESLint types
type LintResult = ESLint.LintResult;
type LintResultData = ESLint.LintResultData;
type Severity = Linter.Severity;
type LintMessage = Linter.LintMessage;
// Full ESLint Linter namespace is also exported
export { Linter } from 'eslint';interface LintResult {
filePath: string; // Absolute path to the file
errorCount: number; // Number of errors in the file
warningCount: number; // Number of warnings in the file
messages: LintMessage[]; // Array of lint messages
}interface LintMessage {
line?: number; // Line number (1-indexed)
column?: number; // Column number (1-indexed)
severity: 1 | 2; // 1 for warning, 2 for error
message: string; // The lint message text
ruleId?: string | null; // The rule ID that generated this message (null if no rule)
fatal?: boolean; // Whether this is a fatal error
}interface LintResultData {
rulesMeta?: {
[ruleId: string]: {
docs?: {
url?: string; // URL to rule documentation
};
};
};
}supports-hyperlinks package