Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Add checkstyle formatter (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
sshev authored and jkillian committed Apr 20, 2016
1 parent 3c80d83 commit e8f09c7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/formatters/checkstyleFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {AbstractFormatter} from "../language/formatter/abstractFormatter";
import {RuleFailure} from "../language/rule/rule";

export class Formatter extends AbstractFormatter {
public format(failures: RuleFailure[]): string {
let output = '<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3">';

if (failures.length) {
output += `<file name="${this.escapeXml(failures[0].getFileName())}">`;
for (let failure of failures) {
output += `<error line="${failure.getStartPosition().getLineAndCharacter().line + 1}" `;
output += `column="${failure.getStartPosition().getLineAndCharacter().character + 1}" `;
output += `severity="warning" `;
output += `message="${this.escapeXml(failure.getFailure())}" `;
// checkstyle parser wants "source" to have structure like <anything>dot<category>dot<type>
output += `source="failure.tslint.${this.escapeXml(failure.getRuleName())}" />`;
}
output += "</file>";
}

output += "</checkstyle>";
return output;
}

private escapeXml(str: string): string {
return str
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/'/g, "&#39;")
.replace(/"/g, "&quot;");
}
}
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"tslint-cli.ts",
"tslint.ts",
"utils.ts",
"formatters/checkstyleFormatter.ts",
"formatters/index.ts",
"formatters/jsonFormatter.ts",
"formatters/msbuildFormatter.ts",
Expand Down
40 changes: 40 additions & 0 deletions test/formatters/checkstyleFormatterTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as ts from "typescript";
import {IFormatter, RuleFailure, TestUtils} from "../lint";

describe("Checkstyle Formatter", () => {
const TEST_FILE = "formatters/pmdFormatter.test.ts"; // reuse existing sample file
let sourceFile: ts.SourceFile;
let formatter: IFormatter;

before(() => {
const Formatter = TestUtils.getFormatter("checkstyle");
sourceFile = TestUtils.getSourceFile(TEST_FILE);
formatter = new Formatter();
});

it("formats failures", () => {
const maxPosition = sourceFile.getFullWidth();

const failures = [
new RuleFailure(sourceFile, 0, 1, "first failure", "first-name"),
new RuleFailure(sourceFile, 2, 3, "&<>'\" should be escaped", "escape"),
new RuleFailure(sourceFile, maxPosition - 1, maxPosition, "last failure", "last-name"),
];
const expectedResult =
'<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3">' +
`<file name="${TEST_FILE}">` +
'<error line="1" column="1" severity="warning" message="first failure" source="failure.tslint.first-name" />' +
'<error line="1" column="3" severity="warning" message="&amp;&lt;&gt;&#39;&quot; should be escaped" ' +
'source="failure.tslint.escape" />' +
'<error line="6" column="3" severity="warning" message="last failure" source="failure.tslint.last-name" />' +
"</file>" +
"</checkstyle>";

assert.equal(formatter.format(failures), expectedResult);
});

it("handles no failures", () => {
const result = formatter.format([]);
assert.deepEqual(result, '<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"></checkstyle>');
});
});
2 changes: 2 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"../src/enableDisableRules.ts",
"../src/formatterLoader.ts",
"../src/formatters.ts",
"../src/formatters/checkstyleFormatter.ts",
"../src/formatters/index.ts",
"../src/formatters/jsonFormatter.ts",
"../src/formatters/msbuildFormatter.ts",
Expand Down Expand Up @@ -133,6 +134,7 @@
"ruleTestRunner.ts",
"utils.ts",
"utilsTests.ts",
"formatters/checkstyleFormatterTests.ts",
"formatters/externalFormatterTest.ts",
"formatters/jsonFormatterTests.ts",
"formatters/msbuildFormatterTests.ts",
Expand Down

0 comments on commit e8f09c7

Please sign in to comment.