mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
fcf848608e
* Add Example wrapper in order to display test failures inline when running in Xcode. * Stop using Swift 5.1-only features so we can compile on Xcode 10.2. * Wrap strings in Example. * Add Changelog entry. * Wrap all examples in Example struct. * Better and more complete capturing of line numbers. * Fix broken test. * Better test traceability. * Address or disable linting warnings. * Add documentation comments. * Disable linter for a few cases. * Limit mutability and add copy-and-mutate utility functions. * Limit scope of mutability.
53 lines
1.6 KiB
Swift
53 lines
1.6 KiB
Swift
import Foundation
|
|
|
|
/// Reports violations as a newline-separated string of comma-separated values (CSV).
|
|
public struct CSVReporter: Reporter {
|
|
// MARK: - Reporter Conformance
|
|
|
|
public static let identifier = "csv"
|
|
public static let isRealtime = false
|
|
|
|
public var description: String {
|
|
return "Reports violations as a newline-separated string of comma-separated values (CSV)."
|
|
}
|
|
|
|
public static func generateReport(_ violations: [StyleViolation]) -> String {
|
|
let keys = [
|
|
"file",
|
|
"line",
|
|
"character",
|
|
"severity",
|
|
"type",
|
|
"reason",
|
|
"rule_id"
|
|
].joined(separator: ",")
|
|
|
|
let rows = [keys] + violations.map(csvRow(for:))
|
|
return rows.joined(separator: "\n")
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private static func csvRow(for violation: StyleViolation) -> String {
|
|
return [
|
|
violation.location.file?.escapedForCSV() ?? "",
|
|
violation.location.line?.description ?? "",
|
|
violation.location.character?.description ?? "",
|
|
violation.severity.rawValue.capitalized,
|
|
violation.ruleName.escapedForCSV(),
|
|
violation.reason.escapedForCSV(),
|
|
violation.ruleIdentifier
|
|
].joined(separator: ",")
|
|
}
|
|
}
|
|
|
|
private extension String {
|
|
func escapedForCSV() -> String {
|
|
let escapedString = replacingOccurrences(of: "\"", with: "\"\"")
|
|
if escapedString.contains(",") || escapedString.contains("\n") {
|
|
return "\"\(escapedString)\""
|
|
}
|
|
return escapedString
|
|
}
|
|
}
|