diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index 70ef5a11d332156699411bdaedd70cdbbd7158bb..ec893bdab08369a48a6c63745ac252abf00ae3c0 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -32,6 +32,18 @@ namespace panda::es2panda::compiler { +static void FindLastStatement(const ir::AstNode *&lastNode, const ir::AstNode *currentNode) +{ + if (currentNode->IsStatement()) { + if (currentNode->Range().start.index > lastNode->Range().start.index) { + lastNode = currentNode; + } + currentNode->Iterate([&lastNode](auto *childNode) { + FindLastStatement(lastNode, childNode); + }); + } +} + static void CompileSourceBlock(PandaGen *pg, const ir::BlockStatement *block) { bool endReturn = false; @@ -51,7 +63,11 @@ static void CompileSourceBlock(PandaGen *pg, const ir::BlockStatement *block) return; } - pg->ImplicitReturn(statements.empty() ? block : statements.back()); + const ir::AstNode *associatedNode = block; + if (!statements.empty()) { + FindLastStatement(associatedNode, statements.back()); + } + pg->ImplicitReturn(associatedNode); } static void CompileFunctionParameterDeclaration(PandaGen *pg, const ir::ScriptFunction *func)