Skip to content
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

Remove dead codes #271

Merged
merged 14 commits into from
Nov 6, 2024
18 changes: 18 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms.git",
"state" : {
"revision" : "f6919dfc309e7f1b56224378b11e28bab5bccc42",
"version" : "1.2.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
Expand All @@ -9,6 +18,15 @@
"version" : "1.5.0"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "0a5bc04095a675662cf24757cc0640aa2204253b",
"version" : "1.0.2"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
Expand Down
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"),
.package(url: "https://github.com/apple/swift-algorithms.git", from: "1.2.0"),
],
targets: [
.executableTarget(
Expand All @@ -26,6 +27,7 @@ let package = Package(
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "Algorithms", package: "swift-algorithms"),
]
),
.testTarget(
Expand Down
6 changes: 3 additions & 3 deletions Sources/MockoloFramework/Models/ArgumentsHistoryModel.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Foundation

final class ArgumentsHistoryModel: Model {
var name: String
var type: SwiftType
var offset: Int64 = .max
let name: String
let type: SwiftType
let offset: Int64 = .max
let capturableParamNames: [String]
let capturableParamTypes: [SwiftType]
let isHistoryAnnotated: Bool
Expand Down
10 changes: 5 additions & 5 deletions Sources/MockoloFramework/Models/ClosureModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import Foundation

final class ClosureModel: Model {
var name: String
var type: SwiftType
var offset: Int64 = .max
let name: String = "" // closure type cannot have a name
let offset: Int64 = .max

let funcReturnType: SwiftType
let genericTypeNames: [String]
let paramNames: [String]
Expand All @@ -30,11 +31,10 @@ final class ClosureModel: Model {
var modelType: ModelType {
return .closure
}
init(name: String, genericTypeParams: [ParamModel], paramNames: [String], paramTypes: [SwiftType], isAsync: Bool, throwing: ThrowingKind, returnType: SwiftType, encloser: String) {

init(genericTypeParams: [ParamModel], paramNames: [String], paramTypes: [SwiftType], isAsync: Bool, throwing: ThrowingKind, returnType: SwiftType, encloser: String) {
// In the mock's call handler, rethrows is unavailable.
let throwing = throwing.coerceRethrowsToThrows
self.name = name + .handlerSuffix
self.isAsync = isAsync
self.throwing = throwing
let genericTypeNameList = genericTypeParams.map(\.name)
Expand Down
10 changes: 3 additions & 7 deletions Sources/MockoloFramework/Models/IfMacroModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
// limitations under the License.
//

import Foundation

final class IfMacroModel: Model {
var name: String
var offset: Int64
var type: SwiftType
let name: String
let offset: Int64
let entities: [Model]

var modelType: ModelType {
return .macro
}
Expand All @@ -36,7 +33,6 @@ final class IfMacroModel: Model {
self.name = name
self.entities = entities
self.offset = offset
self.type = SwiftType(name)
}

func render(with identifier: String, encloser: String, useTemplateFunc: Bool, useMockObservable: Bool, allowSetCallCount: Bool = false, mockFinal: Bool = false, enableFuncArgsHistory: Bool = false, disableCombineDefaultValues: Bool = false) -> String? {
Expand Down
47 changes: 19 additions & 28 deletions Sources/MockoloFramework/Models/MethodModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,21 @@ public enum MethodKind: Equatable {
}

final class MethodModel: Model {
var filePath: String = ""
var data: Data? = nil
var name: String
var type: SwiftType
var offset: Int64
let length: Int64
let name: String
let returnType: SwiftType
let accessLevel: String
var attributes: [String]? = nil
let kind: MethodKind
let offset: Int64
let length: Int64
let genericTypeParams: [ParamModel]
var genericWhereClause: String? = nil
let genericWhereClause: String?
let params: [ParamModel]
let processed: Bool
var modelDescription: String? = nil
var isStatic: Bool
let modelDescription: String?
let isStatic: Bool
let shouldOverride: Bool
let isAsync: Bool
let throwing: ThrowingKind
let kind: MethodKind
let funcsWithArgsHistory: [String]
let customModifiers: [String : Modifier]
var modelType: ModelType {
Expand Down Expand Up @@ -118,7 +115,7 @@ final class MethodModel: Model {
args.append(genericWhereClauseToSignatureComponent)
}
args.append(contentsOf: paramTypes.map(\.displayName))
var displayType = self.type.displayName
var displayType = self.returnType.displayName
let capped = min(displayType.count, 32)
displayType.removeLast(displayType.count-capped)
args.append(displayType)
Expand All @@ -145,21 +142,15 @@ final class MethodModel: Model {
return nil
}

let paramNames = self.params.map(\.name)
let paramTypes = self.params.map(\.type)
let ret = ClosureModel(name: name,
genericTypeParams: genericTypeParams,
paramNames: paramNames,
paramTypes: paramTypes,
isAsync: isAsync,
throwing: throwing,
returnType: type,
encloser: encloser)

return ret
return ClosureModel(genericTypeParams: genericTypeParams,
paramNames: params.map(\.name),
paramTypes: params.map(\.type),
isAsync: isAsync,
throwing: throwing,
returnType: returnType,
encloser: encloser)
}


init(name: String,
typeName: String,
kind: MethodKind,
Expand All @@ -178,7 +169,7 @@ final class MethodModel: Model {
modelDescription: String?,
processed: Bool) {
self.name = name.trimmingCharacters(in: .whitespaces)
self.type = SwiftType(typeName.trimmingCharacters(in: .whitespaces))
self.returnType = SwiftType(typeName.trimmingCharacters(in: .whitespaces))
self.isAsync = isAsync
self.throwing = throwing
self.offset = offset
Expand Down Expand Up @@ -219,7 +210,7 @@ final class MethodModel: Model {
}
}

if let ret = modelDescription?.trimmingCharacters(in: .newlines) ?? self.data?.toString(offset: offset, length: length) {
if let ret = modelDescription?.trimmingCharacters(in: .newlines) {
return prefix + ret
}
return nil
Expand All @@ -237,7 +228,7 @@ final class MethodModel: Model {
genericTypeParams: genericTypeParams,
genericWhereClause: genericWhereClause,
params: params,
returnType: type,
returnType: returnType,
accessLevel: accessLevel,
argsHistory: argsHistory,
handler: handler(encloser: encloser))
Expand Down
47 changes: 3 additions & 44 deletions Sources/MockoloFramework/Models/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,21 @@ public enum ModelType {
}

/// Represents a model for an entity such as var, func, class, etc.
public protocol Model {
protocol Model: AnyObject {
/// Identifier
var name: String { get set }
var name: String { get }

/// Fully qualified identifier
var fullName: String { get }

var underlyingName: String { get }

/// Type of this model
var modelType: ModelType { get }

/// Indicates whether mock generation for this model has been processed
var processed: Bool { get }

/// Indicates whether this model maps to an init method
var isInitializer: Bool { get }

var isStatic: Bool { get }

/// Decl(e.g. class/struct/protocol/enum) or return type (e.g. var/func)
var type: SwiftType { get set }

/// Offset where this type is declared
var offset: Int64 { get set }
var offset: Int64 { get }

/// Applies a corresponding template to this model to output mocks
func render(with identifier: String,
Expand All @@ -69,27 +59,9 @@ public protocol Model {
/// @param level The verbosity level
/// @returns a unique name given the verbosity (default is name)
func name(by level: Int) -> String


func isEqual(_ other: Model) -> Bool

func isLessThan(_ other: Model) -> Bool
}

extension Model {
func isEqual(_ other: Model) -> Bool {
return self.fullName == other.fullName &&
self.type.typeName == other.type.typeName &&
self.modelType == other.modelType
}

func isLessThan(_ other: Model) -> Bool {
if self.offset == other.offset {
return self.name < other.name
}
return self.offset < other.offset
}

func name(by level: Int) -> String {
return name
}
Expand All @@ -98,20 +70,7 @@ extension Model {
return name
}

var underlyingName: String {
return name.safeName
}

var isStatic: Bool {
return false
}

var processed: Bool {
return false
}

var isInitializer: Bool {
return false
}
}

8 changes: 3 additions & 5 deletions Sources/MockoloFramework/Models/NominalModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
// limitations under the License.
//

import Foundation

final class NominalModel: Model {
enum NominalTypeDeclKind: String {
case `class`
case `actor`
}

let namespaces: [String]
var name: String
var offset: Int64
var type: SwiftType
let name: String
let offset: Int64
let type: SwiftType
let attribute: String
let accessLevel: String
let identifier: String
Expand Down
39 changes: 17 additions & 22 deletions Sources/MockoloFramework/Models/ParamModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,26 @@
import Foundation

final class ParamModel: Model {
var name: String
var offset: Int64
var length: Int64
var type: SwiftType
internal init(label: String, name: String, type: SwiftType, isGeneric: Bool, inInit: Bool, needsVarDecl: Bool, offset: Int64, length: Int64) {
uhooi marked this conversation as resolved.
Show resolved Hide resolved
self.label = label
self.name = name
self.type = type
self.isGeneric = isGeneric
self.inInit = inInit
self.needsVarDecl = needsVarDecl
self.offset = offset
self.length = length
}

let label: String
let name: String
let type: SwiftType
let isGeneric: Bool
let inInit: Bool
let needVarDecl: Bool
let needsVarDecl: Bool
let offset: Int64
let length: Int64

var isStatic: Bool {
return false
}

var modelType: ModelType {
return .parameter
}
Expand All @@ -38,18 +45,6 @@ final class ParamModel: Model {
return label + "_" + name
}

init(label: String = "", name: String, typeName: String, isGeneric: Bool = false, inInit: Bool = false, needVarDecl: Bool, offset: Int64, length: Int64) {
self.name = name.trimmingCharacters(in: .whitespaces)
self.type = SwiftType(typeName.trimmingCharacters(in: .whitespaces))
let labelStr = label.trimmingCharacters(in: .whitespaces)
self.label = name != labelStr ? labelStr : ""
self.offset = offset
self.length = length
self.isGeneric = isGeneric
self.inInit = inInit
self.needVarDecl = needVarDecl
}

var underlyingName: String {
return "_\(name)"
}
Expand All @@ -76,7 +71,7 @@ final class ParamModel: Model {
/// }
/// ```
func asInitVarDecl(eraseType: Bool) -> String? {
if self.inInit, self.needVarDecl {
if self.inInit, self.needsVarDecl {
let type: SwiftType
if eraseType {
type = SwiftType(.anyType)
Expand Down
Loading