Files
SwiftLint/Source/SwiftLintFramework/Documentation/RuleDocumentation.swift
T
Zev Eisenberg fcf848608e Add Inline test failure messages (#3040)
* 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.
2020-02-02 10:35:37 +02:00

70 lines
2.3 KiB
Swift

/// User-facing documentation for a SwiftLint rule.
struct RuleDocumentation {
private let ruleType: Rule.Type
/// Creates a RuleDocumentation instance from a Rule type.
///
/// - parameter ruleType: A subtype of the `Rule` protocol to document.
init(_ ruleType: Rule.Type) {
self.ruleType = ruleType
}
/// The name of the documented rule.
var ruleName: String {
return ruleType.description.name
}
/// The web URL fragment for this documentation.
var urlFragment: String {
return "\(ruleType.description.identifier).html"
}
/// The name of the file on disk for this rule documentation.
var fileName: String {
return "\(ruleType.description.identifier).md"
}
/// The contents of the file for this rule documentation.
var fileContents: String {
let description = ruleType.description
var content = [h1(description.name), description.description, detailsSummary(ruleType.init())]
if !description.nonTriggeringExamples.isEmpty {
content += [h2("Non Triggering Examples")]
content += description.nonTriggeringExamples.map(formattedCode)
}
if !description.triggeringExamples.isEmpty {
content += [h2("Triggering Examples")]
content += description.triggeringExamples.map(formattedCode)
}
return content.joined(separator: "\n\n")
}
}
private func h1(_ text: String) -> String {
return "# \(text)"
}
private func h2(_ text: String) -> String {
return "## \(text)"
}
private func detailsSummary(_ rule: Rule) -> String {
return """
* **Identifier:** \(type(of: rule).description.identifier)
* **Enabled by default:** \(rule is OptInRule ? "Disabled" : "Enabled")
* **Supports autocorrection:** \(rule is CorrectableRule ? "Yes" : "No")
* **Kind:** \(type(of: rule).description.kind)
* **Analyzer rule:** \(rule is AnalyzerRule ? "Yes" : "No")
* **Minimum Swift compiler version:** \(type(of: rule).description.minSwiftVersion.rawValue)
* **Default configuration:** \(rule.configurationDescription)
"""
}
private func formattedCode(_ example: Example) -> String {
return """
```swift
\(example.code)
```
"""
}