Skip to content

Commit bfe1695

Browse files
committed
Factor function-parameter utilities into a separate file
We are likely to grow more of these utilities, and they might eventually be sunk down into a common library, so keep the factored out.
1 parent d31be38 commit bfe1695

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

Sources/SwiftSyntaxMacros/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ add_swift_host_library(SwiftSyntaxMacros
2121

2222
AbstractSourceLocation.swift
2323
BasicMacroExpansionContext.swift
24+
FunctionParameterUtils.swift
2425
MacroExpansionContext.swift
26+
MacroReplacement.swift
2527
MacroSystem.swift
2628
Syntax+MacroEvaluation.swift
2729
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import SwiftSyntax
2+
3+
extension FunctionParameterSyntax {
4+
/// Retrieve the name of the parameter as it is used in source.
5+
///
6+
/// Example:
7+
///
8+
/// func f(a: Int, _ b: Int, c see: Int) { ... }
9+
///
10+
/// The parameter names for these three parameters are `a`, `b`, and `see`,
11+
/// respectively.
12+
var parameterName: TokenSyntax? {
13+
// If there were two names, the second is the parameter name.
14+
if let secondName = secondName {
15+
if secondName.text == "_" {
16+
return nil
17+
}
18+
19+
return secondName
20+
}
21+
22+
if let firstName = firstName {
23+
if firstName.text == "_" {
24+
return nil
25+
}
26+
27+
return firstName
28+
}
29+
30+
return nil
31+
}
32+
}

Sources/SwiftSyntaxMacros/MacroReplacement.swift

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,6 @@ import SwiftDiagnostics
22
import SwiftSyntax
33
import SwiftSyntaxBuilder
44

5-
extension FunctionParameterSyntax {
6-
/// Retrieve the name of the parameter as it is used in source.
7-
///
8-
/// Example:
9-
///
10-
/// func f(a: Int, _ b: Int, c see: Int) { ... }
11-
///
12-
/// The parameter names for these three parameters are `a`, `b`, and `see`,
13-
/// respectively.
14-
var parameterName: TokenSyntax? {
15-
// If there were two names, the second is the parameter name.
16-
if let secondName = secondName {
17-
if secondName.text == "_" {
18-
return nil
19-
}
20-
21-
return secondName
22-
}
23-
24-
if let firstName = firstName {
25-
if firstName.text == "_" {
26-
return nil
27-
}
28-
29-
return firstName
30-
}
31-
32-
return nil
33-
}
34-
}
35-
365
enum MacroExpanderError: DiagnosticMessage {
376
case undefined
387
case definitionNotMacroExpansion

0 commit comments

Comments
 (0)