Skip to content

Commit 6234ca3

Browse files
authored
Merge pull request #23 from mattpolzin/fix-reserved-words-in-paths2
attempt to fix keyword escaping
2 parents 41cf9e0 + 5eaed20 commit 6234ca3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Sources/JSONAPISwiftGen/Swift Generators/Test Generators/TestFunctionName.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,18 @@ public struct TestFunctionName: Equatable, RawRepresentable {
141141

142142
/// For swift names, we remove braces, escape reserved words, and convert spaces to underscores.
143143
internal static func swiftName(from string: String) -> String {
144-
return string
144+
let name = string
145145
.replacingOccurrences(of: "{", with: "")
146146
.replacingOccurrences(of: "}", with: "")
147147
.replacingOccurrences(of: " ", with: "_")
148-
.replacingOccurrences(of: "do", with: "`do`")
149-
.replacingOccurrences(of: "try", with: "`try`")
150-
.replacingOccurrences(of: "continue", with: "`continue`")
148+
return Self.escapedKeyword(name)
149+
}
150+
151+
internal static func escapedKeyword(_ string: String) -> String {
152+
if string == "do" { return "`do`" }
153+
if string == "try" { return "`try`" }
154+
if string == "continue" { return "`continue`" }
155+
return string
151156
}
152157

153158
public static var testPrefix = "test__"

Tests/JSONAPISwiftGenTests/TestFunctionNameTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ final class TestFunctionNameTests: XCTestCase {
105105
XCTAssertFalse(t1.fullyQualifiedTestFunctionName.contains(".try."))
106106
}
107107

108+
func test_onlyEscapeKeywords() {
109+
let t1 = TestFunctionName(
110+
path: .init(["go", "domain"]),
111+
endpoint: .post,
112+
direction: .request,
113+
context: TestFunctionLocalContext(functionName: "_hello_world➎")!
114+
)
115+
XCTAssertTrue(t1.fullyQualifiedTestFunctionName.contains("domain"))
116+
}
117+
108118
func test_statusCodeExtraction() {
109119
let test1 = TestFunctionName(
110120
path: .init(["hello_world", "v2"]),

0 commit comments

Comments
 (0)