Skip to content

Commit 5ca578a

Browse files
committed
Improvements for do and related statements
* Leading/trailing spaces are updated to make statements well formatted * `CatchItems` wrapper to build items list with list builder * `CatchClause` convenience initializer utilizing `CatchItems` * Test coverage for above changes
1 parent fb0f401 commit 5ca578a

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5222,7 +5222,7 @@ public enum SyntaxFactory {
52225222
}
52235223
public static func makeDoKeyword(
52245224
leadingTrivia: Trivia = [],
5225-
trailingTrivia: Trivia = .space
5225+
trailingTrivia: Trivia = []
52265226
) -> TokenSyntax {
52275227
return makeToken(.doKeyword, presence: .present,
52285228
leadingTrivia: leadingTrivia,
@@ -5325,16 +5325,16 @@ public enum SyntaxFactory {
53255325
trailingTrivia: trailingTrivia)
53265326
}
53275327
public static func makeWhereKeyword(
5328-
leadingTrivia: Trivia = [],
5328+
leadingTrivia: Trivia = .space,
53295329
trailingTrivia: Trivia = .space
53305330
) -> TokenSyntax {
53315331
return makeToken(.whereKeyword, presence: .present,
53325332
leadingTrivia: leadingTrivia,
53335333
trailingTrivia: trailingTrivia)
53345334
}
53355335
public static func makeCatchKeyword(
5336-
leadingTrivia: Trivia = [],
5337-
trailingTrivia: Trivia = .space
5336+
leadingTrivia: Trivia = .space,
5337+
trailingTrivia: Trivia = []
53385338
) -> TokenSyntax {
53395339
return makeToken(.catchKeyword, presence: .present,
53405340
leadingTrivia: leadingTrivia,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
public struct CatchItems: ExpressibleAsCatchItemList {
16+
private let itemList: CatchItemList
17+
18+
public init(@CatchItemListBuilder itemsBuilder: () -> ExpressibleAsCatchItemList) {
19+
self.itemList = itemsBuilder().createCatchItemList()
20+
}
21+
22+
public func createCatchItemList() -> CatchItemList {
23+
itemList
24+
}
25+
}
26+
27+
extension CatchClause {
28+
/// A convenience initializer that allows passing in catch items as a `CatchItems` wrapper.
29+
public init(
30+
leadingTrivia: Trivia = [],
31+
_ catchItems: CatchItems,
32+
@CodeBlockItemListBuilder bodyBuilder: () -> ExpressibleAsCodeBlockItemList
33+
) {
34+
self.init(
35+
leadingTrivia: leadingTrivia,
36+
catchKeyword: .catch.withTrailingTrivia(.space),
37+
catchItems: catchItems,
38+
body: bodyBuilder()
39+
)
40+
}
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import XCTest
2+
import SwiftSyntaxBuilder
3+
4+
final class DoStmtTests: XCTestCase {
5+
func testDoStmt() {
6+
let buildable = DoStmt(
7+
labelName: nil,
8+
body: CodeBlock(statementsBuilder: {
9+
CodeBlockItem(item: TryExpr(expression: FunctionCallExpr(MemberAccessExpr(base: "a", name: "b"))))
10+
}),
11+
catchClauses: [
12+
CatchClause(CatchItems {
13+
CatchItem(pattern: "Error1")
14+
CatchItem(pattern: "Error2")
15+
}) {
16+
FunctionCallExpr("print") {
17+
TupleExprElement(expression: StringLiteralExpr("Known error"))
18+
}
19+
},
20+
CatchClause(CatchItems {
21+
CatchItem(
22+
pattern: "Error3", whereClause: WhereClause(guardResult: MemberAccessExpr(base: "error", name: "isError4")))
23+
}) {
24+
ThrowStmt(expression: MemberAccessExpr(base: "Error4", name: "error3"))
25+
},
26+
CatchClause {
27+
FunctionCallExpr("print") {
28+
TupleExprElement(expression: "error")
29+
}
30+
}
31+
])
32+
33+
let syntax = buildable.buildSyntax(format: Format())
34+
XCTAssertEqual(
35+
syntax.description,
36+
"""
37+
do {
38+
try a.b()
39+
} catch Error1, Error2 {
40+
print("Known error")
41+
} catch Error3 where error.isError4 {
42+
throw Error4.error3
43+
} catch {
44+
print(error)
45+
}
46+
""")
47+
}
48+
}

Tests/SwiftSyntaxBuilderTest/StructTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class StructTests: XCTestCase {
2222
let nestedStruct = StructDecl(
2323
structKeyword: .struct.withLeadingTrivia(.docLineComment("/// A nested struct\n")),
2424
identifier: "NestedStruct",
25-
genericParameterClause: GenericParameterClause {
25+
genericParameterClause: GenericParameterClause(rightAngleBracket: .rightAngle.withoutTrailingTrivia()) {
2626
GenericParameter(name: "A")
2727
GenericParameter(name: "B", colon: .colon, inheritedType: "C")
2828
GenericParameter(name: "D")

0 commit comments

Comments
 (0)