Skip to content

Commit a86adc0

Browse files
authored
fix(sidekick/swift): handle metatype keywords (#5070)
TIL: for a few keywords the backtick escape does not work. We need to resort to mangling.
1 parent b13b02b commit a86adc0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

internal/sidekick/swift/names.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ var keywords = map[string]bool{
140140

141141
// escapeKeyword escapes a string if it is a keyword.
142142
func escapeKeyword(s string) string {
143-
if _, ok := keywords[s]; ok {
143+
// In Swift we can use backtick escaping for most keywords except `Type`, `Protocol`, and `self`:
144+
// https://docs.swift.org/swift-book/documentation/the-swift-programming-language/types/#Metatype-Type
145+
// In an expression like `Foo.Type` that is *always* the metatype of `Foo` and not the nested type `Type` even if `Foo` has such a nested type.
146+
if s == "Protocol" || s == "Type" || s == "self" {
147+
return fmt.Sprintf("%s_", s)
148+
}
149+
if keywords[s] {
144150
return fmt.Sprintf("`%s`", s)
145151
}
146152
return s

internal/sidekick/swift/names_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func TestEscapeKeyword(t *testing.T) {
3131
{input: "func", want: "`func`"},
3232
{input: "if", want: "`if`"},
3333
{input: "while", want: "`while`"},
34+
// Metatype-related keywords, need custom escaping
35+
{input: "Type", want: "Type_"},
36+
{input: "Protocol", want: "Protocol_"},
37+
{input: "self", want: "self_"},
3438

3539
// Non-keywords requested NOT to be escaped
3640
{input: "secret", want: "secret"},
@@ -53,10 +57,12 @@ func TestCamelCase(t *testing.T) {
5357
{input: "secret_version", want: "secretVersion"},
5458
{input: "display_name", want: "displayName"},
5559
{input: "iam_policy", want: "iamPolicy"},
60+
{input: "Type", want: "type"},
5661

5762
// Keywords that should be escaped after camelCase
5863
{input: "protocol", want: "`protocol`"},
5964
{input: "will_set", want: "`willSet`"},
65+
{input: "Self", want: "self_"},
6066
} {
6167
t.Run(test.input, func(t *testing.T) {
6268
got := camelCase(test.input)

0 commit comments

Comments
 (0)