-
Notifications
You must be signed in to change notification settings - Fork 1
Handle composed and mixed associated type constraints #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,13 +120,16 @@ extension MockedMacro { | |
/// Returns the generic parameter clause to apply to the mock declaration, | ||
/// generated from the associated types defined by the provided protocol. | ||
/// | ||
/// The clause supports associated types with comma-separated constraints, | ||
/// composition (`&`), or a combination of both. | ||
/// | ||
/// ```swift | ||
/// @Mocked | ||
/// protocol Dependency { | ||
/// associatedtype Item: Equatable, Identifiable | ||
/// associatedtype Item: Equatable & Identifiable, Sendable | ||
/// } | ||
/// | ||
/// final class DependencyMock<Item: Equatable & Identifiable>: Dependency {} | ||
/// final class DependencyMock<Item: Sendable & Equatable & Identifiable>: Dependency {} | ||
/// ``` | ||
/// | ||
/// - Parameter protocolDeclaration: The protocol to which the mock must | ||
|
@@ -148,12 +151,21 @@ extension MockedMacro { | |
return GenericParameterClauseSyntax { | ||
for associatedTypeDeclaration in associatedTypeDeclarations { | ||
let genericParameterName = associatedTypeDeclaration.name.trimmed | ||
let genericInheritedType = associatedTypeDeclaration.inheritanceClause? | ||
.inheritedTypes(ofType: IdentifierTypeSyntax.self) | ||
.map(\.name.text) | ||
.joined(separator: " & ") | ||
|
||
if let genericInheritedType { | ||
if let inheritanceClause = associatedTypeDeclaration.inheritanceClause { | ||
let commaSeparatedInheritedTypes = inheritanceClause | ||
.inheritedTypes(ofType: IdentifierTypeSyntax.self) | ||
|
||
let composedInheritedTypes = inheritanceClause | ||
.inheritedTypes(ofType: CompositionTypeSyntax.self) | ||
.flatMap(\.elements) | ||
.compactMap { $0.type.as(IdentifierTypeSyntax.self) } | ||
|
||
let genericInheritedType = | ||
(commaSeparatedInheritedTypes + composedInheritedTypes) | ||
.map(\.name.text) | ||
.joined(separator: " & ") | ||
Comment on lines
+164
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nolinmcfarland It would probably be more correct for us to update this to be an instance of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great call out, I'll look into this! |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merging both comma-separated and composed inherited types allows us to handle mixed constraint cases correctly. |
||
GenericParameterSyntax( | ||
name: genericParameterName, | ||
colon: .colonToken(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clean up the line wrapping a little bit, assuming this doesn't have the same issue with going over the line limit: