From 064f6038089b32702f9e5c5be24c6af5c82117d4 Mon Sep 17 00:00:00 2001 From: Torok Gergo Date: Tue, 22 Jul 2025 11:40:34 +0200 Subject: [PATCH] Fix constructor func type error Reason: Currently misleading error message on constructor function types Description: Extending parser with basic parsing of constructor function types. Adding new diagnostic error message Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICO2QK Signed-off-by: Torok Gergo --- ets2panda/parser/ETSparser.cpp | 9 +++++++- .../parser/ets/constructorFunctionType_n.ets | 21 +++++++++++++++++++ ets2panda/util/diagnostic/syntax.yaml | 4 ++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/parser/ets/constructorFunctionType_n.ets diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index d997007463..6f4f677e27 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -837,13 +837,20 @@ ir::TSTypeAliasDeclaration *ETSParser::ParseTypeAliasDeclaration() ExpectToken(lexer::TokenType::PUNCTUATOR_SUBSTITUTION); + if (Lexer()->TryEatTokenFromKeywordType(lexer::TokenType::KEYW_NEW)) { + LogError(diagnostic::CONSTRUCTOR_FUNC_TYPE_NOT_SUPPORTED, {}, typeStart); + typeAliasDecl->SetTsTypeAnnotation(AllocBrokenType(typeStart)); + } + TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::REPORT_ERROR | TypeAnnotationParsingOptions::TYPE_ALIAS_CONTEXT; ir::TypeNode *typeAnnotation = ParseTypeAnnotation(&options); if (typeAnnotation == nullptr) { return nullptr; } - typeAliasDecl->SetTsTypeAnnotation(typeAnnotation); + if (typeAliasDecl->TypeAnnotation() == nullptr) { + typeAliasDecl->SetTsTypeAnnotation(typeAnnotation); + } typeAnnotation->SetParent(typeAliasDecl); typeAliasDecl->SetRange({typeStart, Lexer()->GetToken().End()}); return typeAliasDecl; diff --git a/ets2panda/test/ast/parser/ets/constructorFunctionType_n.ets b/ets2panda/test/ast/parser/ets/constructorFunctionType_n.ets new file mode 100644 index 0000000000..b5087a482b --- /dev/null +++ b/ets2panda/test/ast/parser/ets/constructorFunctionType_n.ets @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 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 Person { + constructor(name: string,age: number) {} +} +type PersonCtor = new (name: string, age: number) => Person + +/* @@? 19:1 Error SyntaxError: Constructor function types are not supported. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index bfc4c833fe..7ffcda40f0 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -145,6 +145,10 @@ syntax: id: 127 message: "Conflicting modifiers '!' and '?' on field." +- name: CONSTRUCTOR_FUNC_TYPE_NOT_SUPPORTED + id: 93489 + message: "Constructor function types are not supported." + - name: DECALRE_IN_AMBIENT_CONTEXT id: 104 message: "A 'declare' modifier cannot be used in an already ambient context." -- Gitee