From b73261bb6542b7c282f8ba2e743d2d8869d38df8 Mon Sep 17 00:00:00 2001 From: songqi Date: Wed, 15 Feb 2023 15:57:24 +0800 Subject: [PATCH] Fix parser of get and set in class Issue: I6FATM Tests: parser/compiler/tsc/test262 Signed-off-by: songqi Change-Id: Ieed7068312b419dfbf5ad92ecc013958a810683a --- es2panda/parser/parserImpl.cpp | 6 +-- .../classes/test-ts-classes-10-expected.txt | 4 ++ .../conformance/classes/test-ts-classes-10.ts | 50 +++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10-expected.txt create mode 100644 es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10.ts diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index a245d55f62..fd6e60390c 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -2109,7 +2109,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"); @@ -2895,7 +2895,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 +3030,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/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 0000000000..a6d6c2448b --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10-expected.txt @@ -0,0 +1,4 @@ +common set str +common get function +set name success +get first-last 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 0000000000..adef411b77 --- /dev/null +++ b/es2panda/test/compiler/ts/cases/conformance/classes/test-ts-classes-10.ts @@ -0,0 +1,50 @@ +/* + * 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 C { + firstName: string; + lastName: string; + + constructor(firstName: string, lastName: string) { + this.firstName = firstName; + this.lastName = lastName; + } + + get fullName () { + return 'get ' + this.firstName + '-' + this.lastName; + } + + set fullName (val) { + let names = val.split('-'); + this.firstName = names[0]; + this.lastName = names[1]; + print("set name success"); + } + + set (a:T) { + print(`common set ${a}`); + } + + get () { + print(`common get function`); + } +} + +var c1 = new C("", ""); +c1.set("str"); +c1.get(); +c1.fullName = "first-last"; +print(c1.fullName); -- Gitee