From 37706c89374d24aef45734e670227bde962a2d9a Mon Sep 17 00:00:00 2001 From: Martin Redington Date: Tue, 5 Nov 2024 22:31:16 +0000 Subject: [PATCH] CR comments --- .../Rules/Style/AttributesRule.swift | 15 ++++---- .../Documentation/RuleDocumentation.swift | 34 +++++++++++-------- .../Models/RuleDescription.swift | 18 +--------- 3 files changed, 28 insertions(+), 39 deletions(-) diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/AttributesRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/AttributesRule.swift index 38d4cee778..836fe48498 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/AttributesRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/AttributesRule.swift @@ -12,21 +12,20 @@ struct AttributesRule: OptInRule { imports """, rationale: """ - See https://ericasadun.com/2016/10/02/quick-style-survey/ for discussion. + Erica Sadun says: - Summarizing here: "[Erica Sadun's] take on things after the poll and after talking directly with a number of \ + > my take on things after the poll and after talking directly with a number of \ developers is this: Placing attributes like `@objc`, `@testable`, `@available`, `@discardableResult` on \ - their own lines before a member declaration has become a conventional Swift style." + their own lines before a member declaration has become a conventional Swift style. - "This approach limits declaration length. It allows a member to float below its attribute and supports \ + > This approach limits declaration length. It allows a member to float below its attribute and supports \ flush-left access modifiers, so `internal`, `public`, etc appear in the leftmost column. Many developers \ - mix-and-match styles for short Swift attributes like `@objc`" + mix-and-match styles for short Swift attributes like `@objc` + + See https://ericasadun.com/2016/10/02/quick-style-survey/ for discussion. SwiftLint's rule requires attributes to be on their own lines for functions and types, but on the same line \ for variables and imports. - - The `attributes_with_arguments_always_on_line_above`, `always_on_same_line`, and `always_on_line_above` \ - configuration parameters can be used to fine-tune the rules behaviour for particular attributes. """, kind: .style, nonTriggeringExamples: AttributesRuleExamples.nonTriggeringExamples, diff --git a/Source/SwiftLintCore/Documentation/RuleDocumentation.swift b/Source/SwiftLintCore/Documentation/RuleDocumentation.swift index 2eb5f7b749..1520d78a3b 100644 --- a/Source/SwiftLintCore/Documentation/RuleDocumentation.swift +++ b/Source/SwiftLintCore/Documentation/RuleDocumentation.swift @@ -58,20 +58,7 @@ struct RuleDocumentation { } private func formattedRationale(_ rationale: String) -> String { - var insideMultilineString = false - return rationale.components(separatedBy: "\n").map { line in - if line.contains("```") { - if insideMultilineString { - insideMultilineString = false - } else { - insideMultilineString = true - if line.hasSuffix("```") { - return line + "swift" - } - } - } - return line - }.joined(separator: "\n") + rationale.formattedAsRationale } private func formattedCode(_ example: Example) -> String { @@ -119,3 +106,22 @@ private func detailsSummary(_ rule: some Rule) -> String { } return ruleDescription } + +extension String { + var formattedAsRationale: String { + var insideMultilineString = false + return components(separatedBy: "\n").map { line in + if line.contains("```") { + if insideMultilineString { + insideMultilineString = false + } else { + insideMultilineString = true + if line.hasSuffix("```") { + return line + "swift" + } + } + } + return line + }.joined(separator: "\n") + } +} diff --git a/Source/SwiftLintCore/Models/RuleDescription.swift b/Source/SwiftLintCore/Models/RuleDescription.swift index 014db45749..df944ed798 100644 --- a/Source/SwiftLintCore/Models/RuleDescription.swift +++ b/Source/SwiftLintCore/Models/RuleDescription.swift @@ -64,23 +64,7 @@ public struct RuleDescription: Equatable, Sendable { guard let rationale else { return nil } - var insideMultilineString = false - return rationale.components(separatedBy: "\n").map { line in - if line.contains("```") { - if insideMultilineString { - insideMultilineString = false - } else { - insideMultilineString = true - if line.hasSuffix("```") { - return line + "swift" - } - } - } - if insideMultilineString { - return " \(line)" - } - return line - }.joined(separator: "\n") + return rationale.formattedAsRationale } /// All identifiers that have been used to uniquely identify this rule in past and current SwiftLint versions.