We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b13b02b commit a86adc0Copy full SHA for a86adc0
internal/sidekick/swift/names.go
@@ -140,7 +140,13 @@ var keywords = map[string]bool{
140
141
// escapeKeyword escapes a string if it is a keyword.
142
func escapeKeyword(s string) string {
143
- if _, ok := keywords[s]; ok {
+ // 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] {
150
return fmt.Sprintf("`%s`", s)
151
}
152
return s
internal/sidekick/swift/names_test.go
@@ -31,6 +31,10 @@ func TestEscapeKeyword(t *testing.T) {
31
{input: "func", want: "`func`"},
32
{input: "if", want: "`if`"},
33
{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_"},
38
39
// Non-keywords requested NOT to be escaped
40
{input: "secret", want: "secret"},
@@ -53,10 +57,12 @@ func TestCamelCase(t *testing.T) {
53
57
{input: "secret_version", want: "secretVersion"},
54
58
{input: "display_name", want: "displayName"},
55
59
{input: "iam_policy", want: "iamPolicy"},
60
+ {input: "Type", want: "type"},
56
61
62
// Keywords that should be escaped after camelCase
63
{input: "protocol", want: "`protocol`"},
64
{input: "will_set", want: "`willSet`"},
65
+ {input: "Self", want: "self_"},
66
} {
67
t.Run(test.input, func(t *testing.T) {
68
got := camelCase(test.input)
0 commit comments