diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index f7dd8b5d568c34e585e8d7b820d76083cd50e2c1..8289504ecc1549603a81f67be75deadf063666ec 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -412,7 +412,9 @@ void Binder::BuildClassDefinition(ir::ClassDefinition *classDef) ASSERT(!className.Empty()); ScopeFindResult res = scope_->Find(className); - ASSERT(res.variable && res.variable->Declaration()->IsClassDecl()); + ASSERT(res.variable && (res.variable->Declaration()->IsClassDecl() || + (res.variable->Declaration()->IsFunctionDecl() && + res.variable->Declaration()->AsFunctionDecl()->GetDeclClass() != nullptr))); res.variable->AddFlag(VariableFlags::INITIALIZED); } diff --git a/es2panda/binder/declaration.h b/es2panda/binder/declaration.h index e1b1ad05b7f8cce483008c5db33a80e0d8a904fc..88e468b475ffff081b3fd896b5f442aa9f63bb65 100644 --- a/es2panda/binder/declaration.h +++ b/es2panda/binder/declaration.h @@ -193,20 +193,6 @@ public: } }; -class FunctionDecl : public MultiDecl { -public: - explicit FunctionDecl(ArenaAllocator *allocator, util::StringView name, const ir::AstNode *node) - : MultiDecl(allocator, name) - { - node_ = node; - } - - DeclType Type() const override - { - return DeclType::FUNC; - } -}; - class TypeParameterDecl : public Decl { public: explicit TypeParameterDecl(util::StringView name, const ir::AstNode *node); @@ -302,12 +288,47 @@ public: class ClassDecl : public Decl { public: - explicit ClassDecl(util::StringView name) : Decl(name) {} + explicit ClassDecl(util::StringView name, bool declare) : Decl(name), declare_(declare) {} DeclType Type() const override { return DeclType::CLASS; } + + bool IsDeclare() const + { + return declare_; + } + +private: + bool declare_; +}; + +class FunctionDecl : public MultiDecl { +public: + explicit FunctionDecl(ArenaAllocator *allocator, util::StringView name, const ir::AstNode *node) + : MultiDecl(allocator, name) + { + node_ = node; + } + + DeclType Type() const override + { + return DeclType::FUNC; + } + + void SetDeclClass(ClassDecl *declClass) + { + declClass_ = declClass; + } + + ClassDecl *GetDeclClass() + { + return declClass_; + } + +private: + ClassDecl *declClass_ {nullptr}; }; class ParameterDecl : public Decl { diff --git a/es2panda/binder/scope.cpp b/es2panda/binder/scope.cpp index 17ba3df8e8ed2ce5b02a849ec650d44d2af3835e..73cc00a70603c26367e876963ac72850c03a7939 100644 --- a/es2panda/binder/scope.cpp +++ b/es2panda/binder/scope.cpp @@ -301,6 +301,9 @@ bool FunctionScope::AddBinding(ArenaAllocator *allocator, Variable *currentVaria case DeclType::FUNC: { return AddFunction(allocator, currentVariable, newDecl, extension); } + case DeclType::CLASS: { + return AddClass(allocator, currentVariable, newDecl); + } case DeclType::ENUM_LITERAL: { return AddTSBinding(allocator, newDecl, VariableFlags::ENUM_LITERAL); } @@ -336,6 +339,9 @@ bool GlobalScope::AddBinding(ArenaAllocator *allocator, Variable *currentVariabl case DeclType::FUNC: { return AddFunction(allocator, currentVariable, newDecl, extension); } + case DeclType::CLASS: { + return AddClass(allocator, currentVariable, newDecl); + } case DeclType::ENUM_LITERAL: { return AddTSBinding(allocator, newDecl, VariableFlags::ENUM_LITERAL); } @@ -397,12 +403,20 @@ bool ModuleScope::AddBinding(ArenaAllocator *allocator, Variable *currentVariabl } case DeclType::FUNC: { if (currentVariable) { - return false; + auto decl = currentVariable->Declaration(); + if (!decl->IsClassDecl() || !decl->AsClassDecl()->IsDeclare()) { + return false; + } } return newDecl->IsImportOrExportDecl() ? AddFunction(allocator, currentVariable, newDecl, extension) : AddFunction(allocator, currentVariable, newDecl, extension); } + case DeclType::CLASS: { + return newDecl->IsImportOrExportDecl() ? + AddClass(allocator, currentVariable, newDecl) : + AddClass(allocator, currentVariable, newDecl); + } case DeclType::ENUM_LITERAL: { return AddTSBinding(allocator, newDecl, VariableFlags::ENUM_LITERAL); } diff --git a/es2panda/binder/scope.h b/es2panda/binder/scope.h index d2bc3419d4204e9dc01a91e78d572a9b703dbe6f..1790828f8f099c160e31af5db9f3d0b3d0c75513 100644 --- a/es2panda/binder/scope.h +++ b/es2panda/binder/scope.h @@ -428,6 +428,9 @@ protected: bool AddFunction(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl, [[maybe_unused]] ScriptExtension extension); + template + bool AddClass(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl); + template bool AddTSBinding(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl, VariableFlags flags); @@ -900,6 +903,13 @@ bool VariableScope::AddFunction(ArenaAllocator *allocator, Variable *currentVari return true; } + auto decl = currentVariable->Declaration(); + if (decl->IsClassDecl() && decl->AsClassDecl()->IsDeclare()) { + newDecl->AsFunctionDecl()->SetDeclClass(decl->AsClassDecl()); + bindings_[newDecl->Name()] = allocator->New(newDecl, flags); + return true; + } + if (extension != ScriptExtension::JS) { return false; } @@ -918,6 +928,27 @@ bool VariableScope::AddFunction(ArenaAllocator *allocator, Variable *currentVari return true; } +template +bool VariableScope::AddClass(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl) +{ + ASSERT(newDecl->IsClassDecl()); + + VariableFlags flags = DeclFlagToVariableFlag(newDecl->Flags()); + + if (!currentVariable) { + bindings_.insert({newDecl->Name(), allocator->New(newDecl, flags)}); + return true; + } + + auto decl = currentVariable->Declaration(); + if (newDecl->AsClassDecl()->IsDeclare() && decl->IsFunctionDecl()) { + decl->AsFunctionDecl()->SetDeclClass(newDecl->AsClassDecl()); + return true; + } + + return false; +} + template bool VariableScope::AddTSBinding(ArenaAllocator *allocator, [[maybe_unused]] Variable *currentVariable, Decl *newDecl, VariableFlags flags) diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index 89c321bb548d7f6e676a6e1bfb50ee4710f9a432..e717e94b81e103325900826eb2ed96a42830fff5 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -157,7 +157,7 @@ static void CompileFunction(PandaGen *pg) const auto *decl = pg->RootNode()->AsScriptFunction(); // TODO(szilagyia): move after super call - if (decl->IsConstructor()) { + if (decl->IsConstructor() && pg->Binder()->Program()->Extension() != ScriptExtension::TS) { CompileInstanceFields(pg, decl); } diff --git a/es2panda/ir/statements/blockStatement.cpp b/es2panda/ir/statements/blockStatement.cpp index d27cc8840e736a7cace093e14ecf204d225481d2..873b41c754424f1ebb88232839adbf84e1b17a90 100644 --- a/es2panda/ir/statements/blockStatement.cpp +++ b/es2panda/ir/statements/blockStatement.cpp @@ -89,9 +89,10 @@ void BlockStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) } } -void BlockStatement::AddStatementInFront(Statement *statement) +void BlockStatement::AddStatementAtPos(size_t insertPos, Statement *statement) { - statements_.insert(statements_.begin(), statement); + ASSERT(insertPos <= statements_.size()); + statements_.insert(statements_.begin() + insertPos, statement); } } // namespace panda::es2panda::ir diff --git a/es2panda/ir/statements/blockStatement.h b/es2panda/ir/statements/blockStatement.h index c9225bfdac9b86dd4a8b437b8cdfc4e6f18662c3..8b4a9c21effb72199de73237307da3cb36d0875b 100644 --- a/es2panda/ir/statements/blockStatement.h +++ b/es2panda/ir/statements/blockStatement.h @@ -50,7 +50,7 @@ public: return statements_; } - void AddStatementInFront(Statement *statement); + void AddStatementAtPos(size_t insertPos, Statement *statement); void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; diff --git a/es2panda/lexer/token/tokenType.h b/es2panda/lexer/token/tokenType.h index 29ddcfe28d6f2e39002f3b8ecca4df17ef8d9293..1c076bf81b82ce05a705afcc5aac829580a6a89a 100644 --- a/es2panda/lexer/token/tokenType.h +++ b/es2panda/lexer/token/tokenType.h @@ -159,12 +159,12 @@ enum class TokenType { KEYW_UNIQUE, KEYW_MODULE, KEYW_NAMESPACE, + KEYW_GLOBAL, KEYW_INFER, KEYW_DECLARE, KEYW_ARGUMENTS, KEYW_EVAL, KEYW_STATIC, - KEYW_GLOBAL, KEYW_IS, KEYW_ASSERTS, diff --git a/es2panda/parser/expressionParser.cpp b/es2panda/parser/expressionParser.cpp index f1bb14994b7ffd34ee1bf657a2a4fe1287a14ec6..169bbd492a38e13305ada834851375b07e613593 100644 --- a/es2panda/parser/expressionParser.cpp +++ b/es2panda/parser/expressionParser.cpp @@ -1061,7 +1061,7 @@ ir::Expression *ParserImpl::ParsePrimaryExpression(ExpressionParseFlags flags) return ParseCoverParenthesizedExpressionAndArrowParameterList(); } case lexer::TokenType::PUNCTUATOR_LEFT_BRACE: { - return ParseObjectExpression(CarryPatternFlags(flags)); + return ParseObjectExpression(CarryAllowTsParamAndPatternFlags(flags)); } case lexer::TokenType::KEYW_FUNCTION: { return ParseFunctionExpression(); diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index a245d55f623237c7552bbaa1efe1f2d3e0ac0393..ddedd5cfece97fec07fe0bd61f897933d3f9cbb6 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -250,6 +250,13 @@ ExpressionParseFlags ParserImpl::CarryPatternFlags(ExpressionParseFlags flags) ExpressionParseFlags::OBJECT_PATTERN); } +ExpressionParseFlags ParserImpl::CarryAllowTsParamAndPatternFlags(ExpressionParseFlags flags) +{ + return CarryExpressionParserFlag(flags, ExpressionParseFlags::ALLOW_TS_PARAM_TOKEN | + ExpressionParseFlags::POTENTIALLY_IN_PATTERN | + ExpressionParseFlags::OBJECT_PATTERN); +} + bool ParserImpl::CurrentLiteralIsBasicType() { ASSERT(lexer_->GetToken().Type() == lexer::TokenType::LITERAL_IDENT || @@ -312,13 +319,13 @@ ir::TSTypeReference *ParserImpl::ParseTsConstExpression() return typeReference; } -ir::Expression *ParserImpl::ParseTsIdentifierReference() +ir::Expression *ParserImpl::ParseTsIdentifierReference(TypeAnnotationParsingOptions options) { if (CurrentLiteralIsBasicType() && lexer_->Lookahead() != LEX_CHAR_DOT) { return ParseTsBasicType(); } - return ParseTsTypeReferenceOrQuery(); + return ParseTsTypeReferenceOrQuery(options, false); } bool ParserImpl::IsStartOfMappedType() const @@ -426,7 +433,7 @@ ir::Expression *ParserImpl::ParseTsTypeLiteralOrTsMappedType(ir::Expression *typ } ir::Expression *ParserImpl::ParseTsTypeReferenceOrTsTypePredicate(ir::Expression *typeAnnotation, - bool canBeTsTypePredicate) + bool canBeTsTypePredicate, bool throwError) { if (typeAnnotation) { return nullptr; @@ -436,7 +443,7 @@ ir::Expression *ParserImpl::ParseTsTypeReferenceOrTsTypePredicate(ir::Expression return ParseTsTypePredicate(); } - return ParseTsTypeOperatorOrTypeReference(); + return ParseTsTypeOperatorOrTypeReference(throwError); } ir::Expression *ParserImpl::ParseTsThisTypeOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate, @@ -542,7 +549,7 @@ ir::Expression *ParserImpl::ParseTsTypeAnnotationElement(ir::Expression *typeAnn return ParseTsArrayType(typeAnnotation); } - return ParseTsIndexAccessType(typeAnnotation); + return ParseTsIndexAccessType(typeAnnotation, *options & TypeAnnotationParsingOptions::THROW_ERROR); } return ParseTsTupleType(); @@ -568,7 +575,7 @@ ir::Expression *ParserImpl::ParseTsTypeAnnotationElement(ir::Expression *typeAnn break; } - return ParseTsTypeReferenceOrQuery(true); + return ParseTsTypeReferenceOrQuery(*options, true); } case lexer::TokenType::KEYW_IMPORT: { if (typeAnnotation) { @@ -593,7 +600,8 @@ ir::Expression *ParserImpl::ParseTsTypeAnnotationElement(ir::Expression *typeAnn } return ParseTsTypeReferenceOrTsTypePredicate( - typeAnnotation, *options & TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE); + typeAnnotation, *options & TypeAnnotationParsingOptions::CAN_BE_TS_TYPE_PREDICATE, + *options & TypeAnnotationParsingOptions::THROW_ERROR); } case lexer::TokenType::KEYW_EXTENDS: { if (*options & (TypeAnnotationParsingOptions::IN_UNION | TypeAnnotationParsingOptions::IN_INTERSECTION)) { @@ -601,7 +609,7 @@ ir::Expression *ParserImpl::ParseTsTypeAnnotationElement(ir::Expression *typeAnn } if (!typeAnnotation) { - return ParseTsIdentifierReference(); + return ParseTsIdentifierReference(*options); } return ParseTsConditionalType(typeAnnotation, *options & TypeAnnotationParsingOptions::RESTRICT_EXTENDS); @@ -773,9 +781,10 @@ ir::Expression *ParserImpl::ParseTsTypeAnnotation(TypeAnnotationParsingOptions * return typeAnnotation; } -ir::Expression *ParserImpl::ParseTsTypeOperatorOrTypeReference() +ir::Expression *ParserImpl::ParseTsTypeOperatorOrTypeReference(bool throwError) { - TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::THROW_ERROR; + TypeAnnotationParsingOptions options = throwError ? + TypeAnnotationParsingOptions::THROW_ERROR : TypeAnnotationParsingOptions::NO_OPTS; if (lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_READONLY) { lexer::SourcePosition typeOperatorStart = lexer_->GetToken().Start(); @@ -846,7 +855,7 @@ ir::Expression *ParserImpl::ParseTsTypeOperatorOrTypeReference() return inferType; } - return ParseTsIdentifierReference(); + return ParseTsIdentifierReference(options); } bool ParserImpl::IsTSNamedTupleMember() @@ -1007,7 +1016,7 @@ ir::Expression *ParserImpl::ParseTsQualifiedReference(ir::Expression *typeName) return typeName; } -ir::Expression *ParserImpl::ParseTsIndexAccessType(ir::Expression *typeName) +ir::Expression *ParserImpl::ParseTsIndexAccessType(ir::Expression *typeName, bool throwError) { TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::THROW_ERROR; @@ -1017,7 +1026,10 @@ ir::Expression *ParserImpl::ParseTsIndexAccessType(ir::Expression *typeName) ir::Expression *indexType = ParseTsTypeAnnotation(&options); if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_RIGHT_SQUARE_BRACKET) { - ThrowSyntaxError("']' exprected"); + if (!throwError) { + return nullptr; + } + ThrowSyntaxError("']' expected"); } lexer_->NextToken(); // eat ']' @@ -1030,7 +1042,7 @@ ir::Expression *ParserImpl::ParseTsIndexAccessType(ir::Expression *typeName) return typeName; } -ir::Expression *ParserImpl::ParseTsTypeReferenceOrQuery(bool parseQuery) +ir::Expression *ParserImpl::ParseTsTypeReferenceOrQuery(TypeAnnotationParsingOptions options, bool parseQuery) { lexer::SourcePosition referenceStartLoc = lexer_->GetToken().Start(); @@ -1084,7 +1096,7 @@ ir::Expression *ParserImpl::ParseTsTypeReferenceOrQuery(bool parseQuery) typeName->SetRange({referenceStartLoc, lexer_->GetToken().End()}); - return ParseTsIndexAccessType(typeName); + return ParseTsIndexAccessType(typeName, options & TypeAnnotationParsingOptions::THROW_ERROR); } ir::Expression *returnNode = nullptr; @@ -1746,6 +1758,9 @@ ir::Expression *ParserImpl::ParseTsParenthesizedOrFunctionType(ir::Expression *t if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS && lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_LESS_THAN) { + if (!throwError) { + return nullptr; + } ThrowSyntaxError("'(' expected"); } } @@ -2109,7 +2124,7 @@ void ParserImpl::ParseClassKeyModifiers(ClassElmentDescriptor *desc) if ((Extension() == ScriptExtension::JS && nextCp != LEX_CHAR_LEFT_PAREN) || (Extension() == ScriptExtension::TS && nextCp != LEX_CHAR_EQUALS && nextCp != LEX_CHAR_SEMICOLON && - nextCp != LEX_CHAR_LEFT_PAREN)) { + nextCp != LEX_CHAR_LEFT_PAREN && nextCp != LEX_CHAR_LESS_THAN)) { if (lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_GET) { if (desc->isPrivateIdent) { ThrowSyntaxError("Private identifier can not be getter"); @@ -2258,11 +2273,10 @@ ir::Expression *ParserImpl::ParseClassKey(ClassElmentDescriptor *desc, bool isDe propName = ParseExpression(ExpressionParseFlags::ACCEPT_COMMA); if (Extension() == ScriptExtension::TS) { + // TODO(songqi): Determine whether MemberExpression is a symbol during type check. desc->invalidComputedProperty = !propName->IsNumberLiteral() && !propName->IsStringLiteral() && - !(propName->IsMemberExpression() && propName->AsMemberExpression()->Object()->IsIdentifier() && - propName->AsMemberExpression()->Object()->AsIdentifier()->Name().Is("Symbol")) && - !propName->IsIdentifier(); + !propName->IsMemberExpression() && !propName->IsIdentifier(); } if (lexer_->GetToken().Type() != lexer::TokenType::PUNCTUATOR_RIGHT_SQUARE_BRACKET) { @@ -2895,7 +2909,7 @@ ir::ClassDefinition *ParserImpl::ParseClassDefinition(bool isDeclaration, bool i if (!isDeclare && !isCtorContinuousDefined) { ThrowSyntaxError("Constructor implementation is missing.", property->Start()); } - + if (hasConstructorFuncBody) { ThrowSyntaxError("Multiple constructor implementations are not allowed.", property->Start()); } @@ -3030,7 +3044,7 @@ ir::TSEnumDeclaration *ParserImpl::ParseEnumDeclaration(bool isExport, bool isDe auto enumCtx = binder::LexicalScope(Binder(), enumMemberBindings); auto *enumDeclaration = ParseEnumMembers(key, enumStart, isExport, isDeclare, isConst); res->Declaration()->AsEnumLiteralDecl()->Add(enumDeclaration); - + return enumDeclaration; } diff --git a/es2panda/parser/parserImpl.h b/es2panda/parser/parserImpl.h index cd12f818eb074aad483505785dba4c48508b877b..f69f00a76952403300138752c621dd318f503e55 100644 --- a/es2panda/parser/parserImpl.h +++ b/es2panda/parser/parserImpl.h @@ -231,6 +231,7 @@ private: bool CheckTopStatementsForRequiredDeclare(const ArenaVector &statements); static ExpressionParseFlags CarryExpressionParserFlag(ExpressionParseFlags origin, ExpressionParseFlags carry); static ExpressionParseFlags CarryPatternFlags(ExpressionParseFlags flags); + static ExpressionParseFlags CarryAllowTsParamAndPatternFlags(ExpressionParseFlags flags); bool CurrentIsBasicType(); bool CurrentLiteralIsBasicType(); static bool CheckTypeNameIsReserved(const util::StringView ¶mName); @@ -238,8 +239,8 @@ private: static bool IsMemberExpressionsAreSame(const ir::MemberExpression *mExp1, const ir::MemberExpression *mExp2); static bool IsMethodDefinitionsAreSame(const ir::MethodDefinition *property, ir::MethodDefinition *overload); ir::TSTypeReference *ParseTsConstExpression(); - ir::Expression *ParseTsTypeOperatorOrTypeReference(); - ir::Expression *ParseTsIdentifierReference(); + ir::Expression *ParseTsTypeOperatorOrTypeReference(bool throwError); + ir::Expression *ParseTsIdentifierReference(TypeAnnotationParsingOptions options); ir::Expression *ParseTsBasicType(); ir::TSIntersectionType *ParseTsIntersectionType(ir::Expression *type, bool inUnion, bool restrictExtends); ir::TSUnionType *ParseTsUnionType(ir::Expression *type, bool restrictExtends); @@ -257,9 +258,9 @@ private: ir::Expression *ParseTsTypeLiteralOrInterfaceMember(); ArenaVector ParseTsTypeLiteralOrInterface(); ir::Expression *ParseTsThisType(bool throwError); - ir::Expression *ParseTsIndexAccessType(ir::Expression *typeName); + ir::Expression *ParseTsIndexAccessType(ir::Expression *typeName, bool throwError); ir::Expression *ParseTsQualifiedReference(ir::Expression *typeName); - ir::Expression *ParseTsTypeReferenceOrQuery(bool parseQuery = false); + ir::Expression *ParseTsTypeReferenceOrQuery(TypeAnnotationParsingOptions options, bool parseQuery = false); bool IsTSNamedTupleMember(); void HandleRestType(ir::AstNodeType elementType, bool *hasRestType) const; ir::Expression *ParseTsTupleElement(ir::TSTupleKind *kind, bool *seenOptional, bool *hasRestType); @@ -267,7 +268,8 @@ private: ir::TSImportType *ParseTsImportType(const lexer::SourcePosition &startLoc, bool isTypeof = false); ir::Expression *ParseTsTypeAnnotation(TypeAnnotationParsingOptions *options); ir::Expression *ParseTsTypeLiteralOrTsMappedType(ir::Expression *typeAnnotation); - ir::Expression *ParseTsTypeReferenceOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate); + ir::Expression *ParseTsTypeReferenceOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate, + bool throwError); ir::Expression *ParseTsThisTypeOrTsTypePredicate(ir::Expression *typeAnnotation, bool canBeTsTypePredicate, bool throwError); ir::Expression *ParseTsTemplateLiteralType(); diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index e8f67909e36dec0a196f52e9ea5f1b01461ae326..06f2768c126421be6d0e019a3bf504ea803173f8 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -663,7 +663,7 @@ ir::ClassDeclaration *ParserImpl::ParseClassDeclaration(bool idRequired, ArenaVe ASSERT(!className.Empty()); binder::DeclarationFlags flag = isExported ? binder::DeclarationFlags::EXPORT : binder::DeclarationFlags::NONE; - auto *decl = Binder()->AddDecl(location, flag, className); + auto *decl = Binder()->AddDecl(location, flag, className, classDefinition->Declare()); decl->BindNode(classDefinition); @@ -1170,13 +1170,13 @@ ir::FunctionDeclaration *ParserImpl::ParseFunctionDeclaration(bool canBeAnonymou if (Extension() == ScriptExtension::TS) { const auto &bindings = Binder()->GetScope()->Bindings(); auto res = bindings.find(ident); + binder::Decl *currentDecl = res == bindings.end() ? nullptr : res->second->Declaration(); binder::FunctionDecl *decl {}; - if (res == bindings.end()) { + if (res == bindings.end() || + (currentDecl->IsClassDecl() && currentDecl->AsClassDecl()->IsDeclare())) { decl = Binder()->AddDecl(identNode->Start(), declflag, Allocator(), ident, func); } else { - binder::Decl *currentDecl = res->second->Declaration(); - if (!currentDecl->IsFunctionDecl()) { Binder()->ThrowRedeclaration(startLoc, currentDecl->Name()); } diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index fbf2812a2eb7e23a67665a8543476e98a099bcff..bcb98930eb11b6a8d6a9eaeae94c91eb7e57de5c 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -36,6 +36,7 @@ #include "ir/expressions/memberExpression.h" #include "ir/expressions/objectExpression.h" #include "ir/expressions/sequenceExpression.h" +#include "ir/expressions/superExpression.h" #include "ir/expressions/templateLiteral.h" #include "ir/expressions/thisExpression.h" #include "ir/module/exportDefaultDeclaration.h" @@ -134,19 +135,13 @@ void Transformer::PushVariablesToNearestStatements(ir::BlockStatement *ast) auto *scope = it.second; if (scope == nullptr) { auto scopeCtx = binder::LexicalScope::Enter(Binder(), ast->Scope()); - ast->AddStatementInFront(CreateVariableDeclarationWithIdentify(it.first, VariableParsingFlags::VAR, + ast->AddStatementAtPos(0, CreateVariableDeclarationWithIdentify(it.first, VariableParsingFlags::VAR, nullptr, false)); - } else if (scope->IsFunctionScope()) { + } else if (scope->IsFunctionScope() || scope->IsTSModuleScope()) { auto *body = scope->Node()->AsScriptFunction()->Body(); ASSERT(body->IsBlockStatement()); auto scopeCtx = binder::LexicalScope::Enter(Binder(), scope); - body->AsBlockStatement()->AddStatementInFront(CreateVariableDeclarationWithIdentify(it.first, - VariableParsingFlags::VAR, nullptr, false)); - } else if (scope->IsTSModuleScope()) { - auto *body = scope->Node()->AsTSModuleDeclaration()->Body(); - ASSERT(body->IsTSModuleBlock()); - auto scopeCtx = binder::LexicalScope::Enter(Binder(), scope); - body->AsTSModuleBlock()->AddStatementInFront(CreateVariableDeclarationWithIdentify(it.first, + body->AsBlockStatement()->AddStatementAtPos(0, CreateVariableDeclarationWithIdentify(it.first, VariableParsingFlags::VAR, nullptr, false)); } } @@ -160,7 +155,7 @@ binder::Scope *Transformer::FindExportVariableInTsModuleScope(util::StringView n binder::Variable *v = currentScope->FindLocal(name, binder::ResolveBindingOptions::ALL); bool isTSModuleScope = currentScope->IsTSModuleScope(); if (v != nullptr) { - if (!v->HasFlag(binder::VariableFlags::VAR)) { + if (!v->Declaration()->IsVarDecl() && !v->Declaration()->IsLetDecl() && !v->Declaration()->IsConstDecl()) { break; } if (isTSModuleScope && currentScope->AsTSModuleScope()->FindExportVariable(name)) { @@ -304,7 +299,6 @@ ir::UpdateNodes Transformer::VisitTSNode(ir::AstNode *childNode) auto *node = childNode->AsClassDefinition(); VisitPrivateProperty(node); VisitComputedProperty(node); - VisitTSParameterProperty(node); auto res = VisitTSNodes(childNode); SetOriginalNode(res, childNode); return res; @@ -402,9 +396,13 @@ ir::UpdateNodes Transformer::VisitClassExpression(ir::ClassExpression *node) * ##var_1.a = 1, * ##var_1) */ + auto instanceComputedProperty = VisitInstanceProperty(node->Definition()); + + VisitTSParameterProperty(node->Definition()); + auto varName = CreateNewVariable(false); auto staticProperty = VisitStaticProperty(node->Definition(), varName); - if (staticProperty.empty()) { + if (instanceComputedProperty.empty() && staticProperty.empty()) { return node; } AddVariableToNearestStatements(varName); @@ -413,9 +411,14 @@ ir::UpdateNodes Transformer::VisitClassExpression(ir::ClassExpression *node) node->AsExpression(), lexer::TokenType::PUNCTUATOR_SUBSTITUTION); ArenaVector sequence(Allocator()->Adapter()); sequence.push_back(assignment); + + for (auto *it : instanceComputedProperty) { + sequence.push_back(it->GetExpression()); + } for (auto *it : staticProperty) { sequence.push_back(it->GetExpression()); } + sequence.push_back(CreateReferenceIdentifier(varName)); return AllocNode(std::move(sequence)); } @@ -430,21 +433,24 @@ void Transformer::VisitComputedProperty(ir::ClassDefinition *node) * @f * [a](){} * static [b] = 1 + * [c] = 1 * } * * To: * var ##var_1; * var ##var_2; + * var ##var_3; * class C { * @f * [##var_1 = a](){} * static [##var_2 = b] = 1 + * [##var_3 = c] = 1 * } */ for (auto *it : node->Body()) { if (it->IsClassProperty()) { auto *classProperty = it->AsClassProperty(); - if (!classProperty->IsComputed() || (!classProperty->HasDecorators() && !classProperty->IsStatic())) { + if (!classProperty->IsComputed()) { continue; } auto *key = classProperty->Key(); @@ -522,6 +528,38 @@ util::StringView Transformer::CreatePrivatePropertyBindName(util::StringView nam return uniqueName; } +std::vector Transformer::VisitInstanceProperty(ir::ClassDefinition *node) +{ + std::vector addToClassOutside; + std::vector addToCtor; + for (auto *it : node->Body()) { + if (it->IsClassProperty() && !it->AsClassProperty()->IsStatic() && it->AsClassProperty()->Value() != nullptr) { + addToCtor.push_back(it->AsClassProperty()); + } + } + if (addToCtor.empty()) { + return addToClassOutside; + } + + ir::BlockStatement *blockStat = node->Ctor()->Function()->Body()->AsBlockStatement(); + size_t insertPos = (node->Super() == nullptr) ? 0 : 1; + for (auto *it : addToCtor) { + if (it->IsComputed()) { + addToClassOutside.push_back(AllocNode(it->Key())); + } + auto *member = GetClassMemberName(it->Key(), it->IsComputed(), it); + auto *left = AllocNode(AllocNode(), member, + ir::MemberExpression::MemberExpressionKind::PROPERTY_ACCESS, + it->IsComputed(), false); + auto assignment = AllocNode(left, it->Value(), + lexer::TokenType::PUNCTUATOR_SUBSTITUTION); + + blockStat->AddStatementAtPos(insertPos, AllocNode(assignment)); + insertPos++; + } + return addToClassOutside; +} + void Transformer::VisitTSParameterProperty(ir::ClassDefinition *node) { /* @@ -544,6 +582,7 @@ void Transformer::VisitTSParameterProperty(ir::ClassDefinition *node) return; } auto blockStatement = body->AsBlockStatement(); + size_t insertPos = (node->Super() == nullptr) ? 0 : 1; for (auto *it : func->Params()) { if (!it->IsTSParameterProperty()) { continue; @@ -565,7 +604,8 @@ void Transformer::VisitTSParameterProperty(ir::ClassDefinition *node) auto right = CreateReferenceIdentifier(name); auto assignment = AllocNode(left, right, lexer::TokenType::PUNCTUATOR_SUBSTITUTION); - blockStatement->AddStatementInFront(AllocNode(assignment)); + blockStatement->AddStatementAtPos(insertPos, AllocNode(assignment)); + insertPos++; } } @@ -633,6 +673,13 @@ ir::UpdateNodes Transformer::VisitClassDeclaration(ir::ClassDeclaration *node) res.push_back(node); } + auto instanceComputedProperty = VisitInstanceProperty(node->Definition()); + if (!instanceComputedProperty.empty()) { + res.insert(res.end(), instanceComputedProperty.begin(), instanceComputedProperty.end()); + } + + VisitTSParameterProperty(node->Definition()); + auto staticProperty = VisitStaticProperty(node->Definition(), name); if (!staticProperty.empty()) { res.insert(res.end(), staticProperty.begin(), staticProperty.end()); @@ -1024,7 +1071,14 @@ std::vector Transformer::VisitExportNamedVariable(ir::Statement * } } } else if (decl->IsFunctionDeclaration() || decl->IsClassDeclaration()) { - res.push_back(VisitTSNodes(decl)); + auto newDecl = VisitTSNode(decl); + if (std::holds_alternative(newDecl)) { + res.push_back(std::get(newDecl)); + } else { + auto statements = std::get>(newDecl); + res.insert(res.end(), statements.begin(), statements.end()); + } + auto name = decl->IsFunctionDeclaration() ? decl->AsFunctionDeclaration()->Function()->Id() : decl->AsClassDeclaration()->Definition()->Ident(); diff --git a/es2panda/parser/transformer/transformer.h b/es2panda/parser/transformer/transformer.h index f7a55878f2cfdd548f6499f22e950fe08c13e8df..b704621fc2955a38be7e009d69c82db53269976d 100644 --- a/es2panda/parser/transformer/transformer.h +++ b/es2panda/parser/transformer/transformer.h @@ -105,6 +105,7 @@ private: ir::UpdateNodes VisitClassDeclaration(ir::ClassDeclaration *node); ir::UpdateNodes VisitClassExpression(ir::ClassExpression *node); void VisitTSParameterProperty(ir::ClassDefinition *node); + std::vector VisitInstanceProperty(ir::ClassDefinition *node); std::vector VisitStaticProperty(ir::ClassDefinition *node, util::StringView name); void VisitPrivateProperty(ir::ClassDefinition *node); diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-18-expected.txt b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-18-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-18-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-18.ts b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-18.ts new file mode 100644 index 0000000000000000000000000000000000000000..b337e400d2990410745dc3d07ff4fc0f8960689b --- /dev/null +++ b/es2panda/test/compiler/ts/cases/compiler/test-ts-namespace-18.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. + */ + + +const computed:unique symbol = Symbol("symbol"); + +namespace ns { + export class A { + static [computed] = 1; + } +} + +print(ns.A[computed]); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..475e261729999cab777c771e991980a0fb130e2a --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10-expected.txt @@ -0,0 +1,13 @@ +str1 +str2 +str3 +str4 +str5 +str6 +s_str1 +s_str2 +s_str3 +s_str4 +s_str5 +s_str6 +s_str7 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10.ts new file mode 100644 index 0000000000000000000000000000000000000000..e57f9ccf12ec56a3425a0633cca3db35658dff88 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10.ts @@ -0,0 +1,49 @@ +/* + * 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. + */ + + +const computed1: unique symbol = Symbol("symbol1"); + +class A { + prop = "str1"; + 1 = "str2"; + "str_prop" = "str3"; + [computed1] = "str4"; + ["computed2"] = "str5"; + [2] = "str6"; + static sprop = "s_str1"; + static 1 = "s_str2"; + static "s_str_prop" = "s_str3"; + static [computed1] = "s_str4"; + static ["computed3"] = "s_str5"; + static [3] = "s_str6"; + static "str_prop" = "s_str7"; +} + +var obj = new A(); +print(obj.prop); +print(obj[1]); +print(obj["str_prop"]); +print(obj[computed1]); +print(obj["computed2"]); +print(obj[2]); + +print(A.sprop); +print(A[1]); +print(A["s_str_prop"]); +print(A[computed1]); +print(A["computed3"]); +print(A[3]); +print(A["str_prop"]); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-11-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-11-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..bb5c0a58b9abd3152a57e6d8aebbbe907d8cb8e4 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-11-expected.txt @@ -0,0 +1,14 @@ +final_str1 +str2 +final_str3 +str4 +str5 +str6 +final_s_str6 +s_str1 +s_str2 +s_str3 +s_str4 +s_str5 +s_str6 +s_str7 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-11.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-11.ts new file mode 100644 index 0000000000000000000000000000000000000000..92f0a751c5d55bbfca4162b2482a2e0d367b030e --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-11.ts @@ -0,0 +1,55 @@ +/* + * 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. + */ + + +const computed1: unique symbol = Symbol("symbol1"); + +class A { + constructor() { + this.prop = "final_str1"; + this["str_prop"] = "final_str3"; + this[3] = "final_s_str6"; + } + prop = "str1"; + 1 = "str2"; + "str_prop" = "str3"; + [computed1] = "str4"; + ["computed2"] = "str5"; + [2] = "str6"; + static sprop = "s_str1"; + static 1 = "s_str2"; + static "s_str_prop" = "s_str3"; + static [computed1] = "s_str4"; + static ["computed3"] = "s_str5"; + static [3] = "s_str6"; + static "str_prop" = "s_str7"; +} + +var obj = new A(); +print(obj.prop); +print(obj[1]); +print(obj["str_prop"]); +print(obj[computed1]); +print(obj["computed2"]); +print(obj[2]); +print(obj[3]); + +print(A.sprop); +print(A[1]); +print(A["s_str_prop"]); +print(A[computed1]); +print(A["computed3"]); +print(A[3]); +print(A["str_prop"]); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-12-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-12-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..50b40881db8e68e68183245dba6801bb447796b8 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-12-expected.txt @@ -0,0 +1,9 @@ +A_str_prop +A_computed1 +A_s_str +ctor_B_prop +ctor_B_prop1 +base_prop2 +B_ctor_str_prop +tsparam_prop +C_computed1 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-12.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-12.ts new file mode 100644 index 0000000000000000000000000000000000000000..473b6558491dbff8b443e4a22e9546d964462a2e --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-12.ts @@ -0,0 +1,67 @@ +/* + * 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. + */ + + +const computed1: unique symbol = Symbol("symbol1"); + +class Base { + prop = "base_prop"; + prop1 = "base_prop1"; + prop2 = "base_prop2"; + "str_prop" = "base_str_prop"; + 1 = "base_1"; + [computed1] = "base_computed1"; + static "s_str_prop" = "base_static_str_prop"; +} + +class A extends Base { + "str_prop" = "A_str_prop"; + [computed1] = "A_computed1"; + static "s_str_prop" = "A_s_str"; +} + +class B extends Base { + prop:string; + constructor() { + super(); + this.prop = "ctor_B_prop"; + this.prop1 = "ctor_B_prop1"; + this["str_prop"] = "B_ctor_str_prop"; + } + prop1 = "B_prop1"; + "str_prop" = "B_str"; +} + +class C extends Base { + constructor(public prop = "tsparam_prop") { + super(); + this[computed1] = "C_computed1"; + } +} + +var objA = new A(); +print(objA["str_prop"]); +print(objA[computed1]); +print(A["s_str_prop"]); + +var objB = new B(); +print(objB.prop); +print(objB.prop1); +print(objB.prop2); +print(objB["str_prop"]); + +var objC = new C(); +print(objC.prop); +print(objC[computed1]); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-13-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-13-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..ab0df832995e1e45d6aa3ea637a38892c3f860e5 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-13-expected.txt @@ -0,0 +1,4 @@ +str1 +Str_NSEA +str_B +str_NSB diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-13.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-13.ts new file mode 100644 index 0000000000000000000000000000000000000000..1820db8d0a08bc346a0dc14827628632be3558a4 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-13.ts @@ -0,0 +1,43 @@ +/* + * 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. + */ + + +class A { + prop = "str1"; +} +var obj = new A(); +print(obj.prop); + +namespace ns { + export class NSEA { + prop = "Str_NSEA"; + } +} +var objNSEA = new ns.NSEA(); +print(objNSEA.prop); + +var cExpr = class B { + prop = "str_B"; +} +var objCExpr = new cExpr(); +print(objCExpr.prop); + +namespace ns1 { + export var nsClass = class NSB { + prop = "str_NSB"; + } +} +var objNSClass = new ns1.nsClass(); +print(objNSClass.prop); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-14-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-14-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..8da806606fa4f7113017afca03aea589f7c3c28c --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-14-expected.txt @@ -0,0 +1,8 @@ +prop_ns.A +str_prop_ns.A +computed_ns.A +symbol_ns.A +prop_ns.B +str_prop_ns.B +computed_ns.B +symbol_ns.B diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-14.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-14.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f5e31c23e90bfd80c7abbd1ddeaafa54df03926 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-14.ts @@ -0,0 +1,44 @@ +/* + * 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. + */ + + +const computed1 : unique symbol = Symbol("symbol"); + +namespace ns { + export class A { + prop = "prop_ns.A"; + "str_prop" = "str_prop_ns.A"; + ["str_computed"] = "computed_ns.A"; + [computed1] = "symbol_ns.A"; + } + + export var varB = class B { + prop = "prop_ns.B"; + "str_prop" = "str_prop_ns.B"; + ["str_computed"] = "computed_ns.B"; + [computed1] = "symbol_ns.B"; + } +} +var objNSA = new ns.A(); +print(objNSA.prop); +print(objNSA["str_prop"]); +print(objNSA["str_computed"]); +print(objNSA[computed1]); + +var objNSB = new ns.varB(); +print(objNSB.prop); +print(objNSB["str_prop"]); +print(objNSB["str_computed"]); +print(objNSB[computed1]); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-15-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-15-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..1191247b6d9a206f6ba3d8ac79e26d041dd86941 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-15-expected.txt @@ -0,0 +1,2 @@ +1 +2 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-15.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-15.ts new file mode 100644 index 0000000000000000000000000000000000000000..19c8eb2368f991627ea7bfdf3af688d4a1b21fb5 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-15.ts @@ -0,0 +1,26 @@ +/* + * 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. + */ + + +const a : unique symbol = Symbol("symbol"); + +class C { + [a] = 1; + a = 2; +} + +var obj = new C(); +print(obj[a]); +print(obj.a); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-16-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-16-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..d00491fd7e5bb6fa28c517a0bb32b8b506539d4d --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-16-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-16.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-16.ts new file mode 100644 index 0000000000000000000000000000000000000000..3d018bda381b235d401e5926800f599cb688df1d --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-16.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. + */ + + +namespace ns { + export const x : unique symbol = Symbol(); +} + +class C { + [ns.x] = 1; +} + +var obj = new C(); +print(obj[ns.x]); + diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-17-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-17-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..e91e85c0947c46fd32d3d6fe9de9b03ed5458e55 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-17-expected.txt @@ -0,0 +1,2 @@ +flag1 +flag2 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-17.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-17.ts new file mode 100644 index 0000000000000000000000000000000000000000..eb0ac9b08a6c7c03f5d20645c28e45fb424cf1b1 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-17.ts @@ -0,0 +1,31 @@ +/* + * 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. + */ + + +function f(ctor) { + return class extends ctor { + constructor() { + super(); + this.a = "flag1"; + } + b = "flag2"; + }; +} + +let C = class C {}; +C = f(C); +var c = new C(); +print(c.a); +print(c.b); diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-18-expected.txt b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-18-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..2cf92cdbeb168c94bc36ce94810f1563920770f9 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-18-expected.txt @@ -0,0 +1,4 @@ +4 +2 +4 +3 diff --git a/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-18.ts b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-18.ts new file mode 100644 index 0000000000000000000000000000000000000000..579abc7cc45a90bb2ab3b296869089a6264cb564 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-18.ts @@ -0,0 +1,45 @@ +/* + * 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. + */ + + +class Base { + prop = 1; +} + +class A extends Base { + constructor() { + super(); + this.prop = 4; + } + prop = 2; + prop2 = this.prop; +} + + +class B extends Base { + constructor(public prop = 3) { + super(); + this.prop = 4; + } + prop2 = this.prop; +} + +var a = new A(); +print(a.prop); +print(a.prop2); + +var b = new B(); +print(b.prop); +print(b.prop2); diff --git a/es2panda/test/parser/ts/test-class-definiton23-expected.txt b/es2panda/test/parser/ts/test-class-definiton23-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..a3730a7354591837ac5bacba5ceadfbf4975565d --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton23-expected.txt @@ -0,0 +1,258 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "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": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "get", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "kind": "method", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 8 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 16 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-class-definiton23.ts b/es2panda/test/parser/ts/test-class-definiton23.ts new file mode 100644 index 0000000000000000000000000000000000000000..8411930945fcaf4339fb4f3501b2c15ae370832c --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton23.ts @@ -0,0 +1,19 @@ +/* + * 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. + */ + + +class C{ + get() {} +} diff --git a/es2panda/test/parser/ts/test-class-definiton24-expected.txt b/es2panda/test/parser/ts/test-class-definiton24-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..3685333522f72471100441a3d15093378534f352 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton24-expected.txt @@ -0,0 +1,561 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSModuleDeclaration", + "id": { + "type": "Identifier", + "name": "ns", + "loc": { + "start": { + "line": 17, + "column": 19 + }, + "end": { + "line": 17, + "column": 21 + } + } + }, + "body": { + "type": "TSModuleBlock", + "body": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 18, + "column": 11 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "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": [], + "declare": true, + "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": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 20, + "column": 13 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 20, + "column": 21 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 21 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 21 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 23, + "column": 14 + }, + "end": { + "line": 23, + "column": 16 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 21 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 24, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 24, + "column": 10 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 24, + "column": 11 + }, + "end": { + "line": 24, + "column": 13 + } + } + }, + "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": [], + "declare": true, + "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": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 16 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 24, + "column": 14 + }, + "end": { + "line": 24, + "column": 16 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "declare": true, + "global": false, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 26, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 26, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-class-definiton24.ts b/es2panda/test/parser/ts/test-class-definiton24.ts new file mode 100644 index 0000000000000000000000000000000000000000..35d3884acab8515e35bda1a54f58887997d266db --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton24.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. + */ + + +declare namespace ns { + class C1 {} + function C1() : C1 + function C1() : C1 + + function C2() : C2 + function C2() : C2 + class C2 {} +} diff --git a/es2panda/test/parser/ts/test-class-definiton25-expected.txt b/es2panda/test/parser/ts/test-class-definiton25-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..4407dce5a4a4e369ee97c4b2ff709af315f321e1 --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton25-expected.txt @@ -0,0 +1,1067 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 17, + "column": 15 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "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": [], + "declare": true, + "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": 9 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 19, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 19, + "column": 9 + } + } + }, + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSAnyKeyword", + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 21, + "column": 15 + }, + "end": { + "line": 21, + "column": 17 + } + } + }, + "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": [], + "declare": true, + "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": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 22, + "column": 18 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 27 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 23, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 23, + "column": 8 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 20 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 25, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 25, + "column": 10 + } + } + }, + { + "type": "TSModuleDeclaration", + "id": { + "type": "Identifier", + "name": "ns", + "loc": { + "start": { + "line": 25, + "column": 11 + }, + "end": { + "line": 25, + "column": 13 + } + } + }, + "body": { + "type": "TSModuleBlock", + "body": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 26, + "column": 19 + }, + "end": { + "line": 26, + "column": 21 + } + } + }, + "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": [], + "declare": true, + "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": 26, + "column": 13 + }, + "end": { + "line": 26, + "column": 24 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 26, + "column": 22 + }, + "end": { + "line": 26, + "column": 24 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 13 + }, + "end": { + "line": 26, + "column": 24 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 27, + "column": 14 + }, + "end": { + "line": 27, + "column": 16 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 27, + "column": 21 + }, + "end": { + "line": 27, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 21 + }, + "end": { + "line": 27, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 28, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 5 + }, + "end": { + "line": 28, + "column": 13 + } + } + }, + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C1", + "loc": { + "start": { + "line": 28, + "column": 14 + }, + "end": { + "line": 28, + "column": 16 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSAnyKeyword", + "loc": { + "start": { + "line": 28, + "column": 21 + }, + "end": { + "line": 28, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 28, + "column": 25 + }, + "end": { + "line": 28, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 27 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 30, + "column": 19 + }, + "end": { + "line": 30, + "column": 21 + } + } + }, + "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": [], + "declare": true, + "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": 30, + "column": 13 + }, + "end": { + "line": 30, + "column": 24 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 30, + "column": 22 + }, + "end": { + "line": 30, + "column": 24 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 13 + }, + "end": { + "line": 30, + "column": 24 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 31, + "column": 22 + }, + "end": { + "line": 31, + "column": 24 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 31, + "column": 29 + }, + "end": { + "line": 31, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 29 + }, + "end": { + "line": 31, + "column": 31 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 31, + "column": 13 + }, + "end": { + "line": 32, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 31, + "column": 13 + }, + "end": { + "line": 32, + "column": 12 + } + } + }, + { + "type": "TSDeclareFunction", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 32, + "column": 22 + }, + "end": { + "line": 32, + "column": 24 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "C2", + "loc": { + "start": { + "line": 32, + "column": 29 + }, + "end": { + "line": 32, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 29 + }, + "end": { + "line": 32, + "column": 31 + } + } + }, + "declare": true, + "loc": { + "start": { + "line": 32, + "column": 13 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 32, + "column": 13 + }, + "end": { + "line": 33, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 25, + "column": 14 + }, + "end": { + "line": 33, + "column": 2 + } + } + }, + "declare": false, + "global": false, + "loc": { + "start": { + "line": 25, + "column": 1 + }, + "end": { + "line": 34, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 34, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-class-definiton25.ts b/es2panda/test/parser/ts/test-class-definiton25.ts new file mode 100644 index 0000000000000000000000000000000000000000..b97ed7f7df9102235b15265f5fe3f225e1d0762d --- /dev/null +++ b/es2panda/test/parser/ts/test-class-definiton25.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. + */ + + +declare class C1 {} +function C1() : C1 +function C1() : any {} + +declare class C2 {} +declare function C2() : C2 +declare function C2() : C2 + +namespace ns { + declare class C1 {} + function C1() : C1 + function C1() : any {} + + declare class C2 {} + declare function C2() : C2 + declare function C2() : C2 +} diff --git a/es2panda/test/parser/ts/test-function-generic-expected.txt b/es2panda/test/parser/ts/test-function-generic-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..9be1f6f7670b4d785827a4c51ce0076270fcd56e --- /dev/null +++ b/es2panda/test/parser/ts/test-function-generic-expected.txt @@ -0,0 +1 @@ +SyntaxError: Identifier expected. [test-function-generic.ts:21:13] diff --git a/es2panda/test/parser/ts/test-function-generic.ts b/es2panda/test/parser/ts/test-function-generic.ts new file mode 100644 index 0000000000000000000000000000000000000000..b327e9e1ebe6196a28c3178110eaf88fe20cfe65 --- /dev/null +++ b/es2panda/test/parser/ts/test-function-generic.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function func (a : T) { + console.log(a); +} + +func(10); diff --git a/es2panda/test/parser/ts/test-keyword-identify8-expected.txt b/es2panda/test/parser/ts/test-keyword-identify8-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..7f090e1e88771b93bb2a20da22005ca8bcaf1556 --- /dev/null +++ b/es2panda/test/parser/ts/test-keyword-identify8-expected.txt @@ -0,0 +1,312 @@ +{ + "type": "Program", + "statements": [ + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "f", + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "global", + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 19 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "FunctionDeclaration", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "g", + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 11 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "global", + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "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": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "body": [], + "indexSignatures": [], + "loc": { + "start": { + "line": 22, + "column": 18 + }, + "end": { + "line": 22, + "column": 20 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 23, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 24, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-keyword-identify8.ts b/es2panda/test/parser/ts/test-keyword-identify8.ts new file mode 100644 index 0000000000000000000000000000000000000000..9acc3fb0254781d9975585a4988bc0485ec34ea4 --- /dev/null +++ b/es2panda/test/parser/ts/test-keyword-identify8.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. + */ + + +function f() { + let global = 1; +} + +function g() { + class global {} +} diff --git a/es2panda/test/parser/ts/test-lessThan-expression1-expected.txt b/es2panda/test/parser/ts/test-lessThan-expression1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..0ec43a4e3236cc72cb0087d18c1319306e0cffa9 --- /dev/null +++ b/es2panda/test/parser/ts/test-lessThan-expression1-expected.txt @@ -0,0 +1,314 @@ +{ + "type": "Program", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "arr", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 10 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + { + "type": "NumberLiteral", + "value": 3, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + { + "type": "NumberLiteral", + "value": 4, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 22 + } + } + } + ], + "kind": "const", + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "right": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "arr", + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 12 + } + } + }, + "property": { + "type": "BinaryExpression", + "operator": "+", + "left": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "loc": { + "start": { + "line": 19, + "column": 17 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 18 + } + } + }, + "computed": true, + "optional": false, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "print", + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "helloWorld", + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 23 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 25 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-lessThan-expression1.ts b/es2panda/test/parser/ts/test-lessThan-expression1.ts new file mode 100644 index 0000000000000000000000000000000000000000..610a0f18a5af412782defb64fdadb5a29cfbda87 --- /dev/null +++ b/es2panda/test/parser/ts/test-lessThan-expression1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +const arr = [1,2,3,4]; + +if (0 < arr[1 + 2]) { + print("helloWorld"); +} diff --git a/es2panda/test/parser/ts/test-lessThan-expression2-expected.txt b/es2panda/test/parser/ts/test-lessThan-expression2-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..4ecc06eb25511d44b9b0a9f310bcd4903c1ea6ec --- /dev/null +++ b/es2panda/test/parser/ts/test-lessThan-expression2-expected.txt @@ -0,0 +1,271 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "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": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "value": { + "type": "BinaryExpression", + "operator": "<", + "left": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "right": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "NewExpression", + "callee": { + "type": "Identifier", + "name": "Date", + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "property": { + "type": "Identifier", + "name": "getTime", + "loc": { + "start": { + "line": 18, + "column": 25 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 35 + } + } + }, + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 35 + } + } + } + ], + "indexSignatures": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-lessThan-expression2.ts b/es2panda/test/parser/ts/test-lessThan-expression2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d5f203287e84d92b1db2f29006e204dbcb638521 --- /dev/null +++ b/es2panda/test/parser/ts/test-lessThan-expression2.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +class A { + a = 1 < (new Date().getTime()); +} diff --git a/es2panda/test/parser/ts/test-object-expression-expected.txt b/es2panda/test/parser/ts/test-object-expression-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..61850b576f7ec17e5f2839649f0bb7265def49a9 --- /dev/null +++ b/es2panda/test/parser/ts/test-object-expression-expected.txt @@ -0,0 +1,247 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSInterfaceDeclaration", + "body": { + "type": "TSInterfaceBody", + "body": [ + { + "type": "TSPropertySignature", + "computed": false, + "optional": false, + "readonly": false, + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 17, + "column": 15 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 15 + }, + "end": { + "line": 17, + "column": 26 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + "id": { + "type": "Identifier", + "name": "I", + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "extends": [], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 26 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ObjectPattern", + "properties": [ + { + "type": "Property", + "method": false, + "shorthand": true, + "computed": false, + "key": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 19, + "column": 3 + }, + "end": { + "line": 19, + "column": 4 + } + } + }, + "value": { + "type": "Identifier", + "name": "a", + "loc": { + "start": { + "line": 19, + "column": 3 + }, + "end": { + "line": 19, + "column": 4 + } + } + }, + "kind": "init", + "loc": { + "start": { + "line": 19, + "column": 3 + }, + "end": { + "line": 19, + "column": 4 + } + } + } + ], + "typeAnnotation": { + "type": "TSTypeReference", + "typeName": { + "type": "Identifier", + "name": "I", + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 9 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 19, + "column": 13 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 19, + "column": 16 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 1 + } + } +} diff --git a/es2panda/test/parser/ts/test-object-expression.ts b/es2panda/test/parser/ts/test-object-expression.ts new file mode 100644 index 0000000000000000000000000000000000000000..0d1bbaa425dad48c4dda543888e6fda201b9f246 --- /dev/null +++ b/es2panda/test/parser/ts/test-object-expression.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +interface I { a: number } + +({a}: I) => {}; diff --git a/es2panda/test/test_tsc_ignore_list.txt b/es2panda/test/test_tsc_ignore_list.txt index 761f6a12a60d378dd107004dcfa7ae526836406a..efddbd2bacecf0fd9ec192ce4134446960dbf7ad 100644 --- a/es2panda/test/test_tsc_ignore_list.txt +++ b/es2panda/test/test_tsc_ignore_list.txt @@ -1,16 +1,10 @@ -es2panda/test/TypeScript/tests/cases/compiler/ambientClassOverloadForFunction.ts es2panda/test/TypeScript/tests/cases/compiler/bom-utf16be.ts -es2panda/test/TypeScript/tests/cases/compiler/classFunctionMerging.ts es2panda/test/TypeScript/tests/cases/compiler/collisionArgumentsInType.ts es2panda/test/TypeScript/tests/cases/compiler/collisionArgumentsInterfaceMembers.ts -es2panda/test/TypeScript/tests/cases/compiler/computedPropertiesTransformedInOtherwiseNonTSClasses.ts -es2panda/test/TypeScript/tests/cases/compiler/constructorOverloads5.ts es2panda/test/TypeScript/tests/cases/compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts es2panda/test/TypeScript/tests/cases/compiler/emitBundleWithShebang1.ts es2panda/test/TypeScript/tests/cases/compiler/fileWithNextLine2.ts es2panda/test/TypeScript/tests/cases/compiler/genericRecursiveImplicitConstructorErrors2.ts -es2panda/test/TypeScript/tests/cases/compiler/globalIsContextualKeyword.ts -es2panda/test/TypeScript/tests/cases/compiler/inferenceErasedSignatures.ts es2panda/test/TypeScript/tests/cases/compiler/isLiteral1.ts es2panda/test/TypeScript/tests/cases/compiler/isLiteral2.ts es2panda/test/TypeScript/tests/cases/compiler/letAsIdentifier2.ts @@ -20,7 +14,6 @@ es2panda/test/TypeScript/tests/cases/compiler/letInVarDeclOfForOf_ES5.ts es2panda/test/TypeScript/tests/cases/compiler/letInVarDeclOfForOf_ES6.ts es2panda/test/TypeScript/tests/cases/compiler/parameterInitializerBeforeDestructuringEmit.ts es2panda/test/TypeScript/tests/cases/compiler/shebang.ts -es2panda/test/TypeScript/tests/cases/compiler/sigantureIsSubTypeIfTheyAreIdentical.ts es2panda/test/TypeScript/tests/cases/compiler/sourceMap-LineBreaks.ts es2panda/test/TypeScript/tests/cases/compiler/withStatementInternalComments.ts es2panda/test/TypeScript/tests/cases/conformance/es6/arrowFunction/emitArrowFunctionWhenUsingArguments03_ES6.ts @@ -64,7 +57,4 @@ es2panda/test/TypeScript/tests/cases/conformance/interfaces/interfaceDeclaration es2panda/test/TypeScript/tests/cases/conformance/parser/ecmascript5/RegressionTests/parser579071.ts es2panda/test/TypeScript/tests/cases/conformance/parser/ecmascript5/parserUnicodeWhitespaceCharacter1.ts es2panda/test/TypeScript/tests/cases/conformance/scanner/ecmascript3/scannerES3NumericLiteral2.ts -es2panda/test/TypeScript/tests/cases/conformance/types/keyof/keyofAndIndexedAccess.ts es2panda/test/TypeScript/tests/cases/conformance/types/members/objectTypeWithStringNamedNumericProperty.ts -es2panda/test/TypeScript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbols.ts -es2panda/test/TypeScript/tests/cases/conformance/types/uniqueSymbol/uniqueSymbolsDeclarations.ts