diff --git a/es2panda/aot/options.cpp b/es2panda/aot/options.cpp index e8d2e3ead687fb1d89491ce5bb3adfc151e7914b..aeb5f3bd35fd788aac3d16941f0dc7b9a7cd5a63 100644 --- a/es2panda/aot/options.cpp +++ b/es2panda/aot/options.cpp @@ -136,6 +136,9 @@ bool Options::Parse(int argc, const char **argv) panda::PandArg opParseOnly("parse-only", false, "Parse the input only"); panda::PandArg opEnableTypeCheck("enable-type-check", false, "Check the type in ts after parse"); panda::PandArg opDumpAst("dump-ast", false, "Dump the parsed AST"); + panda::PandArg opDumpTransformedAst("dump-transformed-ast", false, "Dump the parsed AST after transform"); + panda::PandArg opCheckTransformedAstStructure("check-transformed-ast-structure", false, + "Check the AST structure after transform"); // type extractor panda::PandArg opTypeExtractor("type-extractor", false, "Enable type extractor for typescript"); @@ -180,6 +183,8 @@ bool Options::Parse(int argc, const char **argv) argparser_->Add(&opModule); argparser_->Add(&opCommonjs); argparser_->Add(&opDumpAst); + argparser_->Add(&opDumpTransformedAst); + argparser_->Add(&opCheckTransformedAstStructure); argparser_->Add(&opParseOnly); argparser_->Add(&opEnableTypeCheck); argparser_->Add(&opTypeExtractor); @@ -369,6 +374,8 @@ bool Options::Parse(int argc, const char **argv) compilerOptions_.dumpAsm = opDumpAssembly.GetValue(); compilerOptions_.dumpAst = opDumpAst.GetValue(); + compilerOptions_.dumpTransformedAst = opDumpTransformedAst.GetValue(); + compilerOptions_.checkTransformedAstStructure = opCheckTransformedAstStructure.GetValue(); compilerOptions_.dumpDebugInfo = opDumpDebugInfo.GetValue(); compilerOptions_.isDebug = opDebugInfo.GetValue(); compilerOptions_.parseOnly = opParseOnly.GetValue(); diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index 474a24b0e0586935f133e39ac2110b31fbdf4b2b..46895a8561b1050297e5f45706a3b896a019439c 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -15,43 +15,46 @@ #include "binder.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "binder/scope.h" +#include "binder/tsBinding.h" +#include "es2panda.h" +#include "ir/astNode.h" +#include "ir/base/catchClause.h" +#include "ir/base/classDefinition.h" +#include "ir/base/methodDefinition.h" +#include "ir/base/property.h" +#include "ir/base/scriptFunction.h" +#include "ir/base/spreadElement.h" +#include "ir/expressions/arrayExpression.h" +#include "ir/expressions/assignmentExpression.h" +#include "ir/expressions/identifier.h" +#include "ir/expressions/objectExpression.h" +#include "ir/module/exportNamedDeclaration.h" +#include "ir/module/exportSpecifier.h" +#include "ir/statements/blockStatement.h" +#include "ir/statements/doWhileStatement.h" +#include "ir/statements/forInStatement.h" +#include "ir/statements/forOfStatement.h" +#include "ir/statements/forUpdateStatement.h" +#include "ir/statements/ifStatement.h" +#include "ir/statements/switchCaseStatement.h" +#include "ir/statements/switchStatement.h" +#include "ir/statements/variableDeclaration.h" +#include "ir/statements/variableDeclarator.h" +#include "ir/statements/whileStatement.h" +#include "ir/ts/tsClassImplements.h" +#include "ir/ts/tsConstructorType.h" +#include "ir/ts/tsEnumDeclaration.h" +#include "ir/ts/tsFunctionType.h" +#include "ir/ts/tsIndexSignature.h" +#include "ir/ts/tsMethodSignature.h" +#include "ir/ts/tsModuleBlock.h" +#include "ir/ts/tsModuleDeclaration.h" +#include "ir/ts/tsSignatureDeclaration.h" +#include "ir/ts/tsTypeParameterDeclaration.h" +#include "ir/ts/tsTypeParameterInstantiation.h" +#include "util/concurrent.h" +#include "util/helpers.h" namespace panda::es2panda::binder { void Binder::InitTopScope() @@ -292,7 +295,7 @@ void Binder::BuildVarDeclaratorId(const ir::AstNode *parent, ir::AstNode *childN if (Program()->Extension() == ScriptExtension::TS) { ident->SetVariable(variable); - BuildTSSignatureDeclarationBaseParams(ident->TypeAnnotation()); + BuildTSSignatureDeclarationBaseParamsWithParent(ident, ident->TypeAnnotation()); } variable->AddFlag(VariableFlags::INITIALIZED); @@ -305,7 +308,7 @@ void Binder::BuildVarDeclaratorId(const ir::AstNode *parent, ir::AstNode *childN BuildVarDeclaratorId(childNode, prop); } - BuildTSSignatureDeclarationBaseParams(objPattern->TypeAnnotation()); + BuildTSSignatureDeclarationBaseParamsWithParent(objPattern, objPattern->TypeAnnotation()); break; } case ir::AstNodeType::ARRAY_PATTERN: { @@ -315,7 +318,7 @@ void Binder::BuildVarDeclaratorId(const ir::AstNode *parent, ir::AstNode *childN BuildVarDeclaratorId(childNode, element); } - BuildTSSignatureDeclarationBaseParams(arrayPattern->TypeAnnotation()); + BuildTSSignatureDeclarationBaseParamsWithParent(arrayPattern, arrayPattern->TypeAnnotation()); break; } case ir::AstNodeType::ASSIGNMENT_PATTERN: { @@ -337,11 +340,18 @@ void Binder::BuildVarDeclaratorId(const ir::AstNode *parent, ir::AstNode *childN } } -void Binder::BuildTSSignatureDeclarationBaseParams(const ir::AstNode *typeNode) +void Binder::BuildTSSignatureDeclarationBaseParamsWithParent(const ir::AstNode *parent, ir::AstNode *typeNode) { if (!typeNode) { return; } + typeNode->SetParent(parent); + BuildTSSignatureDeclarationBaseParams(typeNode); +} + +void Binder::BuildTSSignatureDeclarationBaseParams(const ir::AstNode *typeNode) +{ + ASSERT(typeNode != nullptr); Scope *scope = nullptr; @@ -401,12 +411,20 @@ void Binder::BuildClassDefinition(ir::ClassDefinition *classDef) auto scopeCtx = LexicalScope::Enter(this, classDef->Scope()); + if (classDef->TypeParams()) { + ResolveReference(classDef, classDef->TypeParams()); + } + if (classDef->Super()) { ResolveReference(classDef, classDef->Super()); } - for (auto *impl : classDef->Implements()) { - ResolveReference(classDef, impl); + if (classDef->SuperTypeParams()) { + ResolveReference(classDef, classDef->SuperTypeParams()); + } + + for (auto *iter : classDef->Implements()) { + ResolveReference(classDef, iter); } if (classDef->Ident()) { @@ -414,6 +432,8 @@ void Binder::BuildClassDefinition(ir::ClassDefinition *classDef) ASSERT(res.variable && res.variable->Declaration()->IsConstDecl()); res.variable->AddFlag(VariableFlags::INITIALIZED); + + classDef->Ident()->SetParent(classDef); } ResolveReference(classDef, classDef->Ctor()); @@ -421,6 +441,10 @@ void Binder::BuildClassDefinition(ir::ClassDefinition *classDef) for (auto *stmt : classDef->Body()) { ResolveReference(classDef, stmt); } + + for (auto *iter : classDef->IndexSignatures()) { + ResolveReference(classDef, iter); + } } void Binder::BuildForUpdateLoop(ir::ForUpdateStatement *forUpdateStmt) @@ -502,9 +526,22 @@ void Binder::ResolveReference(const ir::AstNode *parent, ir::AstNode *childNode) auto *outerScope = scope_; + if (scriptFunc->Id() != nullptr) { + scriptFunc->Id()->SetParent(scriptFunc); + } + { auto paramScopeCtx = LexicalScope::Enter(this, funcScope->ParamScope()); + if (Program()->Extension() == ScriptExtension::TS) { + if (scriptFunc->TypeParams() != nullptr) { + ResolveReference(scriptFunc, scriptFunc->TypeParams()); + } + if (scriptFunc->ThisParams() != nullptr) { + ResolveReference(scriptFunc, scriptFunc->ThisParams()); + } + } + for (auto *param : scriptFunc->Params()) { ResolveReference(scriptFunc, param); } @@ -545,7 +582,10 @@ void Binder::ResolveReference(const ir::AstNode *parent, ir::AstNode *childNode) break; } case ir::AstNodeType::BLOCK_STATEMENT: { - auto scopeCtx = LexicalScope::Enter(this, childNode->AsBlockStatement()->Scope()); + auto scope = childNode->AsBlockStatement()->Scope(); + auto scopeCtx = scope != nullptr ? + LexicalScope::Enter(this, scope) : + LexicalScope::Enter(this, GetScope()); ResolveReferences(childNode); break; diff --git a/es2panda/binder/binder.h b/es2panda/binder/binder.h index 464df08b5aed413c445c0f2ca91af4574a0cf916..bf84d49caf89ab16eb5b1466cabda7a1b44a9b2e 100644 --- a/es2panda/binder/binder.h +++ b/es2panda/binder/binder.h @@ -200,6 +200,7 @@ private: // TypeScript specific functions void BuildTSSignatureDeclarationBaseParams(const ir::AstNode *typeNode); + void BuildTSSignatureDeclarationBaseParamsWithParent(const ir::AstNode *parent, ir::AstNode *typeNode); parser::Program *program_ {}; FunctionScope *topScope_ {}; diff --git a/es2panda/es2panda.cpp b/es2panda/es2panda.cpp index 7c429743342c221a3f1dc00d63ee1b99e01e5578..d65c8ea77fd76461a842fe0dd1008dca5c007ccc 100644 --- a/es2panda/es2panda.cpp +++ b/es2panda/es2panda.cpp @@ -100,6 +100,12 @@ panda::pandasm::Program *Compiler::Compile(const SourceFile &input, const Compil if (ast.Extension() == ScriptExtension::TS) { transformer_->Transform(&ast); ast.Binder()->IdentifierAnalysis(binder::ResolveBindingFlags::ALL); + if (options.dumpTransformedAst) { + std::cout << ast.Dump() << std::endl; + } + if (options.checkTransformedAstStructure) { + transformer_->CheckTransformedAstStructure(&ast); + } } if (options.parseOnly) { diff --git a/es2panda/es2panda.h b/es2panda/es2panda.h index 8ac8084008a2c8b4016fef985cb682f46c537d8b..70fb7059375e0590134bf2d06469614ed18d877d 100644 --- a/es2panda/es2panda.h +++ b/es2panda/es2panda.h @@ -69,6 +69,8 @@ struct HotfixOptions { struct CompilerOptions { bool isDebug {false}; bool dumpAst {false}; + bool dumpTransformedAst {false}; + bool checkTransformedAstStructure {false}; bool dumpAsm {false}; bool dumpDebugInfo {false}; bool parseOnly {false}; diff --git a/es2panda/ir/base/classDefinition.cpp b/es2panda/ir/base/classDefinition.cpp index c86648d2e66f9b2bb436580d3def1fe118f9635f..c34b33f4562b8218595b7fce147bbae0edf11c09 100644 --- a/es2panda/ir/base/classDefinition.cpp +++ b/es2panda/ir/base/classDefinition.cpp @@ -15,28 +15,29 @@ #include "classDefinition.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "binder/binder.h" +#include "binder/scope.h" +#include "compiler/base/literals.h" +#include "compiler/base/lreference.h" +#include "compiler/core/pandagen.h" +#include "typescript/checker.h" +#include "ir/astDump.h" +#include "ir/base/classProperty.h" +#include "ir/base/methodDefinition.h" +#include "ir/base/scriptFunction.h" +#include "ir/expression.h" +#include "ir/expressions/functionExpression.h" +#include "ir/expressions/identifier.h" +#include "ir/expressions/literals/nullLiteral.h" +#include "ir/expressions/literals/numberLiteral.h" +#include "ir/expressions/literals/stringLiteral.h" +#include "ir/expressions/literals/taggedLiteral.h" +#include "ir/ts/tsClassImplements.h" +#include "ir/ts/tsIndexSignature.h" +#include "ir/ts/tsTypeParameter.h" +#include "ir/ts/tsTypeParameterDeclaration.h" +#include "ir/ts/tsTypeParameterInstantiation.h" +#include "util/helpers.h" namespace panda::es2panda::ir { diff --git a/es2panda/ir/base/classDefinition.h b/es2panda/ir/base/classDefinition.h index acb577bb4020ca50de0adbeab4d22f74c3fa0ed9..b008cabe924c08492da2ef173e367dbc62e8b580 100644 --- a/es2panda/ir/base/classDefinition.h +++ b/es2panda/ir/base/classDefinition.h @@ -76,6 +76,11 @@ public: return ident_; } + Identifier *Ident() + { + return ident_; + } + Expression *Super() { return superClass_; @@ -147,6 +152,16 @@ public: return ctor_; } + const TSTypeParameterInstantiation *SuperTypeParams() const + { + return superTypeParams_; + } + + TSTypeParameterInstantiation *SuperTypeParams() + { + return superTypeParams_; + } + const FunctionExpression *Ctor() const; util::StringView GetName() const; diff --git a/es2panda/ir/base/methodDefinition.cpp b/es2panda/ir/base/methodDefinition.cpp index 25c6ab407cae38b5a28a4ab71acb0754e35993c8..b2ca65e9e2b94d3e90416dcd5a600b0473f5229c 100644 --- a/es2panda/ir/base/methodDefinition.cpp +++ b/es2panda/ir/base/methodDefinition.cpp @@ -47,6 +47,12 @@ void MethodDefinition::Iterate(const NodeTraverser &cb) const for (auto *it : decorators_) { cb(it); } + + for (auto param : paramDecorators_) { + for (auto *it : param.decorators) { + cb(it); + } + } } void MethodDefinition::Dump(ir::AstDumper *dumper) const @@ -107,6 +113,12 @@ void MethodDefinition::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) { *iter = std::get(cb(*iter))->AsDecorator(); } + + for (auto param : paramDecorators_) { + for (auto iter = param.decorators.begin(); iter != param.decorators.end(); iter++) { + *iter = std::get(cb(*iter))->AsDecorator(); + } + } } } // namespace panda::es2panda::ir diff --git a/es2panda/ir/base/scriptFunction.cpp b/es2panda/ir/base/scriptFunction.cpp index ba8d968fc5956e39124072acbe4cf28c0efed5d4..242300dafe4f7456fbd96c40c42a044cb6f56483 100644 --- a/es2panda/ir/base/scriptFunction.cpp +++ b/es2panda/ir/base/scriptFunction.cpp @@ -60,14 +60,14 @@ void ScriptFunction::Iterate(const NodeTraverser &cb) const cb(id_); } - if (thisParam_) { - cb(thisParam_); - } - if (typeParams_) { cb(typeParams_); } + if (thisParam_) { + cb(thisParam_); + } + for (auto *it : params_) { cb(it); } @@ -111,14 +111,14 @@ void ScriptFunction::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) auto paramScopeCtx = binder::LexicalScope::Enter(binder, scope_->ParamScope()); - if (thisParam_) { - thisParam_ = std::get(cb(thisParam_))->AsExpression(); - } - if (typeParams_) { typeParams_ = std::get(cb(typeParams_))->AsTSTypeParameterDeclaration(); } + if (thisParam_) { + thisParam_ = std::get(cb(thisParam_))->AsExpression(); + } + for (auto iter = params_.begin(); iter != params_.end(); iter++) { *iter = std::get(cb(*iter))->AsExpression(); } diff --git a/es2panda/ir/base/scriptFunction.h b/es2panda/ir/base/scriptFunction.h index 0096f653fee53bdac3d79469ee4cbc1e38a37ed4..9e2dadfaef1c89126dacc5867e9be4c497cac5ef 100644 --- a/es2panda/ir/base/scriptFunction.h +++ b/es2panda/ir/base/scriptFunction.h @@ -89,6 +89,21 @@ public: return typeParams_; } + TSTypeParameterDeclaration *TypeParams() + { + return typeParams_; + } + + const Expression *ThisParams() const + { + return thisParam_; + } + + Expression *ThisParams() + { + return thisParam_; + } + const AstNode *Body() const { return body_; diff --git a/es2panda/ir/statements/blockStatement.cpp b/es2panda/ir/statements/blockStatement.cpp index 39bff4eff14646265bacfe1ff4ce150a1da94efa..d27cc8840e736a7cace093e14ecf204d225481d2 100644 --- a/es2panda/ir/statements/blockStatement.cpp +++ b/es2panda/ir/statements/blockStatement.cpp @@ -37,16 +37,22 @@ void BlockStatement::Dump(ir::AstDumper *dumper) const void BlockStatement::Compile(compiler::PandaGen *pg) const { - compiler::LocalRegScope lrs(pg, scope_); + if (scope_ != nullptr) { + compiler::LocalRegScope lrs(pg, scope_); - for (const auto *it : statements_) { - it->Compile(pg); + for (const auto *it : statements_) { + it->Compile(pg); + } + } else { + for (const auto *it : statements_) { + it->Compile(pg); + } } } checker::Type *BlockStatement::Check(checker::Checker *checker) const { - checker::ScopeContext scopeCtx(checker, scope_); + auto scopeCtx = checker::ScopeContext(checker, scope_ != nullptr ? scope_ : checker->Scope()); for (const auto *it : statements_) { it->Check(checker); @@ -57,7 +63,8 @@ checker::Type *BlockStatement::Check(checker::Checker *checker) const void BlockStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) { - auto scopeCtx = binder::LexicalScope::Enter(binder, scope_); + auto scopeCtx = binder::LexicalScope::Enter(binder, + scope_ != nullptr ? scope_ : binder->GetScope()); for (auto iter = statements_.begin(); iter != statements_.end();) { auto newStatements = cb(*iter); diff --git a/es2panda/ir/statements/doWhileStatement.cpp b/es2panda/ir/statements/doWhileStatement.cpp index 83078b77efc90c68227879d3712b58ccaafe02d0..756e4e9b52cc2a28f071f499ba960f1660250975 100644 --- a/es2panda/ir/statements/doWhileStatement.cpp +++ b/es2panda/ir/statements/doWhileStatement.cpp @@ -72,7 +72,7 @@ void DoWhileStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) { { auto loopScopeCtx = binder::LexicalScope::Enter(binder, scope_); - body_ = std::get(cb(body_))->AsStatement(); + body_ = UpdateChildStatement(cb, binder, body_); } test_ = std::get(cb(test_))->AsExpression(); } diff --git a/es2panda/ir/statements/forInStatement.cpp b/es2panda/ir/statements/forInStatement.cpp index 2ccad0cb65b5ab4b604d5b7d8bebceaf453e9a21..67a8e9b86b58a259eb6cf9e0f0fb9007fc717809 100644 --- a/es2panda/ir/statements/forInStatement.cpp +++ b/es2panda/ir/statements/forInStatement.cpp @@ -87,7 +87,7 @@ void ForInStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) right_ = std::get(cb(right_))->AsExpression(); auto loopCtx = binder::LexicalScope::Enter(binder, loopScope); - body_ = std::get(cb(body_))->AsStatement(); + body_ = UpdateChildStatement(cb, binder, body_); } } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/forOfStatement.cpp b/es2panda/ir/statements/forOfStatement.cpp index f653ce3108ac2cc371a09d90282e7fe8e5eadf8b..77a0987aaa186d2535148da72fadd88ec1968ca7 100644 --- a/es2panda/ir/statements/forOfStatement.cpp +++ b/es2panda/ir/statements/forOfStatement.cpp @@ -88,7 +88,7 @@ void ForOfStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) right_ = std::get(cb(right_))->AsExpression(); auto loopCtx = binder::LexicalScope::Enter(binder, loopScope); - body_ = std::get(cb(body_))->AsStatement(); + body_ = UpdateChildStatement(cb, binder, body_); } } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/forUpdateStatement.cpp b/es2panda/ir/statements/forUpdateStatement.cpp index d6275d9817170a63e6a7ffccbea43dc86a7f47e5..fc8989b7b6cae20174ba28e8087d9cfd58a04797 100644 --- a/es2panda/ir/statements/forUpdateStatement.cpp +++ b/es2panda/ir/statements/forUpdateStatement.cpp @@ -129,7 +129,7 @@ void ForUpdateStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binde update_ = std::get(cb(update_))->AsExpression(); } - body_ = std::get(cb(body_))->AsStatement(); + body_ = UpdateChildStatement(cb, binder, body_); } } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/ifStatement.cpp b/es2panda/ir/statements/ifStatement.cpp index 67e2a4cef1be5dedeb65e427ec2b2647eb1bf3ba..1f926d8283749856251ca1fad45862d495648ad3 100644 --- a/es2panda/ir/statements/ifStatement.cpp +++ b/es2panda/ir/statements/ifStatement.cpp @@ -79,17 +79,16 @@ checker::Type *IfStatement::Check(checker::Checker *checker) const void IfStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) { test_ = std::get(cb(test_))->AsExpression(); - consequent_ = UpdateIfStatementChildStatement(cb, binder, consequent_, consequentScope_); + consequent_ = UpdateIfStatementChildStatement(cb, binder, consequent_); if (alternate_) { - alternate_ = UpdateIfStatementChildStatement(cb, binder, alternate_, alternateScope_); + alternate_ = UpdateIfStatementChildStatement(cb, binder, alternate_); } } Statement *IfStatement::UpdateIfStatementChildStatement(const NodeUpdater &cb, const binder::Binder *binder, - Statement *statement, - binder::Scope *scope) const + Statement *statement) const { auto newStatement = cb(statement); if (std::holds_alternative(newStatement)) { @@ -102,7 +101,7 @@ Statement *IfStatement::UpdateIfStatementChildStatement(const NodeUpdater &cb, for (auto *it : newStatements) { statements.push_back(it->AsStatement()); } - return binder->Allocator()->New(scope, std::move(statements)); + return binder->Allocator()->New(nullptr, std::move(statements)); } } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/ifStatement.h b/es2panda/ir/statements/ifStatement.h index e299327eac03ff4dea26a3e1afdc947a8946b4e3..4c3b89939c4a18ead3a537efb05530b9f01e1f5f 100644 --- a/es2panda/ir/statements/ifStatement.h +++ b/es2panda/ir/statements/ifStatement.h @@ -33,14 +33,11 @@ class Expression; class IfStatement : public Statement { public: - explicit IfStatement(Expression *test, Statement *consequent, binder::Scope *consequentScope, - Statement *alternate, binder::Scope *alternateScope) + explicit IfStatement(Expression *test, Statement *consequent, Statement *alternate) : Statement(AstNodeType::IF_STATEMENT), test_(test), consequent_(consequent), - consequentScope_(consequentScope), - alternate_(alternate), - alternateScope_(alternateScope) + alternate_(alternate) { } @@ -67,14 +64,11 @@ public: protected: Statement *UpdateIfStatementChildStatement(const NodeUpdater &cb, const binder::Binder *binder, - Statement *statement, - binder::Scope *scope) const; + Statement *statement) const; Expression *test_; Statement *consequent_; - binder::Scope *consequentScope_; Statement *alternate_; - binder::Scope *alternateScope_; }; } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/loopStatement.cpp b/es2panda/ir/statements/loopStatement.cpp index 4a17cb926af2505c1c3fe3408ebdc607f4479157..8841c4b16b52db8d9a3e535300bb3fb9841ef7d9 100644 --- a/es2panda/ir/statements/loopStatement.cpp +++ b/es2panda/ir/statements/loopStatement.cpp @@ -15,7 +15,26 @@ #include "loopStatement.h" -#include +#include "ir/statements/blockStatement.h" namespace panda::es2panda::ir { + +Statement *LoopStatement::UpdateChildStatement(const NodeUpdater &cb, + const binder::Binder *binder, + Statement *statement) const +{ + auto newStatement = cb(statement); + if (std::holds_alternative(newStatement)) { + return std::get(newStatement)->AsStatement(); + } + + ASSERT(std::holds_alternative>(newStatement)); + ArenaVector statements(binder->Allocator()->Adapter()); + auto newStatements = std::get>(newStatement); + for (auto *it : newStatements) { + statements.push_back(it->AsStatement()); + } + return binder->Allocator()->New(nullptr, std::move(statements)); +} + } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/loopStatement.h b/es2panda/ir/statements/loopStatement.h index f73979ef602f43d98c08d55bdda445e0d629800b..ccbacac5197b632171ae6517d5af0d1ef44588ba 100644 --- a/es2panda/ir/statements/loopStatement.h +++ b/es2panda/ir/statements/loopStatement.h @@ -63,6 +63,8 @@ public: protected: explicit LoopStatement(AstNodeType type, binder::LoopScope *scope) : Statement(type), scope_(scope) {} + Statement *UpdateChildStatement(const NodeUpdater &cb, const binder::Binder *binder, Statement *statement) const; + binder::LoopScope *scope_; }; } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/whileStatement.cpp b/es2panda/ir/statements/whileStatement.cpp index 0c5a09fb3427763c6f203c29aabd11f197973d25..de90eaf38d79c6a1ca1d69fb6103ce108590ab5a 100644 --- a/es2panda/ir/statements/whileStatement.cpp +++ b/es2panda/ir/statements/whileStatement.cpp @@ -72,7 +72,7 @@ void WhileStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) test_ = std::get(cb(test_))->AsExpression(); auto loopScopeCtx = binder::LexicalScope::Enter(binder, scope_); - body_ = std::get(cb(body_))->AsStatement(); + body_ = UpdateChildStatement(cb, binder, body_); } } // namespace panda::es2panda::ir diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index 32f3d2db356419ad52f715d578a70b56748e7667..64f26b086af8881fb136d00e5fa539035bfae4d6 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -1557,19 +1557,7 @@ ir::IfStatement *ParserImpl::ParseIfStatement() } lexer_->NextToken(); - - ir::Statement *consequent = nullptr; - 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(); - } + ir::Statement *consequent = ParseStatement(StatementParsingFlags::IF_ELSE); if (Extension() == ScriptExtension::TS && consequent->IsEmptyStatement()) { ThrowSyntaxError("The body of an if statement cannot be the empty statement"); @@ -1577,23 +1565,14 @@ ir::IfStatement *ParserImpl::ParseIfStatement() endLoc = consequent->End(); ir::Statement *alternate = nullptr; - binder::Scope *alternateScope = nullptr; if (lexer_->GetToken().Type() == lexer::TokenType::KEYW_ELSE) { lexer_->NextToken(); // eat ELSE keyword - auto alternateCtx = binder::LexicalScope(Binder()); - if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_BRACE) { - alternate = ParseBlockStatement(alternateCtx.GetScope()); - lexer_->NextToken(); - } else { - alternate = ParseStatement(StatementParsingFlags::IF_ELSE); - } + alternate = ParseStatement(StatementParsingFlags::IF_ELSE); endLoc = alternate->End(); - alternateScope = alternateCtx.GetScope(); } - auto *ifStatement = AllocNode(test, consequent, consequentScope, - alternate, alternateScope); + auto *ifStatement = AllocNode(test, consequent, alternate); ifStatement->SetRange({startLoc, endLoc}); return ifStatement; } diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index eccc66a4e2a7430561c4cbe609df1a20e843902f..fbf2812a2eb7e23a67665a8543476e98a099bcff 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -1842,4 +1842,37 @@ ir::MemberExpression *Transformer::CreateMemberExpressionFromIdentifier(binder:: return res; } +void Transformer::CheckTransformedAstStructure(const Program *program) const +{ + bool passed = true; + CheckTransformedAstNodes(program->Ast(), &passed); + if (passed) { + std::cout << "Transformed AST structure check passed." << std::endl; + } +} + +void Transformer::CheckTransformedAstNodes(const ir::AstNode *parent, bool *passed) const +{ + parent->Iterate([this, parent, passed](auto *childNode) { CheckTransformedAstNode(parent, childNode, passed); }); +} + +void Transformer::CheckTransformedAstNode(const ir::AstNode *parent, ir::AstNode *childNode, bool *passed) const +{ + if (!(*passed)) { + return; + } + if (childNode->IsClassProperty() && childNode->AsClassProperty()->IsStatic()) { + return; + } + if (childNode->IsDecorator()) { + return; + } + if (childNode->Parent() != parent) { + std::cout << "Illegal ast structure after transforme." << std::endl; + *passed = false; + return; + } + CheckTransformedAstNodes(childNode, passed); +} + } // namespace panda::es2panda::parser diff --git a/es2panda/parser/transformer/transformer.h b/es2panda/parser/transformer/transformer.h index 9e90beb7f2c6b3ca624c5c6faaaa368012f17941..d26a53734e13357ef78d34ce750502c2bd33eddc 100644 --- a/es2panda/parser/transformer/transformer.h +++ b/es2panda/parser/transformer/transformer.h @@ -79,6 +79,7 @@ public: ~Transformer() = default; void Transform(Program *program); + void CheckTransformedAstStructure(const Program *program) const; private: static constexpr std::string_view PRIVATE_PROPERTY_SIGN = "#"; @@ -104,7 +105,8 @@ private: ir::UpdateNodes VisitClassDeclaration(ir::ClassDeclaration *node); ir::UpdateNodes VisitClassExpression(ir::ClassExpression *node); void VisitTSParameterProperty(ir::ClassDefinition *node); - std::vector VisitStaticProperty(ir::ClassDefinition *node, util::StringView name); + std::vector VisitStaticProperty(ir::ClassDefinition *node, + util::StringView name); void VisitPrivateProperty(ir::ClassDefinition *node); void VisitComputedProperty(ir::ClassDefinition *node); @@ -186,6 +188,9 @@ private: binder::Scope *FindEnumMemberScope(const util::StringView name) const; ir::MemberExpression *CreateMemberExpressionFromIdentifier(binder::Scope *scope, ir::Identifier *node); + void CheckTransformedAstNodes(const ir::AstNode *parent, bool *passed) const; + void CheckTransformedAstNode(const ir::AstNode *parent, ir::AstNode *childNode, bool *passed) const; + template ir::UpdateNodes VisitExportClassDeclaration(T *node); diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-if-statement-1-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-if-statement-1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..e95d8a0396b47d2979d942d22269c9336f23c1a6 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-if-statement-1-expected.txt @@ -0,0 +1,3 @@ +1 +2 +undefined diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-if-statement-1.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-if-statement-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..eef1e5a3c1ff8dc0aee66a90623c8e8f36cfdb4f --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-if-statement-1.ts @@ -0,0 +1,23 @@ +/* + * 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. + */ + + +enum E{ a = 1 } +if (1) enum E { b = 2 } +if (1) { enum E { c = 3 } } + +print(E.a) +print(E.b) +print(E.c) diff --git a/es2panda/test/parser/ts/transformed_cases/test-for-statement-1-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-for-statement-1-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..df4602bed6ba7d9da8337581cc5495afc49dbd8c --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-for-statement-1-transformed-expected.txt @@ -0,0 +1,1247 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ForUpdateStatement", + "init": { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "i", + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 15 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "Identifier", + "name": "i", + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 10, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "update": { + "type": "UpdateExpression", + "operator": "++", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "i", + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 3 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 3 + }, + "end": { + "line": 18, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "obj", + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 8 + } + } + }, + "init": { + "type": "ObjectExpression", + "properties": [ + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 22, + "column": 3 + }, + "end": { + "line": 22, + "column": 4 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 22, + "column": 3 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 23, + "column": 3 + }, + "end": { + "line": 23, + "column": 4 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 23, + "column": 3 + }, + "end": { + "line": 23, + "column": 6 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 24, + "column": 3 + }, + "end": { + "line": 24, + "column": 4 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 24, + "column": 3 + }, + "end": { + "line": 24, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 25, + "column": 2 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + { + "type": "ForInStatement", + "left": { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "j", + "loc": { + "start": { + "line": 26, + "column": 10 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 26, + "column": 10 + }, + "end": { + "line": 26, + "column": 11 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 26, + "column": 6 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + "right": { + "type": "Identifier", + "name": "obj", + "loc": { + "start": { + "line": 26, + "column": 15 + }, + "end": { + "line": 26, + "column": 18 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 27, + "column": 3 + }, + "end": { + "line": 27, + "column": 11 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 3 + }, + "end": { + "line": 27, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 28, + "column": 2 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 6 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 30, + "column": 10 + }, + "end": { + "line": 30, + "column": 11 + } + } + }, + { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 30, + "column": 13 + }, + "end": { + "line": 30, + "column": 14 + } + } + }, + { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 30, + "column": 16 + }, + "end": { + "line": 30, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 18 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 30, + "column": 1 + }, + "end": { + "line": 30, + "column": 18 + } + } + }, + { + "type": "ForOfStatement", + "await": false, + "left": { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "k", + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 31, + "column": 11 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 31, + "column": 10 + }, + "end": { + "line": 31, + "column": 11 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 31, + "column": 6 + }, + "end": { + "line": 31, + "column": 11 + } + } + }, + "right": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 31, + "column": 15 + }, + "end": { + "line": 31, + "column": 16 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 32, + "column": 3 + }, + "end": { + "line": 32, + "column": 11 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 3 + }, + "end": { + "line": 32, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 31, + "column": 18 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 1 + }, + "end": { + "line": 33, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 34, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-for-statement-1.ts b/es2panda/test/parser/ts/transformed_cases/test-for-statement-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f1857259fba0afb2bc7e16c0e1c34273f8b2d4ba --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-for-statement-1.ts @@ -0,0 +1,33 @@ +/* + * 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. + */ + + +for (let i = 1; i < 10; i++) { + enum E{} +} + +var obj = { + a:1, + b:2, + c:3 +} +for (let j in obj) { + enum E{} +} + +var a = [1, 2, 3] +for (let k of a) { + enum E{} +} diff --git a/es2panda/test/parser/ts/transformed_cases/test-for-statement-2-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-for-statement-2-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..fdedef23e9648e09c611c9e73d46cfdae8c82a67 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-for-statement-2-transformed-expected.txt @@ -0,0 +1,1247 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ForUpdateStatement", + "init": { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "i", + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 15 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "Identifier", + "name": "i", + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 10, + "loc": { + "start": { + "line": 17, + "column": 21 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "update": { + "type": "UpdateExpression", + "operator": "++", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "i", + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 25 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 39 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "obj", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 8 + } + } + }, + "init": { + "type": "ObjectExpression", + "properties": [ + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 20, + "column": 3 + }, + "end": { + "line": 20, + "column": 4 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 20, + "column": 3 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "b", + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 21, + "column": 4 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 21, + "column": 6 + } + } + }, + { + "type": "Property", + "method": false, + "shorthand": false, + "computed": false, + "key": { + "type": "Identifier", + "name": "c", + "loc": { + "start": { + "line": 22, + "column": 3 + }, + "end": { + "line": 22, + "column": 4 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 22, + "column": 3 + }, + "end": { + "line": 22, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 11 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 23, + "column": 2 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + { + "type": "ForInStatement", + "left": { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "j", + "loc": { + "start": { + "line": 24, + "column": 10 + }, + "end": { + "line": 24, + "column": 11 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 24, + "column": 10 + }, + "end": { + "line": 24, + "column": 11 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 24, + "column": 6 + }, + "end": { + "line": 24, + "column": 11 + } + } + }, + "right": { + "type": "Identifier", + "name": "obj", + "loc": { + "start": { + "line": 24, + "column": 15 + }, + "end": { + "line": 24, + "column": 18 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 24, + "column": 20 + }, + "end": { + "line": 24, + "column": 29 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 20 + }, + "end": { + "line": 24, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 1 + }, + "end": { + "line": 24, + "column": 29 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 26, + "column": 10 + }, + "end": { + "line": 26, + "column": 11 + } + } + }, + { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 26, + "column": 13 + }, + "end": { + "line": 26, + "column": 14 + } + } + }, + { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 26, + "column": 16 + }, + "end": { + "line": 26, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 26, + "column": 9 + }, + "end": { + "line": 26, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 18 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 26, + "column": 1 + }, + "end": { + "line": 26, + "column": 18 + } + } + }, + { + "type": "ForOfStatement", + "await": false, + "left": { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "k", + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 27, + "column": 11 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 27, + "column": 11 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 27, + "column": 6 + }, + "end": { + "line": 27, + "column": 11 + } + } + }, + "right": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 27, + "column": 15 + }, + "end": { + "line": 27, + "column": 16 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E3", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 27, + "column": 18 + }, + "end": { + "line": 27, + "column": 27 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E3", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E3", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E3", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 18 + }, + "end": { + "line": 27, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 1 + }, + "end": { + "line": 27, + "column": 27 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 28, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-for-statement-2.ts b/es2panda/test/parser/ts/transformed_cases/test-for-statement-2.ts new file mode 100644 index 0000000000000000000000000000000000000000..205b9363f3a9a8bb02635c7558a38e925b16b7a8 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-for-statement-2.ts @@ -0,0 +1,27 @@ +/* + * 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. + */ + + +for (let i = 1; i < 10; i++) enum E1{} + +var obj = { + a:1, + b:2, + c:3 +} +for (let j in obj) enum E2{} + +var a = [1, 2, 3] +for (let k of a) enum E3{} diff --git a/es2panda/test/parser/ts/transformed_cases/test-if-statement-1-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-if-statement-1-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3cda9ccc4b33646cc81510d5004ec8ae14c506ff --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-if-statement-1-transformed-expected.txt @@ -0,0 +1,479 @@ +{ + "type": "Program", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 3 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 3 + }, + "end": { + "line": 18, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "alternate": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 3 + }, + "end": { + "line": 20, + "column": 12 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 3 + }, + "end": { + "line": 20, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-if-statement-1.ts b/es2panda/test/parser/ts/transformed_cases/test-if-statement-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..fa510d6e77273c8d6ee89ddc796df23c5c4cdbb6 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-if-statement-1.ts @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +if (1) { + enum E1{} +} else { + enum E2{} +} diff --git a/es2panda/test/parser/ts/transformed_cases/test-if-statement-2-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-if-statement-2-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..383773d333ec5f7af90278545d5396c5f0c95f37 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-if-statement-2-transformed-expected.txt @@ -0,0 +1,479 @@ +{ + "type": "Program", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "alternate": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E2", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 19, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-if-statement-2.ts b/es2panda/test/parser/ts/transformed_cases/test-if-statement-2.ts new file mode 100644 index 0000000000000000000000000000000000000000..11438eb2770608e00cc8b6086d65f7fd17eb8bf0 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-if-statement-2.ts @@ -0,0 +1,18 @@ +/* + * 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. + */ + + +if (1) enum E1{} +else enum E2{} diff --git a/es2panda/test/parser/ts/transformed_cases/test-if-statement-3-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-if-statement-3-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3d1e3801966e2c0aad8fd7cb29fa3bc2984386bb --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-if-statement-3-transformed-expected.txt @@ -0,0 +1,435 @@ +{ + "type": "Program", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 17 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "alternate": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E1", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 19, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-if-statement-3.ts b/es2panda/test/parser/ts/transformed_cases/test-if-statement-3.ts new file mode 100644 index 0000000000000000000000000000000000000000..a68bd3566702791c5bd03edad604e0bfb9f503f1 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-if-statement-3.ts @@ -0,0 +1,18 @@ +/* + * 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. + */ + + +if (1) enum E1{} +else enum E1{} diff --git a/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a7cf5138fd2f8e67830c6659e1a2b15510ecae62 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1-transformed-expected.txt @@ -0,0 +1,270 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ExportDefaultDeclaration", + "declaration": { + "type": "ClassDeclaration", + "definition": { + "id": null, + "superClass": null, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 17, + "column": 30 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 32 + }, + "end": { + "line": 17, + "column": 33 + } + } + }, + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 23 + }, + "end": { + "line": 17, + "column": 33 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "*default*", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "property": { + "type": "StringLiteral", + "value": "a", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 32 + }, + "end": { + "line": 17, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 18, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1.ts b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5b38fd7c2f0842d561a623ef189fe9792622c6af --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + + +export default class {static a=1} diff --git a/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..74a6bd40cea30f01d8731e243efeb20f68be18c2 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2-transformed-expected.txt @@ -0,0 +1,285 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ExportNamedDeclaration", + "declaration": { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "superClass": null, + "implements": [], + "constructor": { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 27 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + "source": null, + "specifiers": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 28 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "C", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "property": { + "type": "StringLiteral", + "value": "a", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 17, + "column": 28 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 18, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2.ts b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a7abbf52c867a6058e77185e6679758cd387563 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + + +export class C {static a=1} diff --git a/es2panda/test/parser/ts/transformed_cases/test-while-statement-1-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-while-statement-1-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..deb43f83f8cc2238f53f9c912c858914cea77f34 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-while-statement-1-transformed-expected.txt @@ -0,0 +1,649 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 10 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + { + "type": "WhileStatement", + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "UpdateExpression", + "operator": "++", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 10, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + { + "type": "DoWhileStatement", + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 13 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 4 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "UpdateExpression", + "operator": "++", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 25, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 25, + "column": 13 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 20, + "loc": { + "start": { + "line": 25, + "column": 16 + }, + "end": { + "line": 25, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 25, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 25, + "column": 19 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 26, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-while-statement-1.ts b/es2panda/test/parser/ts/transformed_cases/test-while-statement-1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d4834c8d589efd1f23c275da555933dabbf81cca --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-while-statement-1.ts @@ -0,0 +1,25 @@ +/* + * 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 = 0; + +while (a++ < 10) { + enum E{} +} + +do { + enum E{} +} while (a++ < 20) diff --git a/es2panda/test/parser/ts/transformed_cases/test-while-statement-2-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-while-statement-2-transformed-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..bf82bd2fbce75065a3d94bb645231e3346d73987 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-while-statement-2-transformed-expected.txt @@ -0,0 +1,649 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 10 + } + } + } + ], + "kind": "var", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + { + "type": "WhileStatement", + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "UpdateExpression", + "operator": "++", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 10, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 26 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + { + "type": "DoWhileStatement", + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 4 + }, + "end": { + "line": 21, + "column": 12 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "arguments": [ + { + "type": "LogicalExpression", + "operator": "||", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "E", + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "right": { + "type": "ObjectExpression", + "properties": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 4 + }, + "end": { + "line": 21, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "UpdateExpression", + "operator": "++", + "prefix": false, + "argument": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 20, + "loc": { + "start": { + "line": 21, + "column": 27 + }, + "end": { + "line": 21, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 30 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 1 + } + } +} +Transformed AST structure check passed. diff --git a/es2panda/test/parser/ts/transformed_cases/test-while-statement-2.ts b/es2panda/test/parser/ts/transformed_cases/test-while-statement-2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b87e5cb091ef177c03e9d43bd4895f6d8cb9ab89 --- /dev/null +++ b/es2panda/test/parser/ts/transformed_cases/test-while-statement-2.ts @@ -0,0 +1,21 @@ +/* + * 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 = 0; + +while (a++ < 10) enum E{} + +do enum E{} while (a++ < 20) diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index 55406d461064c90d2870cb48398715e2f8f2823d..836804a9054acda1a16f66346fffde3515fa0896 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -153,7 +153,7 @@ class Test: return "%s-expected.txt" % (self.path[:self.path.find(".d.ts")]) def run(self, runner): - cmd = runner.cmd_prefix + [runner.es2panda, "--dump-ast"] + cmd = runner.cmd_prefix + [runner.es2panda] cmd.extend(self.flags) cmd.append(self.path) @@ -163,7 +163,7 @@ class Test: out, err = process.communicate() self.output = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") - expected_path = self.get_path_to_expected(); + expected_path = self.get_path_to_expected() try: with open(expected_path, 'r') as fp: expected = fp.read() @@ -845,6 +845,54 @@ class TSDeclarationTest(Test): return "%s-expected.txt" % file_name +class TransformerRunner(Runner): + def __init__(self, args): + Runner.__init__(self, args, "Transformer") + + def add_directory(self, directory, extension, flags): + glob_expression = path.join( + self.test_root, directory, "**/*.%s" % (extension)) + files = glob(glob_expression, recursive=True) + files = fnmatch.filter(files, self.test_root + '**' + self.args.filter) + + self.tests += list(map(lambda f: TransformerTest(f, flags), files)) + + def test_path(self, src): + return src + + +class TransformerTest(Test): + def __init__(self, test_path, flags): + Test.__init__(self, test_path, flags) + + def get_path_to_expected(self): + return "%s-transformed-expected.txt" % (path.splitext(self.path)[0]) + + def run(self, runner): + cmd = runner.cmd_prefix + [runner.es2panda] + cmd.extend(self.flags) + cmd.append(self.path) + + self.log_cmd(cmd) + process = subprocess.Popen( + cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = process.communicate() + self.output = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") + + expected_path = self.get_path_to_expected() + try: + with open(expected_path, 'r') as fp: + expected = fp.read() + self.passed = expected == self.output and process.returncode in [0, 1] + except Exception: + self.passed = False + + if not self.passed: + self.error = err.decode("utf-8", errors="ignore") + + return self + + def main(): args = get_args() @@ -852,16 +900,24 @@ def main(): if args.regression: runner = RegressionRunner(args) - runner.add_directory("parser/concurrent", "js", ["--module"]) - runner.add_directory("parser/js", "js", ["--parse-only"]) + runner.add_directory("parser/concurrent", "js", ["--module", "--dump-ast"]) + runner.add_directory("parser/js", "js", ["--parse-only", "--dump-ast"]) runner.add_directory("parser/ts", "ts", - ["--parse-only", "--module", "--extension=ts"]) + ["--parse-only", "--module", "--extension=ts", "--dump-ast"]) runner.add_directory("parser/ts/type_checker", "ts", - ["--parse-only", "--enable-type-check", "--module", "--extension=ts"]) + ["--parse-only", "--enable-type-check", "--module", "--extension=ts", "--dump-ast"]) runner.add_directory("parser/ts/cases/declaration", "d.ts", - ["--parse-only", "--module", "--extension=ts"], TSDeclarationTest) + ["--parse-only", "--module", "--extension=ts", "--dump-ast"], TSDeclarationTest) + runners.append(runner) + transformer_runner = TransformerRunner(args) + transformer_runner.add_directory("parser/ts/transformed_cases", "ts", + ["--parse-only", "--module", "--extension=ts", "--dump-transformed-ast", + "--check-transformed-ast-structure"]) + + runners.append(transformer_runner) + if args.test262: runners.append(Test262Runner(args))