You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.6 KiB

4 years ago
  1. /**
  2. * @fileoverview HTML reporter
  3. * @author Julian Laval
  4. */
  5. "use strict";
  6. const lodash = require("lodash");
  7. const fs = require("fs");
  8. const path = require("path");
  9. //------------------------------------------------------------------------------
  10. // Helpers
  11. //------------------------------------------------------------------------------
  12. const pageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-page.html"), "utf-8"));
  13. const messageTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-message.html"), "utf-8"));
  14. const resultTemplate = lodash.template(fs.readFileSync(path.join(__dirname, "html-template-result.html"), "utf-8"));
  15. /**
  16. * Given a word and a count, append an s if count is not one.
  17. * @param {string} word A word in its singular form.
  18. * @param {int} count A number controlling whether word should be pluralized.
  19. * @returns {string} The original word with an s on the end if count is not one.
  20. */
  21. function pluralize(word, count) {
  22. return (count === 1 ? word : `${word}s`);
  23. }
  24. /**
  25. * Renders text along the template of x problems (x errors, x warnings)
  26. * @param {string} totalErrors Total errors
  27. * @param {string} totalWarnings Total warnings
  28. * @returns {string} The formatted string, pluralized where necessary
  29. */
  30. function renderSummary(totalErrors, totalWarnings) {
  31. const totalProblems = totalErrors + totalWarnings;
  32. let renderedText = `${totalProblems} ${pluralize("problem", totalProblems)}`;
  33. if (totalProblems !== 0) {
  34. renderedText += ` (${totalErrors} ${pluralize("error", totalErrors)}, ${totalWarnings} ${pluralize("warning", totalWarnings)})`;
  35. }
  36. return renderedText;
  37. }
  38. /**
  39. * Get the color based on whether there are errors/warnings...
  40. * @param {string} totalErrors Total errors
  41. * @param {string} totalWarnings Total warnings
  42. * @returns {int} The color code (0 = green, 1 = yellow, 2 = red)
  43. */
  44. function renderColor(totalErrors, totalWarnings) {
  45. if (totalErrors !== 0) {
  46. return 2;
  47. }
  48. if (totalWarnings !== 0) {
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. /**
  54. * Get HTML (table rows) describing the messages.
  55. * @param {Array} messages Messages.
  56. * @param {int} parentIndex Index of the parent HTML row.
  57. * @param {Object} rulesMeta Dictionary containing metadata for each rule executed by the analysis.
  58. * @returns {string} HTML (table rows) describing the messages.
  59. */
  60. function renderMessages(messages, parentIndex, rulesMeta) {
  61. /**
  62. * Get HTML (table row) describing a message.
  63. * @param {Object} message Message.
  64. * @returns {string} HTML (table row) describing a message.
  65. */
  66. return lodash.map(messages, message => {
  67. const lineNumber = message.line || 0;
  68. const columnNumber = message.column || 0;
  69. let ruleUrl;
  70. if (rulesMeta) {
  71. const meta = rulesMeta[message.ruleId];
  72. ruleUrl = lodash.get(meta, "docs.url", null);
  73. }
  74. return messageTemplate({
  75. parentIndex,
  76. lineNumber,
  77. columnNumber,
  78. severityNumber: message.severity,
  79. severityName: message.severity === 1 ? "Warning" : "Error",
  80. message: message.message,
  81. ruleId: message.ruleId,
  82. ruleUrl
  83. });
  84. }).join("\n");
  85. }
  86. // eslint-disable-next-line jsdoc/require-description
  87. /**
  88. * @param {Array} results Test results.
  89. * @param {Object} rulesMeta Dictionary containing metadata for each rule executed by the analysis.
  90. * @returns {string} HTML string describing the results.
  91. */
  92. function renderResults(results, rulesMeta) {
  93. return lodash.map(results, (result, index) => resultTemplate({
  94. index,
  95. color: renderColor(result.errorCount, result.warningCount),
  96. filePath: result.filePath,
  97. summary: renderSummary(result.errorCount, result.warningCount)
  98. }) + renderMessages(result.messages, index, rulesMeta)).join("\n");
  99. }
  100. //------------------------------------------------------------------------------
  101. // Public Interface
  102. //------------------------------------------------------------------------------
  103. module.exports = function(results, data) {
  104. let totalErrors,
  105. totalWarnings;
  106. const metaData = data ? data.rulesMeta : {};
  107. totalErrors = 0;
  108. totalWarnings = 0;
  109. // Iterate over results to get totals
  110. results.forEach(result => {
  111. totalErrors += result.errorCount;
  112. totalWarnings += result.warningCount;
  113. });
  114. return pageTemplate({
  115. date: new Date(),
  116. reportColor: renderColor(totalErrors, totalWarnings),
  117. reportSummary: renderSummary(totalErrors, totalWarnings),
  118. results: renderResults(results, metaData)
  119. });
  120. };