Skip to content

Commit b04d774

Browse files
authored
Merge pull request swiftlang#650 from fwcd/fwcd/attribute-convenience-init
Add convenience initializer for `CustomAttribute`
2 parents a36ed26 + b3e14ad commit b04d774

File tree

3 files changed

+107
-22
lines changed

3 files changed

+107
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
extension CustomAttribute {
16+
/// A convenience initializer that allows passing in arguments using a result builder
17+
/// and automatically adds parentheses as needed, similar to the convenience
18+
/// initializer for ``FunctionCallExpr``.
19+
public init(
20+
_ attributeName: ExpressibleAsTypeBuildable,
21+
@TupleExprElementListBuilder argumentList: () -> ExpressibleAsTupleExprElementList? = { nil }
22+
) {
23+
let argumentList = argumentList()
24+
self.init(
25+
attributeName: attributeName,
26+
leftParen: argumentList != nil ? .leftParen : nil,
27+
argumentList: argumentList,
28+
rightParen: argumentList != nil ? .rightParen : nil
29+
)
30+
}
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import XCTest
2+
import SwiftSyntax
3+
import SwiftSyntaxBuilder
4+
5+
final class CustomAttributeTests: XCTestCase {
6+
func testCustomAttributeConvenienceInitializer() {
7+
let testCases: [UInt: (CustomAttribute, String)] = [
8+
#line: (CustomAttribute("Test"), "@Test"),
9+
#line: (CustomAttribute("WithParens") {}, "@WithParens()"),
10+
#line: (CustomAttribute("WithArgs") {
11+
TupleExprElement(expression: "value1")
12+
TupleExprElement(label: "labelled", expression: "value2")
13+
}, "@WithArgs(value1, labelled: value2)"),
14+
]
15+
16+
for (line, testCase) in testCases {
17+
let (builder, expected) = testCase
18+
let syntax = builder.buildSyntax(format: Format())
19+
20+
var text = ""
21+
syntax.write(to: &text)
22+
23+
XCTAssertEqual(text, expected, line: line)
24+
}
25+
}
26+
}

Tests/SwiftSyntaxBuilderTest/VariableTests.swift

+49-22
Original file line numberDiff line numberDiff line change
@@ -107,29 +107,56 @@ final class VariableTests: XCTestCase {
107107
}
108108

109109
func testAttributedVariables() {
110-
let attributedVar = VariableDecl(
111-
attributes: CustomAttribute(attributeName: "Test", argumentList: nil),
112-
.var,
113-
name: "x",
114-
type: "Int"
115-
)
116-
117-
XCTAssertEqual(attributedVar.buildSyntax(format: Format()).description, """
118-
@Test var x: Int
119-
""")
110+
let testCases: [UInt: (VariableDecl, String)] = [
111+
#line: (
112+
VariableDecl(
113+
attributes: CustomAttribute("Test"),
114+
.var,
115+
name: "x",
116+
type: "Int"
117+
),
118+
"""
119+
@Test var x: Int
120+
"""
121+
),
122+
#line: (
123+
VariableDecl(
124+
attributes: CustomAttribute("Test"),
125+
name: "y",
126+
type: "String"
127+
) {
128+
StringLiteralExpr("Hello world!")
129+
},
130+
"""
131+
@Test var y: String {
132+
"Hello world!"
133+
}
134+
"""
135+
),
136+
#line: (
137+
VariableDecl(
138+
attributes: CustomAttribute("WithArgs") {
139+
TupleExprElement(expression: "value1")
140+
TupleExprElement(label: "label", expression: "value2")
141+
},
142+
name: "z",
143+
type: "Float"
144+
) {
145+
FloatLiteralExpr(0.0)
146+
},
147+
"""
148+
@WithArgs(value1, label: value2) var z: Float {
149+
0.0
150+
}
151+
"""
152+
),
153+
]
120154

121-
let attributedProperty = VariableDecl(
122-
attributes: CustomAttribute(attributeName: "Test", argumentList: nil),
123-
name: "y",
124-
type: "String"
125-
) {
126-
StringLiteralExpr("Hello world!")
127-
}
155+
for (line, testCase) in testCases {
156+
let (builder, expected) = testCase
157+
let syntax = builder.buildSyntax(format: Format())
128158

129-
XCTAssertEqual(attributedProperty.buildSyntax(format: Format()).description, """
130-
@Test var y: String {
131-
"Hello world!"
132-
}
133-
""")
159+
XCTAssertEqual(syntax.description, expected, line: line)
160+
}
134161
}
135162
}

0 commit comments

Comments
 (0)