eslintFormatter.js 2.27 KB
Newer Older
Dan Abramov's avatar
Dan Abramov committed
1
2
3
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 *
Sophie Alpert's avatar
Sophie Alpert committed
4
5
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
Dan Abramov's avatar
Dan Abramov committed
6
7
 */

8
9
10
'use strict';

const chalk = require('chalk');
Dan Abramov's avatar
Dan Abramov committed
11
const stripAnsi = require('strip-ansi');
12
13
14
15
16
17
18
19
20
21
22
const table = require('text-table');

function isError(message) {
  if (message.fatal || message.severity === 2) {
    return true;
  }
  return false;
}

function formatter(results) {
  let output = '\n';
Dan Abramov's avatar
Dan Abramov committed
23
  let hasErrors = false;
24
  let reportContainsErrorRuleIDs = false;
25
26
27
28
29
30
31
32
33
34
35
36

  results.forEach(result => {
    let messages = result.messages;
    if (messages.length === 0) {
      return;
    }

    messages = messages.map(message => {
      let messageType;
      if (isError(message)) {
        messageType = 'error';
        hasErrors = true;
37
38
39
        if (message.ruleId) {
          reportContainsErrorRuleIDs = true;
        }
40
41
42
43
44
      } else {
        messageType = 'warn';
      }

      let line = message.line || 0;
45
      let position = chalk.bold('Line ' + line + ':');
46
47
48
49
50
      return [
        '',
        position,
        messageType,
        message.message.replace(/\.$/, ''),
51
        chalk.underline(message.ruleId || ''),
52
53
54
55
56
57
58
59
      ];
    });

    // if there are error messages, we want to show only errors
    if (hasErrors) {
      messages = messages.filter(m => m[2] === 'error');
    }

60
    // add color to rule keywords
61
    messages.forEach(m => {
62
      m[4] = m[2] === 'error' ? chalk.red(m[4]) : chalk.yellow(m[4]);
63
      m.splice(2, 1);
64
65
66
67
68
    });

    let outputTable = table(messages, {
      align: ['l', 'l', 'l'],
      stringLength(str) {
Dan Abramov's avatar
Dan Abramov committed
69
        return stripAnsi(str).length;
70
71
72
73
74
75
      },
    });

    output += `${outputTable}\n\n`;
  });

76
  if (reportContainsErrorRuleIDs) {
Dan Abramov's avatar
Dan Abramov committed
77
78
79
80
81
82
83
    // Unlike with warnings, we have to do it here.
    // We have similar code in react-scripts for warnings,
    // but warnings can appear in multiple files so we only
    // print it once at the end. For errors, however, we print
    // it here because we always show at most one error, and
    // we can only be sure it's an ESLint error before exiting
    // this function.
84
85
    output +=
      'Search for the ' +
Dan Abramov's avatar
Dan Abramov committed
86
      chalk.underline(chalk.red('keywords')) +
Dan Abramov's avatar
Dan Abramov committed
87
88
89
      ' to learn more about each error.';
  }

90
91
92
93
  return output;
}

module.exports = formatter;