@@ -39,16 +39,31 @@ extension SyntaxStringInterpolation {
39
39
40
40
public protocol HasTrailingCodeBlock {
41
41
var body : CodeBlockSyntax { get set }
42
+
43
+ init ( _ signature: 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 ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws {
48
+ let stmt = StmtSyntax ( " \( signature) {} " )
49
+ guard let castedStmt = stmt. as ( Self . self) else {
50
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . 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 ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws {
59
+ let stmt = CatchClauseSyntax ( " \( signature) {} " )
60
+ guard let castedStmt = stmt. as ( Self . self) else {
61
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: stmt)
62
+ }
63
+ self = castedStmt
64
+ self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
65
+ }
66
+ }
52
67
extension DeferStmtSyntax : HasTrailingCodeBlock { }
53
68
extension DoStmtSyntax : HasTrailingCodeBlock { }
54
69
extension ForInStmtSyntax : HasTrailingCodeBlock { }
@@ -59,11 +74,17 @@ extension WhileStmtSyntax: HasTrailingCodeBlock {}
59
74
60
75
public protocol HasTrailingOptionalCodeBlock {
61
76
var body : CodeBlockSyntax ? { get set }
77
+
78
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws
62
79
}
63
80
64
- public extension HasTrailingOptionalCodeBlock where Self: SyntaxExpressibleByStringInterpolation {
65
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) {
66
- self = " \( signature) {} "
81
+ public extension HasTrailingOptionalCodeBlock where Self: DeclSyntaxProtocol {
82
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws {
83
+ let decl = DeclSyntax ( " \( signature) {} " )
84
+ guard let castedDecl = decl. as ( Self . self) else {
85
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: decl)
86
+ }
87
+ self = castedDecl
67
88
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
68
89
}
69
90
}
@@ -77,11 +98,17 @@ extension InitializerDeclSyntax: HasTrailingOptionalCodeBlock {}
77
98
78
99
public protocol HasTrailingMemberDeclBlock {
79
100
var members : MemberDeclBlockSyntax { get set }
101
+
102
+ init ( _ signature: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) throws
80
103
}
81
104
82
- public extension HasTrailingMemberDeclBlock where Self: SyntaxExpressibleByStringInterpolation {
83
- init ( _ signature: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) {
84
- self = " \( signature) {} "
105
+ public extension HasTrailingMemberDeclBlock where Self: DeclSyntaxProtocol {
106
+ init ( _ signature: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) throws {
107
+ let decl = DeclSyntax ( " \( signature) {} " )
108
+ guard let castedDecl = decl. as ( Self . self) else {
109
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: decl)
110
+ }
111
+ self = castedDecl
85
112
self . members = MemberDeclBlockSyntax ( members: membersBuilder ( ) )
86
113
}
87
114
}
@@ -98,15 +125,23 @@ extension StructDeclSyntax: HasTrailingMemberDeclBlock {}
98
125
// So we cannot conform to `HasTrailingCodeBlock`
99
126
100
127
public extension IfStmtSyntax {
101
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , @CodeBlockItemListBuilder `else` elseBuilder: ( ) -> CodeBlockItemListSyntax ? = { nil } ) {
102
- self = " \( signature) {} "
128
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , @CodeBlockItemListBuilder `else` elseBuilder: ( ) -> CodeBlockItemListSyntax ? = { nil } ) throws {
129
+ let stmt = StmtSyntax ( " \( signature) {} " )
130
+ guard let ifStmt = stmt. as ( IfStmtSyntax . self) else {
131
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: stmt)
132
+ }
133
+ self = ifStmt
103
134
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
104
135
self . elseBody = elseBuilder ( ) . map { . codeBlock( CodeBlockSyntax ( statements: $0) ) }
105
136
self . elseKeyword = elseBody != nil ? . keyword( . else) : nil
106
137
}
107
138
108
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , elseIf: IfStmtSyntax ) {
109
- self = " \( signature) {} "
139
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , elseIf: IfStmtSyntax ) throws {
140
+ let stmt = StmtSyntax ( " \( signature) {} " )
141
+ guard let ifStmt = stmt. as ( IfStmtSyntax . self) else {
142
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: stmt)
143
+ }
144
+ self = ifStmt
110
145
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
111
146
self . elseBody = . ifStmt( elseIf)
112
147
self . elseKeyword = elseBody != nil ? . keyword( . else) : nil
@@ -118,8 +153,12 @@ public extension IfStmtSyntax {
118
153
// So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
119
154
120
155
public extension SwitchStmtSyntax {
121
- init ( _ signature: PartialSyntaxNodeString , @SwitchCaseListBuilder casesBuilder: ( ) -> SwitchCaseListSyntax = { SwitchCaseListSyntax ( [ ] ) } ) {
122
- self = " \( signature) {} "
156
+ init ( _ signature: PartialSyntaxNodeString , @SwitchCaseListBuilder casesBuilder: ( ) -> SwitchCaseListSyntax = { SwitchCaseListSyntax ( [ ] ) } ) throws {
157
+ let stmt = StmtSyntax ( " \( signature) {} " )
158
+ guard let castedStmt = stmt. as ( Self . self) else {
159
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: stmt)
160
+ }
161
+ self = castedStmt
123
162
self . cases = casesBuilder ( )
124
163
}
125
164
}
0 commit comments