Skip to content

Commit

Permalink
Fix hoisting try inside optional function
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Jun 2, 2023
1 parent e400870 commit 5145179
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/FormattingHelpers.swift
Expand Up @@ -1071,6 +1071,10 @@ extension Formatter {
.startOfScope where tokens[i].isStringDelimiter,
.endOfScope where tokens[i].isStringDelimiter:
break
case .operator("?", .postfix), .operator("!", .postfix):
if prevToken != .startOfScope("(") {
break loop
}
case .operator(_, .postfix), .identifier, .number, .endOfScope:
if !prevToken.isOperator(ofType: .infix),
!prevToken.isOperator(ofType: .postfix)
Expand Down
6 changes: 6 additions & 0 deletions Tests/RulesTests+Hoisting.swift
Expand Up @@ -311,6 +311,12 @@ class HoistingTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.hoistTry)
}

func testHoistTryInsideOptionalFunction() {
let input = "foo?(try bar())"
let output = "try foo?(bar())"
testFormatting(for: input, output, rule: FormatRules.hoistTry)
}

// MARK: - hoistAwait

func testHoistAwait() {
Expand Down

0 comments on commit 5145179

Please sign in to comment.