Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

97 строки
2.3 KiB

  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. const chalk = require('chalk');
  9. const stripAnsi = require('strip-ansi');
  10. const table = require('text-table');
  11. function isError(message) {
  12. if (message.fatal || message.severity === 2) {
  13. return true;
  14. }
  15. return false;
  16. }
  17. function formatter(results) {
  18. let output = '\n';
  19. let hasErrors = false;
  20. let reportContainsErrorRuleIDs = false;
  21. results.forEach(result => {
  22. let messages = result.messages;
  23. if (messages.length === 0) {
  24. return;
  25. }
  26. messages = messages.map(message => {
  27. let messageType;
  28. if (isError(message)) {
  29. messageType = 'error';
  30. hasErrors = true;
  31. if (message.ruleId) {
  32. reportContainsErrorRuleIDs = true;
  33. }
  34. } else {
  35. messageType = 'warn';
  36. }
  37. let line = message.line || 0;
  38. if (message.column) {
  39. line += ':' + message.column;
  40. }
  41. let position = chalk.bold('Line ' + line + ':');
  42. return [
  43. '',
  44. position,
  45. messageType,
  46. message.message.replace(/\.$/, ''),
  47. chalk.underline(message.ruleId || ''),
  48. ];
  49. });
  50. // if there are error messages, we want to show only errors
  51. if (hasErrors) {
  52. messages = messages.filter(m => m[2] === 'error');
  53. }
  54. // add color to rule keywords
  55. messages.forEach(m => {
  56. m[4] = m[2] === 'error' ? chalk.red(m[4]) : chalk.yellow(m[4]);
  57. m.splice(2, 1);
  58. });
  59. let outputTable = table(messages, {
  60. align: ['l', 'l', 'l'],
  61. stringLength(str) {
  62. return stripAnsi(str).length;
  63. },
  64. });
  65. output += `${outputTable}\n\n`;
  66. });
  67. if (reportContainsErrorRuleIDs) {
  68. // Unlike with warnings, we have to do it here.
  69. // We have similar code in react-scripts for warnings,
  70. // but warnings can appear in multiple files so we only
  71. // print it once at the end. For errors, however, we print
  72. // it here because we always show at most one error, and
  73. // we can only be sure it's an ESLint error before exiting
  74. // this function.
  75. output +=
  76. 'Search for the ' +
  77. chalk.underline(chalk.red('keywords')) +
  78. ' to learn more about each error.';
  79. }
  80. return output;
  81. }
  82. module.exports = formatter;