Skip to content

Improvements for do and related statements #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6643,7 +6643,7 @@ public enum SyntaxFactory {
}
public static func makeDoKeyword(
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = .space
trailingTrivia: Trivia = []
) -> TokenSyntax {
return makeToken(.doKeyword, presence: .present,
leadingTrivia: leadingTrivia,
Expand Down Expand Up @@ -6746,16 +6746,16 @@ public enum SyntaxFactory {
trailingTrivia: trailingTrivia)
}
public static func makeWhereKeyword(
leadingTrivia: Trivia = [],
leadingTrivia: Trivia = .space,
trailingTrivia: Trivia = .space
) -> TokenSyntax {
return makeToken(.whereKeyword, presence: .present,
leadingTrivia: leadingTrivia,
trailingTrivia: trailingTrivia)
}
public static func makeCatchKeyword(
leadingTrivia: Trivia = [],
trailingTrivia: Trivia = .space
leadingTrivia: Trivia = .space,
trailingTrivia: Trivia = []
) -> TokenSyntax {
return makeToken(.catchKeyword, presence: .present,
leadingTrivia: leadingTrivia,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax

extension CatchClause {
/// A convenience initializer that calculates spacing around the `catch` keyword.
public init(
leadingTrivia: Trivia = [],
_ catchItems: CatchItemList,
@CodeBlockItemListBuilder bodyBuilder: () -> ExpressibleAsCodeBlockItemList
) {
self.init(
leadingTrivia: leadingTrivia,
catchKeyword: SyntaxFactory.makeCatchKeyword(trailingTrivia: catchItems.elements.isEmpty ? [] : .space),
catchItems: catchItems,
body: bodyBuilder()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let buildableCollectionNodesFile = SourceFile {
inheritanceClause: TypeInheritanceClause {
InheritedType(typeName: type.expressibleAs)
},
genericWhereClause: GenericWhereClause(leadingTrivia: .space) {
genericWhereClause: GenericWhereClause {
GenericRequirement(body: SameTypeRequirement(
leftTypeIdentifier: "Element",
equalityToken: .spacedBinaryOperator("=="),
Expand Down
48 changes: 48 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/DoStmtTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import XCTest
import SwiftSyntaxBuilder

final class DoStmtTests: XCTestCase {
func testDoStmt() {
let buildable = DoStmt(
labelName: nil,
body: CodeBlock(statementsBuilder: {
CodeBlockItem(item: TryExpr(expression: FunctionCallExpr(MemberAccessExpr(base: "a", name: "b"))))
}),
catchClauses: [
CatchClause(CatchItemList {
CatchItem(pattern: "Error1")
CatchItem(pattern: "Error2")
}) {
FunctionCallExpr("print") {
TupleExprElement(expression: StringLiteralExpr("Known error"))
}
},
CatchClause(CatchItemList {
CatchItem(
pattern: "Error3", whereClause: WhereClause(guardResult: MemberAccessExpr(base: "error", name: "isError4")))
}) {
ThrowStmt(expression: MemberAccessExpr(base: "Error4", name: "error3"))
},
CatchClause {
FunctionCallExpr("print") {
TupleExprElement(expression: "error")
}
}
])

let syntax = buildable.buildSyntax(format: Format())
XCTAssertEqual(
syntax.description,
"""
do {
try a.b()
} catch Error1, Error2 {
print("Known error")
} catch Error3 where error.isError4 {
throw Error4.error3
} catch {
print(error)
}
""")
}
}
2 changes: 1 addition & 1 deletion Tests/SwiftSyntaxBuilderTest/StructTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class StructTests: XCTestCase {
],
structKeyword: .struct,
identifier: "NestedStruct",
genericParameterClause: GenericParameterClause {
genericParameterClause: GenericParameterClause(rightAngleBracket: .rightAngle.withoutTrailingTrivia()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a temporary fix needed because of updates for the where keyword. I'm going to remove it with adjusting spaces around angle brackets in my next PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure how big the changes for the angle brackets are but feel free to include them in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not big, however I'd like to make a separate PR for them

GenericParameter(name: "A")
GenericParameter(name: "B", colon: .colon, inheritedType: "C")
GenericParameter(name: "D")
Expand Down