Skip to content

Commit 93dfd8f

Browse files
committed
Adopt With(Optional)CodeBlockSyntax in SwiftSyntaxBuilder
These traits cover the `body` that's a code block in various syntax nodes. Make `HasTrailingCodeBlock` inherit from `WithCodeBlock`. It only applies to a subset of Eliminate `HasTrailingOptionalCodeBlock` in favor of `WithOptionalCodeBlock`. We don't need the extra protocol for customization.
1 parent 967c838 commit 93dfd8f

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

Sources/SwiftSyntaxBuilder/SyntaxNodeWithBody.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ extension SyntaxStringInterpolation {
4040
}
4141
}
4242

43-
// MARK: - HasCodeBlock
44-
45-
public protocol HasTrailingCodeBlock {
46-
var body: CodeBlockSyntax { get set }
43+
// MARK: - HasTrailingCodeBlock
4744

45+
public protocol HasTrailingCodeBlock: WithCodeBlockSyntax {
4846
/// Constructs a syntax node where `header` builds the text of the node before the body in braces and `bodyBuilder` is used to build the node’s body.
4947
///
5048
/// For example, you can construct
@@ -90,11 +88,9 @@ extension ForStmtSyntax: HasTrailingCodeBlock {}
9088
extension GuardStmtSyntax: HasTrailingCodeBlock {}
9189
extension WhileStmtSyntax: HasTrailingCodeBlock {}
9290

93-
// MARK: - HasOptionalCodeBlock
94-
95-
public protocol HasTrailingOptionalCodeBlock {
96-
var body: CodeBlockSyntax? { get set }
91+
// MARK: - WithOptionalCodeBlockSyntax
9792

93+
public extension WithOptionalCodeBlockSyntax where Self: DeclSyntaxProtocol {
9894
/// Constructs a syntax node where `header` builds the text of the node before the body in braces and `bodyBuilder` is used to build the node’s body.
9995
///
10096
/// For example, you can construct
@@ -114,10 +110,6 @@ public protocol HasTrailingOptionalCodeBlock {
114110
/// ```
115111
///
116112
/// Throws an error if `header` defines a different node type than the type the initializer is called on. E.g. if calling `try FunctionDeclSyntax("init") {}`
117-
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax) throws
118-
}
119-
120-
public extension HasTrailingOptionalCodeBlock where Self: DeclSyntaxProtocol {
121113
init(_ header: SyntaxNodeString, @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax) throws {
122114
let decl = DeclSyntax("\(header) {}")
123115
guard let castedDecl = decl.as(Self.self) else {
@@ -128,11 +120,6 @@ public extension HasTrailingOptionalCodeBlock where Self: DeclSyntaxProtocol {
128120
}
129121
}
130122

131-
extension AccessorDeclSyntax: HasTrailingOptionalCodeBlock {}
132-
extension DeinitializerDeclSyntax: HasTrailingOptionalCodeBlock {}
133-
extension FunctionDeclSyntax: HasTrailingOptionalCodeBlock {}
134-
extension InitializerDeclSyntax: HasTrailingOptionalCodeBlock {}
135-
136123
// MARK: HasTrailingMemberDeclBlock
137124

138125
public protocol HasTrailingMemberDeclBlock {

0 commit comments

Comments
 (0)