UNPKG

4.02 kBJavaScriptView Raw
1import _typeof from "@babel/runtime/helpers/esm/typeof";
2import _inherits from "@babel/runtime/helpers/esm/inherits";
3import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
4import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
5import _wrapNativeSuper from "@babel/runtime/helpers/esm/wrapNativeSuper";
6
7function _wrapRegExp(re, groups) { _wrapRegExp = function _wrapRegExp(re, groups) { return new BabelRegExp(re, undefined, groups); }; var _RegExp = _wrapNativeSuper(RegExp); var _super = RegExp.prototype; var _groups = new WeakMap(); function BabelRegExp(re, flags, groups) { var _this = _RegExp.call(this, re, flags); _groups.set(_this, groups || _groups.get(re)); return _this; } _inherits(BabelRegExp, _RegExp); BabelRegExp.prototype.exec = function (str) { var result = _super.exec.call(this, str); if (result) result.groups = buildGroups(result, this); return result; }; BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { if (typeof substitution === "string") { var groups = _groups.get(this); return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) { return "$" + groups[name]; })); } else if (typeof substitution === "function") { var _this = this; return _super[Symbol.replace].call(this, str, function () { var args = []; args.push.apply(args, arguments); if (_typeof(args[args.length - 1]) !== "object") { args.push(buildGroups(args, _this)); } return substitution.apply(this, args); }); } else { return _super[Symbol.replace].call(this, str, substitution); } }; function buildGroups(result, re) { var g = _groups.get(re); return Object.keys(g).reduce(function (groups, name) { groups[name] = result[g[name]]; return groups; }, Object.create(null)); } return _wrapRegExp.apply(this, arguments); }
8
9var os = require('os');
10
11var chalk = require('chalk');
12
13var stripAnsi = require('strip-ansi');
14
15var table = require('text-table');
16
17function pluralize(word, count) {
18 return count === 1 ? word : "".concat(word, "s");
19}
20
21module.exports = function (results) {
22 var output = os.EOL;
23 var errorCount = 0;
24 var warningCount = 0;
25 var fixableErrorCount = 0;
26 var fixableWarningCount = 0;
27 var summaryColor = 'yellow';
28 results.forEach(function (result) {
29 var messages = result.messages;
30
31 if (messages.length === 0) {
32 return;
33 }
34
35 errorCount += result.errorCount;
36 warningCount += result.warningCount;
37 fixableErrorCount += result.fixableErrorCount;
38 fixableWarningCount += result.fixableWarningCount;
39 output += table(messages.map(function (message) {
40 var messageType;
41
42 if (message.fatal || message.severity === 2) {
43 messageType = chalk.red('error');
44 summaryColor = 'red';
45 } else {
46 messageType = chalk.yellow('warning');
47 }
48
49 return ['', message.line || 0, message.column || 0, messageType, message.message.replace(_wrapRegExp(/([\0-\x1F!-\uFFFF])\.$/, {
50 msg: 1
51 }), '$1'), chalk.dim(message.ruleId || '')];
52 }), {
53 align: ['', 'r', 'l'],
54 stringLength: function stringLength(str) {
55 return stripAnsi(str).length;
56 }
57 }).split(os.EOL).map(function (el) {
58 return el.replace(_wrapRegExp(/([0-9]+)[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]+([0-9]+)/, {
59 line: 1,
60 column: 2
61 }), function (m, p1, p2) {
62 return chalk.dim("".concat(p1, ":").concat(p2));
63 });
64 }).join(os.EOL) + os.EOL;
65 });
66 var total = errorCount + warningCount;
67
68 if (total > 0) {
69 if (fixableErrorCount > 0 || fixableWarningCount > 0) {
70 output += os.EOL + // $FlowExpectedError: indexer property is missing in Chalk (problem of Chalk)
71 chalk[summaryColor].bold([' ', fixableErrorCount, pluralize(' error', fixableErrorCount), ' and ', fixableWarningCount, pluralize(' warning', fixableWarningCount), ' potentially fixable with the `--fix` option.\n'].join(''));
72 }
73 }
74
75 return total > 0 ? output : '';
76};
\No newline at end of file