From e9e38bec35215807134e1093417bb7f7789cf409 Mon Sep 17 00:00:00 2001 From: Martin Redington Date: Sat, 27 Jul 2024 16:04:48 +0100 Subject: [PATCH] Better formatting --- .../Rules/Lint/ArrayInitRule.swift | 3 +-- .../Documentation/RuleDocumentation.swift | 11 ++++------- Source/SwiftLintCore/Models/RuleDescription.swift | 10 ++++++++++ Source/swiftlint/Commands/Rules.swift | 4 ++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/ArrayInitRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/ArrayInitRule.swift index 566cbf63124..6b9093be3bd 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/ArrayInitRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/ArrayInitRule.swift @@ -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) diff --git a/Source/SwiftLintCore/Documentation/RuleDocumentation.swift b/Source/SwiftLintCore/Documentation/RuleDocumentation.swift index e5f9eace817..5d1cecc7958 100644 --- a/Source/SwiftLintCore/Documentation/RuleDocumentation.swift +++ b/Source/SwiftLintCore/Documentation/RuleDocumentation.swift @@ -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 { diff --git a/Source/SwiftLintCore/Models/RuleDescription.swift b/Source/SwiftLintCore/Models/RuleDescription.swift index 8e646ee4c5d..a13d5bdc93b 100644 --- a/Source/SwiftLintCore/Models/RuleDescription.swift +++ b/Source/SwiftLintCore/Models/RuleDescription.swift @@ -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] diff --git a/Source/swiftlint/Commands/Rules.swift b/Source/swiftlint/Commands/Rules.swift index 589481390b3..f090d3fed83 100644 --- a/Source/swiftlint/Commands/Rules.swift +++ b/Source/swiftlint/Commands/Rules.swift @@ -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 {