From 5cb24945ff8f4e20184468f8c5bae218a0618bb3 Mon Sep 17 00:00:00 2001 From: Alex Skorulis Date: Thu, 5 Dec 2024 11:25:02 +1100 Subject: [PATCH] Improve type name generation of prefixed names --- Sources/KnitCodeGen/TypeNamer.swift | 7 +++++-- Tests/KnitCodeGenTests/TypeNamerTests.swift | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Sources/KnitCodeGen/TypeNamer.swift b/Sources/KnitCodeGen/TypeNamer.swift index 46fcb0b..259af3f 100644 --- a/Sources/KnitCodeGen/TypeNamer.swift +++ b/Sources/KnitCodeGen/TypeNamer.swift @@ -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 diff --git a/Tests/KnitCodeGenTests/TypeNamerTests.swift b/Tests/KnitCodeGenTests/TypeNamerTests.swift index 0d6b230..8730d80 100644 --- a/Tests/KnitCodeGenTests/TypeNamerTests.swift +++ b/Tests/KnitCodeGenTests/TypeNamerTests.swift @@ -87,6 +87,17 @@ final class TypeNamerTests: XCTestCase { ) } + func testPrefixedName() { + assertComputedIdentifier( + type: "UIApplication", + expectedIdentifier: "uiApplication" + ) + assertComputedIdentifier( + type: "NSURLSession", + expectedIdentifier: "nsurlSession" + ) + } + } private func assertComputedIdentifier(