Skip to content

Add attributes to VariableDecl convenience initializers #644

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
Aug 28, 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
Expand Up @@ -16,13 +16,19 @@ extension VariableDecl {
/// Creates an optionally initialized property.
public init(
leadingTrivia: Trivia = [],
modifiers: ModifierList? = nil,
attributes: ExpressibleAsAttributeList? = nil,
modifiers: ExpressibleAsModifierList? = nil,
_ letOrVarKeyword: TokenSyntax,
name: ExpressibleAsIdentifierPattern,
type: ExpressibleAsTypeAnnotation? = nil,
initializer: ExpressibleAsInitializerClause? = nil
) {
self.init(leadingTrivia: leadingTrivia, modifiers: modifiers, letOrVarKeyword: letOrVarKeyword) {
self.init(
leadingTrivia: leadingTrivia,
attributes: attributes,
modifiers: modifiers,
letOrVarKeyword: attributes != nil ? letOrVarKeyword.withLeadingTrivia(.space) : letOrVarKeyword
) {
PatternBinding(
pattern: name,
typeAnnotation: type,
Expand All @@ -34,12 +40,18 @@ extension VariableDecl {
/// Creates a computed property with the given accessor.
public init(
leadingTrivia: Trivia = [],
modifiers: ModifierList? = nil,
attributes: ExpressibleAsAttributeList? = nil,
modifiers: ExpressibleAsModifierList? = nil,
name: ExpressibleAsIdentifierPattern,
type: ExpressibleAsTypeAnnotation,
@CodeBlockItemListBuilder accessor: () -> ExpressibleAsCodeBlockItemList
) {
self.init(leadingTrivia: leadingTrivia, modifiers: modifiers, letOrVarKeyword: .var) {
self.init(
leadingTrivia: leadingTrivia,
attributes: attributes,
modifiers: modifiers,
letOrVarKeyword: .varKeyword(leadingTrivia: attributes != nil ? .space : .zero)
) {
PatternBinding(
pattern: name,
typeAnnotation: type,
Expand Down
27 changes: 27 additions & 0 deletions Tests/SwiftSyntaxBuilderTest/VariableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,31 @@ 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 attributedProperty = VariableDecl(
attributes: CustomAttribute(attributeName: "Test", argumentList: nil),
name: "y",
type: "String"
) {
StringLiteralExpr("Hello world!")
}

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