diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index f30fcf2f9a5bc71a1dbce7678bdbc243fb8cdb47..f85b20e3bb57c7db113cad1c8417804cab8e0f72 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/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index a245d55f623237c7552bbaa1efe1f2d3e0ac0393..bc75938405d3380dda41fbac184ce77db192d9e5 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -2258,11 +2258,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 +2894,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 +3029,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/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index fbf2812a2eb7e23a67665a8543476e98a099bcff..6f50bf3b5aa010018cad35e03c07568a2d88889b 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,53 @@ util::StringView Transformer::CreatePrivatePropertyBindName(util::StringView nam return uniqueName; } +std::vector Transformer::VisitInstanceProperty(ir::ClassDefinition *node) +{ + /* + * Move class InstanceProperty to constructor. + * Transform: + * class C { + * "prop" = 1; + * } + * + * To: + * class C { + * constructor () { + * this["prop"] = 1; + * } + * } + */ + std::vector addToCtor; + // Non-null computed properties need to be added outside the class. It is a subset of addToCtor. + std::vector computedProps; + for (auto *it : node->Body()) { + if (it->IsClassProperty() && !it->AsClassProperty()->IsStatic() && it->AsClassProperty()->Value() != nullptr) { + addToCtor.push_back(it->AsClassProperty()); + } + } + if (addToCtor.empty()) { + return {}; + } + + ir::BlockStatement *blockStat = node->Ctor()->Function()->Body()->AsBlockStatement(); + size_t insertPos = (node->Super() == nullptr) ? 0 : 1; + for (auto *it : addToCtor) { + if (it->IsComputed()) { + computedProps.push_back(AllocNode(it->Key())); + } + auto *member = GetClassMemberName(it->Key(), it->IsComputed(), it); + auto *left = AllocNode(AllocNode(), member, + ir::MemberExpression::MemberExpressionKind::ELEMENT_ACCESS, + true, false); + auto assignment = AllocNode(left, it->Value(), + lexer::TokenType::PUNCTUATOR_SUBSTITUTION); + + blockStat->AddStatementAtPos(insertPos, AllocNode(assignment)); + insertPos++; + } + return computedProps; +} + void Transformer::VisitTSParameterProperty(ir::ClassDefinition *node) { /* @@ -544,6 +597,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 +619,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++; } } @@ -611,7 +666,7 @@ std::vector Transformer::VisitStaticProperty(ir::Clas } auto *member = GetClassMemberName(classProperty->Key(), classProperty->IsComputed(), classProperty); auto left = AllocNode(CreateReferenceIdentifier(name), member, - ir::MemberExpression::MemberExpressionKind::PROPERTY_ACCESS, classProperty->IsComputed(), false); + ir::MemberExpression::MemberExpressionKind::ELEMENT_ACCESS, true, false); auto assignment = AllocNode(left, right, lexer::TokenType::PUNCTUATOR_SUBSTITUTION); res.push_back(AllocNode(assignment)); } @@ -633,6 +688,14 @@ ir::UpdateNodes Transformer::VisitClassDeclaration(ir::ClassDeclaration *node) res.push_back(node); } + auto instanceComputedProperty = VisitInstanceProperty(node->Definition()); + // instanceComputedProperty has been transformed by VisitComputedPerporty before, here is an assignmentExpression. + 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 +1087,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/transformed_cases/test-ts-export-classes-1-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1-transformed-expected.txt index a7cf5138fd2f8e67830c6659e1a2b15510ecae62..867daffd1e106c4d00d6a2c710c32b86a14bc975 100644 --- a/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1-transformed-expected.txt +++ b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-1-transformed-expected.txt @@ -206,7 +206,7 @@ } } }, - "computed": false, + "computed": true, "optional": false, "loc": { "start": { diff --git a/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2-transformed-expected.txt b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2-transformed-expected.txt index 74a6bd40cea30f01d8731e243efeb20f68be18c2..94d0ab435af056a13dc4ed168d6c9b31e6b0cc4a 100644 --- a/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2-transformed-expected.txt +++ b/es2panda/test/parser/ts/transformed_cases/test-ts-export-classes-2-transformed-expected.txt @@ -221,7 +221,7 @@ } } }, - "computed": false, + "computed": true, "optional": false, "loc": { "start": { diff --git a/es2panda/test/test_tsc_ignore_list.txt b/es2panda/test/test_tsc_ignore_list.txt index 44cd68a6d20aa6a452ad32b60e20d519d7c9c965..f5bf4b5e1f0e6cbe8c25c4028eb8660a9c68ecd2 100644 --- a/es2panda/test/test_tsc_ignore_list.txt +++ b/es2panda/test/test_tsc_ignore_list.txt @@ -3,7 +3,6 @@ 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/globalIsContextualKeyword.ts @@ -48,5 +47,3 @@ es2panda/test/TypeScript/tests/cases/conformance/parser/ecmascript5/RegressionTe 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