Skip to content

Commit 00f0a82

Browse files
committed
Improvements for do and related statements
* Leading/trailing spaces are updated to make statements well formatted * `CatchClause` convenience initializer that calculates spacing around the `catch` keyword * Test coverage for above changes
1 parent 2380374 commit 00f0a82

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6643,7 +6643,7 @@ public enum SyntaxFactory {
66436643
}
66446644
public static func makeDoKeyword(
66456645
leadingTrivia: Trivia = [],
6646-
trailingTrivia: Trivia = .space
6646+
trailingTrivia: Trivia = []
66476647
) -> TokenSyntax {
66486648
return makeToken(.doKeyword, presence: .present,
66496649
leadingTrivia: leadingTrivia,
@@ -6746,16 +6746,16 @@ public enum SyntaxFactory {
67466746
trailingTrivia: trailingTrivia)
67476747
}
67486748
public static func makeWhereKeyword(
6749-
leadingTrivia: Trivia = [],
6749+
leadingTrivia: Trivia = .space,
67506750
trailingTrivia: Trivia = .space
67516751
) -> TokenSyntax {
67526752
return makeToken(.whereKeyword, presence: .present,
67536753
leadingTrivia: leadingTrivia,
67546754
trailingTrivia: trailingTrivia)
67556755
}
67566756
public static func makeCatchKeyword(
6757-
leadingTrivia: Trivia = [],
6758-
trailingTrivia: Trivia = .space
6757+
leadingTrivia: Trivia = .space,
6758+
trailingTrivia: Trivia = []
67596759
) -> TokenSyntax {
67606760
return makeToken(.catchKeyword, presence: .present,
67616761
leadingTrivia: leadingTrivia,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
extension CatchClause {
16+
/// A convenience initializer that calculates spacing around the `catch` keyword.
17+
public init(
18+
leadingTrivia: Trivia = [],
19+
_ catchItems: CatchItemList,
20+
@CodeBlockItemListBuilder bodyBuilder: () -> ExpressibleAsCodeBlockItemList
21+
) {
22+
self.init(
23+
leadingTrivia: leadingTrivia,
24+
catchKeyword: SyntaxFactory.makeCatchKeyword(trailingTrivia: catchItems.elements.isEmpty ? [] : .space),
25+
catchItems: catchItems,
26+
body: bodyBuilder()
27+
)
28+
}
29+
}

Sources/SwiftSyntaxBuilderGeneration/Templates/BuildableCollectionNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ let buildableCollectionNodesFile = SourceFile {
5555
inheritanceClause: TypeInheritanceClause {
5656
InheritedType(typeName: type.expressibleAs)
5757
},
58-
genericWhereClause: GenericWhereClause(leadingTrivia: .space) {
58+
genericWhereClause: GenericWhereClause {
5959
GenericRequirement(body: SameTypeRequirement(
6060
leftTypeIdentifier: "Element",
6161
equalityToken: .spacedBinaryOperator("=="),
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(CatchItemList {
13+
CatchItem(pattern: "Error1")
14+
CatchItem(pattern: "Error2")
15+
}) {
16+
FunctionCallExpr("print") {
17+
TupleExprElement(expression: StringLiteralExpr("Known error"))
18+
}
19+
},
20+
CatchClause(CatchItemList {
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
@@ -28,7 +28,7 @@ final class StructTests: XCTestCase {
2828
],
2929
structKeyword: .struct,
3030
identifier: "NestedStruct",
31-
genericParameterClause: GenericParameterClause {
31+
genericParameterClause: GenericParameterClause(rightAngleBracket: .rightAngle.withoutTrailingTrivia()) {
3232
GenericParameter(name: "A")
3333
GenericParameter(name: "B", colon: .colon, inheritedType: "C")
3434
GenericParameter(name: "D")

0 commit comments

Comments
 (0)