Skip to content

Commit b923209

Browse files
BrennaniumMatyasKriz
authored andcommitted
Address code-review nitpicks
1 parent f61dee1 commit b923209

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

Generator/Sources/Internal/Templates/MockTemplate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ extension Templates {
1616
extension {{ container.parentFullyQualifiedName }} {
1717
{% endif %}
1818
{% if container.hasPrimaryAssociatedTypes %}
19-
@available(iOS 16.0.0, macOS 13.0.0, watchOS 9.0, tvOS 16, *) // runtime support for constrained protocols with primary associated types
19+
// runtime support for constrained protocols with primary associated types
20+
@available(iOS 16, macOS 13, watchOS 9, tvOS 16, *)
2021
{% endif %}
2122
{{ container.accessibility|withSpace }}class {{ container.mockName }}{{ container.genericParameters }}:{% if container.isNSObjectProtocol %} NSObject,{% endif %} {{ container.name }}{% if container.isImplementation %}{{ container.genericArguments }}{% endif %},{% if container.isImplementation %} Cuckoo.ClassMock{% else %} Cuckoo.ProtocolMock{% endif %}, @unchecked Sendable {
2223
{% if container.isGeneric and not container.isImplementation and not container.hasOnlyPrimaryAssociatedTypes %}

Generator/Sources/Internal/Tokens/Capabilities/HasGenerics.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ extension HasGenerics {
3131
}
3232

3333
func genericsSerialize() -> GeneratorContext {
34-
var genericProtocolIdentity: String?
35-
var genericPrimaryAssociatedTypeArguments: String?
36-
37-
if let protocolDeclaration = asProtocol {
38-
genericProtocolIdentity = genericParameters.map { "\(Templates.staticGenericParameter).\($0.name) == \($0.name)" }.joined(separator: ", ")
39-
if !protocolDeclaration.primaryAssociatedTypes.isEmpty {
40-
let arguments = protocolDeclaration.primaryAssociatedTypes.map { $0.name }.joined(separator: ", ")
41-
genericPrimaryAssociatedTypeArguments = "<\(arguments)>"
42-
}
34+
let genericProtocolIdentity = isProtocol
35+
? genericParameters
36+
.map { "\(Templates.staticGenericParameter).\($0.name) == \($0.name)" }
37+
.joined(separator: ", ")
38+
: nil
39+
let genericPrimaryAssociatedTypeArguments: String?
40+
if let protocolDeclaration = asProtocol, hasPrimaryAssociatedTypes {
41+
let arguments = protocolDeclaration.primaryAssociatedTypes.map { $0.name }.joined(separator: ", ")
42+
genericPrimaryAssociatedTypeArguments = "<\(arguments)>"
43+
} else {
44+
genericPrimaryAssociatedTypeArguments = nil
4345
}
4446

4547
return [

Generator/Sources/Internal/Tokens/ComplexType.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,16 @@ extension ComplexType {
244244

245245
func replaceType(named typeName: String, with replacement: String) -> ComplexType? {
246246
switch self {
247-
case .attributed(attributes: let attributes, baseType: let baseType):
247+
case .attributed(let attributes, let baseType):
248248
return baseType.replaceType(named: typeName, with: replacement)
249249
.map { ComplexType.attributed(attributes: attributes, baseType: $0) }
250-
case .optional(wrappedType: let wrappedType, isImplicit: let isImplicit):
250+
case .optional(let wrappedType, let isImplicit):
251251
return wrappedType.replaceType(named: typeName, with: replacement)
252252
.map { ComplexType.optional(wrappedType: $0, isImplicit: isImplicit) }
253-
case .array(elementType: let elementType):
253+
case .array(let elementType):
254254
return elementType.replaceType(named: typeName, with: replacement)
255255
.map { ComplexType.array(elementType: $0) }
256-
case .dictionary(keyType: let keyType, valueType: let valueType):
256+
case .dictionary(let keyType, let valueType):
257257
let newKey = keyType.replaceType(named: typeName, with: replacement)
258258
let newValue = valueType.replaceType(named: typeName, with: replacement)
259259
if newKey == nil && newValue == nil { return nil }
@@ -294,13 +294,13 @@ extension ComplexType {
294294

295295
func containsType(named typeName: String) -> Bool {
296296
switch self {
297-
case .attributed(attributes: _, baseType: let baseType):
297+
case .attributed(_, let baseType):
298298
baseType.containsType(named: typeName)
299-
case .optional(wrappedType: let wrappedType, isImplicit: _):
299+
case .optional(let wrappedType, _):
300300
wrappedType.containsType(named: typeName)
301-
case .array(elementType: let elementType):
301+
case .array(let elementType):
302302
elementType.containsType(named: typeName)
303-
case .dictionary(keyType: let keyType, valueType: let valueType):
303+
case .dictionary(let keyType, let valueType):
304304
keyType.containsType(named: typeName) || valueType.containsType(named: typeName)
305305
case .closure(let closure):
306306
(closure.parameters.map(\.type) + [closure.returnType]).contains(where: { $0.containsType(named: typeName)})

Generator/Sources/Internal/Tokens/Method.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,21 @@ extension Method {
7474
.map(\.call)
7575
.joined(separator: ", ")
7676

77-
var staticGenericCall = "(\(call))"
78-
77+
let staticGenericCall: String
7978
if let parent = parent.asProtocol, !parent.nonPrimaryAssociatedTypes.isEmpty {
8079
let nonPrimary = parent.nonPrimaryAssociatedTypes.map(\.name)
8180

8281
let staticGenericCallableParameters = signature.parameters
8382
.map { $0.callAndCastTypes(named: nonPrimary, as: { Templates.staticGenericParameter + ".\($0)" }) }
8483
.joined(separator: ", ")
8584

86-
staticGenericCall = "(\(staticGenericCallableParameters))"
87-
88-
if let returnType, returnType.containsTypes(named: nonPrimary) {
89-
staticGenericCall = staticGenericCall.forceCast(as: returnType)
85+
staticGenericCall = if let returnType, returnType.containsTypes(named: nonPrimary) {
86+
"(\(staticGenericCallableParameters))".forceCast(as: returnType)
87+
} else {
88+
"(\(staticGenericCallableParameters))"
9089
}
90+
} else {
91+
staticGenericCall = "(\(call))"
9192
}
9293

9394
let stubFunctionPrefix = parent.isClass ? "Class" : "Protocol"

0 commit comments

Comments
 (0)