@@ -18,13 +18,30 @@ import SwiftSyntax
18
18
import XCTest
19
19
import _SwiftSyntaxTestSupport
20
20
21
+ fileprivate func assertFormatted< T: SyntaxProtocol > (
22
+ tree: T ,
23
+ expected: String ,
24
+ using format: BasicFormat = BasicFormat ( indentationWidth: . spaces( 4 ) ) ,
25
+ file: StaticString = #file,
26
+ line: UInt = #line
27
+ ) {
28
+ assertStringsEqualWithDiff ( tree. formatted ( using: format) . description, expected, file: file, line: line)
29
+ }
30
+
21
31
fileprivate func assertFormatted(
22
32
source: String ,
23
33
expected: String ,
34
+ using format: BasicFormat = BasicFormat ( indentationWidth: . spaces( 4 ) ) ,
24
35
file: StaticString = #file,
25
36
line: UInt = #line
26
37
) {
27
- assertStringsEqualWithDiff ( Parser . parse ( source: source) . formatted ( ) . description, expected, file: file, line: line)
38
+ assertFormatted (
39
+ tree: Parser . parse ( source: source) ,
40
+ expected: expected,
41
+ using: format,
42
+ file: file,
43
+ line: line
44
+ )
28
45
}
29
46
30
47
final class BasicFormatTest : XCTestCase {
@@ -190,4 +207,57 @@ final class BasicFormatTest: XCTestCase {
190
207
"""
191
208
)
192
209
}
210
+
211
+ func testDontInsertTrailingWhitespaceIfNextTokenStartsWithLeadingWhitespace( ) {
212
+ let tree = VariableDeclSyntax (
213
+ bindingKeyword: . keyword( . var) ,
214
+ bindings: PatternBindingListSyntax ( [
215
+ PatternBindingSyntax (
216
+ pattern: PatternSyntax ( IdentifierPatternSyntax ( identifier: . identifier( " x " ) ) ) ,
217
+ typeAnnotation: TypeAnnotationSyntax (
218
+ colon: . colonToken( trailingTrivia: . space) ,
219
+ type: TypeSyntax ( SimpleTypeIdentifierSyntax ( name: . identifier( " Int " ) ) )
220
+ ) ,
221
+ accessor: PatternBindingSyntax . Accessor (
222
+ AccessorBlockSyntax (
223
+ leftBrace: . leftBraceToken( leadingTrivia: . space) ,
224
+ accessors: AccessorListSyntax ( [ ] ) ,
225
+ rightBrace: . rightBraceToken( leadingTrivia: . newline)
226
+ )
227
+ )
228
+ )
229
+ ] )
230
+ )
231
+ assertFormatted (
232
+ tree: tree,
233
+ expected: """
234
+ var x: Int {
235
+ }
236
+ """
237
+ )
238
+ }
239
+
240
+ func testAccessor( ) {
241
+ let source = """
242
+ struct Point {
243
+ var computed: Int {
244
+ get { 0 }
245
+ }
246
+ }
247
+ """
248
+
249
+ assertFormatted (
250
+ source: source,
251
+ expected: """
252
+ struct Point {
253
+ var computed: Int {
254
+ get {
255
+ 0
256
+ }
257
+ }
258
+ }
259
+ """ ,
260
+ using: BasicFormat ( indentationWidth: . spaces( 2 ) )
261
+ )
262
+ }
193
263
}
0 commit comments