mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fcf848608e
* Add Example wrapper in order to display test failures inline when running in Xcode. * Stop using Swift 5.1-only features so we can compile on Xcode 10.2. * Wrap strings in Example. * Add Changelog entry. * Wrap all examples in Example struct. * Better and more complete capturing of line numbers. * Fix broken test. * Better test traceability. * Address or disable linting warnings. * Add documentation comments. * Disable linter for a few cases. * Limit mutability and add copy-and-mutate utility functions. * Limit scope of mutability.
58 lines
1.7 KiB
Swift
58 lines
1.7 KiB
Swift
import Foundation
|
|
|
|
/// Reports violations as markdown formated (with tables).
|
|
public struct MarkdownReporter: Reporter {
|
|
// MARK: - Reporter Conformance
|
|
|
|
public static let identifier = "markdown"
|
|
public static let isRealtime = false
|
|
|
|
public var description: String {
|
|
return "Reports violations as markdown formated (with tables)."
|
|
}
|
|
|
|
public static func generateReport(_ violations: [StyleViolation]) -> String {
|
|
let keys = [
|
|
"file",
|
|
"line",
|
|
"severity",
|
|
"reason",
|
|
"rule_id"
|
|
].joined(separator: " | ")
|
|
|
|
let rows = [keys, "--- | --- | --- | --- | ---"] + violations.map(markdownRow(for:))
|
|
return rows.joined(separator: "\n")
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private static func markdownRow(for violation: StyleViolation) -> String {
|
|
return [
|
|
violation.location.file?.escapedForMarkdown() ?? "",
|
|
violation.location.line?.description ?? "",
|
|
severity(for: violation.severity),
|
|
violation.ruleName.escapedForMarkdown() + ": " + violation.reason.escapedForMarkdown(),
|
|
violation.ruleIdentifier
|
|
].joined(separator: " | ")
|
|
}
|
|
|
|
private static func severity(for severity: ViolationSeverity) -> String {
|
|
switch severity {
|
|
case .error:
|
|
return ":stop\\_sign:"
|
|
case .warning:
|
|
return ":warning:"
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension String {
|
|
func escapedForMarkdown() -> String {
|
|
let escapedString = replacingOccurrences(of: "\"", with: "\"\"")
|
|
if escapedString.contains("|") || escapedString.contains("\n") {
|
|
return "\"\(escapedString)\""
|
|
}
|
|
return escapedString
|
|
}
|
|
}
|