diff --git a/ets2panda/ir/base/classDefinition.cpp b/ets2panda/ir/base/classDefinition.cpp index 1c322b198478f8bf58d2d5e00a9571339c1c7a17..931eafcf701c489b67172b585aafe74fdb0be275 100644 --- a/ets2panda/ir/base/classDefinition.cpp +++ b/ets2panda/ir/base/classDefinition.cpp @@ -318,11 +318,16 @@ void ClassDefinition::Dump(ir::AstDumper *dumper) const void ClassDefinition::DumpGlobalClass(ir::SrcDumper *dumper) const { ES2PANDA_ASSERT(IsGlobal()); + ir::ClassStaticBlock *classStaticBlock = nullptr; for (auto elem : Body()) { if (elem->IsClassProperty()) { elem->Dump(dumper); dumper->Endl(); } + + if (elem->IsClassStaticBlock()) { + classStaticBlock = elem->AsClassStaticBlock(); + } } for (auto elem : Body()) { if (elem->IsMethodDefinition()) { @@ -330,6 +335,25 @@ void ClassDefinition::DumpGlobalClass(ir::SrcDumper *dumper) const dumper->Endl(); } } + + if (classStaticBlock == nullptr) { + return; + } + + auto bodyStmts = + classStaticBlock->Value()->AsFunctionExpression()->Function()->Body()->AsBlockStatement()->Statements(); + for (auto statement : bodyStmts) { + if (statement->IsExpressionStatement() && + statement->AsExpressionStatement()->GetExpression()->IsAssignmentExpression() && + statement->AsExpressionStatement()->GetExpression()->AsAssignmentExpression()->IsIgnoreConstAssign()) { + // skip the dummy assignment expression created for const variable decl in the class static block. + continue; + } + statement->Dump(dumper); + if (statement != bodyStmts.back()) { + dumper->Endl(); + } + } } // This method is needed by OHOS CI code checker diff --git a/ets2panda/public/es2panda_lib_impl.inc.erb b/ets2panda/public/es2panda_lib_impl.inc.erb index e2ecc211727769deef7456b20c04ab0633f0e68c..6e6468b21c7f6313f1bfdcd3b5c859facf9d3fb1 100644 --- a/ets2panda/public/es2panda_lib_impl.inc.erb +++ b/ets2panda/public/es2panda_lib_impl.inc.erb @@ -259,6 +259,11 @@ extern "C" <%= classData.constructor_type().lib_type_to_str() auto oriOverloads = e2pOriginal->AsMethodDefinition()->Overloads(); newE2pNode->AsMethodDefinition()->SetOverloads(std::move(oriOverloads)); +% end +% if className == "AssignmentExpression" + if (e2pOriginal->AsAssignmentExpression()->IsIgnoreConstAssign()) { + newE2pNode->SetIgnoreConstAssign(); + } % end return reinterpret_cast<<%= classData.constructor_type().lib_type_to_str() %>>(newE2pNode); diff --git a/ets2panda/test/unit/plugin/CMakeLists.txt b/ets2panda/test/unit/plugin/CMakeLists.txt index 4967dab97dd81598b2d196a95b39761beb77b91d..1fc092314251c6855296d8439d22db9faf38bc4b 100644 --- a/ets2panda/test/unit/plugin/CMakeLists.txt +++ b/ets2panda/test/unit/plugin/CMakeLists.txt @@ -116,6 +116,7 @@ set(PLUGIN_TESTS "plugin_proceed_to_state_function_dump compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "pugin_proceed_to_state_annotationUsage_source_range_access compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_optional_language compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" + "plugin_proceed_to_state_dump_src_for_etsglobal_test compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" ) set(RUNTIME_ARGUMENTS diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_dump_src_for_etsglobal_test.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_dump_src_for_etsglobal_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebfbfd98a602c3d7e6731eb1d5410ea3e2ee7cdb --- /dev/null +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_dump_src_for_etsglobal_test.cpp @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "os/library_loader.h" + +#include "public/es2panda_lib.h" +#include "util.h" + +// NOLINTBEGIN + +static es2panda_Impl *impl = nullptr; + +static std::string g_source = R"( +let a: int = 11; +const b: int = 666; +a = 20; +foo() +function foo() {} +)"; + +static std::string expected = R"( +let a: int; + +const b: int = 666; + +function main() {} + +function foo() {} + +a = 20; +foo(); +)"; + +int main(int argc, char **argv) +{ + if (argc < MIN_ARGC) { + return INVALID_ARGC_ERROR_CODE; + } + + impl = GetImpl(); + if (impl == nullptr) { + return NULLPTR_IMPL_ERROR_CODE; + } + + const char **args = const_cast(&(argv[1])); + auto config = impl->CreateConfig(argc - 1, args); + auto context = impl->CreateContextFromString(config, g_source.data(), argv[argc - 1]); + auto *program = impl->ContextProgram(context); + impl->ProceedToState(context, ES2PANDA_STATE_CHECKED); + auto *entryAst = impl->ProgramAst(context, program); + [[maybe_unused]] std::string actual = impl->AstNodeDumpEtsSrcConst(context, entryAst); + ASSERT(expected == actual); + return 0; +} + +// NOLINTEND \ No newline at end of file