1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | const chalk = require('chalk');
|
11 | const table = require('text-table');
|
12 |
|
13 | function isError(message) {
|
14 | if (message.fatal || message.severity === 2) {
|
15 | return true;
|
16 | }
|
17 | return false;
|
18 | }
|
19 |
|
20 | function formatter(results) {
|
21 | let output = '\n';
|
22 | let hasErrors = false;
|
23 | let reportContainsErrorRuleIDs = false;
|
24 |
|
25 | results.forEach(result => {
|
26 | let messages = result.messages;
|
27 | if (messages.length === 0) {
|
28 | return;
|
29 | }
|
30 |
|
31 | messages = messages.map(message => {
|
32 | let messageType;
|
33 | if (isError(message)) {
|
34 | messageType = 'error';
|
35 | hasErrors = true;
|
36 | if (message.ruleId) {
|
37 | reportContainsErrorRuleIDs = true;
|
38 | }
|
39 | } else {
|
40 | messageType = 'warn';
|
41 | }
|
42 |
|
43 | let line = message.line || 0;
|
44 | let position = chalk.bold('Line ' + line + ':');
|
45 | return [
|
46 | '',
|
47 | position,
|
48 | messageType,
|
49 | message.message.replace(/\.$/, ''),
|
50 | chalk.underline(message.ruleId || ''),
|
51 | ];
|
52 | });
|
53 |
|
54 |
|
55 | if (hasErrors) {
|
56 | messages = messages.filter(m => m[2] === 'error');
|
57 | }
|
58 |
|
59 |
|
60 | messages.forEach(m => {
|
61 | m[4] = m[2] === 'error' ? chalk.red(m[4]) : chalk.yellow(m[4]);
|
62 | m.splice(2, 1);
|
63 | });
|
64 |
|
65 | let outputTable = table(messages, {
|
66 | align: ['l', 'l', 'l'],
|
67 | stringLength(str) {
|
68 | return chalk.stripColor(str).length;
|
69 | },
|
70 | });
|
71 |
|
72 | output += `${outputTable}\n\n`;
|
73 | });
|
74 |
|
75 | if (reportContainsErrorRuleIDs) {
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | output +=
|
84 | 'Search for the ' +
|
85 | chalk.underline(chalk.red('keywords')) +
|
86 | ' to learn more about each error.';
|
87 | }
|
88 |
|
89 | return output;
|
90 | }
|
91 |
|
92 | module.exports = formatter;
|