From 796c4771111d7145a32794f098b7203732698067 Mon Sep 17 00:00:00 2001 From: songqi Date: Tue, 14 Feb 2023 16:59:08 +0800 Subject: [PATCH] Support decorators for abstract class Issue: I6F2H1 Tests: parser/compiler/tsc/test262 Signed-off-by: songqi Change-Id: I23412c1f13fa34666f43f429e54a0959d8b99c2c --- es2panda/parser/parserImpl.cpp | 3 +- es2panda/parser/statementParser.cpp | 4 ++- es2panda/parser/transformer/transformer.cpp | 3 ++ .../test-ts-decorators-14-expected.txt | 2 ++ .../decorators/test-ts-decorators-14.ts | 23 +++++++++++++++ ...s-test-decorators-export-exec-expected.txt | 1 + .../ts-test-decorators-export-exec.ts | 29 +++++++++++++++++++ 7 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-14-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-14.ts create mode 100644 es2panda/test/compiler/ts/projects/ts_test_decorators/ts-test-decorators-export-exec-expected.txt create mode 100644 es2panda/test/compiler/ts/projects/ts_test_decorators/ts-test-decorators-export-exec.ts diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index 723076cab3..deb1e99bd5 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -2538,7 +2538,8 @@ ArenaVector ParserImpl::ParseDecorators() (context_.Status() & ParserStatus::IN_CLASS_BODY) == 0 && lexer_->GetToken().Type() != lexer::TokenType::KEYW_EXPORT && !(lexer_->GetToken().Type() == lexer::TokenType::LITERAL_IDENT && - lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_DECLARE)) { + (lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_DECLARE || + lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_ABSTRACT))) { ThrowSyntaxError("Decorators are not valid here.", decorators.front()->Start()); } diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index e8f67909e3..2575e9e569 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -2505,7 +2505,9 @@ ir::ExportNamedDeclaration *ParserImpl::ParseNamedExportDeclaration(const lexer: } if (!decorators.empty() && lexer_->GetToken().Type() != lexer::TokenType::KEYW_CLASS && - (context_.Status() & ParserStatus::IN_CLASS_BODY) == 0) { + (context_.Status() & ParserStatus::IN_CLASS_BODY) == 0 && + !(lexer_->GetToken().Type() == lexer::TokenType::LITERAL_IDENT && + (lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_ABSTRACT))) { ThrowSyntaxError("Decorators are not valid here.", decorators.front()->Start()); } diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index d1c8b5f08b..457c1a2c2c 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -281,6 +281,9 @@ ir::UpdateNodes Transformer::VisitTSNode(ir::AstNode *childNode) } case ir::AstNodeType::CLASS_DECLARATION: { auto *node = childNode->AsClassDeclaration(); + if (node->Definition()->Declare()) { + return node; + } DuringClass duringClass(&classList_, node->Definition()->GetName()); node = VisitTSNodes(node)->AsClassDeclaration(); auto res = VisitClassDeclaration(node); diff --git a/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-14-expected.txt b/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-14-expected.txt new file mode 100644 index 0000000000..516badf46f --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-14-expected.txt @@ -0,0 +1,2 @@ +test decorators +test decorators diff --git a/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-14.ts b/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-14.ts new file mode 100644 index 0000000000..b572e23ed4 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/decorators/test-ts-decorators-14.ts @@ -0,0 +1,23 @@ +/* + * 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 f(constructor: Function) { print("test decorators"); } + +@f +class C1 { } + +@f +abstract class C2 { } diff --git a/es2panda/test/compiler/ts/projects/ts_test_decorators/ts-test-decorators-export-exec-expected.txt b/es2panda/test/compiler/ts/projects/ts_test_decorators/ts-test-decorators-export-exec-expected.txt new file mode 100644 index 0000000000..58c7af63cb --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_test_decorators/ts-test-decorators-export-exec-expected.txt @@ -0,0 +1 @@ +test decorators diff --git a/es2panda/test/compiler/ts/projects/ts_test_decorators/ts-test-decorators-export-exec.ts b/es2panda/test/compiler/ts/projects/ts_test_decorators/ts-test-decorators-export-exec.ts new file mode 100644 index 0000000000..d4789692e7 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_test_decorators/ts-test-decorators-export-exec.ts @@ -0,0 +1,29 @@ +/* + * 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 f(constructor: Function) { print("test decorators"); } + +@f +export declare class C1 { } + +@f +export abstract class C2 { } + +@f +export abstract declare class C3 { } + +@f +export declare abstract class C4 { } -- Gitee