diff --git a/Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift b/Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift index 6c4c3a302b9..7b2d8b77964 100644 --- a/Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift +++ b/Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift @@ -346,7 +346,7 @@ extension LabeledExprSyntax { public init(label: String? = nil, expression: some ExprSyntaxProtocol) { self.init( label: label.map { .identifier($0) }, - colon: label == nil ? nil : .colonToken(), + colon: label == nil ? nil : .colonToken(trailingTrivia: .space), expression: expression ) } diff --git a/Tests/SwiftSyntaxBuilderTest/Assertions.swift b/Tests/SwiftSyntaxBuilderTest/Assertions.swift index da1af7c6eb8..5e9d2afc4a5 100644 --- a/Tests/SwiftSyntaxBuilderTest/Assertions.swift +++ b/Tests/SwiftSyntaxBuilderTest/Assertions.swift @@ -19,10 +19,14 @@ func assertBuildResult( _ buildable: T, _ expectedResult: String, trimTrailingWhitespace: Bool = true, + format: Bool = true, file: StaticString = #file, line: UInt = #line ) { - var buildableDescription = buildable.formatted().description + var buildableDescription = + format + ? buildable.formatted().description + : buildable.description var expectedResult = expectedResult if trimTrailingWhitespace { buildableDescription = buildableDescription.trimmingTrailingWhitespace() diff --git a/Tests/SwiftSyntaxBuilderTest/LabeledExprSyntaxTests.swift b/Tests/SwiftSyntaxBuilderTest/LabeledExprSyntaxTests.swift new file mode 100644 index 00000000000..64075f57c07 --- /dev/null +++ b/Tests/SwiftSyntaxBuilderTest/LabeledExprSyntaxTests.swift @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import SwiftSyntax +import SwiftSyntaxBuilder +import XCTest + +final class LabeledExprSyntaxTests: XCTestCase { + func testLabeledExprSyntax() { + let syntax = LabeledExprSyntax(label: "arg", expression: NilLiteralExprSyntax()) + assertBuildResult(syntax, "arg: nil", format: false) + } +}