Skip to content

Commit 6bde5f4

Browse files
authored
Merge pull request #478 from evnik/TupleExprElement
Convenience initializer for TupleExprElement
2 parents ead746b + dace338 commit 6bde5f4

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed
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 - 2022 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+
15+
public extension TupleExprElement {
16+
/// A convenience initializer that allows passing in label as an optional string.
17+
/// The presence of the colon will be inferred based on the presence of the label.
18+
init(label: String? = nil, expression: ExpressibleAsExprBuildable) {
19+
self.init(
20+
label: label.map(TokenSyntax.identifier), colon: label == nil ? nil : .colon, expression: expression)
21+
}
22+
}

Sources/SwiftSyntaxBuilderGeneration/Templates/TokensFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ let tokensFile = SourceFile {
8888
let body = CodeBlock {
8989
FunctionCallExpr(MemberAccessExpr(base: "SyntaxFactory", name: "makeToken")) {
9090
TupleExprElement(expression: MemberAccessExpr(name: "eof"))
91-
TupleExprElement(label: TokenSyntax.identifier("presence"), colon: .colon, expression: MemberAccessExpr(name: "present"))
91+
TupleExprElement(label: "presence", expression: MemberAccessExpr(name: "present"))
9292
}
9393
}
9494

Tests/SwiftSyntaxBuilderTest/FunctionTests.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ final class FunctionTests: XCTestCase {
5656
func testArguments() {
5757
let buildable = FunctionCallExpr("test") {
5858
for param in (1...5) {
59-
if param.isMultiple(of: 2) {
60-
TupleExprElement(label: .identifier("p\(param)"), colon: .colon, expression: "value\(param)")
61-
} else {
62-
TupleExprElement(expression: "value\(param)")
63-
}
59+
TupleExprElement(label: param.isMultiple(of: 2) ? "p\(param)" : nil, expression: "value\(param)")
6460
}
6561
}
6662
let syntax = buildable.buildSyntax(format: Format())

0 commit comments

Comments
 (0)