Skip to content

Commit c204749

Browse files
committed
adding attributes to parameters
1 parent 2da069a commit c204749

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

Sources/SyntaxKit/Function.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,20 @@ public struct Function: CodeBlock {
110110
paramList = FunctionParameterListSyntax(
111111
parameters.enumerated().compactMap { index, param in
112112
guard !param.name.isEmpty, !param.type.isEmpty else { return nil }
113+
114+
// Build parameter attributes
115+
let paramAttributes = buildAttributeList(from: param.attributes)
116+
117+
// Determine spacing for firstName based on whether attributes are present
118+
let firstNameLeadingTrivia: Trivia = paramAttributes.isEmpty ? [] : .space
119+
113120
var paramSyntax = FunctionParameterSyntax(
121+
attributes: paramAttributes,
114122
firstName: param.isUnnamed
115-
? .wildcardToken(trailingTrivia: .space) : .identifier(param.name),
123+
? .wildcardToken(leadingTrivia: firstNameLeadingTrivia)
124+
: .identifier(param.name, leadingTrivia: firstNameLeadingTrivia),
116125
secondName: param.isUnnamed ? .identifier(param.name) : nil,
117-
colon: .colonToken(leadingTrivia: .space, trailingTrivia: .space),
126+
colon: .colonToken(trailingTrivia: .space),
118127
type: IdentifierTypeSyntax(name: .identifier(param.type)),
119128
defaultValue: param.defaultValue.map {
120129
InitializerClauseSyntax(

Sources/SyntaxKit/Parameter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public struct Parameter: CodeBlock {
3737
let type: String
3838
let defaultValue: String?
3939
let isUnnamed: Bool
40-
private var attributes: [AttributeInfo] = []
40+
internal var attributes: [AttributeInfo] = []
4141

4242
/// Creates a parameter for a function or initializer.
4343
/// - Parameters:

Tests/SyntaxKitTests/AttributeTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,36 @@ import Testing
164164
#expect(generated.contains("17.0"))
165165
#expect(generated.contains("var bar"))
166166
}
167+
168+
@Test("Parameter with attribute generates correct syntax")
169+
func testParameterWithAttribute() throws {
170+
let function = Function("process") {
171+
Parameter(name: "data", type: "Data")
172+
.attribute("escaping")
173+
} _: {
174+
Variable(.let, name: "result", type: "String", equals: "processed")
175+
}
176+
177+
let generated = function.syntax.description
178+
#expect(generated.contains("@escaping"))
179+
#expect(generated.contains("data: Data"))
180+
#expect(generated.contains("func process"))
181+
}
182+
183+
@Test("Parameter with attribute arguments generates correct syntax")
184+
func testParameterWithAttributeArguments() throws {
185+
let function = Function("validate") {
186+
Parameter(name: "input", type: "String")
187+
.attribute("available", arguments: ["iOS", "17.0"])
188+
} _: {
189+
Variable(.let, name: "result", type: "Bool", equals: "true")
190+
}
191+
192+
let generated = function.syntax.description
193+
#expect(generated.contains("@available"))
194+
#expect(generated.contains("iOS"))
195+
#expect(generated.contains("17.0"))
196+
#expect(generated.contains("input: String"))
197+
#expect(generated.contains("func validate"))
198+
}
167199
}

0 commit comments

Comments
 (0)