diff --git a/ets2panda/test/ast/compiler/ets/nested_var_declaration.ets b/ets2panda/test/ast/compiler/ets/nested_var_declaration.ets new file mode 100644 index 0000000000000000000000000000000000000000..fc663fbc08a0f36b94681c9bc70d293241d0b770 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/nested_var_declaration.ets @@ -0,0 +1,24 @@ +/* + * 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() { + let x = 1; + { + var x = 1; + } +} + +/* @@? 19:13 Error SyntaxError: 'var' keyword is not supported. Use 'let' instead. */ +/* @@? 19:13 Error TypeError: Variable 'x' has already been declared. */ diff --git a/ets2panda/varbinder/varbinder.h b/ets2panda/varbinder/varbinder.h index ff674f639bedec5df0df93fdecf5d21aab8b3379..65ca0bc65243e868f1d05653a028b270959ef213 100644 --- a/ets2panda/varbinder/varbinder.h +++ b/ets2panda/varbinder/varbinder.h @@ -427,6 +427,10 @@ std::tuple VarBinder::NewVarDecl(const lexer::Source var = scope_->FindLocal(decl->Name(), ResolveBindingOptions::BINDINGS); } + if (var == nullptr && decl->IsVarDecl()) { + var = scope_->Find(decl->Name(), ResolveBindingOptions::BINDINGS).variable; + } + ES2PANDA_ASSERT(var != nullptr); return {decl, var}; }