Skip to content

Commit

Permalink
Better formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mildm8nnered committed Jul 29, 2024
1 parent 7255ce9 commit e9e38be
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Source/SwiftLintBuiltInRules/Rules/Lint/ArrayInitRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ struct ArrayInitRule: OptInRule {
name: "Array Init",
description: "Prefer using `Array(seq)` over `seq.map { $0 }` to convert a sequence into an Array",
rationale: """
When converting a the elements of sequence directly into an `Array`, for clarity and to better convey \
intent, prefer using the `Array` constructor over calling `map`. For example
When converting the elements of sequence directly into an `Array`, for clarity, prefer using the `Array` constructor over calling `map`. For example
```
Array(foo)
Expand Down
11 changes: 4 additions & 7 deletions Source/SwiftLintCore/Documentation/RuleDocumentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,20 @@ struct RuleDocumentation {
}

private func formattedRationale(_ rationale: String) -> String {
var result = ""
var insideMultilineString = false
rationale.enumerateLines { line, _ in
var formattedLine = line
return rationale.components(separatedBy: "\n").map { line in
if line.contains("```") {
if insideMultilineString {
insideMultilineString = false
} else {
insideMultilineString = true
if line.hasSuffix("```") {
formattedLine += "swift"
return line + "swift"
}
}
}
result += formattedLine + "\n"
}
return result
return line
}.joined(separator: "\n")
}

private func formattedCode(_ example: Example) -> String {
Expand Down
10 changes: 10 additions & 0 deletions Source/SwiftLintCore/Models/RuleDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public struct RuleDescription: Equatable, Sendable {
/// The console-printable string for this description.
public var consoleDescription: String { "\(name) (\(identifier)): \(description)" }

/// The console-printable rationale for this description.
public var consoleRationale: String? {
guard let rationale else {
return nil
}
return rationale.components(separatedBy: "\n").compactMap { line in
line.contains("```") ? nil : line
}.joined(separator: "\n")
}

/// All identifiers that have been used to uniquely identify this rule in past and current SwiftLint versions.
public var allIdentifiers: [String] {
Array(deprecatedAliases) + [identifier]
Expand Down
4 changes: 2 additions & 2 deletions Source/swiftlint/Commands/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ extension SwiftLint {
}

print("\(description.consoleDescription)")
if let rationale = description.rationale {
print("\nRationale:\n\n\(rationale)")
if let consoleRationale = description.consoleRationale {
print("\nRationale:\n\n\(consoleRationale)")
}
let configDescription = rule.createConfigurationDescription()
if configDescription.hasContent {
Expand Down

0 comments on commit e9e38be

Please sign in to comment.