Skip to content

Convenience initializer for TupleExprElement #478

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 1 commit into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 - 2022 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

public extension TupleExprElement {
/// A convenience initializer that allows passing in label as an optional string.
/// The presence of the colon will be inferred based on the presence of the label.
init(label: String? = nil, expression: ExpressibleAsExprBuildable) {
self.init(
label: label.map(TokenSyntax.identifier), colon: label == nil ? nil : .colon, expression: expression)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ let tokensFile = SourceFile {
let body = CodeBlock {
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "makeToken")) {
TupleExprElement(expression: MemberAccessExpr(name: "eof"))
TupleExprElement(label: TokenSyntax.identifier("presence"), colon: .colon, expression: MemberAccessExpr(name: "present"))
TupleExprElement(label: "presence", expression: MemberAccessExpr(name: "present"))
}
}

Expand Down
6 changes: 1 addition & 5 deletions Tests/SwiftSyntaxBuilderTest/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ final class FunctionTests: XCTestCase {
func testArguments() {
let buildable = FunctionCallExpr("test") {
for param in (1...5) {
if param.isMultiple(of: 2) {
TupleExprElement(label: .identifier("p\(param)"), colon: .colon, expression: "value\(param)")
} else {
TupleExprElement(expression: "value\(param)")
}
TupleExprElement(label: param.isMultiple(of: 2) ? "p\(param)" : nil, expression: "value\(param)")
}
}
let syntax = buildable.buildSyntax(format: Format())
Expand Down