diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 5e0df49a9bb18da6f6815138bc1aada0ea70b150..5389783229dffc14d0622ee993c2c04840f50d9f 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -877,6 +877,19 @@ checker::Type *ETSAnalyzer::Check(ir::ArrayExpression *expr) const return expr->TsType(); } +static void ArrowFunctionRecursiveHandel(const ir::ArrowFunctionExpression *expr, Signature *signature, + ETSChecker *checker) +{ + auto *parent = expr->Parent(); + auto *variable = parent->IsVariableDeclarator() ? parent->AsVariableDeclarator()->Id()->AsIdentifier()->Variable() + : parent->IsClassProperty() + ? parent->AsClassProperty()->AsClassElement()->Key()->AsIdentifier()->Variable() + : nullptr; + if (variable != nullptr && variable->TsType() == nullptr) { + variable->SetTsType(checker->CreateETSArrowType(signature)); + } +} + checker::Type *ETSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const { ETSChecker *checker = GetETSChecker(); @@ -928,6 +941,7 @@ checker::Type *ETSAnalyzer::Check(ir::ArrowFunctionExpression *expr) const auto *signature = expr->Function()->Signature(); checker->Context().SetContainingSignature(signature); + ArrowFunctionRecursiveHandel(expr, signature, checker); expr->Function()->Body()->Check(checker); auto *funcType = checker->CreateETSArrowType(signature); diff --git a/ets2panda/test/runtime/ets/arrow_function_recursion_1.ets b/ets2panda/test/runtime/ets/arrow_function_recursion_1.ets new file mode 100644 index 0000000000000000000000000000000000000000..3d738efa1616047be5bf65ac51c6c9130be65302 --- /dev/null +++ b/ets2panda/test/runtime/ets/arrow_function_recursion_1.ets @@ -0,0 +1,26 @@ +/* + * 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. + */ + +const calculateFibonacci = (n : number): number => { + if (n === 0 || n === 1){ + return n + } else { + return calculateFibonacci(n - 1) + calculateFibonacci( n - 2) + } +} + + +const result = calculateFibonacci(9) +console.log("test calculateFibonacci(9)", result) \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/arrow_function_recursion_2.ets b/ets2panda/test/runtime/ets/arrow_function_recursion_2.ets new file mode 100644 index 0000000000000000000000000000000000000000..e238835fd588b6dce70c5bc00e1523d4323c1585 --- /dev/null +++ b/ets2panda/test/runtime/ets/arrow_function_recursion_2.ets @@ -0,0 +1,26 @@ +/* + * 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. + */ + +function main(){ +const calculateFibonacci = (n : number): number => { + if (n === 0 || n === 1){ + return n + } else { + return calculateFibonacci(n - 1) + calculateFibonacci( n - 2) + } + } + const result = calculateFibonacci(9) + console.log("test calculateFibonacci(9)", result) +} \ No newline at end of file