From 6498522350708a2ade44dc1894e54b2ee60ddffc Mon Sep 17 00:00:00 2001 From: Nick Lockwood Date: Wed, 31 Jan 2024 08:33:28 +0000 Subject: [PATCH] Fix bug in `hoistAwait` rule --- Sources/FormattingHelpers.swift | 2 ++ Tests/RulesTests+Hoisting.swift | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/Sources/FormattingHelpers.swift b/Sources/FormattingHelpers.swift index 28a34e5a..f8c78bcd 100644 --- a/Sources/FormattingHelpers.swift +++ b/Sources/FormattingHelpers.swift @@ -1058,6 +1058,8 @@ extension Formatter { if [.startOfScope("("), .startOfScope("[")].contains(prevToken), isEffectCapturingAt(i) { return } + case .keyword("try") where keyword == "await": + break loop case let .keyword(name) where ["is", "as", "try", "await"].contains(name): break case .operator(_, .prefix), .stringBody, diff --git a/Tests/RulesTests+Hoisting.swift b/Tests/RulesTests+Hoisting.swift index 11163729..6e4b4575 100644 --- a/Tests/RulesTests+Hoisting.swift +++ b/Tests/RulesTests+Hoisting.swift @@ -562,6 +562,13 @@ class HoistingTests: RulesTests { options: FormatOptions(swiftVersion: "5.5")) } + func testNoHoistAwaitBeforeTry() { + let input = "try foo(await bar())" + let output = "try await foo(bar())" + testFormatting(for: input, output, rule: FormatRules.hoistAwait, + options: FormatOptions(swiftVersion: "5.5")) + } + func testNoHoistAwaitInCapturingFunction() { let input = "foo(await bar)" testFormatting(for: input, rule: FormatRules.hoistAwait,