Files
SwiftLint/Source/SwiftLintFramework/Reporters/CheckstyleReporter.swift
T
JP Simard b83e0991b9 Remove all file headers
The MIT license doesn't require that all files be prepended with this
licensing or copyright information. Realm confirmed that they're ok with this
change. This will enable some companies to contribute to SwiftLint and the
date & authorship information will remain accessible via git source control.
2018-05-04 13:42:02 -07:00

46 lines
1.7 KiB
Swift

import Foundation
public struct CheckstyleReporter: Reporter {
public static let identifier = "checkstyle"
public static let isRealtime = false
public var description: String {
return "Reports violations as Checkstyle XML."
}
public static func generateReport(_ violations: [StyleViolation]) -> String {
return [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<checkstyle version=\"4.3\">",
violations
.group(by: { ($0.location.file ?? "<nopath>").escapedForXML() })
.sorted(by: { $0.key < $1.key })
.map(generateForViolationFile).joined(),
"\n</checkstyle>"
].joined()
}
private static func generateForViolationFile(_ file: String, violations: [StyleViolation]) -> String {
return [
"\n\t<file name=\"", file, "\">\n",
violations.map(generateForSingleViolation).joined(),
"\t</file>"
].joined()
}
private static func generateForSingleViolation(_ violation: StyleViolation) -> String {
let line: Int = violation.location.line ?? 0
let col: Int = violation.location.character ?? 0
let severity: String = violation.severity.rawValue
let reason: String = violation.reason.escapedForXML()
let identifier: String = violation.ruleDescription.identifier
let source: String = "swiftlint.rules.\(identifier)".escapedForXML()
return [
"\t\t<error line=\"\(line)\" ",
"column=\"\(col)\" ",
"severity=\"", severity, "\" ",
"message=\"", reason, "\" ",
"source=\"\(source)\"/>\n"
].joined()
}
}