Skip to content

Commit

Permalink
Fix isValidClosureName to handle generic types correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Asif Mujteba committed Jun 23, 2020
1 parent d3fb34b commit 1ed50f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 1 addition & 2 deletions SourceryRuntime/Sources/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public extension String {

/// :nodoc:
func isValidClosureName() -> Bool {
return components(separatedBy: "->", excludingDelimiterBetween: ("(", ")")).count > 1
return components(separatedBy: "->", excludingDelimiterBetween: (["(", "<"], [")", ">"])).count > 1
}

/// :nodoc:
Expand Down Expand Up @@ -139,7 +139,6 @@ public extension String {
var quotesCount: Int = 0
var item = ""
var items = [String]()
var matchedDelimiter = (alreadyMatched: "", leftToMatch: delimiter)

var i = self.startIndex
while i < self.endIndex {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,7 @@ public extension String {
/// :nodoc:
func isValidClosureName() -> Bool {
return components(separatedBy: "->", excludingDelimiterBetween: ("(", ")")).count > 1
return components(separatedBy: "->", excludingDelimiterBetween: (["(", "<"], [")", ">"])).count > 1
}
/// :nodoc:
Expand Down Expand Up @@ -2010,7 +2010,6 @@ public extension String {
var quotesCount: Int = 0
var item = ""
var items = [String]()
var matchedDelimiter = (alreadyMatched: "", leftToMatch: delimiter)
var i = self.startIndex
while i < self.endIndex {
Expand Down
13 changes: 13 additions & 0 deletions SourceryTests/Models/TypeNameSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,23 @@ class TypeNameSpec: QuickSpec {
expect(TypeName("() -> (Int, Int)").isClosure).to(beTrue())
expect(TypeName("() -> (Int) -> (Int)").isClosure).to(beTrue())
expect(TypeName("((Int) -> (Int)) -> ()").isClosure).to(beTrue())
expect(TypeName("(Foo<String>) -> Bool)").isClosure).to(beTrue())
expect(TypeName("(Int) -> Foo<Bool>").isClosure).to(beTrue())
expect(TypeName("(Foo<String>) -> Foo<Bool>)").isClosure).to(beTrue())
expect(TypeName("((Int, Int) -> (), Int)").isClosure).to(beFalse())
}
}

context("given closure type inside generic type") {
it("reports closure correctly") {
expect(TypeName("Foo<() -> ()>").isClosure).to(beFalse())
expect(TypeName("Foo<(String) -> Bool>").isClosure).to(beFalse())
expect(TypeName("Foo<(String) -> Bool?>").isClosure).to(beFalse())
expect(TypeName("Foo<(Bar<String>) -> Bool)>").isClosure).to(beFalse())
expect(TypeName("Foo<(Bar<String>) -> Bar<Bool>)>").isClosure).to(beFalse())
}
}

it("removes attributes in unwrappedTypeName") {
expect(TypeName("@escaping (@escaping ()->())->()", attributes: [
"escaping": Attribute(name: "escaping")
Expand Down

0 comments on commit 1ed50f6

Please sign in to comment.