@@ -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 {
@@ -257,4 +274,58 @@ final class BasicFormatTest: XCTestCase {
257
274
"""
258
275
)
259
276
}
277
+
278
+
279
+ func testDontInsertTrailingWhitespaceIfNextTokenStartsWithLeadingWhitespace( ) {
280
+ let tree = VariableDeclSyntax (
281
+ bindingKeyword: . keyword( . var) ,
282
+ bindings: PatternBindingListSyntax ( [
283
+ PatternBindingSyntax (
284
+ pattern: PatternSyntax ( IdentifierPatternSyntax ( identifier: . identifier( " x " ) ) ) ,
285
+ typeAnnotation: TypeAnnotationSyntax (
286
+ colon: . colonToken( trailingTrivia: . space) ,
287
+ type: TypeSyntax ( SimpleTypeIdentifierSyntax ( name: . identifier( " Int " ) ) )
288
+ ) ,
289
+ accessor: PatternBindingSyntax . Accessor (
290
+ AccessorBlockSyntax (
291
+ leftBrace: . leftBraceToken( leadingTrivia: . space) ,
292
+ accessors: AccessorListSyntax ( [ ] ) ,
293
+ rightBrace: . rightBraceToken( leadingTrivia: . newline)
294
+ )
295
+ )
296
+ )
297
+ ] )
298
+ )
299
+ assertFormatted (
300
+ tree: tree,
301
+ expected: """
302
+ var x: Int {
303
+ }
304
+ """
305
+ )
306
+ }
307
+
308
+ func testAccessor( ) {
309
+ let source = """
310
+ struct Point {
311
+ var computed: Int {
312
+ get { 0 }
313
+ }
314
+ }
315
+ """
316
+
317
+ assertFormatted (
318
+ source: source,
319
+ expected: """
320
+ struct Point {
321
+ var computed: Int {
322
+ get {
323
+ 0
324
+ }
325
+ }
326
+ }
327
+ """ ,
328
+ using: BasicFormat ( indentationWidth: . spaces( 2 ) )
329
+ )
330
+ }
260
331
}
0 commit comments