4 * Copyright 2018 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
27 /* tslint:enable:object-literal-sort-keys */
28 Formatter.prototype.format = function (failures, _fixes, fileNames) {
30 var groupedFailures = {};
31 for (var _i = 0, failures_1 = failures; _i < failures_1.length; _i++) {
32 var failure = failures_1[_i];
33 var fileName = failure.getFileName();
34 if (groupedFailures[fileName] !== undefined) {
35 groupedFailures[fileName].push(failure);
38 groupedFailures[fileName] = [failure];
41 if (fileNames === undefined) {
42 fileNames = Object.keys(groupedFailures);
44 var formattedFiles = fileNames.map(function (fileName) {
45 var formattedFailures = groupedFailures[fileName] !== undefined
46 ? groupedFailures[fileName].map(function (f) { return _this.formatFailure(f); })
48 var joinedFailures = formattedFailures.join(""); // may be empty
49 return "<file name=\"" + _this.escapeXml(fileName) + "\">" + joinedFailures + "</file>";
51 var joinedFiles = formattedFiles.join("");
52 return "<?xml version=\"1.0\" encoding=\"utf-8\"?><checkstyle version=\"4.3\">" + joinedFiles + "</checkstyle>";
54 Formatter.prototype.formatFailure = function (failure) {
55 var line = failure.getStartPosition().getLineAndCharacter().line + 1;
56 var column = failure.getStartPosition().getLineAndCharacter().character + 1;
57 var severity = failure.getRuleSeverity();
58 var message = this.escapeXml(failure.getFailure());
59 // checkstyle parser wants "source" to have structure like <anything>dot<category>dot<type>
60 var source = "failure.tslint." + this.escapeXml(failure.getRuleName());
61 return "<error line=\"" + line + "\" column=\"" + column + "\" severity=\"" + severity + "\" message=\"" + message + "\" source=\"" + source + "\" />";
63 Formatter.prototype.escapeXml = function (str) {
65 .replace(/&/g, "&")
66 .replace(/</g, "<")
67 .replace(/>/g, ">")
68 .replace(/'/g, "'")
69 .replace(/"/g, """);
71 /* tslint:disable:object-literal-sort-keys */
72 Formatter.metadata = {
73 formatterName: "checkstyle",
74 description: "Formats errors as though they were Checkstyle output.",
75 descriptionDetails: Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Imitates the XMLLogger from Checkstyle 4.3. All failures have the 'warning' severity. Files without errors are still included."], ["\n Imitates the XMLLogger from Checkstyle 4.3. All failures have the 'warning' severity. Files without errors are still included."]))),
76 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>"]))),
80 }(abstractFormatter_1.AbstractFormatter));
81 exports.Formatter = Formatter;
82 var templateObject_1, templateObject_2;