mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
93 lines
3.1 KiB
Swift
93 lines
3.1 KiB
Swift
import Foundation
|
|
|
|
extension RuleList {
|
|
public func generateDocumentation() -> String {
|
|
let rules = list.sorted { $0.0 < $1.0 }.map { $0.value }
|
|
let rulesText = rules.map(ruleMarkdown)
|
|
let rulesSummary = rules.map(ruleSummary)
|
|
|
|
var text = h1("Rules")
|
|
text += rulesSummary.joined()
|
|
text += "--------\n"
|
|
text += rulesText.joined(separator: "\n\n")
|
|
|
|
return text
|
|
}
|
|
|
|
private func ruleSummary(_ rule: Rule.Type) -> String {
|
|
return summaryItem(rule.description.name)
|
|
}
|
|
|
|
private func ruleMarkdown(_ rule: Rule.Type) -> String {
|
|
let description = rule.description
|
|
var content = h2(description.name)
|
|
content += detailsSummary(rule.init())
|
|
content += description.description + "\n"
|
|
|
|
if !description.nonTriggeringExamples.isEmpty || !description.triggeringExamples.isEmpty {
|
|
content += h3("Examples")
|
|
}
|
|
|
|
if !description.nonTriggeringExamples.isEmpty {
|
|
let examples = description.nonTriggeringExamples.map(formattedCode).joined(separator: "\n")
|
|
content += details(summary: "Non Triggering Examples", details: examples)
|
|
}
|
|
|
|
if !description.triggeringExamples.isEmpty {
|
|
let examples = description.triggeringExamples.map(formattedCode).joined(separator: "\n")
|
|
content += details(summary: "Triggering Examples", details: examples)
|
|
}
|
|
|
|
return content
|
|
}
|
|
|
|
private func details(summary: String, details: String) -> String {
|
|
var content = "<details>\n"
|
|
content += "<summary>\(summary)</summary>\n\n"
|
|
content += details + "\n"
|
|
content += "</details>\n"
|
|
|
|
return content
|
|
}
|
|
|
|
private func formattedCode(_ code: String) -> String {
|
|
var content = "```swift\n"
|
|
content += code
|
|
content += "\n```\n"
|
|
|
|
return content
|
|
}
|
|
|
|
private func detailsSummary(_ rule: Rule) -> String {
|
|
let columns = ["Identifier", "Enabled by default", "Supports autocorrection",
|
|
"Kind", "Minimum Swift Compiler Version"]
|
|
var content = columns.joined(separator: " | ") + "\n"
|
|
content += columns.map { _ in "---" }.joined(separator: " | ") + "\n"
|
|
let identifier = type(of: rule).description.identifier
|
|
let defaultStatus = rule is OptInRule ? "Disabled" : "Enabled"
|
|
let correctable = rule is CorrectableRule ? "Yes" : "No"
|
|
let kind = type(of: rule).description.kind
|
|
let minSwiftVersion = type(of: rule).description.minSwiftVersion.rawValue
|
|
content += "`\(identifier)` | \(defaultStatus) | \(correctable) | \(kind) | \(minSwiftVersion) \n\n"
|
|
|
|
return content
|
|
}
|
|
|
|
private func h1(_ text: String) -> String {
|
|
return "\n# \(text)\n\n"
|
|
}
|
|
|
|
private func h2(_ text: String) -> String {
|
|
return "\n## \(text)\n\n"
|
|
}
|
|
|
|
private func h3(_ text: String) -> String {
|
|
return "\n### \(text)\n\n"
|
|
}
|
|
|
|
private func summaryItem(_ text: String) -> String {
|
|
let anchor = text.lowercased().components(separatedBy: .whitespaces).joined(separator: "-")
|
|
return "* [\(text)](#\(anchor))\n"
|
|
}
|
|
}
|