mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fe905f8ae9
While browsing the rules documentation, I noticed **many** rules were both in the Default Rules and in the Opt In Rules section. After looking into it, the docs of the `drop(while predicate: (Element) throws -> Bool` function states: A closure that takes an element of the sequence as its argument and returns true if the element should be skipped or false if it should be included. **Once the predicate returns false it will not be called again.** This caused the `defaultRuleDocumentations` array to contain almost all `ruleDocumentation`.
56 lines
1.9 KiB
Swift
56 lines
1.9 KiB
Swift
import Foundation
|
|
|
|
/// User-facing documentation for a SwiftLint RuleList.
|
|
public struct RuleListDocumentation {
|
|
private let ruleDocumentations: [RuleDocumentation]
|
|
|
|
/// Creates a RuleListDocumentation instance from a RuleList.
|
|
///
|
|
/// - parameter list: A RuleList to document.
|
|
public init(_ list: RuleList) {
|
|
ruleDocumentations = list.list
|
|
.sorted { $0.0 < $1.0 }
|
|
.map { RuleDocumentation($0.value) }
|
|
}
|
|
|
|
/// Write the rule list documentation as markdown files to the specified directory.
|
|
///
|
|
/// - parameter url: Local URL for directory where the markdown files for this documentation should be saved.
|
|
///
|
|
/// - throws: Throws if the files could not be written to.
|
|
public func write(to url: URL) throws {
|
|
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
|
|
func write(_ text: String, toFile file: String) throws {
|
|
try text.write(to: url.appendingPathComponent(file), atomically: false, encoding: .utf8)
|
|
}
|
|
try write(indexContents, toFile: "Rule Directory.md")
|
|
for doc in ruleDocumentations {
|
|
try write(doc.fileContents, toFile: doc.fileName)
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private var indexContents: String {
|
|
let defaultRuleDocumentations = ruleDocumentations.filter { !$0.isOptInRule }
|
|
let optInRuleDocumentations = ruleDocumentations.filter { $0.isOptInRule }
|
|
|
|
return """
|
|
# Rule Directory
|
|
|
|
## Default Rules
|
|
|
|
\(defaultRuleDocumentations
|
|
.map { "* `\($0.ruleIdentifier)`: \($0.ruleName)" }
|
|
.joined(separator: "\n"))
|
|
|
|
## Opt-In Rules
|
|
|
|
\(optInRuleDocumentations
|
|
.map { "* `\($0.ruleIdentifier)`: \($0.ruleName)" }
|
|
.joined(separator: "\n"))
|
|
|
|
"""
|
|
}
|
|
}
|