This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "&") | ||
.replace(/</g, "<") | ||
.replace(/>/g, ">") | ||
.replace(/'/g, "'") | ||
.replace(/"/g, """); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="&<>'" 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>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters