SwiftLint/Source/SwiftLintFramework/Reporters/JSONReporter.swift

33 lines
1.0 KiB
Swift

import Foundation
import SourceKittenFramework
/// Reports violations as a JSON array.
public struct JSONReporter: Reporter {
// MARK: - Reporter Conformance
public static let identifier = "json"
public static let isRealtime = false
public var description: String {
return "Reports violations as a JSON array."
}
public static func generateReport(_ violations: [StyleViolation]) -> String {
return toJSON(violations.map(dictionary(for:)))
}
// MARK: - Private
private static func dictionary(for violation: StyleViolation) -> [String: Any] {
return [
"file": violation.location.file ?? NSNull() as Any,
"line": violation.location.line ?? NSNull() as Any,
"character": violation.location.character ?? NSNull() as Any,
"severity": violation.severity.rawValue.capitalized,
"type": violation.ruleDescription.name,
"rule_id": violation.ruleDescription.identifier,
"reason": violation.reason
]
}
}