-
-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added generic requirements and generic parameters to Subscript (#1242)
- Loading branch information
1 parent
23179e8
commit ab0d133
Showing
25 changed files
with
1,322 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
SourceryFramework/Sources/Parsing/SwiftSyntax/AST/GenericParameter+SwiftSyntax.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Foundation | ||
import SwiftSyntax | ||
import SourceryRuntime | ||
|
||
extension GenericParameter { | ||
convenience init(_ node: GenericParameterSyntax) { | ||
self.init(name: node.name.description.trimmed, inheritedTypeName: node.inheritedType.flatMap(TypeName.init(_:))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#if !canImport(ObjectiveC) | ||
import Foundation | ||
// For DynamicMemberLookup we need to import Stencil, | ||
// however, this is different from SourceryRuntime.content.generated.swift, because | ||
// it cannot reference Stencil | ||
import Stencil | ||
|
||
/// Describes Swift AssociatedType | ||
public final class AssociatedType: NSObject, SourceryModel, DynamicMemberLookup { | ||
public subscript(dynamicMember member: String) -> Any? { | ||
switch member { | ||
case "name": | ||
return name | ||
case "typeName": | ||
return typeName | ||
case "type": | ||
return type | ||
default: | ||
fatalError("unable to lookup: \(member) in \(self)") | ||
} | ||
} | ||
|
||
/// Associated type name | ||
public let name: String | ||
|
||
/// Associated type type constraint name, if specified | ||
public let typeName: TypeName? | ||
|
||
// sourcery: skipEquality, skipDescription | ||
/// Associated type constrained type, if known, i.e. if the type is declared in the scanned sources. | ||
public var type: Type? | ||
|
||
/// :nodoc: | ||
public init(name: String, typeName: TypeName? = nil, type: Type? = nil) { | ||
self.name = name | ||
self.typeName = typeName | ||
self.type = type | ||
} | ||
|
||
/// :nodoc: | ||
override public var description: String { | ||
var string = "\(Swift.type(of: self)): " | ||
string += "name = \(String(describing: self.name)), " | ||
string += "typeName = \(String(describing: self.typeName))" | ||
return string | ||
} | ||
|
||
public func diffAgainst(_ object: Any?) -> DiffableResult { | ||
let results = DiffableResult() | ||
guard let castObject = object as? AssociatedType else { | ||
results.append("Incorrect type <expected: AssociatedType, received: \(Swift.type(of: object))>") | ||
return results | ||
} | ||
results.append(contentsOf: DiffableResult(identifier: "name").trackDifference(actual: self.name, expected: castObject.name)) | ||
results.append(contentsOf: DiffableResult(identifier: "typeName").trackDifference(actual: self.typeName, expected: castObject.typeName)) | ||
return results | ||
} | ||
|
||
public override var hash: Int { | ||
var hasher = Hasher() | ||
hasher.combine(self.name) | ||
hasher.combine(self.typeName) | ||
return hasher.finalize() | ||
} | ||
|
||
/// :nodoc: | ||
public override func isEqual(_ object: Any?) -> Bool { | ||
guard let rhs = object as? AssociatedType else { return false } | ||
if self.name != rhs.name { return false } | ||
if self.typeName != rhs.typeName { return false } | ||
return true | ||
} | ||
|
||
// sourcery:inline:AssociatedType.AutoCoding | ||
|
||
/// :nodoc: | ||
required public init?(coder aDecoder: NSCoder) { | ||
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 | ||
self.typeName = aDecoder.decode(forKey: "typeName") | ||
self.type = aDecoder.decode(forKey: "type") | ||
} | ||
|
||
/// :nodoc: | ||
public func encode(with aCoder: NSCoder) { | ||
aCoder.encode(self.name, forKey: "name") | ||
aCoder.encode(self.typeName, forKey: "typeName") | ||
aCoder.encode(self.type, forKey: "type") | ||
} | ||
// sourcery:end | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#if canImport(ObjectiveC) | ||
import Foundation | ||
|
||
/// Descibes Swift generic parameter | ||
@objcMembers | ||
public final class GenericParameter: NSObject, SourceryModel, Diffable { | ||
|
||
/// Generic parameter name | ||
public var name: String | ||
|
||
/// Generic parameter inherited type | ||
public var inheritedTypeName: TypeName? | ||
|
||
/// :nodoc: | ||
public init(name: String, inheritedTypeName: TypeName? = nil) { | ||
self.name = name | ||
self.inheritedTypeName = inheritedTypeName | ||
} | ||
|
||
/// :nodoc: | ||
override public var description: String { | ||
var string = "\(Swift.type(of: self)): " | ||
string += "name = \(String(describing: self.name)), " | ||
string += "inheritedTypeName = \(String(describing: self.inheritedTypeName))" | ||
return string | ||
} | ||
|
||
public func diffAgainst(_ object: Any?) -> DiffableResult { | ||
let results = DiffableResult() | ||
guard let castObject = object as? GenericParameter else { | ||
results.append("Incorrect type <expected: GenericParameter, received: \(Swift.type(of: object))>") | ||
return results | ||
} | ||
results.append(contentsOf: DiffableResult(identifier: "name").trackDifference(actual: self.name, expected: castObject.name)) | ||
results.append(contentsOf: DiffableResult(identifier: "inheritedTypeName").trackDifference(actual: self.inheritedTypeName, expected: castObject.inheritedTypeName)) | ||
return results | ||
} | ||
|
||
public override var hash: Int { | ||
var hasher = Hasher() | ||
hasher.combine(self.name) | ||
hasher.combine(self.inheritedTypeName) | ||
return hasher.finalize() | ||
} | ||
|
||
/// :nodoc: | ||
public override func isEqual(_ object: Any?) -> Bool { | ||
guard let rhs = object as? GenericParameter else { return false } | ||
if self.name != rhs.name { return false } | ||
if self.inheritedTypeName != rhs.inheritedTypeName { return false } | ||
return true | ||
} | ||
|
||
// sourcery:inline:GenericParameter.AutoCoding | ||
|
||
/// :nodoc: | ||
required public init?(coder aDecoder: NSCoder) { | ||
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 | ||
self.inheritedTypeName = aDecoder.decode(forKey: "inheritedTypeName") | ||
} | ||
|
||
/// :nodoc: | ||
public func encode(with aCoder: NSCoder) { | ||
aCoder.encode(self.name, forKey: "name") | ||
aCoder.encode(self.inheritedTypeName, forKey: "inheritedTypeName") | ||
} | ||
|
||
// sourcery:end | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#if !canImport(ObjectiveC) | ||
import Foundation | ||
// For DynamicMemberLookup we need to import Stencil, | ||
// however, this is different from SourceryRuntime.content.generated.swift, because | ||
// it cannot reference Stencil | ||
import Stencil | ||
|
||
/// Descibes Swift generic parameter | ||
public final class GenericParameter: NSObject, SourceryModel, Diffable, DynamicMemberLookup { | ||
public subscript(dynamicMember member: String) -> Any? { | ||
switch member { | ||
case "name": | ||
return name | ||
case "inheritedTypeName": | ||
return inheritedTypeName | ||
default: | ||
fatalError("unable to lookup: \(member) in \(self)") | ||
} | ||
} | ||
|
||
/// Generic parameter name | ||
public var name: String | ||
|
||
/// Generic parameter inherited type | ||
public var inheritedTypeName: TypeName? | ||
|
||
/// :nodoc: | ||
public init(name: String, inheritedTypeName: TypeName? = nil) { | ||
self.name = name | ||
self.inheritedTypeName = inheritedTypeName | ||
} | ||
|
||
/// :nodoc: | ||
override public var description: String { | ||
var string = "\(Swift.type(of: self)): " | ||
string += "name = \(String(describing: self.name)), " | ||
string += "inheritedTypeName = \(String(describing: self.inheritedTypeName))" | ||
return string | ||
} | ||
|
||
public func diffAgainst(_ object: Any?) -> DiffableResult { | ||
let results = DiffableResult() | ||
guard let castObject = object as? GenericParameter else { | ||
results.append("Incorrect type <expected: GenericParameter, received: \(Swift.type(of: object))>") | ||
return results | ||
} | ||
results.append(contentsOf: DiffableResult(identifier: "name").trackDifference(actual: self.name, expected: castObject.name)) | ||
results.append(contentsOf: DiffableResult(identifier: "inheritedTypeName").trackDifference(actual: self.inheritedTypeName, expected: castObject.inheritedTypeName)) | ||
return results | ||
} | ||
|
||
public override var hash: Int { | ||
var hasher = Hasher() | ||
hasher.combine(self.name) | ||
hasher.combine(self.inheritedTypeName) | ||
return hasher.finalize() | ||
} | ||
|
||
/// :nodoc: | ||
public override func isEqual(_ object: Any?) -> Bool { | ||
guard let rhs = object as? GenericParameter else { return false } | ||
if self.name != rhs.name { return false } | ||
if self.inheritedTypeName != rhs.inheritedTypeName { return false } | ||
return true | ||
} | ||
|
||
// sourcery:inline:GenericParameter.AutoCoding | ||
|
||
/// :nodoc: | ||
required public init?(coder aDecoder: NSCoder) { | ||
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 | ||
self.inheritedTypeName = aDecoder.decode(forKey: "inheritedTypeName") | ||
} | ||
|
||
/// :nodoc: | ||
public func encode(with aCoder: NSCoder) { | ||
aCoder.encode(self.name, forKey: "name") | ||
aCoder.encode(self.inheritedTypeName, forKey: "inheritedTypeName") | ||
} | ||
|
||
// sourcery:end | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.