diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index 7496cc6aa4569f1c2c7566571a44d899391dc17f..bb9ff930298f958d8b5f3b7dae7353679820523a 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -572,6 +572,10 @@ void Binder::BuildClassDefinition(ir::ClassDefinition *classDef) ResolveReference(classDef, stmt); } + if (extension_ == ScriptExtension::TS) { + ResetParentForTransformedAtsNode(classDef, classDef->Ctor()); + } + for (auto *iter : classDef->IndexSignatures()) { ResolveReference(classDef, iter); } @@ -806,6 +810,17 @@ void Binder::ResolveReferences(const ir::AstNode *parent) parent->Iterate([this, parent](auto *childNode) { ResolveReference(parent, childNode); }); } +void Binder::ResetParentForTransformedAtsNodes(const ir::AstNode *parent) +{ + parent->Iterate([this, parent](auto *childNode) { ResetParentForTransformedAtsNode(parent, childNode); }); +} + +void Binder::ResetParentForTransformedAtsNode(const ir::AstNode *parent, ir::AstNode *childNode) +{ + childNode->SetParent(parent); + ResetParentForTransformedAtsNodes(childNode); +} + void Binder::AddMandatoryParam(const std::string_view &name) { ASSERT(scope_->IsFunctionVariableScope()); diff --git a/es2panda/binder/binder.h b/es2panda/binder/binder.h index 1f51a5ed9eb334293b93800f12abfe9b3a7c574a..3ec11c72f59a9b99391e74d8f089af2ee7839c51 100644 --- a/es2panda/binder/binder.h +++ b/es2panda/binder/binder.h @@ -210,6 +210,8 @@ private: void LookupIdentReference(ir::Identifier *ident); void ResolveReference(const ir::AstNode *parent, ir::AstNode *childNode); void ResolveReferences(const ir::AstNode *parent); + void ResetParentForTransformedAtsNode(const ir::AstNode *parent, ir::AstNode *childNode); + void ResetParentForTransformedAtsNodes(const ir::AstNode *parent); void ValidateExportDecl(const ir::ExportNamedDeclaration *exportDecl); void StoreAndCheckSpecialFunctionName(std::string &internalNameStr, std::string recordName); void ReplaceConstReferenceWithInitialization(const ir::Identifier *ident, const Decl *decl); diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index 0a2b7f0f7571dcfaabd17ddd9b7b60cc4636bccc..79aee10f7674b5a20ed22ae323bed13dfe42acce 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -2032,10 +2032,6 @@ void Transformer::CheckTransformedAstNode(const ir::AstNode *parent, ir::AstNode (childNode->AsClassProperty()->IsStatic() || childNode->AsClassProperty()->Value() != nullptr)) { return; } - if (childNode->IsMethodDefinition() && - childNode->AsMethodDefinition()->Kind() == ir::MethodDefinitionKind::CONSTRUCTOR) { - return; - } if (childNode->IsDecorator()) { return; } diff --git a/es2panda/test/bytecode/ts/class/test-class-property1-expected.txt b/es2panda/test/bytecode/ts/class/test-class-property1-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..b80bd4ee097364a21132f8d4770534c45d11b372 --- /dev/null +++ b/es2panda/test/bytecode/ts/class/test-class-property1-expected.txt @@ -0,0 +1,49 @@ +.language ECMAScript + +.function any .#4021257117105775747#(any a0, any a1, any a2) { +label_1: +label_0: + tryldglobalbyname 0x0, print + sta v0 + ldlexvar 0x0, 0x1 + sta v1 + lda v0 + callarg1 0x1, v1 + ldundefined + returnundefined +label_2: +} + +.function any .A(any a0, any a1, any a2) { +label_1: +label_0: + newlexenv 0x2 + lda a1 + stlexvar 0x0, 0x0 + lda a2 + stlexvar 0x0, 0x1 + ldlexvar 0x0, 0x1 + sta v0 + definefunc 0x0, .#4021257117105775747#, 0x0 + stobjbyname 0x1, prop, v0 + ldlexvar 0x0, 0x1 + return +label_2: +} + +.function any .func_main_0(any a0, any a1, any a2) { +label_1: +label_0: + ldhole + sta v0 + defineclasswithbuffer 0x0, .A, _0, 0x0, v0 + sta v0 + ldobjbyname 0x1, prototype + lda v0 + sttoglobalrecord 0x3, A + ldundefined + returnundefined +label_2: +} + + diff --git a/es2panda/test/bytecode/ts/class/test-class-property1.ts b/es2panda/test/bytecode/ts/class/test-class-property1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b6441164082f7be4fe0f06b2c8e3e80fd41b672f --- /dev/null +++ b/es2panda/test/bytecode/ts/class/test-class-property1.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 { + private prop = () => { print(this) } +} \ No newline at end of file diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index 0b9428c5cebba35f6c520d095617fd3a553475b7..3bf5ff4d597e39c76193ba2e29873d24b47d0211 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -1506,6 +1506,7 @@ def main(): runner = BytecodeRunner(args) runner.add_directory("bytecode/commonjs", "js", ["--commonjs", "--dump-assembly"]) runner.add_directory("bytecode/js", "js", ["--dump-assembly"]) + runner.add_directory("bytecode/ts", "ts", ["--dump-assembly"]) runners.append(runner)