Skip to content

Commit

Permalink
feat: simplify isGeneric logic
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzablocki committed Dec 17, 2016
1 parent e99ee61 commit 6419dec
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ For each type you can access following properties:

- `name` <- name
- `kind` <- convience accessor that will contain one of `enum`, `class`, `struct`, `protocol`, it will also provide `extension` for types that are unknown to us(e.g. 3rd party or objc), but had extension in the project
- `isGeneric` <- info whether the type (or any of its parent) was generic
- `isGeneric` <- info whether the type is generic
- `localName` <- name within parent scope
- `staticVariables` <- list of static variables
- `variables` <- list of instance variables
Expand Down
1 change: 0 additions & 1 deletion Sourcery/CodeGenerated/Description.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ extension Type {
string += "inheritedTypes = \(inheritedTypes), "
string += "containedTypes = \(containedTypes), "
string += "parentName = \(parentName), "
string += "hasGenericComponent = \(hasGenericComponent), "
return string
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sourcery/CodeGenerated/Equality.generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ extension Type {
guard let rhs = object as? Type else { return false }
if self.isExtension != rhs.isExtension { return false }
if self.accessLevel != rhs.accessLevel { return false }
if self.isGeneric != rhs.isGeneric { return false }
if self.localName != rhs.localName { return false }
if self.staticVariables != rhs.staticVariables { return false }
if self.variables != rhs.variables { return false }
if self.annotations != rhs.annotations { return false }
if self.inheritedTypes != rhs.inheritedTypes { return false }
if self.containedTypes != rhs.containedTypes { return false }
if self.parentName != rhs.parentName { return false }
if self.hasGenericComponent != rhs.hasGenericComponent { return false }

return true
}
Expand Down
14 changes: 4 additions & 10 deletions Sourcery/Models/Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class Type: NSObject {
return "\(parentName).\(localName)"
}

/// Is this type generic? regardless whether it's this type directly or one of its parent types
var isGeneric: Bool {
return (parent?.isGeneric ?? false) || hasGenericComponent
}
/// Is this type generic?
var isGeneric: Bool

/// Name in parent scope
var localName: String
Expand Down Expand Up @@ -67,16 +65,12 @@ class Type: NSObject {
}
}

/// Whether the type has generic component
/// This is not the same as `isGeneric` because this is only local information, you should use `isGeneric`
var hasGenericComponent: Bool = false

/// sourcery: skipEquality
/// Underlying parser data, never to be used by anything else
/// sourcery: skipDescription
internal var __parserData: Any?

init(name: String = "", parent: Type? = nil, accessLevel: AccessLevel = .internal, isExtension: Bool = false, variables: [Variable] = [], staticVariables: [Variable] = [], inheritedTypes: [String] = [], containedTypes: [Type] = [], annotations: [String: NSObject] = [:], hasGenericComponent: Bool = false) {
init(name: String = "", parent: Type? = nil, accessLevel: AccessLevel = .internal, isExtension: Bool = false, variables: [Variable] = [], staticVariables: [Variable] = [], inheritedTypes: [String] = [], containedTypes: [Type] = [], annotations: [String: NSObject] = [:], isGeneric: Bool = false) {
self.localName = name
self.accessLevel = accessLevel
self.isExtension = isExtension
Expand All @@ -87,7 +81,7 @@ class Type: NSObject {
self.parent = parent
self.parentName = parent?.name
self.annotations = annotations
self.hasGenericComponent = hasGenericComponent
self.isGeneric = isGeneric

super.init()
containedTypes.forEach { $0.parent = self }
Expand Down
2 changes: 1 addition & 1 deletion Sourcery/Parsing/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ final class Parser {
return nil
}

type.hasGenericComponent = isGeneric(source: source)
type.isGeneric = isGeneric(source: source)
type.setSource(source: source)
type.annotations = parseAnnotations(source)

Expand Down
8 changes: 1 addition & 7 deletions SourceryTests/Models/TypeSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ class TypeSpec: QuickSpec {
describe("isGeneric") {
context("given generic type") {
it("recognizes correctly for simple generic") {
let sut = Type(name: "Foo", hasGenericComponent: true)

expect(sut.isGeneric).to(beTrue())
}

it("recognizes correctly for type contained inside generic") {
let sut = Type(name: "Foo", parent: Type(name: "Root", hasGenericComponent: true))
let sut = Type(name: "Foo", isGeneric: true)

expect(sut.isGeneric).to(beTrue())
}
Expand Down
2 changes: 1 addition & 1 deletion SourceryTests/ParserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class ParserSpec: QuickSpec {
it("extracts generic struct properly") {
expect(parse("struct Foo<Something> { }"))
.to(equal([
Struct(name: "Foo", hasGenericComponent: true)
Struct(name: "Foo", isGeneric: true)
]))
}

Expand Down

0 comments on commit 6419dec

Please sign in to comment.