Skip to content

Fix missing space in LabeledExprSyntax init #2291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 1 deletion Tests/SwiftSyntaxBuilderTest/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahoppen do you remember why you changed this to format by default? Makes all these basic format tests too, which maybe we want, but it does end up meaning we miss things like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t remember. But I believe it was something along the lines of: You are using SwiftSyntaxBuilder to build your syntax tree and if you are doing that, it’s expected that you call .formatted in the end go get something that actually parses again, so that’s what we’re testing here.

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)
}
}