checkstyleFormatter.js 4.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 Palantir Technologies, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var tslib_1 = require("tslib");
  20. var abstractFormatter_1 = require("../language/formatter/abstractFormatter");
  21. var Utils = require("../utils");
  22. var Formatter = /** @class */ (function (_super) {
  23. tslib_1.__extends(Formatter, _super);
  24. function Formatter() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. /* tslint:enable:object-literal-sort-keys */
  28. Formatter.prototype.format = function (failures) {
  29. var output = '<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3">';
  30. if (failures.length !== 0) {
  31. var failuresSorted = failures.sort(function (a, b) { return a.getFileName().localeCompare(b.getFileName()); });
  32. var previousFilename = null;
  33. for (var _i = 0, failuresSorted_1 = failuresSorted; _i < failuresSorted_1.length; _i++) {
  34. var failure = failuresSorted_1[_i];
  35. var severity = failure.getRuleSeverity();
  36. if (failure.getFileName() !== previousFilename) {
  37. if (previousFilename !== null) {
  38. output += "</file>";
  39. }
  40. previousFilename = failure.getFileName();
  41. output += "<file name=\"" + this.escapeXml(failure.getFileName()) + "\">";
  42. }
  43. output += "<error line=\"" + (failure.getStartPosition().getLineAndCharacter().line + 1) + "\" ";
  44. output += "column=\"" + (failure.getStartPosition().getLineAndCharacter().character + 1) + "\" ";
  45. output += "severity=\"" + severity + "\" ";
  46. output += "message=\"" + this.escapeXml(failure.getFailure()) + "\" ";
  47. // checkstyle parser wants "source" to have structure like <anything>dot<category>dot<type>
  48. output += "source=\"failure.tslint." + this.escapeXml(failure.getRuleName()) + "\" />";
  49. }
  50. if (previousFilename !== null) {
  51. output += "</file>";
  52. }
  53. }
  54. output += "</checkstyle>";
  55. return output;
  56. };
  57. Formatter.prototype.escapeXml = function (str) {
  58. return str
  59. .replace(/&/g, "&amp;")
  60. .replace(/</g, "&lt;")
  61. .replace(/>/g, "&gt;")
  62. .replace(/'/g, "&#39;")
  63. .replace(/"/g, "&quot;");
  64. };
  65. /* tslint:disable:object-literal-sort-keys */
  66. Formatter.metadata = {
  67. formatterName: "checkstyle",
  68. description: "Formats errors as through they were Checkstyle output.",
  69. descriptionDetails: Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Imitates the XMLLogger from Checkstyle 4.3. All failures have the 'warning' severity."], ["\n Imitates the XMLLogger from Checkstyle 4.3. All failures have the 'warning' severity."]))),
  70. sample: Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n <?xml version=\"1.0\" encoding=\"utf-8\"?>\n <checkstyle version=\"4.3\">\n <file name=\"myFile.ts\">\n <error line=\"1\" column=\"14\" severity=\"warning\" message=\"Missing semicolon\" source=\"failure.tslint.semicolon\" />\n </file>\n </checkstyle>"], ["\n <?xml version=\"1.0\" encoding=\"utf-8\"?>\n <checkstyle version=\"4.3\">\n <file name=\"myFile.ts\">\n <error line=\"1\" column=\"14\" severity=\"warning\" message=\"Missing semicolon\" source=\"failure.tslint.semicolon\" />\n </file>\n </checkstyle>"]))),
  71. consumer: "machine",
  72. };
  73. return Formatter;
  74. }(abstractFormatter_1.AbstractFormatter));
  75. exports.Formatter = Formatter;
  76. var templateObject_1, templateObject_2;