Files
SwiftLint/Source/SwiftLintFramework/Reporters/CodeClimateReporter.swift
T
JP Simard 05ac3c9d75 Require macOS 12 & Swift 5.6 (#4037)
This will unblock using Swift Concurrency features and updating to the
latest versions of Swift Argument Parser.

This won't drop support for linting projects with an older toolchain /
Xcode selected, as long as SwiftLint was _built_ with 5.6+ and is
_running_ on macOS 12+. So the main breaking change for end users here
is requiring macOS 12 to run.

However, the upside to using Swift Concurrency features is worth the
breaking change in my opinion. Also being able to stay on recent Swift
Argument Parser releases.
2022-07-26 03:55:36 -04:00

56 lines
1.8 KiB
Swift

#if canImport(CryptoSwift)
import CryptoSwift
#endif
import Foundation
import SourceKittenFramework
/// Reports violations as a JSON array in Code Climate format.
public struct CodeClimateReporter: Reporter {
// MARK: - Reporter Conformance
public static let identifier = "codeclimate"
public static let isRealtime = false
public var description: String {
return "Reports violations as a JSON array in Code Climate format."
}
public static func generateReport(_ violations: [StyleViolation]) -> String {
return toJSON(violations.map(dictionary(for:)))
.replacingOccurrences(of: "\\/", with: "/")
}
// MARK: - Private
private static func dictionary(for violation: StyleViolation) -> [String: Any] {
return [
"check_name": violation.ruleName,
"description": violation.reason,
"engine_name": "SwiftLint",
"fingerprint": generateFingerprint(violation),
"location": [
"path": violation.location.relativeFile ?? NSNull() as Any,
"lines": [
"begin": violation.location.line ?? NSNull() as Any,
"end": violation.location.line ?? NSNull() as Any
]
],
"severity": violation.severity == .error ? "MAJOR" : "MINOR",
"type": "issue"
]
}
internal static func generateFingerprint(_ violation: StyleViolation) -> String {
let fingerprintLocation = Location(
file: violation.location.relativeFile,
line: violation.location.line,
character: violation.location.character
)
return [
"\(fingerprintLocation)",
"\(violation.ruleIdentifier)"
].joined().sha256()
}
}