Add support for do throws(Type)

This commit is contained in:
Nick Lockwood
2024-03-09 07:05:15 +00:00
parent 29cf62fd87
commit 1f03823a7d
4 changed files with 69 additions and 1 deletions
+1 -1
View File
@@ -3231,7 +3231,7 @@ public struct _FormatRules {
// Make sure this is a type of scope that supports implicit returns
if formatter.isConditionalStatement(at: startOfScopeIndex) ||
["do", "else", "catch"].contains(formatter.lastSignificantKeyword(at: startOfScopeIndex))
["do", "else", "catch"].contains(formatter.lastSignificantKeyword(at: startOfScopeIndex, excluding: ["throws"]))
{
return
}
+8
View File
@@ -360,6 +360,14 @@ class BracesTests: RulesTests {
exclude: ["emptyBraces"])
}
func testAllmanBraceDoThrowsCatchClauseIndent() {
let input = "do throws(Foo) {\n try foo\n}\ncatch {\n}"
let output = "do throws(Foo)\n{\n try foo\n}\ncatch\n{\n}"
let options = FormatOptions(allmanBraces: true)
testFormatting(for: input, output, rule: FormatRules.braces, options: options,
exclude: ["emptyBraces"])
}
func testAllmanBraceRepeatWhileIndent() {
let input = "repeat {\n foo\n}\nwhile x"
let output = "repeat\n{\n foo\n}\nwhile x"
+21
View File
@@ -130,6 +130,12 @@ class HoistingTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.hoistTry)
}
func testNoHoistTryInsideDoThrows() {
let input = "do throws(Foo) { rg.box.seal(.fulfilled(try body(error))) }"
let output = "do throws(Foo) { try rg.box.seal(.fulfilled(body(error))) }"
testFormatting(for: input, output, rule: FormatRules.hoistTry)
}
func testNoHoistTryInsideMultilineDo() {
let input = """
do {
@@ -459,6 +465,21 @@ class HoistingTests: RulesTests {
options: FormatOptions(swiftVersion: "5.5"))
}
func testNoHoistAwaitInsideDoThrows() {
let input = """
do throws(Foo) {
rg.box.seal(.fulfilled(await body(error)))
}
"""
let output = """
do throws(Foo) {
await rg.box.seal(.fulfilled(body(error)))
}
"""
testFormatting(for: input, output, rule: FormatRules.hoistAwait,
options: FormatOptions(swiftVersion: "5.5"))
}
func testHoistAwaitInExpressionWithNoSpaces() {
let input = "let foo=bar(contentsOf:await baz())"
let output = "let foo=await bar(contentsOf:baz())"
+39
View File
@@ -873,6 +873,12 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, output, rule: FormatRules.redundantLetError)
}
func testCatchLetErrorWithTypedThrows() {
let input = "do throws(Foo) {} catch let error {}"
let output = "do throws(Foo) {} catch {}"
testFormatting(for: input, output, rule: FormatRules.redundantLetError)
}
// MARK: - redundantObjc
func testRedundantObjcRemovedFromBeforeOutlet() {
@@ -2300,6 +2306,20 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, rule: FormatRules.redundantReturn, options: options)
}
func testNoRemoveReturnInDoThrowsCatch() {
let input = """
func foo() -> Int {
do throws(Foo) {
return try Bar()
} catch {
return -1
}
}
"""
let options = FormatOptions(swiftVersion: "5.1")
testFormatting(for: input, rule: FormatRules.redundantReturn, options: options)
}
func testNoRemoveReturnInDoCatchLet() {
let input = """
func foo() -> Int {
@@ -2314,6 +2334,20 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, rule: FormatRules.redundantReturn, options: options)
}
func testNoRemoveReturnInDoThrowsCatchLet() {
let input = """
func foo() -> Int {
do throws(Foo) {
return try Bar()
} catch let e as Error {
return -1
}
}
"""
let options = FormatOptions(swiftVersion: "5.1")
testFormatting(for: input, rule: FormatRules.redundantReturn, options: options)
}
func testNoRemoveReturnInForIn() {
let input = "for foo in bar { return 5 }"
testFormatting(for: input, rule: FormatRules.redundantReturn, exclude: ["wrapLoopBodies"])
@@ -3481,6 +3515,11 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, rule: FormatRules.redundantSelf)
}
func testNoRemoveSelfForErrorInDoThrowsCatch() {
let input = "do throws(Foo) {} catch { self.error = error }"
testFormatting(for: input, rule: FormatRules.redundantSelf)
}
func testNoRemoveSelfForNewValueInSet() {
let input = "var foo: Int { set { self.newValue = newValue } get { return 0 } }"
testFormatting(for: input, rule: FormatRules.redundantSelf)