@@ -39,16 +39,27 @@ extension SyntaxStringInterpolation {
39
39
40
40
public protocol HasTrailingCodeBlock {
41
41
var body : CodeBlockSyntax { get set }
42
+
43
+ init ( _ header: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws
42
44
}
43
45
44
- public extension HasTrailingCodeBlock where Self: SyntaxExpressibleByStringInterpolation {
45
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) {
46
- self = " \( signature) {} "
46
+ public extension HasTrailingCodeBlock where Self: StmtSyntaxProtocol {
47
+ init ( _ header: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws {
48
+ let stmt = StmtSyntax ( " \( header) {} " )
49
+ guard let castedStmt = stmt. as ( Self . self) else {
50
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: Self . self, actualNode: stmt)
51
+ }
52
+ self = castedStmt
47
53
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
48
54
}
49
55
}
50
56
51
- extension CatchClauseSyntax : HasTrailingCodeBlock { }
57
+ extension CatchClauseSyntax : HasTrailingCodeBlock {
58
+ public init ( _ header: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws {
59
+ self = CatchClauseSyntax ( " \( header) {} " )
60
+ self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
61
+ }
62
+ }
52
63
extension DeferStmtSyntax : HasTrailingCodeBlock { }
53
64
extension DoStmtSyntax : HasTrailingCodeBlock { }
54
65
extension ForInStmtSyntax : HasTrailingCodeBlock { }
@@ -59,11 +70,17 @@ extension WhileStmtSyntax: HasTrailingCodeBlock {}
59
70
60
71
public protocol HasTrailingOptionalCodeBlock {
61
72
var body : CodeBlockSyntax ? { get set }
73
+
74
+ init ( _ header: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws
62
75
}
63
76
64
- public extension HasTrailingOptionalCodeBlock where Self: SyntaxExpressibleByStringInterpolation {
65
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) {
66
- self = " \( signature) {} "
77
+ public extension HasTrailingOptionalCodeBlock where Self: DeclSyntaxProtocol {
78
+ init ( _ header: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws {
79
+ let decl = DeclSyntax ( " \( header) {} " )
80
+ guard let castedDecl = decl. as ( Self . self) else {
81
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: Self . self, actualNode: decl)
82
+ }
83
+ self = castedDecl
67
84
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
68
85
}
69
86
}
@@ -77,11 +94,17 @@ extension InitializerDeclSyntax: HasTrailingOptionalCodeBlock {}
77
94
78
95
public protocol HasTrailingMemberDeclBlock {
79
96
var members : MemberDeclBlockSyntax { get set }
97
+
98
+ init ( _ header: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) throws
80
99
}
81
100
82
- public extension HasTrailingMemberDeclBlock where Self: SyntaxExpressibleByStringInterpolation {
83
- init ( _ signature: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) {
84
- self = " \( signature) {} "
101
+ public extension HasTrailingMemberDeclBlock where Self: DeclSyntaxProtocol {
102
+ init ( _ header: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) throws {
103
+ let decl = DeclSyntax ( " \( header) {} " )
104
+ guard let castedDecl = decl. as ( Self . self) else {
105
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: Self . self, actualNode: decl)
106
+ }
107
+ self = castedDecl
85
108
self . members = MemberDeclBlockSyntax ( members: membersBuilder ( ) )
86
109
}
87
110
}
@@ -98,15 +121,23 @@ extension StructDeclSyntax: HasTrailingMemberDeclBlock {}
98
121
// So we cannot conform to `HasTrailingCodeBlock`
99
122
100
123
public extension IfStmtSyntax {
101
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , @CodeBlockItemListBuilder `else` elseBuilder: ( ) -> CodeBlockItemListSyntax ? = { nil } ) {
102
- self = " \( signature) {} "
124
+ init ( _ header: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , @CodeBlockItemListBuilder `else` elseBuilder: ( ) -> CodeBlockItemListSyntax ? = { nil } ) throws {
125
+ let stmt = StmtSyntax ( " \( header) {} " )
126
+ guard let ifStmt = stmt. as ( IfStmtSyntax . self) else {
127
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: Self . self, actualNode: stmt)
128
+ }
129
+ self = ifStmt
103
130
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
104
131
self . elseBody = elseBuilder ( ) . map { . codeBlock( CodeBlockSyntax ( statements: $0) ) }
105
132
self . elseKeyword = elseBody != nil ? . keyword( . else) : nil
106
133
}
107
134
108
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , elseIf: IfStmtSyntax ) {
109
- self = " \( signature) {} "
135
+ init ( _ header: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , elseIf: IfStmtSyntax ) throws {
136
+ let stmt = StmtSyntax ( " \( header) {} " )
137
+ guard let ifStmt = stmt. as ( IfStmtSyntax . self) else {
138
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: Self . self, actualNode: stmt)
139
+ }
140
+ self = ifStmt
110
141
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
111
142
self . elseBody = . ifStmt( elseIf)
112
143
self . elseKeyword = elseBody != nil ? . keyword( . else) : nil
@@ -118,8 +149,12 @@ public extension IfStmtSyntax {
118
149
// So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
119
150
120
151
public extension SwitchStmtSyntax {
121
- init ( _ signature: PartialSyntaxNodeString , @SwitchCaseListBuilder casesBuilder: ( ) -> SwitchCaseListSyntax = { SwitchCaseListSyntax ( [ ] ) } ) {
122
- self = " \( signature) {} "
152
+ init ( _ header: PartialSyntaxNodeString , @SwitchCaseListBuilder casesBuilder: ( ) -> SwitchCaseListSyntax = { SwitchCaseListSyntax ( [ ] ) } ) throws {
153
+ let stmt = StmtSyntax ( " \( header) {} " )
154
+ guard let castedStmt = stmt. as ( Self . self) else {
155
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: Self . self, actualNode: stmt)
156
+ }
157
+ self = castedStmt
123
158
self . cases = casesBuilder ( )
124
159
}
125
160
}
@@ -129,8 +164,12 @@ public extension SwitchStmtSyntax {
129
164
// So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
130
165
131
166
public extension VariableDeclSyntax {
132
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder accessor: ( ) -> CodeBlockItemListSyntax ) {
133
- self = " \( signature) {} "
167
+ init ( _ header: PartialSyntaxNodeString , @CodeBlockItemListBuilder accessor: ( ) -> CodeBlockItemListSyntax ) throws {
168
+ let decl = DeclSyntax ( " \( header) {} " )
169
+ guard let castedDecl = decl. as ( Self . self) else {
170
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: Self . self, actualNode: decl)
171
+ }
172
+ self = castedDecl
134
173
assert ( self . bindings. count == 1 )
135
174
var binding : PatternBindingSyntax ? = self . bindings. last
136
175
binding? . accessor = . getter( CodeBlockSyntax ( statements: accessor ( ) ) )
0 commit comments