From 0f3c6d2511de6500031b9113b4c0f9e583799477 Mon Sep 17 00:00:00 2001 From: songqi Date: Fri, 17 Mar 2023 17:12:18 +0800 Subject: [PATCH] Make declare syntax not generate bytecode Issue: I6HNR4 Tests: parser/compiler/tsc/test262 Signed-off-by: songqi Change-Id: I72e4820b96f901aedd2a2c06358bed63deab5b4c --- es2panda/binder/binder.h | 15 +++--- es2panda/binder/declaration.h | 34 ++++++------- es2panda/compiler/base/hoisting.cpp | 14 ++++-- es2panda/compiler/core/pandagen.cpp | 2 +- es2panda/ir/statements/classDeclaration.cpp | 3 ++ es2panda/ir/statements/variableDeclarator.cpp | 6 ++- es2panda/parser/parserImpl.cpp | 16 +++--- es2panda/parser/parserImpl.h | 4 +- es2panda/parser/statementParser.cpp | 49 ++++++++++--------- es2panda/parser/transformer/transformer.cpp | 6 +-- .../compiler/test-ts-declare-1-expected.txt | 1 + .../ts/cases/compiler/test-ts-declare-1.ts | 44 +++++++++++++++++ 12 files changed, 127 insertions(+), 67 deletions(-) create mode 100644 es2panda/test/compiler/ts/cases/compiler/test-ts-declare-1-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/compiler/test-ts-declare-1.ts diff --git a/es2panda/binder/binder.h b/es2panda/binder/binder.h index 085756078a..3d3bfe8f28 100644 --- a/es2panda/binder/binder.h +++ b/es2panda/binder/binder.h @@ -67,13 +67,13 @@ public: void IdentifierAnalysis(ResolveBindingFlags flags = ResolveBindingFlags::ALL); template - T *AddDecl(const lexer::SourcePosition &pos, Args &&... args); + T *AddDecl(const lexer::SourcePosition &pos, bool isDeclare, Args &&... args); template - T *AddDecl(const lexer::SourcePosition &pos, DeclarationFlags flag, Args &&... args); + T *AddDecl(const lexer::SourcePosition &pos, DeclarationFlags flag, bool isDeclare, Args &&... args); template - T *AddTsDecl(const lexer::SourcePosition &pos, Args &&... args); + T *AddTsDecl(const lexer::SourcePosition &pos, bool isDeclare, Args &&... args); ParameterDecl *AddParamDecl(const ir::AstNode *param); @@ -261,9 +261,10 @@ private: }; template -T *Binder::AddTsDecl(const lexer::SourcePosition &pos, Args &&... args) +T *Binder::AddTsDecl(const lexer::SourcePosition &pos, bool isDeclare, Args &&... args) { T *decl = Allocator()->New(std::forward(args)...); + decl->SetDeclare(isDeclare); if (scope_->AddTsDecl(Allocator(), decl, program_->Extension())) { AddDeclarationName(decl->Name()); @@ -274,9 +275,10 @@ T *Binder::AddTsDecl(const lexer::SourcePosition &pos, Args &&... args) } template -T *Binder::AddDecl(const lexer::SourcePosition &pos, Args &&... args) +T *Binder::AddDecl(const lexer::SourcePosition &pos, bool isDeclare, Args &&... args) { T *decl = Allocator()->New(std::forward(args)...); + decl->SetDeclare(isDeclare); if (scope_->AddDecl(Allocator(), decl, program_->Extension())) { AddDeclarationName(decl->Name(), decl->Type()); @@ -287,10 +289,11 @@ T *Binder::AddDecl(const lexer::SourcePosition &pos, Args &&... args) } template -T *Binder::AddDecl(const lexer::SourcePosition &pos, DeclarationFlags flag, Args &&... args) +T *Binder::AddDecl(const lexer::SourcePosition &pos, DeclarationFlags flag, bool isDeclare, Args &&... args) { T *decl = Allocator()->New(std::forward(args)...); decl->AddFlag(flag); + decl->SetDeclare(isDeclare); if (scope_->AddDecl(Allocator(), decl, program_->Extension())) { AddDeclarationName(decl->Name(), decl->Type()); diff --git a/es2panda/binder/declaration.h b/es2panda/binder/declaration.h index 88e468b475..611241d570 100644 --- a/es2panda/binder/declaration.h +++ b/es2panda/binder/declaration.h @@ -110,12 +110,23 @@ public: return HasFlag(DeclarationFlags::IMPORT | DeclarationFlags::EXPORT); } + void SetDeclare(bool isDeclare) + { + isDeclare_ = isDeclare; + } + + bool IsDeclare() const + { + return isDeclare_; + } + protected: explicit Decl(util::StringView name) : name_(name) {} util::StringView name_; DeclarationFlags flags_ {}; const ir::AstNode *node_ {}; + bool isDeclare_ {false}; }; template @@ -142,10 +153,9 @@ private: class EnumLiteralDecl : public MultiDecl { public: - explicit EnumLiteralDecl(ArenaAllocator *allocator, util::StringView name, - bool isExport, bool isDeclare, bool isConst) : MultiDecl(allocator, name), - isExport_(isExport), isDeclare_(isDeclare), isConst_(isConst) {} - + explicit EnumLiteralDecl(ArenaAllocator *allocator, util::StringView name, bool isExport, bool isConst) + : MultiDecl(allocator, name), isExport_(isExport), isConst_(isConst) {} + DeclType Type() const override { return DeclType::ENUM_LITERAL; @@ -156,11 +166,6 @@ public: return isExport_; } - bool IsDeclare() const - { - return isDeclare_; - } - bool IsConst() const { return isConst_; @@ -179,7 +184,6 @@ public: private: TSEnumScope *scope_ {nullptr}; bool isExport_ {}; - bool isDeclare_ {}; bool isConst_ {}; }; @@ -288,20 +292,12 @@ public: class ClassDecl : public Decl { public: - explicit ClassDecl(util::StringView name, bool declare) : Decl(name), declare_(declare) {} + explicit ClassDecl(util::StringView name) : Decl(name) {} DeclType Type() const override { return DeclType::CLASS; } - - bool IsDeclare() const - { - return declare_; - } - -private: - bool declare_; }; class FunctionDecl : public MultiDecl { diff --git a/es2panda/compiler/base/hoisting.cpp b/es2panda/compiler/base/hoisting.cpp index 741c6f08fd..10cbed321e 100644 --- a/es2panda/compiler/base/hoisting.cpp +++ b/es2panda/compiler/base/hoisting.cpp @@ -15,10 +15,11 @@ #include "hoisting.h" -#include #include #include #include +#include +#include #include namespace panda::es2panda::compiler { @@ -37,8 +38,11 @@ static void StoreModuleVarOrLocalVar(PandaGen *pg, binder::ScopeFindResult &resu static void HoistVar(PandaGen *pg, binder::Variable *var, const binder::VarDecl *decl) { - auto *scope = pg->Scope(); + if (decl->IsDeclare()) { + return; + } + auto *scope = pg->Scope(); if (scope->IsGlobalScope()) { pg->LoadConst(decl->Node(), Constant::JS_UNDEFINED); pg->StoreGlobalVar(decl->Node(), decl->Name()); @@ -59,10 +63,12 @@ static void HoistVar(PandaGen *pg, binder::Variable *var, const binder::VarDecl static void HoistFunction(PandaGen *pg, binder::Variable *var, const binder::FunctionDecl *decl) { const ir::ScriptFunction *scriptFunction = decl->Node()->AsScriptFunction(); - auto *scope = pg->Scope(); + if (scriptFunction->Declare()) { + return; + } const auto &internalName = scriptFunction->Scope()->InternalName(); - + auto *scope = pg->Scope(); if (scope->IsGlobalScope()) { pg->DefineFunction(decl->Node(), scriptFunction, internalName); pg->StoreGlobalVar(decl->Node(), var->Declaration()->Name()); diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index e1c6658801..23fab34a87 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -302,7 +302,7 @@ void PandaGen::LoadVar(const ir::Identifier *node, const binder::ScopeFindResult { auto *var = result.variable; - if (!var) { + if (!var || var->Declaration()->IsDeclare()) { TryLoadGlobalByName(node, result.name); return; } diff --git a/es2panda/ir/statements/classDeclaration.cpp b/es2panda/ir/statements/classDeclaration.cpp index b52fe2b484..a1b3611d4d 100644 --- a/es2panda/ir/statements/classDeclaration.cpp +++ b/es2panda/ir/statements/classDeclaration.cpp @@ -44,6 +44,9 @@ void ClassDeclaration::Compile(compiler::PandaGen *pg) const // [ClassDeclaration] without [Identifier] must have parent node // of [ExportDefaultDeclaration] during compiling phase. So we use // the parent node to create a lreference with boundName of [*default*]. + if (def_->Declare()) { + return; + } const auto *node = def_->Ident() ? def_->Ident() : this->Parent(); auto lref = compiler::LReference::CreateLRef(pg, node, true); def_->Compile(pg); diff --git a/es2panda/ir/statements/variableDeclarator.cpp b/es2panda/ir/statements/variableDeclarator.cpp index 1c4d57bae7..d442b857fe 100644 --- a/es2panda/ir/statements/variableDeclarator.cpp +++ b/es2panda/ir/statements/variableDeclarator.cpp @@ -48,8 +48,12 @@ void VariableDeclarator::Dump(ir::AstDumper *dumper) const void VariableDeclarator::Compile(compiler::PandaGen *pg) const { - compiler::LReference lref = compiler::LReference::CreateLRef(pg, id_, true); const ir::VariableDeclaration *decl = parent_->AsVariableDeclaration(); + if (decl->Declare()) { + return; + } + + compiler::LReference lref = compiler::LReference::CreateLRef(pg, id_, true); if (init_) { init_->Compile(pg); diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 80ccd186be..8e4805a1e6 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -2765,7 +2765,7 @@ bool ParserImpl::IsMethodDefinitionsAreSame(const ir::MethodDefinition *property return IsPropertyKeysAreSame(property->Key(), overload->Key()); } -ir::Identifier *ParserImpl::SetIdentNodeInClassDefinition() +ir::Identifier *ParserImpl::SetIdentNodeInClassDefinition(bool isDeclare) { lexer::TokenType keywType = lexer_->GetToken().KeywordType(); CheckStrictReservedWord(); @@ -2776,7 +2776,7 @@ ir::Identifier *ParserImpl::SetIdentNodeInClassDefinition() const util::StringView &identStr = lexer_->GetToken().Ident(); - Binder()->AddDecl(lexer_->GetToken().Start(), identStr); + Binder()->AddDecl(lexer_->GetToken().Start(), isDeclare, identStr); auto *identNode = AllocNode(identStr); identNode->SetRange(lexer_->GetToken().Loc()); @@ -2799,7 +2799,7 @@ ir::ClassDefinition *ParserImpl::ParseClassDefinition(bool isDeclaration, bool i if (lexer_->GetToken().Type() == lexer::TokenType::LITERAL_IDENT && (Extension() != ScriptExtension::TS || lexer_->GetToken().KeywordType() != lexer::TokenType::KEYW_IMPLEMENTS)) { - identNode = SetIdentNodeInClassDefinition(); + identNode = SetIdentNodeInClassDefinition(isDeclare); } else if (isDeclaration && idRequired) { ThrowSyntaxError("Unexpected token, expected an identifier."); } @@ -2964,12 +2964,12 @@ ir::TSEnumDeclaration *ParserImpl::ParseEnumMembers(ir::Identifier *key, const l if (lexer_->GetToken().Type() == lexer::TokenType::LITERAL_IDENT) { memberKey = AllocNode(lexer_->GetToken().Ident()); - decl = Binder()->AddDecl(keyStartLoc, lexer_->GetToken().Ident()); + decl = Binder()->AddDecl(keyStartLoc, isDeclare, lexer_->GetToken().Ident()); memberKey->SetRange(lexer_->GetToken().Loc()); lexer_->NextToken(); } else if (lexer_->GetToken().Type() == lexer::TokenType::LITERAL_STRING) { memberKey = AllocNode(lexer_->GetToken().String()); - decl = Binder()->AddDecl(keyStartLoc, lexer_->GetToken().String()); + decl = Binder()->AddDecl(keyStartLoc, isDeclare, lexer_->GetToken().String()); memberKey->SetRange(lexer_->GetToken().Loc()); lexer_->NextToken(); } else { @@ -3024,8 +3024,8 @@ ir::TSEnumDeclaration *ParserImpl::ParseEnumDeclaration(bool isExport, bool isDe } } if (res == nullptr) { - Binder()->AddTsDecl(lexer_->GetToken().Start(), Allocator(), - ident, isExport, isDeclare, isConst); + Binder()->AddTsDecl(lexer_->GetToken().Start(), isDeclare, + Allocator(), ident, isExport, isConst); res = currentScope->FindLocalTSVariable(ident); if (isExport && currentScope->IsTSModuleScope()) { currentScope->AsTSModuleScope()->AddExportTSVariable(ident, res); @@ -3177,7 +3177,7 @@ ir::TSTypeParameter *ParserImpl::ParseTsTypeParameter(bool throwError, bool addB auto *paramIdent = AllocNode(ident); if (addBinding) { - Binder()->AddDecl(lexer_->GetToken().Start(), ident); + Binder()->AddDecl(lexer_->GetToken().Start(), false, ident); } paramIdent->SetRange({lexer_->GetToken().Start(), lexer_->GetToken().End()}); diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index f4b347d9fe..626be76fa2 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -309,7 +309,7 @@ private: ir::MethodDefinition *CheckClassMethodOverload(ir::Statement *property, ir::MethodDefinition **ctor, bool isDeclare, lexer::SourcePosition errorInfo, ir::MethodDefinition *lastOverload, bool implExists, bool isAbstract = false); - ir::Identifier *SetIdentNodeInClassDefinition(); + ir::Identifier *SetIdentNodeInClassDefinition(bool isDeclare); ir::ClassDefinition *ParseClassDefinition(bool isDeclaration, bool idRequired = true, bool isDeclare = false, bool isAbstract = false); @@ -496,7 +496,7 @@ private: ir::TSEnumDeclaration *ParseEnumMembers(ir::Identifier *key, const lexer::SourcePosition &enumStart, bool isExport, bool isDeclare, bool isConst); ir::TSEnumDeclaration *ParseEnumDeclaration(bool isExport = false, bool isDeclare = false, bool isConst = false); - ir::TSInterfaceDeclaration *ParseTsInterfaceDeclaration(); + ir::TSInterfaceDeclaration *ParseTsInterfaceDeclaration(bool isDeclare); ir::SwitchCaseStatement *ParseSwitchCaseStatement(bool *seenDefault); ir::SwitchStatement *ParseSwitchStatement(); ir::ThrowStatement *ParseThrowStatement(); diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index 653b17fcf6..f4e0950906 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -388,7 +388,7 @@ ir::TSModuleDeclaration *ParserImpl::ParseTsModuleOrNamespaceDelaration(const le } } if (res == nullptr) { - Binder()->AddTsDecl(lexer_->GetToken().Start(), Allocator(), name); + Binder()->AddTsDecl(lexer_->GetToken().Start(), isDeclare, Allocator(), name); res = parentScope->FindLocalTSVariable(name); if (isExport && parentScope->IsTSModuleScope()) { parentScope->AsTSModuleScope()->AddExportTSVariable(name, res); @@ -473,7 +473,7 @@ ir::TSImportEqualsDeclaration *ParserImpl::ParseTsImportEqualsDeclaration(const if (lexer_->GetToken().KeywordType() != lexer::TokenType::KEYW_REQUIRE || lexer_->Lookahead() != LEX_CHAR_LEFT_PAREN) { binder::DeclarationFlags declflag = binder::DeclarationFlags::NONE; - auto *decl = Binder()->AddDecl(id->Start(), declflag, id->Name()); + auto *decl = Binder()->AddDecl(id->Start(), declflag, false, id->Name()); decl->BindNode(id); auto *scope = Binder()->GetScope(); auto name = id->Name(); @@ -637,7 +637,7 @@ ir::Statement *ParserImpl::ParsePotentialExpressionStatement(StatementParsingFla return ParseTsTypeAliasDeclaration(isDeclare); } case lexer::TokenType::KEYW_INTERFACE: { - return ParseTsInterfaceDeclaration(); + return ParseTsInterfaceDeclaration(isDeclare); } default: break; @@ -671,7 +671,7 @@ ir::ClassDeclaration *ParserImpl::ParseClassDeclaration(bool idRequired, ArenaVe ASSERT(!className.Empty()); binder::DeclarationFlags flag = isExported ? binder::DeclarationFlags::EXPORT : binder::DeclarationFlags::NONE; - auto *decl = Binder()->AddDecl(location, flag, className, classDefinition->Declare()); + auto *decl = Binder()->AddDecl(location, flag, classDefinition->Declare(), className); decl->BindNode(classDefinition); @@ -700,7 +700,7 @@ ir::TSTypeAliasDeclaration *ParserImpl::ParseTsTypeAliasDeclaration(bool isDecla const util::StringView &ident = lexer_->GetToken().Ident(); binder::TSBinding tsBinding(Allocator(), ident); - auto *decl = Binder()->AddTsDecl(lexer_->GetToken().Start(), tsBinding.View()); + auto *decl = Binder()->AddTsDecl(lexer_->GetToken().Start(), isDeclare, tsBinding.View()); auto *id = AllocNode(ident); id->SetRange(lexer_->GetToken().Loc()); @@ -728,7 +728,7 @@ ir::TSTypeAliasDeclaration *ParserImpl::ParseTsTypeAliasDeclaration(bool isDecla return typeAliasDecl; } -ir::TSInterfaceDeclaration *ParserImpl::ParseTsInterfaceDeclaration() +ir::TSInterfaceDeclaration *ParserImpl::ParseTsInterfaceDeclaration(bool isDeclare) { ASSERT(lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_INTERFACE); context_.Status() |= ParserStatus::ALLOW_THIS_TYPE; @@ -754,7 +754,8 @@ ir::TSInterfaceDeclaration *ParserImpl::ParseTsInterfaceDeclaration() binder::InterfaceDecl *decl {}; if (res == bindings.end()) { - decl = Binder()->AddTsDecl(lexer_->GetToken().Start(), Allocator(), tsBinding.View()); + decl = Binder()->AddTsDecl(lexer_->GetToken().Start(), isDeclare, + Allocator(), tsBinding.View()); } else if (!res->second->Declaration()->IsInterfaceDecl()) { Binder()->ThrowRedeclaration(lexer_->GetToken().Start(), ident); } else { @@ -1143,7 +1144,7 @@ ir::FunctionDeclaration *ParserImpl::ParseFunctionDeclaration(bool canBeAnonymou binder::DeclarationFlags declflag = (newStatus & ParserStatus::EXPORT_REACHED) ? binder::DeclarationFlags::EXPORT : binder::DeclarationFlags::NONE; - Binder()->AddDecl(startLoc, declflag, Allocator(), + Binder()->AddDecl(startLoc, declflag, isDeclare, Allocator(), parser::SourceTextModuleRecord::DEFAULT_LOCAL_NAME, func); return funcDecl; @@ -1199,7 +1200,8 @@ void ParserImpl::AddFunctionToBinder(ir::ScriptFunction *func, ParserStatus newS if (res == bindings.end() || (currentDecl->IsClassDecl() && currentDecl->AsClassDecl()->IsDeclare())) { - decl = Binder()->AddDecl(identNode->Start(), declflag, Allocator(), ident, func); + decl = Binder()->AddDecl(identNode->Start(), declflag, func->Declare(), + Allocator(), ident, func); } else { if (!currentDecl->IsFunctionDecl()) { Binder()->ThrowRedeclaration(startLoc, currentDecl->Name()); @@ -1217,7 +1219,8 @@ void ParserImpl::AddFunctionToBinder(ir::ScriptFunction *func, ParserStatus newS decl->Add(func); } else { - Binder()->AddDecl(identNode->Start(), declflag, Allocator(), ident, func); + Binder()->AddDecl(identNode->Start(), declflag, func->Declare(), + Allocator(), ident, func); } } @@ -2051,11 +2054,11 @@ ir::VariableDeclarator *ParserImpl::ParseVariableDeclarator(VariableParsingFlags binder::DeclarationFlags::EXPORT : binder::DeclarationFlags::NONE; if (flags & VariableParsingFlags::VAR) { - decl = Binder()->AddDecl(startLoc, declflag, binding->Name()); + decl = Binder()->AddDecl(startLoc, declflag, isDeclare, binding->Name()); } else if (flags & VariableParsingFlags::LET) { - decl = Binder()->AddDecl(startLoc, declflag, binding->Name()); + decl = Binder()->AddDecl(startLoc, declflag, isDeclare, binding->Name()); } else { - decl = Binder()->AddDecl(startLoc, declflag, binding->Name()); + decl = Binder()->AddDecl(startLoc, declflag, isDeclare, binding->Name()); } decl->BindNode(init); @@ -2250,7 +2253,7 @@ void ParserImpl::AddExportStarEntryItem(const lexer::SourcePosition &startLoc, c */ auto namespaceExportInternalName = GetNamespaceExportInternalName(); auto *decl = Binder()->AddDecl(startLoc, binder::DeclarationFlags::EXPORT, - namespaceExportInternalName); + false, namespaceExportInternalName); decl->BindNode(exported); auto *importEntry = moduleRecord->NewEntry( @@ -2372,7 +2375,7 @@ ir::ExportDefaultDeclaration *ParserImpl::ParseExportDefaultDeclaration(const le declNode = ParseFunctionDeclaration(false, ParserStatus::ASYNC_FUNCTION | ParserStatus::EXPORT_REACHED); } else if (Extension() == ScriptExtension::TS && lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_INTERFACE) { - declNode = ParseTsInterfaceDeclaration(); + declNode = ParseTsInterfaceDeclaration(false); } else if (Extension() == ScriptExtension::TS && lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_ABSTRACT) { lexer_->NextToken(); @@ -2383,7 +2386,7 @@ ir::ExportDefaultDeclaration *ParserImpl::ParseExportDefaultDeclaration(const le } else { declNode = ParseExpression(); Binder()->AddDecl(declNode->Start(), binder::DeclarationFlags::EXPORT, - parser::SourceTextModuleRecord::DEFAULT_LOCAL_NAME); + false, parser::SourceTextModuleRecord::DEFAULT_LOCAL_NAME); eatSemicolon = true; } @@ -2566,7 +2569,7 @@ ir::ExportNamedDeclaration *ParserImpl::ParseNamedExportDeclaration(const lexer: break; } case lexer::TokenType::KEYW_INTERFACE: { - decl = ParseTsInterfaceDeclaration(); + decl = ParseTsInterfaceDeclaration(isDeclare); break; } case lexer::TokenType::KEYW_TYPE: { @@ -2726,7 +2729,7 @@ void ParserImpl::ParseNameSpaceImport(ArenaVector *specifiers) specifiers->push_back(specifier); auto *decl = Binder()->AddDecl(namespaceStart, binder::DeclarationFlags::NAMESPACE_IMPORT, - local->Name()); + false, local->Name()); decl->BindNode(specifier); lexer_->NextToken(); // eat local name @@ -2785,7 +2788,7 @@ void ParserImpl::ParseNamedImportSpecifiers(ArenaVector *specifie specifiers->push_back(specifier); auto *decl = Binder()->AddDecl(local->Start(), binder::DeclarationFlags::IMPORT, - local->Name()); + false, local->Name()); decl->BindNode(specifier); if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COMMA) { @@ -2847,9 +2850,8 @@ ir::AstNode *ParserImpl::ParseImportDefaultSpecifier(ArenaVector } if (lexer_->GetToken().KeywordType() != lexer::TokenType::KEYW_REQUIRE || lexer_->Lookahead() != LEX_CHAR_LEFT_PAREN) { - auto *decl = Binder()->AddDecl(local->Start(), - binder::DeclarationFlags::NONE, - local->Name()); + auto *decl = Binder()->AddDecl(local->Start(), binder::DeclarationFlags::NONE, + false, local->Name()); decl->BindNode(local); auto *scope = Binder()->GetScope(); auto *var = scope->FindLocalTSVariable(local->Name()); @@ -2866,7 +2868,8 @@ ir::AstNode *ParserImpl::ParseImportDefaultSpecifier(ArenaVector specifier->SetRange(specifier->Local()->Range()); specifiers->push_back(specifier); - auto *decl = Binder()->AddDecl(local->Start(), binder::DeclarationFlags::IMPORT, local->Name()); + auto *decl = Binder()->AddDecl(local->Start(), binder::DeclarationFlags::IMPORT, + false, local->Name()); decl->BindNode(specifier); if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_COMMA) { diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index 02462744df..939063f533 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -1204,11 +1204,11 @@ ir::VariableDeclaration *Transformer::CreateVariableDeclarationWithIdentify(util binder::DeclarationFlags::EXPORT : binder::DeclarationFlags::NONE; if (flags & VariableParsingFlags::VAR) { - decl = Binder()->AddDecl(startPos, declflag, name); + decl = Binder()->AddDecl(startPos, declflag, false, name); } else if (flags & VariableParsingFlags::LET) { - decl = Binder()->AddDecl(startPos, declflag, name); + decl = Binder()->AddDecl(startPos, declflag, false, name); } else { - decl = Binder()->AddDecl(startPos, declflag, name); + decl = Binder()->AddDecl(startPos, declflag, false, name); } decl->BindNode(declaration); } diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-declare-1-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-declare-1-expected.txt new file mode 100644 index 0000000000..3da1ec26e9 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-declare-1-expected.txt @@ -0,0 +1 @@ +HelloWorld diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-declare-1.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-declare-1.ts new file mode 100644 index 0000000000..20ec9ce6fa --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-declare-1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 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. + */ + + +declare var v1: string; +declare let v2: string; +declare const v3: string; + +declare function AssertType(a: string, b: number): void; +declare function print(arg:any, arg1?:any):string; + +declare class A1 { + static a: number; + b: string; + func1(): void; + func2(p: string): number; +} +declare abstract class A2 { } + +declare type t = 1; +var v4: t; + +declare module m { export var a; }; + +declare namespace ns { export var a; }; + +declare enum E { a = 1 }; + +declare interface I { a: string }; +var v5: I; + +print("HelloWorld"); -- Gitee