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