diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1-expected.txt b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b806d76bbaa65cd53c0be052853c9919fb4bfc30 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1-expected.txt @@ -0,0 +1,141 @@ +======> literal array buffer <====== +------------------------------------ +slot _.func_main_0_-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _2 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 0 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 2 + val: 0 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 2 + val: 0 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 2 + val: 0 +}, +{ + index: 5 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1.ts b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..31e1b8430c3a0294545a3e611f710c71a993080e --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-1.ts @@ -0,0 +1,3 @@ +type b = a +type a = b[] +const v: b[] = [] diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2-expected.txt b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..7306bd8324109347c08f6cb7b870a0c0f484e88f --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2-expected.txt @@ -0,0 +1,161 @@ +======> literal array buffer <====== +------------------------------------ +slot _.func_main_0_-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 4 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 2 +}, +{ + index: 4 + tag: 0 + val: 25 +}, +{ + index: 5 + tag: 25 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _2 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 0 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 2 + val: 0 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 2 + val: 0 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 2 + val: 0 +}, +{ + index: 5 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2.ts b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d8ed0a69c045c4b64e067db8b35dc00bea34a04 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-2.ts @@ -0,0 +1,5 @@ +type x = y +type y = z | x[] +type z = w +type w = number +var a: x diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3-expected.txt b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..e2030554b8bd7c4888cf620607969f326666b67d --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3-expected.txt @@ -0,0 +1,161 @@ +======> literal array buffer <====== +------------------------------------ +slot _.func_main_0_-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 4 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 2 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _2 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 0 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 2 + val: 0 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 2 + val: 0 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 2 + val: 0 +}, +{ + index: 5 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3.ts b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3.ts new file mode 100644 index 0000000000000000000000000000000000000000..ddbea053304de88c7dc5cf0d418b1a4e0206b865 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-type-alias-for-loop-3.ts @@ -0,0 +1,5 @@ +type a = b +type b = c | a[] +type c = d +type d = c[] +var v: a; diff --git a/es2panda/typescript/extractor/typeExtractor.cpp b/es2panda/typescript/extractor/typeExtractor.cpp index 174c95d70b405bdebf43b544771d5a873c99e1a6..430e4515728279517e43f9eeb87896aa7dae848e 100644 --- a/es2panda/typescript/extractor/typeExtractor.cpp +++ b/es2panda/typescript/extractor/typeExtractor.cpp @@ -463,8 +463,14 @@ int64_t TypeExtractor::GetTypeIndexFromImportNode(const ir::AstNode *node, [[may int64_t TypeExtractor::GetTypeIndexFromTypeAliasNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance) { - auto typeIndex = GetTypeIndexFromAnnotation(node->AsTSTypeAliasDeclaration()->TypeAnnotation()); + auto typeNode = node->AsTSTypeAliasDeclaration()->TypeAnnotation(); + if (!AddSearchingTypeRefNodes(typeNode)) { + RemoveSearchingTypeRefNodes(typeNode); + return PrimitiveType::ANY; + } + auto typeIndex = GetTypeIndexFromAnnotation(typeNode); TLOG(node->Type(), typeIndex); + RemoveSearchingTypeRefNodes(typeNode); return typeIndex; } diff --git a/es2panda/typescript/extractor/typeExtractor.h b/es2panda/typescript/extractor/typeExtractor.h index 1d0bedf0f23c1c2a4b4eee2be235f4ac0c72fc7e..3533e9df158999eff044a4a1c1bce38032c86957 100644 --- a/es2panda/typescript/extractor/typeExtractor.h +++ b/es2panda/typescript/extractor/typeExtractor.h @@ -18,6 +18,7 @@ #include #include +#include #include "typeRecorder.h" @@ -60,8 +61,30 @@ public: static int64_t GetBuiltinTypeIndex(util::StringView name); + bool AddSearchingTypeRefNodes(const ir::Expression * node) + { + if (!IsInSearchingTypeRefNodes(node)) { + searchingTypeRefNodes_.insert(node); + return true; + } + return false; + } + + void RemoveSearchingTypeRefNodes(const ir::Expression * node) + { + if (IsInSearchingTypeRefNodes(node)) { + searchingTypeRefNodes_.erase(node); + } + } + + bool IsInSearchingTypeRefNodes(const ir::Expression * node) + { + return searchingTypeRefNodes_.find(node) != searchingTypeRefNodes_.end(); + } + private: const ir::BlockStatement *rootNode_; + std::unordered_set searchingTypeRefNodes_; const bool typeDtsExtractor_; const bool typeDtsBuiltin_; std::unique_ptr recorder_;