diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp index 6dd04a39169bc1a6322c4ae0f50be0252214ab9c..01d16758d4595307200813b8c8be3e96f7a5850f 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.cpp @@ -293,7 +293,7 @@ void TSDeclGen::GenImportDeclarations() } template -void TSDeclGen::GenSeparated(const T &container, const CB &cb, const char *separator, bool isReExport) +void TSDeclGen::GenSeparated(const T &container, const CB &cb, const char *separator, bool isReExport, bool isDtsExport) { if (container.empty()) { return; @@ -304,7 +304,9 @@ void TSDeclGen::GenSeparated(const T &container, const CB &cb, const char *separ if (isReExport) { OutTs(separator); } - OutDts(separator); + if (isDtsExport) { + OutDts(separator); + } cb(container[i]); } } @@ -965,6 +967,7 @@ void TSDeclGen::GenAnnotations(const T *node) if (!state_.inGlobalClass && (state_.inClass || state_.inInterface)) { ProcessIndent(); } + importSet_.insert(anno->GetBaseName()->Name().Mutf8()); OutDts("@", anno->GetBaseName()->Name()); GenAnnotationProperties(anno); OutEndlDts(); @@ -1041,52 +1044,63 @@ void TSDeclGen::GenImportDeclaration(const ir::ETSImportDeclaration *importDecla } else if (specifierFirst->IsImportSpecifier()) { GenNamedImports(importDeclaration, specifiers, source, typeStr); } - OutEndlDts(); - OutEndlTs(); } void TSDeclGen::GenNamespaceImport(const ir::AstNode *specifier, const std::string &source) { const auto local = specifier->AsImportNamespaceSpecifier()->Local()->Name(); + OutTs("import * as ", local, " from \"", source, "\";"); + OutEndlTs(); if (importSet_.find(local.Mutf8()) == importSet_.end()) { return; } OutDts("import * as ", local, " from \"", source, "\";"); - OutTs("import * as ", local, " from \"", source, "\";"); + OutEndlDts(); } void TSDeclGen::GenDefaultImport(const ir::AstNode *specifier, const std::string &source, const std::string &typeStr) { const auto local = specifier->AsImportDefaultSpecifier()->Local()->Name(); + OutTs("import ", typeStr, local, " from \"", source, "\";"); + OutEndlTs(); if (importSet_.find(local.Mutf8()) == importSet_.end()) { return; } OutDts("import ", typeStr, local, " from \"", source, "\";"); - OutTs("import ", typeStr, local, " from \"", source, "\";"); + OutEndlDts(); } void TSDeclGen::GenNamedImports(const ir::ETSImportDeclaration *importDeclaration, const ArenaVector &specifiers, const std::string &source, const std::string &typeStr) { + if (specifiers.empty()) { + return; + } + OutTs("import ", typeStr, "{ "); + GenSeparated( + specifiers, + [this, &importDeclaration](ir::AstNode *specifier) { + GenSingleNamedImport(specifier, importDeclaration, true); + }, + ", ", true, false); + OutTs(" } from \"", source, "\";"); + OutEndlTs(); auto importSpecifiers = FilterValidImportSpecifiers(specifiers); if (importSpecifiers.empty()) { return; } - OutDts("import ", typeStr, "{ "); - OutTs("import ", typeStr, "{ "); - GenSeparated( importSpecifiers, [this, &importDeclaration](ir::AstNode *specifier) { GenSingleNamedImport(specifier, importDeclaration); }, - ", ", true); - + ", "); OutDts(" } from \"", source, "\";"); - OutTs(" } from \"", source, "\";"); + OutEndlDts(); } -void TSDeclGen::GenSingleNamedImport(ir::AstNode *specifier, const ir::ETSImportDeclaration *importDeclaration) +void TSDeclGen::GenSingleNamedImport(ir::AstNode *specifier, const ir::ETSImportDeclaration *importDeclaration, + bool isGlueCode) { if (!specifier->IsImportSpecifier()) { LogError(diagnostic::IMPORT_SPECIFIERS_SUPPORT, {}, importDeclaration->Start()); @@ -1094,11 +1108,9 @@ void TSDeclGen::GenSingleNamedImport(ir::AstNode *specifier, const ir::ETSImport const auto local = specifier->AsImportSpecifier()->Local()->Name().Mutf8(); const auto imported = specifier->AsImportSpecifier()->Imported()->Name().Mutf8(); if (local != imported) { - OutDts(imported, " as ", local); - OutTs(imported, " as ", local); + isGlueCode ? OutTs(imported, " as ", local) : OutDts(imported, " as ", local); } else { - OutDts(local); - OutTs(local); + isGlueCode ? OutTs(local) : OutDts(local); } } @@ -1106,6 +1118,9 @@ std::vector TSDeclGen::FilterValidImportSpecifiers(const ArenaVec { std::vector importSpecifiers; for (auto specifier : specifiers) { + if (specifier->AsImportSpecifier()->IsRemovable()) { + continue; + } const auto local = specifier->AsImportSpecifier()->Local()->Name().Mutf8(); if (importSet_.find(local) != importSet_.end()) { importSpecifiers.push_back(specifier); @@ -1344,6 +1359,9 @@ void TSDeclGen::ProcessTSArrayType(const ir::TSArrayType *tsArrayType) void TSDeclGen::ProcessETSFunctionType(const ir::ETSFunctionType *etsFunction) { + if (etsFunction->TypeParams() != nullptr) { + GenTypeParameters(etsFunction->TypeParams()); + } bool inUnionBody = !state_.inUnionBodyStack.empty() && state_.inUnionBodyStack.top(); OutDts(inUnionBody ? "((" : "("); GenSeparated(etsFunction->Params(), [this](ir::Expression *param) { @@ -1630,10 +1648,10 @@ void TSDeclGen::HandleClassDeclarationTypeInfo(const ir::ClassDefinition *classD void TSDeclGen::HandleClassInherit(const ir::Expression *expr) { - if (!expr->TsType()->IsTypeError()) { - GenType(expr->TsType()); - } else if (expr->IsETSTypeReference()) { + if (expr->IsETSTypeReference()) { ProcessETSTypeReferenceType(expr->AsETSTypeReference()); + } else if (!expr->TsType()->IsTypeError()) { + GenType(expr->TsType()); } } @@ -1933,6 +1951,10 @@ void TSDeclGen::ProcessClassPropertyType(const ir::ClassProperty *classProp) ProcessTypeAnnotationType(classProp->TypeAnnotation(), classProp->TsType()); return; } + if (value->IsArrowFunctionExpression() && value->AsArrowFunctionExpression()->Function() != nullptr && + value->AsArrowFunctionExpression()->Function()->TypeParams() != nullptr) { + GenTypeParameters(value->AsArrowFunctionExpression()->Function()->TypeParams()); + } GenType(classProp->TsType()); } diff --git a/ets2panda/declgen_ets2ts/declgenEts2Ts.h b/ets2panda/declgen_ets2ts/declgenEts2Ts.h index fda8c826a15d05038efc471d48d552d46139fdf6..fc580114dbb24890da1bee7db07cecb8fdc4768f 100644 --- a/ets2panda/declgen_ets2ts/declgenEts2Ts.h +++ b/ets2panda/declgen_ets2ts/declgenEts2Ts.h @@ -123,7 +123,8 @@ private: void GenNamedImports(const ir::ETSImportDeclaration *importDeclaration, const ArenaVector &specifiers, const std::string &source, const std::string &typeStr); - void GenSingleNamedImport(ir::AstNode *specifier, const ir::ETSImportDeclaration *importDeclaration); + void GenSingleNamedImport(ir::AstNode *specifier, const ir::ETSImportDeclaration *importDeclaration, + bool isGlueCode = false); void GenReExportDeclaration(const ir::ETSReExportDeclaration *reExportDeclaration); void GenTypeAliasDeclaration(const ir::TSTypeAliasDeclaration *typeAlias); void GenEnumDeclaration(const ir::ClassProperty *enumMember); @@ -155,7 +156,8 @@ private: bool ShouldEmitDeclarationSymbol(const ir::Identifier *symbol); template - void GenSeparated(const T &container, const CB &cb, const char *separator = ", ", bool isReExport = false); + void GenSeparated(const T &container, const CB &cb, const char *separator = ", ", bool isReExport = false, + bool isDtsExport = true); void PrepareClassDeclaration(const ir::ClassDefinition *classDef); bool ShouldSkipMethodDeclaration(const ir::MethodDefinition *methodDef); diff --git a/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt b/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt index 71f12afe4c9bb68fe73fbdc61b3869e18984e746..2bfb00fa8abc1d7c934e1d468ed4b7e6c7d62661 100644 --- a/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt +++ b/ets2panda/test/test-lists/declgenets2ts/ets-runtime/declgen-ets2ts-runtime-ignored.txt @@ -18,3 +18,4 @@ lambda_with_receiver/lambda_with_receiver_trailing_in_function_return_this_rotat static-invoke.ets top_level_03.ets multisource_inheritance.ets +as_string.ets