diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index 93b5a3978d3e3c5e9a8c66d7df293643bfeba4c8..0f29aeb48d49b8037b562ab173e46bce627b6563 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -1554,12 +1554,16 @@ ir::IfStatement *ParserImpl::ParseIfStatement() lexer_->NextToken(); ir::Statement *consequent = nullptr; - auto consequentCtx = binder::LexicalScope(Binder()); - if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_BRACE) { - consequent = ParseBlockStatement(consequentCtx.GetScope()); - lexer_->NextToken(); - } else { - consequent = ParseStatement(StatementParsingFlags::IF_ELSE); + binder::Scope *consequentScope = nullptr; + { + auto consequentCtx = binder::LexicalScope(Binder()); + if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_BRACE) { + consequent = ParseBlockStatement(consequentCtx.GetScope()); + lexer_->NextToken(); + } else { + consequent = ParseStatement(StatementParsingFlags::IF_ELSE); + } + consequentScope = consequentCtx.GetScope(); } if (Extension() == ScriptExtension::TS && consequent->IsEmptyStatement()) { @@ -1583,7 +1587,7 @@ ir::IfStatement *ParserImpl::ParseIfStatement() alternateScope = alternateCtx.GetScope(); } - auto *ifStatement = AllocNode(test, consequent, consequentCtx.GetScope(), + auto *ifStatement = AllocNode(test, consequent, consequentScope, alternate, alternateScope); ifStatement->SetRange({startLoc, endLoc}); return ifStatement; diff --git a/es2panda/test/compiler/ts/cases/conformance/statements/ts-test-if-statement-1-expected.txt b/es2panda/test/compiler/ts/cases/conformance/statements/ts-test-if-statement-1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/statements/ts-test-if-statement-1-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/ts/cases/conformance/statements/ts-test-if-statement-1.ts b/es2panda/test/compiler/ts/cases/conformance/statements/ts-test-if-statement-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f0f08c934ef2af0c099b285412ff154e17d585d1 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/statements/ts-test-if-statement-1.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 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. + */ + + +var a = 1; +var b = 2; +if (0) { + const a = 10; +} else { + b = a; +} +print(b);