Files
Sourcery/SourceryRuntime/Sources/Linux/AST/ClosureParameter_Linux.swift
Ruslan Alikhamov cec7895f0a Adjusted file structure to accommodate two generated files (#1299)
* Adjusted file structure to accommodate two generated files

* Adjusted scripting

* Removed Description, Diffable, Equality for now

* Removed Stencil import

* Updated templates

* Updated scripting

* Updated generated coding

* Disable build deletion (temp)

* Updated template

* Updated generated code

* updated generated code

* Updated generated code

* Adjusted scripting

* Updated scripting

* Updated generated code

* Reverted template deletion

* Removed Stencil imports

* Updated generated code

* trigger CI

* Removed description, diffable and equality stencil templates

* Reverted temporary changes

* Commented failing tests

* Skipping JSExport for description & hash

* Updated generated code

* Enabled failing tests

* Adding stencil templates back to test tests

* Reset generated file for linux for test

* Reverted Extensions for testing

* Reverted ParserResultsComposed

* Attempt to fix unit tests

* Attempt to resolve unit test

* Reverted TypeName asSource

* Reverted TypeName revertion

* Reverted revert of ParserResultComposed

* Reverted revert of Extensions

* Reverted revert of Linux.content.generated

* Reverted attempts to fix unit tests

* Fix for the failing codegen tests

* Added clarifying comment

* Removed description, diffable and equality stencil templates

* Updated generated code

* Tinkering with optimization level for speed boost

* Excluded stencil templates for codegen

* Fixed wrong compiler flag

* Removed speed optimization to a separate PR

* Reverted test code
2024-03-13 00:01:29 +04:00

175 lines
6.6 KiB
Swift

#if !canImport(ObjectiveC)
import Foundation
// sourcery: skipDiffing
public final class ClosureParameter: NSObject, SourceryModel, Typed, Annotated, SourceryDynamicMemberLookup {
public subscript(dynamicMember member: String) -> Any? {
switch member {
case "argumentLabel":
return argumentLabel
case "name":
return name
case "typeName":
return typeName
case "inout":
return `inout`
case "type":
return type
case "isVariadic":
return isVariadic
case "defaultValue":
return defaultValue
default:
fatalError("unable to lookup: \(member) in \(self)")
}
}
/// Parameter external name
public var argumentLabel: String?
/// Parameter internal name
public let name: String?
/// Parameter type name
public let typeName: TypeName
/// Parameter flag whether it's inout or not
public let `inout`: Bool
// sourcery: skipEquality, skipDescription
/// Parameter type, if known
public var type: Type?
/// Parameter if the argument has a variadic type or not
public let isVariadic: Bool
/// Parameter type attributes, i.e. `@escaping`
public var typeAttributes: AttributeList {
return typeName.attributes
}
/// Method parameter default value expression
public var defaultValue: String?
/// Annotations, that were created with // sourcery: annotation1, other = "annotation value", alterantive = 2
public var annotations: Annotations = [:]
/// :nodoc:
public init(argumentLabel: String? = nil, name: String? = nil, typeName: TypeName, type: Type? = nil,
defaultValue: String? = nil, annotations: [String: NSObject] = [:], isInout: Bool = false,
isVariadic: Bool = false) {
self.typeName = typeName
self.argumentLabel = argumentLabel
self.name = name
self.type = type
self.defaultValue = defaultValue
self.annotations = annotations
self.`inout` = isInout
self.isVariadic = isVariadic
}
public var asSource: String {
let typeInfo = "\(`inout` ? "inout " : "")\(typeName.asSource)\(isVariadic ? "..." : "")"
if argumentLabel?.nilIfNotValidParameterName == nil, name?.nilIfNotValidParameterName == nil {
return typeInfo
}
let typeSuffix = ": \(typeInfo)"
guard argumentLabel != name else {
return name ?? "" + typeSuffix
}
let labels = [argumentLabel ?? "_", name?.nilIfEmpty]
.compactMap { $0 }
.joined(separator: " ")
return (labels.nilIfEmpty ?? "_") + typeSuffix
}
/// :nodoc:
// sourcery: skipJSExport
public override var hash: Int {
var hasher = Hasher()
hasher.combine(self.argumentLabel)
hasher.combine(self.name)
hasher.combine(self.typeName)
hasher.combine(self.`inout`)
hasher.combine(self.isVariadic)
hasher.combine(self.defaultValue)
hasher.combine(self.annotations)
return hasher.finalize()
}
/// :nodoc:
// sourcery: skipJSExport
override public var description: String {
var string = "\(Swift.type(of: self)): "
string.append("argumentLabel = \(String(describing: self.argumentLabel)), ")
string.append("name = \(String(describing: self.name)), ")
string.append("typeName = \(String(describing: self.typeName)), ")
string.append("`inout` = \(String(describing: self.`inout`)), ")
string.append("typeAttributes = \(String(describing: self.typeAttributes)), ")
string.append("defaultValue = \(String(describing: self.defaultValue)), ")
string.append("annotations = \(String(describing: self.annotations)), ")
string.append("asSource = \(String(describing: self.asSource))")
return string
}
/// :nodoc:
public override func isEqual(_ object: Any?) -> Bool {
guard let rhs = object as? ClosureParameter else { return false }
if self.argumentLabel != rhs.argumentLabel { return false }
if self.name != rhs.name { return false }
if self.typeName != rhs.typeName { return false }
if self.`inout` != rhs.`inout` { return false }
if self.isVariadic != rhs.isVariadic { return false }
if self.defaultValue != rhs.defaultValue { return false }
if self.annotations != rhs.annotations { return false }
return true
}
// sourcery:inline:ClosureParameter.AutoCoding
/// :nodoc:
required public init?(coder aDecoder: NSCoder) {
self.argumentLabel = aDecoder.decode(forKey: "argumentLabel")
self.name = aDecoder.decode(forKey: "name")
guard let typeName: TypeName = aDecoder.decode(forKey: "typeName") else {
withVaList(["typeName"]) { arguments in
NSException.raise(NSExceptionName.parseErrorException, format: "Key '%@' not found.", arguments: arguments)
}
fatalError()
}; self.typeName = typeName
self.`inout` = aDecoder.decode(forKey: "`inout`")
self.type = aDecoder.decode(forKey: "type")
self.isVariadic = aDecoder.decode(forKey: "isVariadic")
self.defaultValue = aDecoder.decode(forKey: "defaultValue")
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
}
/// :nodoc:
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.argumentLabel, forKey: "argumentLabel")
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.typeName, forKey: "typeName")
aCoder.encode(self.`inout`, forKey: "`inout`")
aCoder.encode(self.type, forKey: "type")
aCoder.encode(self.isVariadic, forKey: "isVariadic")
aCoder.encode(self.defaultValue, forKey: "defaultValue")
aCoder.encode(self.annotations, forKey: "annotations")
}
// sourcery:end
}
extension Array where Element == ClosureParameter {
public var asSource: String {
"(\(map { $0.asSource }.joined(separator: ", ")))"
}
}
#endif