From 73a64d674c352097cf42725178f590d554f802d9 Mon Sep 17 00:00:00 2001 From: JP Simard Date: Fri, 9 Dec 2022 12:56:50 -0500 Subject: [PATCH] Fix rewrite issue with `comma` rule (#4635) --- CHANGELOG.md | 12 ++++++++---- .../SwiftLintFramework/Rules/Style/CommaRule.swift | 11 ++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d911f844b..9793fa9ea5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,16 +43,20 @@ [SimplyDanny](https://github.com/SimplyDanny) [#4612](https://github.com/realm/SwiftLint/issues/4612) -* Skip `defer` statements being last in an `#if` block if the `#if` statement is - not itself the last statement in a block. +* Skip `defer` statements being last in an `#if` block if the `#if` + statement is not itself the last statement in a block. [SimplyDanny](https://github.com/SimplyDanny) [#4615](https://github.com/realm/SwiftLint/issues/4615) -* Fix false positives in `empty_enum_arguments` when the called expression - is an identifier or an init call. +* Fix false positives in `empty_enum_arguments` when the called + expression is an identifier or an init call. [Steffen Matthischke](https://github.com/heeaad) [#4597](https://github.com/realm/SwiftLint/issues/4597) +* Fix correction issue in `comma` when there was too much whitespace + following the comma. + [JP Simard](https://github.com/jpsim) + ## 0.50.1: Artisanal Clothes Pegs Fixup Edition #### Breaking diff --git a/Source/SwiftLintFramework/Rules/Style/CommaRule.swift b/Source/SwiftLintFramework/Rules/Style/CommaRule.swift index f19ce0846f..9b6e8a143e 100644 --- a/Source/SwiftLintFramework/Rules/Style/CommaRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/CommaRule.swift @@ -49,7 +49,8 @@ struct CommaRule: CorrectableRule, ConfigurationProviderRule, SourceKitFreeRule message: My.Custom.message↓ , another: parameter, doIt: true, alignment: .center) - """) + """), + Example(#"Logger.logError("Hat is too large"↓, info: [])"#) ], corrections: [ Example("func abc(a: String↓,b: String) {}\n"): Example("func abc(a: String, b: String) {}\n"), @@ -83,7 +84,9 @@ struct CommaRule: CorrectableRule, ConfigurationProviderRule, SourceKitFreeRule message: My.Custom.message, another: parameter, doIt: true, alignment: .center) - """) + """), + Example(#"Logger.logError("Hat is too large"↓, info: [])"#): + Example(#"Logger.logError("Hat is too large", info: [])"#) ] ) @@ -109,7 +112,9 @@ struct CommaRule: CorrectableRule, ConfigurationProviderRule, SourceKitFreeRule let nextIsNewline = next.leadingTrivia.containsNewlines() return (ByteRange(location: start, length: end - start), shouldAddSpace: !nextIsNewline) } else if !current.trailingTrivia.starts(with: [.spaces(1)]), !next.leadingTrivia.containsNewlines() { - return (ByteRange(location: ByteCount(current.position), length: 1), shouldAddSpace: true) + let start = ByteCount(current.position) + let end = ByteCount(next.positionAfterSkippingLeadingTrivia) + return (ByteRange(location: start, length: end - start), shouldAddSpace: true) } else { return nil }