Skip to content

Commit

Permalink
Merge pull request realm#339 from realm/jp-escape-csv
Browse files Browse the repository at this point in the history
escape strings when using CSV reporter
  • Loading branch information
jpsim committed Jan 11, 2016
2 parents 0d1c3d2 + a0a76d8 commit fe4ff29
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
[Norio Nomura](https://github.com/norio-nomura)
[#324](https://github.com/realm/SwiftLint/issues/324)

* Escape strings when using CSV reporter.
[JP Simard](https://github.com/jpsim)

## 0.5.5: Magic Drying Fluff Balls™

<http://www.amazon.com/Magic-Drying-Fluff-Balls-Softening/dp/B001EIW1SG>
Expand Down
16 changes: 13 additions & 3 deletions Source/SwiftLintFramework/Reporters/CSVReporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@

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
Expand All @@ -31,12 +41,12 @@ public struct CSVReporter: Reporter {

private static func arrayForViolation(violation: StyleViolation) -> [String] {
let values: [AnyObject?] = [
violation.location.file,
violation.location.file?.escapedForCSV(),
violation.location.line,
violation.location.character,
violation.severity.rawValue,
violation.ruleDescription.name,
violation.reason,
violation.ruleDescription.name.escapedForCSV(),
violation.reason.escapedForCSV(),
violation.ruleDescription.identifier
]
return values.map({ $0?.description ?? "" })
Expand Down

0 comments on commit fe4ff29

Please sign in to comment.