Skip to content

Commit fa403e5

Browse files
authored
Fix issue where async effect could be confused for async modifier, breaking docCommentsBeforeModifiers rule in protocol body with async functions (#2220)
1 parent 03f8656 commit fa403e5

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Sources/ParsingHelpers.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ extension Formatter {
403403
/// Returns true if the modifiers list for the given declaration contain a
404404
/// modifier matching the specified predicate
405405
func modifiersForDeclaration(at index: Int, contains: (Int, String) -> Bool) -> Bool {
406+
var declarationType: String?
407+
if tokens[index].isDeclarationTypeKeyword {
408+
declarationType = tokens[index].string
409+
}
410+
406411
var index = index
407412
while var prevIndex = self.index(of: .nonSpaceOrCommentOrLinebreak, before: index) {
408413
switch tokens[prevIndex] {
@@ -414,6 +419,12 @@ extension Formatter {
414419
// Part of previous declaration
415420
return false
416421
}
422+
423+
// Async is only a valid modifier on local let/var declarations.
424+
if token == .identifier("async"), !["let", "var"].contains(declarationType) {
425+
return false
426+
}
427+
417428
if contains(prevIndex, token.string) {
418429
return true
419430
}

Tests/Rules/DocCommentsBeforeModifiersTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,20 @@ class DocCommentsBeforeModifiersTests: XCTestCase {
304304

305305
testFormatting(for: input, rule: .docCommentsBeforeModifiers, exclude: [.propertyTypes])
306306
}
307+
308+
func testIssue2216() {
309+
let input = """
310+
protocol TestIssue2216() {
311+
/// Documentation comment explaining the function
312+
/// with multiple lines of explanation
313+
func method1(value: String) async
314+
315+
/// Documentation comment explaining the function
316+
/// with multiple lines of explanation
317+
func method2(value: String) async
318+
}
319+
"""
320+
321+
testFormatting(for: input, rule: .docCommentsBeforeModifiers)
322+
}
307323
}

0 commit comments

Comments
 (0)