12
12
13
13
import SwiftSyntax
14
14
15
+ // MARK: - Error
16
+
15
17
// MARK: - PartialSyntaxNode
16
18
17
19
/// A type that is expressible by string interpolation the same way that syntax
@@ -39,16 +41,22 @@ extension SyntaxStringInterpolation {
39
41
40
42
public protocol HasTrailingCodeBlock {
41
43
var body : CodeBlockSyntax { get set }
44
+
45
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws
42
46
}
43
47
44
- public extension HasTrailingCodeBlock where Self: SyntaxExpressibleByStringInterpolation {
45
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) {
46
- self = " \( signature) {} "
48
+ public extension HasTrailingCodeBlock where Self: StmtSyntaxProtocol {
49
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws {
50
+ let stmt = StmtSyntax ( " \( signature) {} " )
51
+ guard let castedStmt = stmt. as ( Self . self) else {
52
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: stmt)
53
+ }
54
+ self = castedStmt
47
55
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
48
56
}
49
57
}
50
58
51
- extension CatchClauseSyntax : HasTrailingCodeBlock { }
59
+ // extension CatchClauseSyntax: HasTrailingCodeBlock {}
52
60
extension DeferStmtSyntax : HasTrailingCodeBlock { }
53
61
extension DoStmtSyntax : HasTrailingCodeBlock { }
54
62
extension ForInStmtSyntax : HasTrailingCodeBlock { }
@@ -59,11 +67,17 @@ extension WhileStmtSyntax: HasTrailingCodeBlock {}
59
67
60
68
public protocol HasTrailingOptionalCodeBlock {
61
69
var body : CodeBlockSyntax ? { get set }
70
+
71
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws
62
72
}
63
73
64
- public extension HasTrailingOptionalCodeBlock where Self: SyntaxExpressibleByStringInterpolation {
65
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) {
66
- self = " \( signature) {} "
74
+ public extension HasTrailingOptionalCodeBlock where Self: DeclSyntaxProtocol {
75
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax ) throws {
76
+ let decl = DeclSyntax ( " \( signature) {} " )
77
+ guard let castedDecl = decl. as ( Self . self) else {
78
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: decl)
79
+ }
80
+ self = castedDecl
67
81
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
68
82
}
69
83
}
@@ -77,11 +91,17 @@ extension InitializerDeclSyntax: HasTrailingOptionalCodeBlock {}
77
91
78
92
public protocol HasTrailingMemberDeclBlock {
79
93
var members : MemberDeclBlockSyntax { get set }
94
+
95
+ init ( _ signature: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) throws
80
96
}
81
97
82
- public extension HasTrailingMemberDeclBlock where Self: SyntaxExpressibleByStringInterpolation {
83
- init ( _ signature: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) {
84
- self = " \( signature) {} "
98
+ public extension HasTrailingMemberDeclBlock where Self: DeclSyntaxProtocol {
99
+ init ( _ signature: PartialSyntaxNodeString , @MemberDeclListBuilder membersBuilder: ( ) -> MemberDeclListSyntax ) throws {
100
+ let decl = DeclSyntax ( " \( signature) {} " )
101
+ guard let castedDecl = decl. as ( Self . self) else {
102
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: decl)
103
+ }
104
+ self = castedDecl
85
105
self . members = MemberDeclBlockSyntax ( members: membersBuilder ( ) )
86
106
}
87
107
}
@@ -98,15 +118,23 @@ extension StructDeclSyntax: HasTrailingMemberDeclBlock {}
98
118
// So we cannot conform to `HasTrailingCodeBlock`
99
119
100
120
public extension IfStmtSyntax {
101
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , @CodeBlockItemListBuilder `else` elseBuilder: ( ) -> CodeBlockItemListSyntax ? = { nil } ) {
102
- self = " \( signature) {} "
121
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , @CodeBlockItemListBuilder `else` elseBuilder: ( ) -> CodeBlockItemListSyntax ? = { nil } ) throws {
122
+ let stmt = StmtSyntax ( " \( signature) {} " )
123
+ guard let ifStmt = stmt. as ( IfStmtSyntax . self) else {
124
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: stmt)
125
+ }
126
+ self = ifStmt
103
127
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
104
128
self . elseBody = elseBuilder ( ) . map { . codeBlock( CodeBlockSyntax ( statements: $0) ) }
105
129
self . elseKeyword = elseBody != nil ? . keyword( . else) : nil
106
130
}
107
131
108
- init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , elseIf: IfStmtSyntax ) {
109
- self = " \( signature) {} "
132
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder bodyBuilder: ( ) -> CodeBlockItemListSyntax , elseIf: IfStmtSyntax ) throws {
133
+ let stmt = StmtSyntax ( " \( signature) {} " )
134
+ guard let ifStmt = stmt. as ( IfStmtSyntax . self) else {
135
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: stmt)
136
+ }
137
+ self = ifStmt
110
138
self . body = CodeBlockSyntax ( statements: bodyBuilder ( ) )
111
139
self . elseBody = . ifStmt( elseIf)
112
140
self . elseKeyword = elseBody != nil ? . keyword( . else) : nil
@@ -118,8 +146,12 @@ public extension IfStmtSyntax {
118
146
// So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
119
147
120
148
public extension SwitchStmtSyntax {
121
- init ( _ signature: PartialSyntaxNodeString , @SwitchCaseListBuilder casesBuilder: ( ) -> SwitchCaseListSyntax = { SwitchCaseListSyntax ( [ ] ) } ) {
122
- self = " \( signature) {} "
149
+ init ( _ signature: PartialSyntaxNodeString , @SwitchCaseListBuilder casesBuilder: ( ) -> SwitchCaseListSyntax = { SwitchCaseListSyntax ( [ ] ) } ) throws {
150
+ let stmt = StmtSyntax ( " \( signature) {} " )
151
+ guard let castedStmt = stmt. as ( Self . self) else {
152
+ throw SyntaxStringInterpolationError . producedInvalidNodeType ( expectedType: IfStmtSyntax . self, actualNode: stmt)
153
+ }
154
+ self = castedStmt
123
155
self . cases = casesBuilder ( )
124
156
}
125
157
}
0 commit comments