Skip to content

Commit

Permalink
Disabled type resolving for local method generic parameters (#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
art-divin committed May 1, 2024
1 parent 6ecf791 commit 08567d9
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ extension SourceryMethod {
if let generics = genericParameterClause?.parameters {
fullName = funcName + "<\(generics.description.trimmed)>"
}

let genericParameters = genericParameterClause?.parameters.compactMap { parameter in
return GenericParameter(parameter)
} ?? []
var genericRequirements: [GenericRequirement] = []
if let genericWhereClause = genericWhereClause {
genericRequirements = genericWhereClause.requirements.compactMap { requirement in
Expand All @@ -98,6 +102,18 @@ extension SourceryMethod {
}
return nil
}

if !genericParameters.isEmpty {
// assign types from `where` clause to the associated generic parameters
for parameter in genericParameters where parameter.inheritedTypeName == nil {
if let lookupInGenericReqs = genericRequirements.first(where: {
$0.leftType.name == parameter.name
}) {
parameter.inheritedTypeName = lookupInGenericReqs.rightType.typeName
}
}
}

// TODO: TBR
returnTypeName = TypeName(name: returnTypeName.name + " \(genericWhereClause.withoutTrivia().description.trimmed)",
unwrappedTypeName: returnTypeName.unwrappedTypeName,
Expand Down Expand Up @@ -143,7 +159,8 @@ extension SourceryMethod {
annotations: annotations,
documentation: documentation,
definedInTypeName: typeName,
genericRequirements: genericRequirements
genericRequirements: genericRequirements,
genericParameters: genericParameters
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,14 @@ internal struct ParserResultsComposed {
} else
if let generic = lookupName.generic {
var needsUpdate = false

generic.typeParameters.forEach { parameter in
// Detect if the generic type is local to the method
if let method {
for genericParameter in method.genericParameters where parameter.typeName.name == genericParameter.name {
return
}
}

parameter.type = resolveTypeWithName(parameter.typeName)
if parameter.typeName.actualTypeName != nil {
needsUpdate = true
Expand Down
47 changes: 35 additions & 12 deletions SourceryRuntime/Sources/Linux/AST/Method_Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
/// list of generic requirements
public var genericRequirements: [GenericRequirement]

/// List of generic parameters
///
/// - Example:
///
/// ```swift
/// func method<GenericParameter>(foo: GenericParameter)
/// ^ ~ a generic parameter
/// ```
public var genericParameters: [GenericParameter]

/// :nodoc:
public init(name: String,
selectorName: String? = nil,
Expand All @@ -236,7 +246,8 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
annotations: [String: NSObject] = [:],
documentation: [String] = [],
definedInTypeName: TypeName? = nil,
genericRequirements: [GenericRequirement] = []) {
genericRequirements: [GenericRequirement] = [],
genericParameters: [GenericParameter] = []) {
self.name = name
self.selectorName = selectorName ?? name
self.parameters = parameters
Expand All @@ -254,6 +265,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
self.documentation = documentation
self.definedInTypeName = definedInTypeName
self.genericRequirements = genericRequirements
self.genericParameters = genericParameters
}

/// :nodoc:
Expand All @@ -276,7 +288,8 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
string.append("definedInTypeName = \(String(describing: self.definedInTypeName)), ")
string.append("attributes = \(String(describing: self.attributes)), ")
string.append("modifiers = \(String(describing: self.modifiers)), ")
string.append("genericRequirements = \(String(describing: self.genericRequirements))")
string.append("genericRequirements = \(String(describing: self.genericRequirements)), ")
string.append("genericRequirements = \(String(describing: self.genericParameters))")
return string
}

Expand All @@ -303,6 +316,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
results.append(contentsOf: DiffableResult(identifier: "attributes").trackDifference(actual: self.attributes, expected: castObject.attributes))
results.append(contentsOf: DiffableResult(identifier: "modifiers").trackDifference(actual: self.modifiers, expected: castObject.modifiers))
results.append(contentsOf: DiffableResult(identifier: "genericRequirements").trackDifference(actual: self.genericRequirements, expected: castObject.genericRequirements))
results.append(contentsOf: DiffableResult(identifier: "genericParameters").trackDifference(actual: self.genericParameters, expected: castObject.genericParameters))
return results
}

Expand All @@ -327,6 +341,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
hasher.combine(self.attributes)
hasher.combine(self.modifiers)
hasher.combine(self.genericRequirements)
hasher.combine(self.genericParameters)
return hasher.finalize()
}

Expand All @@ -351,32 +366,33 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
if self.attributes != rhs.attributes { return false }
if self.modifiers != rhs.modifiers { return false }
if self.genericRequirements != rhs.genericRequirements { return false }
if self.genericParameters != rhs.genericParameters { return false }
return true
}

// sourcery:inline:Method.AutoCoding

/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
guard let name: String = aDecoder.decode(forKey: "name") else {
guard let name: String = aDecoder.decode(forKey: "name") else {
withVaList(["name"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.name = name
guard let selectorName: String = aDecoder.decode(forKey: "selectorName") else {
guard let selectorName: String = aDecoder.decode(forKey: "selectorName") else {
withVaList(["selectorName"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.selectorName = selectorName
guard let parameters: [MethodParameter] = aDecoder.decode(forKey: "parameters") else {
guard let parameters: [MethodParameter] = aDecoder.decode(forKey: "parameters") else {
withVaList(["parameters"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.parameters = parameters
guard let returnTypeName: TypeName = aDecoder.decode(forKey: "returnTypeName") else {
guard let returnTypeName: TypeName = aDecoder.decode(forKey: "returnTypeName") else {
withVaList(["returnTypeName"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
Expand All @@ -386,7 +402,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
self.isAsync = aDecoder.decode(forKey: "isAsync")
self.`throws` = aDecoder.decode(forKey: "`throws`")
self.`rethrows` = aDecoder.decode(forKey: "`rethrows`")
guard let accessLevel: String = aDecoder.decode(forKey: "accessLevel") else {
guard let accessLevel: String = aDecoder.decode(forKey: "accessLevel") else {
withVaList(["accessLevel"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
Expand All @@ -395,38 +411,44 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
self.isStatic = aDecoder.decode(forKey: "isStatic")
self.isClass = aDecoder.decode(forKey: "isClass")
self.isFailableInitializer = aDecoder.decode(forKey: "isFailableInitializer")
guard let annotations: Annotations = aDecoder.decode(forKey: "annotations") else {
guard let annotations: Annotations = aDecoder.decode(forKey: "annotations") else {
withVaList(["annotations"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.annotations = annotations
guard let documentation: Documentation = aDecoder.decode(forKey: "documentation") else {
guard let documentation: Documentation = aDecoder.decode(forKey: "documentation") else {
withVaList(["documentation"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.documentation = documentation
self.definedInTypeName = aDecoder.decode(forKey: "definedInTypeName")
self.definedInType = aDecoder.decode(forKey: "definedInType")
guard let attributes: AttributeList = aDecoder.decode(forKey: "attributes") else {
guard let attributes: AttributeList = aDecoder.decode(forKey: "attributes") else {
withVaList(["attributes"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.attributes = attributes
guard let modifiers: [SourceryModifier] = aDecoder.decode(forKey: "modifiers") else {
guard let modifiers: [SourceryModifier] = aDecoder.decode(forKey: "modifiers") else {
withVaList(["modifiers"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.modifiers = modifiers
guard let genericRequirements: [GenericRequirement] = aDecoder.decode(forKey: "genericRequirements") else {
guard let genericRequirements: [GenericRequirement] = aDecoder.decode(forKey: "genericRequirements") else {
withVaList(["genericRequirements"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.genericRequirements = genericRequirements
guard let genericParameters: [GenericParameter] = aDecoder.decode(forKey: "genericParameters") else {
withVaList(["genericParameters"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.genericParameters = genericParameters
}

/// :nodoc:
Expand All @@ -450,6 +472,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
aCoder.encode(self.attributes, forKey: "attributes")
aCoder.encode(self.modifiers, forKey: "modifiers")
aCoder.encode(self.genericRequirements, forKey: "genericRequirements")
aCoder.encode(self.genericParameters, forKey: "genericParameters")
}
// sourcery:end
}
Expand Down
27 changes: 25 additions & 2 deletions SourceryRuntime/Sources/macOS/AST/Method.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
/// list of generic requirements
public var genericRequirements: [GenericRequirement]

/// List of generic parameters
///
/// - Example:
///
/// ```swift
/// func method<GenericParameter>(foo: GenericParameter)
/// ^ ~ a generic parameter
/// ```
public var genericParameters: [GenericParameter]

/// :nodoc:
public init(name: String,
selectorName: String? = nil,
Expand All @@ -197,7 +207,8 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
annotations: [String: NSObject] = [:],
documentation: [String] = [],
definedInTypeName: TypeName? = nil,
genericRequirements: [GenericRequirement] = []) {
genericRequirements: [GenericRequirement] = [],
genericParameters: [GenericParameter] = []) {
self.name = name
self.selectorName = selectorName ?? name
self.parameters = parameters
Expand All @@ -215,6 +226,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
self.documentation = documentation
self.definedInTypeName = definedInTypeName
self.genericRequirements = genericRequirements
self.genericParameters = genericParameters
}

/// :nodoc:
Expand All @@ -237,7 +249,8 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
string.append("definedInTypeName = \(String(describing: self.definedInTypeName)), ")
string.append("attributes = \(String(describing: self.attributes)), ")
string.append("modifiers = \(String(describing: self.modifiers)), ")
string.append("genericRequirements = \(String(describing: self.genericRequirements))")
string.append("genericRequirements = \(String(describing: self.genericRequirements)), ")
string.append("genericRequirements = \(String(describing: self.genericParameters))")
return string
}

Expand All @@ -264,6 +277,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
results.append(contentsOf: DiffableResult(identifier: "attributes").trackDifference(actual: self.attributes, expected: castObject.attributes))
results.append(contentsOf: DiffableResult(identifier: "modifiers").trackDifference(actual: self.modifiers, expected: castObject.modifiers))
results.append(contentsOf: DiffableResult(identifier: "genericRequirements").trackDifference(actual: self.genericRequirements, expected: castObject.genericRequirements))
results.append(contentsOf: DiffableResult(identifier: "genericParameters").trackDifference(actual: self.genericParameters, expected: castObject.genericParameters))
return results
}

Expand All @@ -288,6 +302,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
hasher.combine(self.attributes)
hasher.combine(self.modifiers)
hasher.combine(self.genericRequirements)
hasher.combine(self.genericParameters)
return hasher.finalize()
}

Expand All @@ -312,6 +327,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
if self.attributes != rhs.attributes { return false }
if self.modifiers != rhs.modifiers { return false }
if self.genericRequirements != rhs.genericRequirements { return false }
if self.genericParameters != rhs.genericParameters { return false }
return true
}

Expand Down Expand Up @@ -388,6 +404,12 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
}
fatalError()
}; self.genericRequirements = genericRequirements
guard let genericParameters: [GenericParameter] = aDecoder.decode(forKey: "genericParameters") else {
withVaList(["genericParameters"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.genericParameters = genericParameters
}

/// :nodoc:
Expand All @@ -411,6 +433,7 @@ public final class Method: NSObject, SourceryModel, Annotated, Documented, Defin
aCoder.encode(self.attributes, forKey: "attributes")
aCoder.encode(self.modifiers, forKey: "modifiers")
aCoder.encode(self.genericRequirements, forKey: "genericRequirements")
aCoder.encode(self.genericParameters, forKey: "genericParameters")
}
// sourcery:end
}
Expand Down

0 comments on commit 08567d9

Please sign in to comment.