Files
SwiftLint/Source/SwiftLintFramework/Reporters/CSVReporter.swift
T
JP Simard 5bf0f89835 Revert "fix escaped double quote character"
This reverts commit a0a76d898f.

Turns out that the previous version was correct.
See https://en.wikipedia.org/wiki/Comma-separated_values
2016-01-13 18:22:00 -08:00

55 lines
1.6 KiB
Swift

//
// CSVReporter.swift
// SwiftLint
//
// Created by JP Simard on 9/19/15.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
extension String {
private func escapedForCSV() -> String {
let escapedString = stringByReplacingOccurrencesOfString("\"", withString: "\"\"")
if escapedString.containsString(",") || escapedString.containsString("\n") {
return "\"\(escapedString)\""
}
return escapedString
}
}
public struct CSVReporter: Reporter {
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"
]
return (keys + violations.flatMap(arrayForViolation)).joinWithSeparator(",")
}
private static func arrayForViolation(violation: StyleViolation) -> [String] {
let values: [AnyObject?] = [
violation.location.file?.escapedForCSV(),
violation.location.line,
violation.location.character,
violation.severity.rawValue,
violation.ruleDescription.name.escapedForCSV(),
violation.reason.escapedForCSV(),
violation.ruleDescription.identifier
]
return values.map({ $0?.description ?? "" })
}
}