From 65a9f5a438e9f023042ea724542547abb8255ecc Mon Sep 17 00:00:00 2001 From: huangyu Date: Tue, 8 Aug 2023 11:24:20 +0800 Subject: [PATCH] Fix ts non-null expression compile issue and codecheck warnings Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I7RDBL Signed-off-by: huangyu Change-Id: If06695bdafbb62efc41bb3d9dc764f51ce7aa96c --- es2panda/compiler/core/pandagen.cpp | 3 +- es2panda/ir/expressions/chainExpression.cpp | 3 ++ .../test-ts-non-null-expression-expected.txt | 1 + .../compiler/test-ts-non-null-expression.ts | 31 +++++++++++++++++++ es2panda/test/runner.py | 3 +- es2panda/typescript/extractor/typeSystem.h | 2 +- 6 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-expression-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-expression.ts diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index 0eb85b5cbcd..05f4a2d0d7b 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -1693,7 +1693,8 @@ void PandaGen::LoadExternalModuleVariable(const ir::AstNode *node, const binder: auto index = variable->Index(); if (Context()->IsTypeExtractorEnabled()) { const ir::Identifier *identifier = nullptr; - const ir::AstNode *declareNode = Context()->TypeExtractor()->GetDeclNodeFromIdentifier(node->AsIdentifier(), &identifier); + const ir::AstNode *declareNode = + Context()->TypeExtractor()->GetDeclNodeFromIdentifier(node->AsIdentifier(), &identifier); int64_t typeIndex = Context()->TypeRecorder()->GetNodeTypeIndex(declareNode); if (typeIndex != extractor::TypeRecorder::PRIMITIVETYPE_ANY) { index <= util::Helpers::MAX_INT8 ? ra_.EmitWithType(node, typeIndex, index) : diff --git a/es2panda/ir/expressions/chainExpression.cpp b/es2panda/ir/expressions/chainExpression.cpp index d2656c05ac7..27c0d429879 100644 --- a/es2panda/ir/expressions/chainExpression.cpp +++ b/es2panda/ir/expressions/chainExpression.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace panda::es2panda::ir { @@ -39,6 +40,8 @@ void ChainExpression::Compile(compiler::PandaGen *pg) const if (expression_->IsMemberExpression()) { expression_->AsMemberExpression()->Compile(pg); + } else if (expression_->IsTSNonNullExpression()) { + expression_->AsTSNonNullExpression()->Expr()->AsMemberExpression()->Compile(pg); } else { assert(expression_->IsCallExpression()); expression_->AsCallExpression()->Compile(pg); diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-expression-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-expression-expected.txt new file mode 100644 index 00000000000..4c814060532 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-expression-expected.txt @@ -0,0 +1 @@ +non-null-test diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-expression.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-expression.ts new file mode 100644 index 00000000000..39fbff0ebd9 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-non-null-expression.ts @@ -0,0 +1,31 @@ +function parse(page) { + return page; +} + +class Page { + process(context) { + return parse(context?.page!); + } +} + +function getPropertyAccess(obj) { + return obj!.property; +} + +function getElementAccess(arr, index) { + return arr![index]; +} + +function getCall(func, arg) { + func!(arg); +} + +function getCondition(value) { + return value ? value! : "defaultValue"; +} + +function getAsExpr(value) { + return value! as number; +} + +print("non-null-test"); diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index 0b9428c5ceb..b32168bdac5 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -1282,7 +1282,8 @@ class TypeExtractorRunner(Runner): if directory.startswith("testcases_with_assert") or directory.startswith("testcases_with_running"): if (self.ld_library_path == "" or self.ark_aot_compiler == ""): break - test = TypeExtractorWithAOTTest(f, flags, directory.startswith("testcases_with_running"), directory.endswith("projects")) + test = TypeExtractorWithAOTTest(f, flags, directory.startswith("testcases_with_running"), + directory.endswith("projects")) self.tests.append(test) else: test = TypeExtractorTest(f, flags, is_dts_test) diff --git a/es2panda/typescript/extractor/typeSystem.h b/es2panda/typescript/extractor/typeSystem.h index 3c26630935e..72f369b44c0 100644 --- a/es2panda/typescript/extractor/typeSystem.h +++ b/es2panda/typescript/extractor/typeSystem.h @@ -572,7 +572,7 @@ private: if (param->IsAssignmentPattern()) { param = param->AsAssignmentPattern()->Left(); } - + // Identifier / SpreadElement / RestElement / ArrayExpression / ObjectExpression auto identifier = extractor_->GetIdentifierFromExpression(param); if (identifier != nullptr) { -- Gitee