From e2bbf4faf8f5ba30b0be40feaae1e4eb5effde47 Mon Sep 17 00:00:00 2001 From: songqi Date: Mon, 27 Mar 2023 19:19:30 +0800 Subject: [PATCH] Fix MandatoryParams duplication for decorators Issue: I6QQK8 Tests: parser/compiler/tsc/test262 Signed-off-by: songqi Change-Id: I79f98b018f9414f51bb7c85a25ceb00dc0f1ef40 --- es2panda/binder/binder.cpp | 4 +++ es2panda/binder/scope.h | 11 ++++++ es2panda/ir/base/classProperty.h | 5 --- es2panda/parser/transformer/transformer.cpp | 14 -------- es2panda/parser/transformer/transformer.h | 2 -- .../test-ts-decorators-15-expected.txt | 1 + .../decorators/test-ts-decorators-15.ts | 34 +++++++++++++++++++ ...-export-classes-1-transformed-expected.txt | 14 ++++++++ ...-export-classes-2-transformed-expected.txt | 14 ++++++++ 9 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-15-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-15.ts diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index 0fc1b4f9d5..5fe1e69e3d 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -310,7 +310,11 @@ void Binder::LookupIdentReference(ir::Identifier *ident) void Binder::BuildFunction(FunctionScope *funcScope, util::StringView name, const ir::ScriptFunction *func) { + if (funcScope->InFunctionScopes()) { + return; + } functionScopes_.push_back(funcScope); + funcScope->SetInFunctionScopes(); bool funcNameWithoutDot = (name.Find(".") == std::string::npos); bool funcNameWithoutBackslash = (name.Find("\\") == std::string::npos); diff --git a/es2panda/binder/scope.h b/es2panda/binder/scope.h index c638fcf320..2583696234 100644 --- a/es2panda/binder/scope.h +++ b/es2panda/binder/scope.h @@ -595,12 +595,23 @@ public: return internalName_; } + bool InFunctionScopes() const + { + return inFunctionScopes_; + } + + void SetInFunctionScopes() + { + inFunctionScopes_ = true; + } + bool AddBinding(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl, [[maybe_unused]] ScriptExtension extension) override; private: util::StringView name_ {}; util::StringView internalName_ {}; + bool inFunctionScopes_ {false}; }; class LocalScope : public Scope { diff --git a/es2panda/ir/base/classProperty.h b/es2panda/ir/base/classProperty.h index 0f033db9e5..b2cc33c8d7 100644 --- a/es2panda/ir/base/classProperty.h +++ b/es2panda/ir/base/classProperty.h @@ -71,11 +71,6 @@ public: return value_; } - void RemoveValue() - { - value_ = nullptr; - } - const Expression *TypeAnnotation() const { return typeAnnotation_; diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index 02462744df..7c42bd3340 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -301,7 +301,6 @@ ir::UpdateNodes Transformer::VisitTSNode(ir::AstNode *childNode) node = VisitTSNodes(node)->AsClassDeclaration(); auto res = VisitClassDeclaration(node); SetOriginalNode(res, childNode); - RemoveOriginNodeValueForClassPerporty(node->Definition()); return res; } case ir::AstNodeType::CLASS_EXPRESSION: { @@ -310,7 +309,6 @@ ir::UpdateNodes Transformer::VisitTSNode(ir::AstNode *childNode) node = VisitTSNodes(node)->AsClassExpression(); auto res = VisitClassExpression(node); SetOriginalNode(res, childNode); - RemoveOriginNodeValueForClassPerporty(node->Definition()); return res; } case ir::AstNodeType::CLASS_DEFINITION: { @@ -1966,18 +1964,6 @@ void Transformer::CheckTransformedAstNode(const ir::AstNode *parent, ir::AstNode CheckTransformedAstNodes(childNode, passed); } -void Transformer::RemoveOriginNodeValueForClassPerporty(const ir::ClassDefinition *node) -{ - // In the transformer, after classPerporty is moved to Constructor, the original node will not be deleted. - // When classPerporty value is a function, two mandatory parameters will be added in the binder of this function. - // The original classPerporty value is deleted here, and the key is reserved for type extraction. - for (auto *it : node->Body()) { - if (it->IsClassProperty()) { - it->AsClassProperty()->RemoveValue(); - } - } -} - void Transformer::ResetParentScopeForAstNodes(const ir::AstNode *parent) const { parent->Iterate([this](auto *childNode) { ResetParentScopeForAstNode(childNode); }); diff --git a/es2panda/parser/transformer/transformer.h b/es2panda/parser/transformer/transformer.h index d6eb2670b7..80dab1af87 100644 --- a/es2panda/parser/transformer/transformer.h +++ b/es2panda/parser/transformer/transformer.h @@ -195,8 +195,6 @@ private: void ResetParentScopeForAstNodes(const ir::AstNode *parent) const; void ResetParentScopeForAstNode(ir::AstNode *childNode) const; - void RemoveOriginNodeValueForClassPerporty(const ir::ClassDefinition *node); - template ir::UpdateNodes VisitExportClassDeclaration(T *node); diff --git a/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-15-expected.txt b/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-15-expected.txt new file mode 100644 index 0000000000..e965047ad7 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-15-expected.txt @@ -0,0 +1 @@ +Hello diff --git a/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-15.ts b/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-15.ts new file mode 100644 index 0000000000..b31fa9e36a --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-15.ts @@ -0,0 +1,34 @@ +/* + * 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 enumerable(value: any) { + return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { + descriptor.enumerable = value("xxx"); + print("Hello"); + }; +} + +class Greeter { + greeting: string; + constructor(message: string) { + this.greeting = message; + } + + @enumerable(function (p: string) { return false }) + greet() { + return "Hello, " + this.greeting; + } +} 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 6ddbf498fe..867daffd1e 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 @@ -104,6 +104,20 @@ } } }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 32 + }, + "end": { + "line": 17, + "column": 33 + } + } + }, "static": true, "readonly": false, "declare": false, 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 bb7c7f3804..94d0ab435a 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 @@ -117,6 +117,20 @@ } } }, + "value": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 27 + } + } + }, "static": true, "readonly": false, "declare": false, -- Gitee