mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Add support for do throws(Type)
This commit is contained in:
+1
-1
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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())"
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user