Skip to content

Commit

Permalink
fix: method argument name collision (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
t0rr3sp3dr0 authored May 26, 2024
1 parent 9a7ebe2 commit f0b4ddf
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion types/objc/type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"regexp"
"strings"
"unicode"
)

// References:
Expand Down Expand Up @@ -145,6 +144,17 @@ func getLastCapitalizedPart(s string) string {
return strings.ToLower(s[start:])
}

func isReserved(s string) bool {
switch s {
case "alignas", "alignof", "auto", "bool", "break", "case", "char", "const", "constexpr", "continue", "default", "do", "double", "else", "enum", "extern", "false", "float", "for", "goto", "if", "inline", "int", "long", "nullptr", "register", "restrict", "return", "short", "signed", "sizeof", "static", "static_assert", "struct", "switch", "thread_local", "true", "typedef", "typeof", "typeof_unqual", "union", "unsigned", "void", "volatile", "while":
return true // C Keywords
case "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept", "bitand", "bitor", "catch", "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "consteval", "constinit", "const_cast", "co_await", "co_return", "co_yield", "decltype", "delete", "dynamic_cast", "explicit", "export", "friend", "mutable", "namespace", "new", "noexcept", "not", "not_eq", "operator", "or", "or_eq", "private", "protected", "public", "reflexpr", "reinterpret_cast", "requires", "static_cast", "synchronized", "template", "this", "throw", "try", "typeid", "typename", "using", "virtual", "wchar_t", "xor", "xor_eq":
return true // C++ Keywords
default:
return false
}
}

func getMethodWithArgs(method, returnType string, args []string) string {
if len(args) <= 2 {
return fmt.Sprintf("(%s)%s;", returnType, method)
Expand All @@ -157,6 +167,9 @@ func getMethodWithArgs(method, returnType string, args []string) string {
if len(parts) > 1 { // method has arguments based on SEL having ':'
for idx, part := range parts {
argName := getLastCapitalizedPart(part)
if isReserved(argName) {
argName = "_" + argName
}
if len(part) == 0 || idx >= len(args) {
break
}
Expand Down

0 comments on commit f0b4ddf

Please sign in to comment.