Skip to content

Commit

Permalink
even prettier printing
Browse files Browse the repository at this point in the history
  • Loading branch information
zburke committed May 31, 2024
1 parent 54fe764 commit dd6653f
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/commands/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ const { contextMiddleware } = importLazy('../cli/context-middleware');
const fs = importLazy('fs');
const process = importLazy('process');
const _ = importLazy('lodash');
const chalk = importLazy('chalk');

const { ESLint } = require('eslint');

function createESLintInstance(overrideConfig) {
return new ESLint({ overrideConfig });
}

const lineFormat = (line, column) => `${_.padStart(line, 4)}:${_.padEnd(column, 3)}`;
const messageFormat = (message) => _.padEnd(message, 90);
const severityFormat = (severity) => _.padEnd(severity === 1 ? 'warning' : 'error', 10);
const lineFormat = (line, column) => chalk.green(`${_.padStart(line, 4)}:${_.padEnd(column, 3)}`);
const messageFormat = (message, maxLen) => _.padEnd(message, maxLen);
const severityFormat = (severity) => severity === 1 ? chalk.green(_.padEnd('warning', 10)) : chalk.red(_.padEnd('error', 10));

Check failure on line 17 in lib/commands/lint.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Arrow function used ambiguously with a conditional expression

Check failure on line 17 in lib/commands/lint.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Arrow function used ambiguously with a conditional expression
const messagesFormat = (list) => {
return list.map(m => `${lineFormat(m.line, m.column)}${severityFormat(m.severity)}${messageFormat(m.message)}${m.ruleId}`).join('\n');
const maxLen = Math.max(...list.map(m => m.message.length)) + 2;
return list.map(m => `${lineFormat(m.line, m.column)}${severityFormat(m.severity)}${messageFormat(m.message, maxLen)}${m.ruleId}`).join('\n');
};

const resultFormatter = (message) => (
`${message.filePath}\n${messagesFormat(message.messages)}\n`
`${chalk.underline(message.filePath)}\n${messagesFormat(message.messages)}\n`
);

const lintCommand = async () => {
Expand All @@ -42,13 +45,26 @@ const lintCommand = async () => {
0,
);

const fwarnings = results.reduce(
(acc, result) => acc + result.fixableWarningCount,
0,
);
const ferrors = results.reduce(
(acc, result) => acc + result.fixableErrorCount,
0,
);

for (const m of results.filter(i => i.warningCount > 0 || i.errorCount > 0)) {
console.log(resultFormatter(m));
}

const dingCount = errors + warnings;
const theme = errors ? chalk.bold.red : chalk.bold.green;
if (errors || warnings) {
console.warn(`✖ ${dingCount} problem${dingCount === 1 ? '' : 's'} (${errors} errors, ${warnings} warnings)`);
console.warn(theme(`✖ ${dingCount} problem${dingCount === 1 ? '' : 's'} (${errors} errors, ${warnings} warnings)`));
}
if (ferrors || fwarnings) {
console.warn(theme(` ${ferrors} error${ferrors === 1 ? '' : 's'} and ${fwarnings} warning${fwarnings === 1 ? '' : 's'} potentially fixable with the \`--fix\` option.`));
}

if (errors) {
Expand Down

0 comments on commit dd6653f

Please sign in to comment.