Skip to content

Commit

Permalink
Merge pull request #9 from p-x9/feature/custom-argument-label
Browse files Browse the repository at this point in the history
make input argument label of function customizable
  • Loading branch information
p-x9 authored Jun 30, 2023
2 parents c662bbf + 37195cf commit e859001
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
62 changes: 60 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,85 @@ For example, the `ViewController` can also be referenced as a `VC` by writing th
class ViewContoroller: UIViewController {
/* --- */
}

/* ↓↓↓↓↓ */

print(ViewContoroller.self) // => "ViewController"
print(VC.self) // => "ViewController"
```

### Variable
You can define an alias for a variable as follows
```swift
class SomeClass {
@Alias("title")
var text: String
var text: String = "hello"
}

/* ↓↓↓↓↓ */

let someClass = SomeClass()

print(text) // => "hello"
print(title) // => "hello"
```

### Function/Method
You can define an alias for a function as follows.
In this way, you can call both `hello("aaa", Date())` and `こんにちは("aaa", Date())`.
In this way, you can call both `hello("aaa", at: Date())` and `こんにちは("aaa", at: Date())`.
```swift
class SomeClass {
@Alias("こんにちは")
func hello(_ text: String, at date: Date) {
/* --- */
print(text)
}
}

/* ↓↓↓↓↓ */

let someClass = SomeClass()

someClass.hello("aaa", at: Date()) // => "aaa"
someClass.こんにちは("aaa", at: Date()) // => "aaa"
```

#### Customize Argument Label
Argument labels can also be customized by separating them with ":".

```swift
class SomeClass {
@Alias("こんにちは:いつ")
func hello(_ text: String, at date: Date) {
/* --- */
print(text)
}

@Alias("こんにちは2:_") // Omit argument labels
func hello2(_ text: String, at date: Date) {
/* --- */
print(text)
}

@Alias("こんにちは3::宛") // The first argument label is inherited. The second is customized
func hello3(_ text: String, at date: Date, to: String) {
/* --- */
print(text)
}
}

/* ↓↓↓↓↓ */

let someClass = SomeClass()

someClass.hello("aaa", at: Date()) // => "aaa"
someClass.こんにちは("aaa", いつ: Date()) // => "aaa"

someClass.hello2("aaa", at: Date()) // => "aaa"
someClass.こんにちは2("aaa", Date()) // => "aaa"

someClass.hello3("aaa", at: Date(), to: "you") // => "aaa"
someClass.こんにちは3("aaa", at: Date(), : "あなた") // => "aaa"
```

### Multiple Aliases
Expand Down
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 e859001

Please sign in to comment.