Skip to content

Commit 6286203

Browse files
authored
feat: add protocol information to native types (#247)
* feat: add protocol information to native types * fix: correct typing for C String return type
1 parent daceac1 commit 6286203

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

metadata-generator/src/TypeScript/DefinitionWriter.cpp

+23-6
Original file line numberDiff line numberDiff line change
@@ -883,11 +883,9 @@ std::string DefinitionWriter::tsifyType(const Type& type, const bool isFuncParam
883883
case TypeSelector:
884884
return "string";
885885
case TypeCString: {
886-
std::string res = "string";
887-
if (isFuncParam) {
888-
Type typeVoid(TypeVoid);
889-
res += " | " + tsifyType(::Meta::PointerType(&typeVoid), isFuncParam);
890-
}
886+
std::string res = isFuncParam ? "string | " : "";
887+
Type typeVoid(TypeVoid);
888+
res += tsifyType(::Meta::PointerType(&typeVoid), isFuncParam);
891889
return res;
892890
}
893891
case TypeProtocol:
@@ -976,7 +974,26 @@ std::string DefinitionWriter::tsifyType(const Type& type, const bool isFuncParam
976974
}
977975
}
978976

979-
if (interface.name == "NSArray" && isFuncParam) {
977+
std::vector<std::string> protocols;
978+
if (type.is(TypeType::TypeInterface) && type.as<InterfaceType>().protocols.size() > 0) {
979+
for (auto & protocol : type.as<InterfaceType>().protocols) {
980+
if (protocol->jsName != "NSCopying") {
981+
protocols.push_back(protocol->jsName);
982+
}
983+
}
984+
}
985+
986+
if (protocols.size() > 0) {
987+
// Example: -(NSObject<Option> *) getOption;
988+
// Expected: getOption(): NSObject & Option;
989+
for (auto & protocol : protocols) {
990+
output << " & " << protocol;
991+
}
992+
} else if (interface.name == "NSArray" && isFuncParam) {
993+
// In this case, NSArray<string> maps into NSArray<string> | string[]
994+
// We only do this if there are no protocols, though
995+
// for the very rare case where someone would do NSArray with a protocol
996+
// as we can't marshal a JS array into NSArray + protocol
980997
if (hasClosedGenerics) {
981998
std::string arrayType = firstElementType;
982999
output << " | " << arrayType << "[]";

0 commit comments

Comments
 (0)