Skip to content

Commit e395b3b

Browse files
committed
SwiftExtract: Include a few general purpose is... checks
1 parent a07aadf commit e395b3b

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

Sources/SwiftExtract/SwiftTypes/SwiftFunctionSignature.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public struct SwiftFunctionSignature: Equatable {
3838
effectSpecifiers.contains(.throws)
3939
}
4040

41+
/// Whether any parameter is variadic (`T...`).
42+
public var hasVariadicParams: Bool {
43+
parameters.contains(where: \.isVariadic)
44+
}
45+
4146
public init(
4247
selfParameter: SwiftSelfParameter? = nil,
4348
parameters: [SwiftParameter],

Sources/SwiftExtract/SwiftTypes/SwiftParameter.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,44 @@ public struct SwiftParameter: Equatable {
2020
public var parameterName: String?
2121
public var type: SwiftType
2222

23+
/// Whether this is a variadic parameter (`T...`).
24+
public var isVariadic: Bool
25+
/// Whether the parameter declares a default value.
26+
public var hasDefaultValue: Bool
27+
/// The default-value expression source, if any (e.g. `42`, `[]`).
28+
public var defaultValueExpression: String?
29+
2330
public init(
2431
convention: SwiftParameterConvention,
2532
argumentLabel: String? = nil,
2633
parameterName: String? = nil,
27-
type: SwiftType
34+
type: SwiftType,
35+
isVariadic: Bool = false,
36+
hasDefaultValue: Bool = false,
37+
defaultValueExpression: String? = nil
2838
) {
2939
self.convention = convention
3040
self.argumentLabel = argumentLabel
3141
self.parameterName = parameterName
3242
self.type = type
43+
self.isVariadic = isVariadic
44+
self.hasDefaultValue = hasDefaultValue
45+
self.defaultValueExpression = defaultValueExpression
46+
}
47+
48+
/// The simple parameter name, falling back to the argument label.
49+
public var name: String {
50+
parameterName ?? argumentLabel ?? "_"
51+
}
52+
53+
/// The external argument label, falling back to the parameter name.
54+
public var effectiveLabel: String {
55+
argumentLabel ?? name
56+
}
57+
58+
/// Whether this parameter is passed `inout`.
59+
public var isInout: Bool {
60+
convention == .inout
3361
}
3462
}
3563

@@ -75,6 +103,9 @@ extension SwiftParameter {
75103
self.argumentLabel = nil
76104
self.parameterName = node.firstName?.identifier?.name
77105
self.argumentLabel = node.firstName?.identifier?.name
106+
self.isVariadic = false
107+
self.hasDefaultValue = node.defaultValue != nil
108+
self.defaultValueExpression = node.defaultValue?.value.trimmedDescription
78109
}
79110
}
80111

@@ -113,6 +144,11 @@ extension SwiftParameter {
113144
// Determine the type.
114145
self.type = try SwiftType(type, lookupContext: lookupContext)
115146

147+
// Variadic / default-value information.
148+
self.isVariadic = node.ellipsis != nil
149+
self.hasDefaultValue = node.defaultValue != nil
150+
self.defaultValueExpression = node.defaultValue?.value.trimmedDescription
151+
116152
// FIXME: swift-syntax itself should have these utilities based on identifiers.
117153
if let secondName = node.secondName {
118154
self.argumentLabel = node.firstName.identifier?.name

0 commit comments

Comments
 (0)