checkstyle.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @fileoverview CheckStyle XML reporter
  3. * @author Ian Christian Myers
  4. */
  5. "use strict";
  6. const xmlEscape = require("../xml-escape");
  7. //------------------------------------------------------------------------------
  8. // Helper Functions
  9. //------------------------------------------------------------------------------
  10. /**
  11. * Returns the severity of warning or error
  12. * @param {Object} message message object to examine
  13. * @returns {string} severity level
  14. * @private
  15. */
  16. function getMessageType(message) {
  17. if (message.fatal || message.severity === 2) {
  18. return "error";
  19. }
  20. return "warning";
  21. }
  22. //------------------------------------------------------------------------------
  23. // Public Interface
  24. //------------------------------------------------------------------------------
  25. module.exports = function(results) {
  26. let output = "";
  27. output += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  28. output += "<checkstyle version=\"4.3\">";
  29. results.forEach(result => {
  30. const messages = result.messages;
  31. output += `<file name="${xmlEscape(result.filePath)}">`;
  32. messages.forEach(message => {
  33. output += [
  34. `<error line="${xmlEscape(message.line || 0)}"`,
  35. `column="${xmlEscape(message.column || 0)}"`,
  36. `severity="${xmlEscape(getMessageType(message))}"`,
  37. `message="${xmlEscape(message.message)}${message.ruleId ? ` (${message.ruleId})` : ""}"`,
  38. `source="${message.ruleId ? xmlEscape(`eslint.rules.${message.ruleId}`) : ""}" />`
  39. ].join(" ");
  40. });
  41. output += "</file>";
  42. });
  43. output += "</checkstyle>";
  44. return output;
  45. };