@html-validate/stylish
Version:
ESLint compatible stylish formatter
246 lines (238 loc) • 7.42 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// node_modules/text-table/index.js
var require_text_table = __commonJS({
"node_modules/text-table/index.js"(exports, module2) {
module2.exports = function(rows_, opts) {
if (!opts)
opts = {};
var hsep = opts.hsep === void 0 ? " " : opts.hsep;
var align = opts.align || [];
var stringLength = opts.stringLength || function(s) {
return String(s).length;
};
var dotsizes = reduce(rows_, function(acc, row) {
forEach(row, function(c, ix) {
var n = dotindex(c);
if (!acc[ix] || n > acc[ix])
acc[ix] = n;
});
return acc;
}, []);
var rows = map(rows_, function(row) {
return map(row, function(c_, ix) {
var c = String(c_);
if (align[ix] === ".") {
var index = dotindex(c);
var size = dotsizes[ix] + (/\./.test(c) ? 1 : 2) - (stringLength(c) - index);
return c + Array(size).join(" ");
} else
return c;
});
});
var sizes = reduce(rows, function(acc, row) {
forEach(row, function(c, ix) {
var n = stringLength(c);
if (!acc[ix] || n > acc[ix])
acc[ix] = n;
});
return acc;
}, []);
return map(rows, function(row) {
return map(row, function(c, ix) {
var n = sizes[ix] - stringLength(c) || 0;
var s = Array(Math.max(n + 1, 1)).join(" ");
if (align[ix] === "r" || align[ix] === ".") {
return s + c;
}
if (align[ix] === "c") {
return Array(Math.ceil(n / 2 + 1)).join(" ") + c + Array(Math.floor(n / 2 + 1)).join(" ");
}
return c + s;
}).join(hsep).replace(/\s+$/, "");
}).join("\n");
};
function dotindex(c) {
var m = /\.[^.]*$/.exec(c);
return m ? m.index + 1 : c.length;
}
function reduce(xs, f, init) {
if (xs.reduce)
return xs.reduce(f, init);
var i = 0;
var acc = arguments.length >= 3 ? init : xs[i++];
for (; i < xs.length; i++) {
f(acc, xs[i], i);
}
return acc;
}
function forEach(xs, f) {
if (xs.forEach)
return xs.forEach(f);
for (var i = 0; i < xs.length; i++) {
f.call(xs, xs[i], i);
}
}
function map(xs, f) {
if (xs.map)
return xs.map(f);
var res = [];
for (var i = 0; i < xs.length; i++) {
res.push(f.call(xs, xs[i], i));
}
return res;
}
}
});
// src/index.ts
var src_exports = {};
__export(src_exports, {
Severity: () => Severity,
stylish: () => stylish
});
module.exports = __toCommonJS(src_exports);
// src/stylish.ts
var import_text_table = __toESM(require_text_table());
// src/colors-browser.ts
function passtru(value) {
return value;
}
var reset = passtru;
var bold = passtru;
var dim = passtru;
var underline = passtru;
var yellow = passtru;
var red = passtru;
// src/severity.ts
var Severity = /* @__PURE__ */ ((Severity2) => {
Severity2[Severity2["OFF"] = 0] = "OFF";
Severity2[Severity2["WARN"] = 1] = "WARN";
Severity2[Severity2["ERROR"] = 2] = "ERROR";
return Severity2;
})(Severity || {});
// src/stylish.ts
function pluralize(word, count) {
return count === 1 ? word : `${word}s`;
}
function centerLineColumn(el) {
return el.replace(/(\d+)\s+(\d+)/u, (_m, p1, p2) => dim(`${p1}:${p2}`));
}
function stripAnsi(text) {
return text.replace(/\u001B\[[0-9;]*m/g, "");
}
function stylish(results) {
let output = "\n";
let errorCount = 0;
let warningCount = 0;
let fixableErrorCount = 0;
let fixableWarningCount = 0;
let summaryColor = yellow;
results.forEach((result) => {
const messages = result.messages;
if (messages.length === 0) {
return;
}
errorCount += result.errorCount;
warningCount += result.warningCount;
fixableErrorCount += result.fixableErrorCount;
fixableWarningCount += result.fixableWarningCount;
const rows = messages.map((message) => {
let messageType;
if (Boolean(message.fatal) || message.severity === 2 /* ERROR */) {
messageType = red("error");
summaryColor = red;
} else {
messageType = yellow("warning");
}
return [
"",
message.line ?? 0,
message.column ?? 0,
messageType,
message.message.replace(/([^ ])\.$/u, "$1"),
message.ruleId ? dim(message.ruleId) : ""
];
});
const options = {
align: ["l", "r", "l"],
stringLength(str) {
return stripAnsi(str).length;
}
};
const formattedTable = (0, import_text_table.default)(rows, options).split("\n").map(centerLineColumn).join("\n");
output += `${underline(result.filePath)}
`;
output += `${formattedTable}
`;
});
const total = errorCount + warningCount;
if (total > 0) {
output += summaryColor(
bold(
[
"\u2716 ",
total,
pluralize(" problem", total),
" (",
errorCount,
pluralize(" error", errorCount),
", ",
warningCount,
pluralize(" warning", warningCount),
")\n"
].join("")
)
);
if (fixableErrorCount > 0 || fixableWarningCount > 0) {
output += summaryColor(
bold(
[
" ",
fixableErrorCount,
pluralize(" error", fixableErrorCount),
" and ",
fixableWarningCount,
pluralize(" warning", fixableWarningCount),
" potentially fixable with the `--fix` option.\n"
].join("")
)
);
}
}
return total > 0 ? reset(output) : "";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Severity,
stylish
});
//# sourceMappingURL=browser.js.map