mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-06 20:11:38 +08:00
R=jacktfranklin@chromium.org Bug: none Change-Id: I961be18530d3ccbc4efed0c2cda9bd603b14e27b Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3372931 Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Tim Van der Lippe <tvanderlippe@chromium.org>
86 lines
2.1 KiB
JavaScript
86 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
const stringFormatter = require('./stringFormatter');
|
|
const { underline, red, yellow, dim, green } = require('picocolors');
|
|
|
|
/** @typedef {import('stylelint').Formatter} Formatter */
|
|
/** @typedef {import('stylelint').Warning} StylelintWarning */
|
|
|
|
/**
|
|
* @type {Formatter}
|
|
*/
|
|
module.exports = function (results, returnValue) {
|
|
let output = stringFormatter(results, returnValue);
|
|
|
|
if (output === '') {
|
|
output = '\n';
|
|
}
|
|
|
|
const sourceWord = results.length > 1 ? 'sources' : 'source';
|
|
const ignoredCount = results.filter((result) => result.ignored).length;
|
|
const checkedDisplay = ignoredCount
|
|
? `${results.length - ignoredCount} of ${results.length}`
|
|
: results.length;
|
|
|
|
output += underline(`${checkedDisplay} ${sourceWord} checked\n`);
|
|
|
|
for (const result of results) {
|
|
let formatting = green;
|
|
|
|
if (result.errored) {
|
|
formatting = red;
|
|
} else if (result.warnings.length) {
|
|
formatting = yellow;
|
|
} else if (result.ignored) {
|
|
formatting = dim;
|
|
}
|
|
|
|
let sourceText = `${result.source}`;
|
|
|
|
if (result.ignored) {
|
|
sourceText += ' (ignored)';
|
|
}
|
|
|
|
output += formatting(` ${sourceText}\n`);
|
|
}
|
|
|
|
const warnings = results.flatMap((r) => r.warnings);
|
|
const warningsBySeverity = groupBy(warnings, (w) => w.severity);
|
|
const problemWord = warnings.length === 1 ? 'problem' : 'problems';
|
|
|
|
output += underline(`\n${warnings.length} ${problemWord} found\n`);
|
|
|
|
for (const [severityLevel, warningList] of Object.entries(warningsBySeverity)) {
|
|
const warningsByRule = groupBy(warningList, (w) => w.rule);
|
|
|
|
output += ` severity level "${severityLevel}": ${warningList.length}\n`;
|
|
|
|
for (const [rule, list] of Object.entries(warningsByRule)) {
|
|
output += dim(` ${rule}: ${list.length}\n`);
|
|
}
|
|
}
|
|
|
|
return `${output}\n`;
|
|
};
|
|
|
|
/**
|
|
* @param {StylelintWarning[]} array
|
|
* @param {(w: StylelintWarning) => string} keyFn
|
|
*/
|
|
function groupBy(array, keyFn) {
|
|
/** @type {{[s: string]: StylelintWarning[]}} */
|
|
const result = {};
|
|
|
|
for (const item of array) {
|
|
const key = keyFn(item);
|
|
|
|
if (!(key in result)) {
|
|
result[key] = [];
|
|
}
|
|
|
|
result[key].push(item);
|
|
}
|
|
|
|
return result;
|
|
}
|