|
12 | 12 |
|
13 | 13 | #if swift(>=6)
|
14 | 14 | public import SwiftSyntax
|
| 15 | +internal import SwiftParser |
15 | 16 | #else
|
16 | 17 | import SwiftSyntax
|
| 18 | +import SwiftParser |
17 | 19 | #endif
|
18 | 20 |
|
19 | 21 | // MARK: - PartialSyntaxNode
|
@@ -124,7 +126,17 @@ extension WithOptionalCodeBlockSyntax where Self: DeclSyntaxProtocol {
|
124 | 126 | _ header: SyntaxNodeString,
|
125 | 127 | @CodeBlockItemListBuilder bodyBuilder: () throws -> CodeBlockItemListSyntax
|
126 | 128 | ) throws {
|
127 |
| - let decl = DeclSyntax("\(header) {}") |
| 129 | + // If the type provides a custom `SyntaxParseable` implementation, use that. Otherwise construct it as a |
| 130 | + // `DeclSyntax`. |
| 131 | + let decl: DeclSyntax |
| 132 | + var stringInterpolation = SyntaxStringInterpolation(literalCapacity: 1, interpolationCount: 1) |
| 133 | + stringInterpolation.appendInterpolation(header) |
| 134 | + stringInterpolation.appendLiteral(" {}") |
| 135 | + if let parsableType = Self.self as? SyntaxParseable.Type { |
| 136 | + decl = parsableType.init(stringInterpolation: stringInterpolation).cast(DeclSyntax.self) |
| 137 | + } else { |
| 138 | + decl = DeclSyntax(stringInterpolation: stringInterpolation) |
| 139 | + } |
128 | 140 | guard let castedDecl = decl.as(Self.self) else {
|
129 | 141 | throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: decl)
|
130 | 142 | }
|
@@ -170,7 +182,17 @@ extension HasTrailingMemberDeclBlock where Self: DeclSyntaxProtocol {
|
170 | 182 | _ header: SyntaxNodeString,
|
171 | 183 | @MemberBlockItemListBuilder membersBuilder: () throws -> MemberBlockItemListSyntax
|
172 | 184 | ) throws {
|
173 |
| - let decl = DeclSyntax("\(header) {}") |
| 185 | + // If the type provides a custom `SyntaxParseable` implementation, use that. Otherwise construct it as a |
| 186 | + // `DeclSyntax`. |
| 187 | + let decl: DeclSyntax |
| 188 | + var stringInterpolation = SyntaxStringInterpolation(literalCapacity: 1, interpolationCount: 1) |
| 189 | + stringInterpolation.appendInterpolation(header) |
| 190 | + stringInterpolation.appendLiteral(" {}") |
| 191 | + if let parsableType = Self.self as? SyntaxParseable.Type { |
| 192 | + decl = parsableType.init(stringInterpolation: stringInterpolation).cast(DeclSyntax.self) |
| 193 | + } else { |
| 194 | + decl = DeclSyntax(stringInterpolation: stringInterpolation) |
| 195 | + } |
174 | 196 | guard let castedDecl = decl.as(Self.self) else {
|
175 | 197 | throw SyntaxStringInterpolationInvalidNodeTypeError(expectedType: Self.self, actualNode: decl)
|
176 | 198 | }
|
|
0 commit comments