Skip to content

Commit

Permalink
Merge pull request #218 from cashapp/skorulis/prefix-naming
Browse files Browse the repository at this point in the history
Improve type name generation of prefixed names
  • Loading branch information
skorulis-ap authored Dec 12, 2024
2 parents b2c54c8 + 5cb2494 commit 8230f0f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Sources/KnitCodeGen/TypeNamer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ enum TypeNamer {
*/
static func computedIdentifierName(type: String) -> String {
let type = sanitizeType(type: type, keepGenerics: false)
if type.uppercased() == type {
let lowercaseIndex = type.firstIndex { $0.isLowercase }
if let lowercaseIndex {
let chars = max(type.distance(from: type.startIndex, to: lowercaseIndex) - 1, 1)
return type.prefix(chars).lowercased() + type.dropFirst(chars)
} else {
return type.lowercased()
}
return type.prefix(1).lowercased() + type.dropFirst()
}

/// Simplifies the type name and removes invalid characters
Expand Down
11 changes: 11 additions & 0 deletions Tests/KnitCodeGenTests/TypeNamerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ final class TypeNamerTests: XCTestCase {
)
}

func testPrefixedName() {
assertComputedIdentifier(
type: "UIApplication",
expectedIdentifier: "uiApplication"
)
assertComputedIdentifier(
type: "NSURLSession",
expectedIdentifier: "nsurlSession"
)
}

}

private func assertComputedIdentifier(
Expand Down

0 comments on commit 8230f0f

Please sign in to comment.