Skip to content

Commit

Permalink
make input argument label of function customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
p-x9 committed Jun 29, 2023
1 parent c662bbf commit ed74378
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion Sources/AliasPlugin/AliasMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ extension AccessControl {
struct AliasMacro {
struct Arguments {
let alias: String
let functionArgumentLabels: [String]
let accessControl: AccessControl

init(alias: String, accessControl: AccessControl?) {
self.alias = alias
let components = alias.components(separatedBy: ":")
self.alias = components.first ?? alias
if components.count > 1 {
self.functionArgumentLabels = Array(components[1...])
} else {
self.functionArgumentLabels = []
}
self.accessControl = accessControl ?? .inherit
}
}
Expand Down Expand Up @@ -154,10 +161,27 @@ extension AliasMacro {
let attributes = functionDecl.attributes?.removed(attribute)
let accessModifier = arguments.accessControl.modifier ?? functionDecl.accessModifier

let parameters: [FunctionParameterSyntax] = functionDecl.signature.input.parameterList.enumerated()
.map { i, param in
if let newLabel = arguments.functionArgumentLabels[safe: i],
newLabel != "" {
if param.secondName != nil {
return param.with(\.firstName, .identifier(newLabel))
} else {
return param
.with(\.firstName, .identifier(newLabel))
.with(\.secondName, param.firstName)

}
}
return param
}

let newDecl = functionDecl
.with(\.identifier, .identifier(arguments.alias))
.with(\.attributes, attributes)
.with(\.accessModifier, accessModifier)
.with(\.signature.input.parameterList, .init(parameters))
.with(\.body, CodeBlockSyntax(statements: CodeBlockItemListSyntax {
functionDecl.callWithSameArguments(
calledExpression: MemberAccessExprSyntax(
Expand Down

0 comments on commit ed74378

Please sign in to comment.