Skip to content

Commit e8659bb

Browse files
authored
Fix missing space in LabeledExprSyntax init (swiftlang#2291)
Updates LabeledExprSyntax convenience initializer to include a trailing space trivia after the colon if an argument label is specified. This ensures the rendered description is 'arg: value' instead of 'arg:value'.
1 parent 1fa18e5 commit e8659bb

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ extension LabeledExprSyntax {
346346
public init(label: String? = nil, expression: some ExprSyntaxProtocol) {
347347
self.init(
348348
label: label.map { .identifier($0) },
349-
colon: label == nil ? nil : .colonToken(),
349+
colon: label == nil ? nil : .colonToken(trailingTrivia: .space),
350350
expression: expression
351351
)
352352
}

Tests/SwiftSyntaxBuilderTest/Assertions.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ func assertBuildResult<T: SyntaxProtocol>(
1919
_ buildable: T,
2020
_ expectedResult: String,
2121
trimTrailingWhitespace: Bool = true,
22+
format: Bool = true,
2223
file: StaticString = #file,
2324
line: UInt = #line
2425
) {
25-
var buildableDescription = buildable.formatted().description
26+
var buildableDescription =
27+
format
28+
? buildable.formatted().description
29+
: buildable.description
2630
var expectedResult = expectedResult
2731
if trimTrailingWhitespace {
2832
buildableDescription = buildableDescription.trimmingTrailingWhitespace()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
import SwiftSyntaxBuilder
15+
import XCTest
16+
17+
final class LabeledExprSyntaxTests: XCTestCase {
18+
func testLabeledExprSyntax() {
19+
let syntax = LabeledExprSyntax(label: "arg", expression: NilLiteralExprSyntax())
20+
assertBuildResult(syntax, "arg: nil", format: false)
21+
}
22+
}

0 commit comments

Comments
 (0)