File tree Expand file tree Collapse file tree 2 files changed +44
-15
lines changed
Sources/SwiftSyntaxBuilder
Tests/SwiftSyntaxBuilderTest Expand file tree Collapse file tree 2 files changed +44
-15
lines changed Original file line number Diff line number Diff line change @@ -123,3 +123,17 @@ public extension SwitchStmtSyntax {
123
123
self . cases = casesBuilder ( )
124
124
}
125
125
}
126
+
127
+ // MARK: - VariableDeclSyntax
128
+ // VariableDeclSyntax is a special scenario as it don't have body or members
129
+ // So we cannot conform to `HasTrailingCodeBlock` or `HasTrailingMemberDeclBlock`
130
+
131
+ public extension VariableDeclSyntax {
132
+ init ( _ signature: PartialSyntaxNodeString , @CodeBlockItemListBuilder accessor: ( ) -> CodeBlockItemListSyntax ) {
133
+ self = " \( signature) {} "
134
+ assert ( self . bindings. count == 1 )
135
+ var binding : PatternBindingSyntax ? = self . bindings. last
136
+ binding? . accessor = . getter( CodeBlockSyntax ( statements: accessor ( ) ) )
137
+ bindings = PatternBindingListSyntax ( [ binding ] . compactMap { $0 } )
138
+ }
139
+ }
Original file line number Diff line number Diff line change @@ -175,22 +175,37 @@ final class VariableTests: XCTestCase {
175
175
}
176
176
177
177
func testComputedProperty( ) {
178
- let buildable = VariableDeclSyntax ( name: " test " , type: TypeAnnotationSyntax ( type: TypeSyntax ( " Int " ) ) ) {
179
- SequenceExprSyntax {
180
- IntegerLiteralExprSyntax ( 4 )
181
- BinaryOperatorExprSyntax ( text: " + " )
182
- IntegerLiteralExprSyntax ( 5 )
183
- }
184
- }
178
+ let testCases : [ UInt : ( VariableDeclSyntax , String ) ] = [
179
+ #line: (
180
+ VariableDeclSyntax ( name: " test " , type: TypeAnnotationSyntax ( type: TypeSyntax ( " Int " ) ) ) {
181
+ SequenceExprSyntax {
182
+ IntegerLiteralExprSyntax ( 4 )
183
+ BinaryOperatorExprSyntax ( text: " + " )
184
+ IntegerLiteralExprSyntax ( 5 )
185
+ }
186
+ } ,
187
+ """
188
+ var test: Int {
189
+ 4 + 5
190
+ }
191
+ """
192
+ ) ,
193
+ #line: (
194
+ VariableDeclSyntax ( " var foo: String " ) {
195
+ ReturnStmtSyntax ( #"return "hello world""# )
196
+ } ,
197
+ """
198
+ var foo: String {
199
+ return " hello world "
200
+ }
201
+ """
202
+ ) ,
203
+ ]
185
204
186
- AssertBuildResult (
187
- buildable,
188
- """
189
- var test: Int {
190
- 4 + 5
191
- }
192
- """
193
- )
205
+ for (line, testCase) in testCases {
206
+ let ( builder, expected) = testCase
207
+ AssertBuildResult ( builder, expected, line: line)
208
+ }
194
209
}
195
210
196
211
func testAccessorList( ) {
You can’t perform that action at this time.
0 commit comments