Skip to content

Add convenience initializer for CustomAttribute #650

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
Aug 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,32 @@
//===----------------------------------------------------------------------===//
//
// 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

extension CustomAttribute {
/// A convenience initializer that allows passing in arguments using a result builder
/// and automatically adds parentheses as needed, similar to the convenience
/// initializer for ``FunctionCallExpr``.
public init(
_ attributeName: ExpressibleAsTypeBuildable,
@TupleExprElementListBuilder argumentList: () -> ExpressibleAsTupleExprElementList? = { nil }
) {
let argumentList = argumentList()
self.init(
attributeName: attributeName,
leftParen: argumentList != nil ? .leftParen : nil,
argumentList: argumentList,
rightParen: argumentList != nil ? .rightParen : nil
)
}
}

26 changes: 26 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/CustomAttributeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import XCTest
import SwiftSyntax
import SwiftSyntaxBuilder

final class CustomAttributeTests: XCTestCase {
func testCustomAttributeConvenienceInitializer() {
let testCases: [UInt: (CustomAttribute, String)] = [
#line: (CustomAttribute("Test"), "@Test"),
#line: (CustomAttribute("WithParens") {}, "@WithParens()"),
#line: (CustomAttribute("WithArgs") {
TupleExprElement(expression: "value1")
TupleExprElement(label: "labelled", expression: "value2")
}, "@WithArgs(value1, labelled: value2)"),
]

for (line, testCase) in testCases {
let (builder, expected) = testCase
let syntax = builder.buildSyntax(format: Format())

var text = ""
syntax.write(to: &text)

XCTAssertEqual(text, expected, line: line)
}
}
}
71 changes: 49 additions & 22 deletions Tests/SwiftSyntaxBuilderTest/VariableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,56 @@ final class VariableTests: XCTestCase {
}

func testAttributedVariables() {
let attributedVar = VariableDecl(
attributes: CustomAttribute(attributeName: "Test", argumentList: nil),
.var,
name: "x",
type: "Int"
)

XCTAssertEqual(attributedVar.buildSyntax(format: Format()).description, """
@Test var x: Int
""")
let testCases: [UInt: (VariableDecl, String)] = [
#line: (
VariableDecl(
attributes: CustomAttribute("Test"),
.var,
name: "x",
type: "Int"
),
"""
@Test var x: Int
"""
),
#line: (
VariableDecl(
attributes: CustomAttribute("Test"),
name: "y",
type: "String"
) {
StringLiteralExpr("Hello world!")
},
"""
@Test var y: String {
"Hello world!"
}
"""
),
#line: (
VariableDecl(
attributes: CustomAttribute("WithArgs") {
TupleExprElement(expression: "value1")
TupleExprElement(label: "label", expression: "value2")
},
name: "z",
type: "Float"
) {
FloatLiteralExpr(0.0)
},
"""
@WithArgs(value1, label: value2) var z: Float {
0.0
}
"""
),
]

let attributedProperty = VariableDecl(
attributes: CustomAttribute(attributeName: "Test", argumentList: nil),
name: "y",
type: "String"
) {
StringLiteralExpr("Hello world!")
}
for (line, testCase) in testCases {
let (builder, expected) = testCase
let syntax = builder.buildSyntax(format: Format())

XCTAssertEqual(attributedProperty.buildSyntax(format: Format()).description, """
@Test var y: String {
"Hello world!"
}
""")
XCTAssertEqual(syntax.description, expected, line: line)
}
}
}