@@ -18,13 +18,30 @@ import SwiftSyntax
1818import XCTest
1919import _SwiftSyntaxTestSupport
2020
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+
2131fileprivate func assertFormatted(
2232 source: String ,
2333 expected: String ,
34+ using format: BasicFormat = BasicFormat ( indentationWidth: . spaces( 4 ) ) ,
2435 file: StaticString = #file,
2536 line: UInt = #line
2637) {
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+ )
2845}
2946
3047final class BasicFormatTest : XCTestCase {
@@ -257,4 +274,57 @@ final class BasicFormatTest: XCTestCase {
257274 """
258275 )
259276 }
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+ }
260330}
0 commit comments