Files
SwiftLint/Source/SwiftLintFramework/Reporters/JSONReporter.swift
T
Danny Mösch a6c4fd98bc Move files from SwiftLintCore to SwiftLintFramework
Ideally, SwiftLintCore would some day only contain components
that are needed to define rules. Consequently, it would be the
only bundle required to import for (external) rule development.
2024-12-23 12:51:43 +01:00

30 lines
1023 B
Swift

import Foundation
import SourceKittenFramework
/// Reports violations as a JSON array.
struct JSONReporter: Reporter {
// MARK: - Reporter Conformance
static let identifier = "json"
static let isRealtime = false
static let description = "Reports violations as a JSON array."
static func generateReport(_ violations: [StyleViolation]) -> String {
toJSON(violations.map(dictionary(for:)), options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes])
}
// MARK: - Private
private static func dictionary(for violation: StyleViolation) -> [String: Any] {
[
"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.ruleName,
"rule_id": violation.ruleIdentifier,
"reason": violation.reason,
]
}
}