Skip to content

Commit 1d7700f

Browse files
authored
Merge pull request #231 from cashapp/skorulis/knit-naming
Add handling for common cases inside TypeNamer
2 parents d6298ef + abc7ff7 commit 1d7700f

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

Sources/KnitCodeGen/TypeNamer.swift

+25-4
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ enum TypeNamer {
3232
}
3333
// Drop any annotation
3434
var type = type.replacingOccurrences(of: "any ", with: "")
35-
let removedCharacters = CharacterSet(charactersIn: "?[]():&, ")
35+
let removedCharacters = CharacterSet(charactersIn: "?[]():& ")
3636
type = type.components(separatedBy: removedCharacters).joined(separator: "")
3737
let regex = try! NSRegularExpression(pattern: "<.*>")
3838
let nsString = type as NSString
3939
if let match = regex.firstMatch(in: type, range: .init(location: 0, length: type.count)) {
4040
let range = match.range
41+
let mainType = nsString.replacingCharacters(in: match.range, with: "")
4142
if keepGenerics {
4243
var genericName = nsString.substring(
4344
with: .init(location: range.location + 1, length: range.length - 2)
@@ -47,18 +48,38 @@ enum TypeNamer {
4748
.replacingOccurrences(of: ",", with: "_")
4849
.replacingOccurrences(of: " ", with: "")
4950
type = nsString.replacingCharacters(in: match.range, with: "_\(genericName)")
51+
} else if let suffix = Self.suffixedGenericTypes.first(where: { mainType.hasSuffix($0)} ) {
52+
let genericName = nsString.substring(
53+
with: .init(location: range.location + 1, length: range.length - 2)
54+
)
55+
if let mainGeneric = genericName.components(separatedBy: .init(charactersIn: ",")).first {
56+
type = mainGeneric + suffix
57+
} else {
58+
type = mainType
59+
}
5060
} else {
51-
type = nsString.replacingCharacters(in: match.range, with: "")
61+
type = mainType
5262
}
63+
} else {
64+
type = type.components(separatedBy: .init(charactersIn: ",")).joined(separator: "")
5365
}
54-
if let dotIndex = type.firstIndex(of: ".") {
66+
if let dotIndex = type.lastIndex(of: ".") {
5567
let nameStart = type.index(after: dotIndex)
56-
type = String(type[nameStart...])
68+
let lastType = String(type[nameStart...])
69+
// Types with a Factory subtype should keep the subject of the factory
70+
if lastType == "Factory" {
71+
let components = type.components(separatedBy: .init(charactersIn: "."))
72+
type = components.suffix(2).joined(separator: "")
73+
} else {
74+
type = lastType
75+
}
5776
}
5877

5978
return type
6079
}
6180

81+
private static let suffixedGenericTypes = ["Publisher", "Subject", "Provider"]
82+
6283
static func isClosure(type: String) -> Bool {
6384
return type.contains("->")
6485
}

Tests/KnitCodeGenTests/TypeNamerTests.swift

+40-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ final class TypeNamerTests: XCTestCase {
7878

7979
XCTAssertEqual(
8080
TypeNamer.sanitizeType(type: "Result<String, Error>", keepGenerics: true),
81-
"Result_StringError"
81+
"Result_String_Error"
8282
)
8383

8484
XCTAssertEqual(
@@ -110,6 +110,45 @@ final class TypeNamerTests: XCTestCase {
110110
)
111111
}
112112

113+
func testFactoryRule() {
114+
assertComputedIdentifier(
115+
type: "MyClass.Factory",
116+
expectedIdentifier: "myClassFactory"
117+
)
118+
119+
assertComputedIdentifier(
120+
type: "Module.MyClass.Factory",
121+
expectedIdentifier: "myClassFactory"
122+
)
123+
124+
assertComputedIdentifier(
125+
type: "Factory",
126+
expectedIdentifier: "factory"
127+
)
128+
}
129+
130+
func testSuffixRule() {
131+
assertComputedIdentifier(
132+
type: "AnyPublisher<String>",
133+
expectedIdentifier: "stringPublisher"
134+
)
135+
136+
assertComputedIdentifier(
137+
type: "AnyPublisher<MyType?, Never>",
138+
expectedIdentifier: "myTypePublisher"
139+
)
140+
141+
assertComputedIdentifier(
142+
type: "CurrentValueSubject<String>",
143+
expectedIdentifier: "stringSubject"
144+
)
145+
146+
assertComputedIdentifier(
147+
type: "ValueProvider<Int>",
148+
expectedIdentifier: "intProvider"
149+
)
150+
}
151+
113152
}
114153

115154
private func assertComputedIdentifier(

0 commit comments

Comments
 (0)