Skip to content

Commit

Permalink
Fix missing space in LabeledExprSyntax init (swiftlang#2291)
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
rauhul committed Oct 17, 2023
1 parent 1fa18e5 commit e8659bb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/SwiftSyntaxBuilder/ConvenienceInitializers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
6 changes: 5 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ func assertBuildResult<T: SyntaxProtocol>(
_ 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()
Expand Down
22 changes: 22 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/LabeledExprSyntaxTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit e8659bb

Please sign in to comment.