diff --git a/ets2panda/util/declgenEts2Ts.cpp b/ets2panda/util/declgenEts2Ts.cpp index 89cb7e1e34359a11593b62fd4faaffeec4f988e0..9c63d8e3869b017d08568cd28f06babd27504378 100644 --- a/ets2panda/util/declgenEts2Ts.cpp +++ b/ets2panda/util/declgenEts2Ts.cpp @@ -85,7 +85,7 @@ void TSDeclGen::Generate() } template -void TSDeclGen::GenCommaSeparated(const T &container, const CB &cb) +void TSDeclGen::GenSeparated(const T &container, const CB &cb, const char *separator) { if (container.empty()) { return; @@ -93,7 +93,7 @@ void TSDeclGen::GenCommaSeparated(const T &container, const CB &cb) cb(container[0]); for (std::size_t i = 1; i < container.size(); ++i) { - Out(", "); + Out(separator); cb(container[i]); } } @@ -170,6 +170,8 @@ void TSDeclGen::GenTypeNonNullish(const checker::Type *checkerType) GenObjectType(checkerType->AsETSObjectType()); } else if (checkerType->IsETSTypeParameter()) { GenTypeParameterType(checkerType->AsETSTypeParameter()); + } else if (checkerType->IsETSUnionType()) { + GenUnionType(checkerType->AsETSUnionType()); } else { // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define TYPE_CHECKS(typeFlag, typeName) \ @@ -225,7 +227,7 @@ void TSDeclGen::GenFunctionType(const checker::ETSFunctionType *etsFunctionType, Out("("); - GenCommaSeparated(sig->Params(), [this](varbinder::LocalVariable *param) { + GenSeparated(sig->Params(), [this](varbinder::LocalVariable *param) { Out(param->Name()); const auto *paramType = param->TsType(); @@ -317,11 +319,17 @@ void TSDeclGen::GenObjectType(const checker::ETSObjectType *objectType) const auto &typeArgs = objectType->TypeArguments(); if (!typeArgs.empty()) { Out("<"); - GenCommaSeparated(typeArgs, [this](checker::Type *arg) { GenType(arg); }); + GenSeparated(typeArgs, [this](checker::Type *arg) { GenType(arg); }); Out(">"); } } +void TSDeclGen::GenUnionType(const checker::ETSUnionType *unionType) +{ + GenSeparated( + unionType->ConstituentTypes(), [this](checker::Type *arg) { GenType(arg); }, " | "); +} + void TSDeclGen::GenTypeParameterType(const checker::ETSTypeParameter *typeParam) { Out(typeParam->GetDeclNode()->Name()->Name()); @@ -331,7 +339,7 @@ void TSDeclGen::GenTypeParameters(const ir::TSTypeParameterDeclaration *typePara { if (typeParams != nullptr) { Out("<"); - GenCommaSeparated(typeParams->Params(), [this](ir::TSTypeParameter *param) { + GenSeparated(typeParams->Params(), [this](ir::TSTypeParameter *param) { Out(param->Name()->Name()); auto *constraint = param->Constraint(); if (constraint != nullptr) { @@ -376,7 +384,7 @@ void TSDeclGen::GenImportDeclaration(const ir::ETSImportDeclaration *importDecla const auto &specifiers = importDeclaration->Specifiers(); Out("import { "); - GenCommaSeparated(specifiers, [this, &importDeclaration](ir::AstNode *specifier) { + GenSeparated(specifiers, [this, &importDeclaration](ir::AstNode *specifier) { if (!specifier->IsImportSpecifier()) { ThrowError("Only import specifiers are supported", importDeclaration->Start()); } @@ -481,7 +489,7 @@ void TSDeclGen::GenClassDeclaration(const ir::ClassDeclaration *classDecl) if (!interfaces.empty()) { Out(" implements "); ASSERT(classDef->TsType()->IsETSObjectType()); - GenCommaSeparated(interfaces, [this](checker::ETSObjectType *interface) { GenType(interface); }); + GenSeparated(interfaces, [this](checker::ETSObjectType *interface) { GenType(interface); }); } Out(" {"); diff --git a/ets2panda/util/declgenEts2Ts.h b/ets2panda/util/declgenEts2Ts.h index f6b1fdee767100b4c97eb34ffb6a10a0e90ae82a..ff6b8b5e0b3a0dabc5b534ba32f0cdf6d8b25911 100644 --- a/ets2panda/util/declgenEts2Ts.h +++ b/ets2panda/util/declgenEts2Ts.h @@ -52,6 +52,7 @@ private: void GenTypeParameterType(const checker::ETSTypeParameter *typeParam); void GenObjectType(const checker::ETSObjectType *objectType); void GenEnumType(const checker::ETSEnumType *enumType); + void GenUnionType(const checker::ETSUnionType *unionType); void GenImportDeclaration(const ir::ETSImportDeclaration *importDeclaration); void GenTypeAliasDeclaration(const ir::TSTypeAliasDeclaration *typeAlias); @@ -67,7 +68,7 @@ private: void GenTypeParameters(const ir::TSTypeParameterDeclaration *typeParams); template - void GenCommaSeparated(const T &container, const CB &cb); + void GenSeparated(const T &container, const CB &cb, const char *separator = ", "); void Out() {} template