diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index d9970074638f4d87b7610d70336eedddf0b3a7ca..6f4f677e275e674c29330aed5abbf4a477cfba00 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 0000000000000000000000000000000000000000..b5087a482b4b6bc37b878d3be1d44f53cddc0e35 --- /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 bfc4c833fee12b73b0133ec6f9476fbc1b61fa0b..7ffcda40f0ff305c46f1a87d8bd39e9c1f6d0030 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."