diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 726d66afd647379164df0f3258aff32da548fb73..53117acfb4847c7612f75ba5ea2032a589ea9fb5 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -207,9 +207,15 @@ ir::Expression *ETSParser::ParsePropertyDefinition(ExpressionParseFlags flags) ir::Expression *value = ParsePropertyValue(&propertyKind, &methodStatus, flags); lexer::SourcePosition end = value->End(); - auto *returnProperty = - AllocNode(propertyKind, key, value, methodStatus != ParserStatus::NO_OPTS, isComputed); - returnProperty->SetRange({start, end}); + ir::Expression *returnProperty = nullptr; + if (propertyKind == ir::PropertyKind::INIT) { + returnProperty = + AllocNode(propertyKind, key, value, methodStatus != ParserStatus::NO_OPTS, isComputed); + returnProperty->SetRange({start, end}); + } else { + returnProperty = AllocBrokenExpression(key->Start()); + LogError(diagnostic::OBJECT_PATTER_CONTAIN_METHODS, {}, returnProperty->Start()); + } return returnProperty; } diff --git a/ets2panda/test/ast/parser/ets/invalid_object_literal.ets b/ets2panda/test/ast/parser/ets/invalid_object_literal.ets new file mode 100644 index 0000000000000000000000000000000000000000..4929e16649b88eca1af1fade82601c60320000c0 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/invalid_object_literal.ets @@ -0,0 +1,41 @@ +/* + * 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 A { + public _num: number = 0; + get num(): number { + return this._num; + } + + set num(n: number) { + this._num = n; + } +} + +let a: A = /* @@ label1 */{ + _num:0, + + get /* @@ label2 */num(): number { + return this._num; + }, + + set /* @@ label3 */num(n: number) { + this._num = n; + } +} + +/* @@@ label1 Error TypeError: The object literal properties must be key-value pairs */ +/* @@@ label2 Error SyntaxError: Object pattern can't contain methods. */ +/* @@@ label3 Error SyntaxError: Object pattern can't contain methods. */ diff --git a/ets2panda/test/ast/parser/ets/unexpected_token_52.ets b/ets2panda/test/ast/parser/ets/unexpected_token_52.ets index 7dee5c70d50cde4aa297b0f425cc4c8cb41a77e9..b447ab92d520874dc11995c29a7c4d1c8fd66ce7 100644 --- a/ets2panda/test/ast/parser/ets/unexpected_token_52.ets +++ b/ets2panda/test/ast/parser/ets/unexpected_token_52.ets @@ -13,10 +13,11 @@ * limitations under the License. */ -let a = { - get [b/* @@ label */)(){}, +let /* @@ label1 */a = { + get [/* @@ label2 */b/* @@ label3 */)(){}, ...a, } -/* @@@ label Error SyntaxError: Unexpected token, expected ']'. */ -/* @@? 16:5 Error TypeError: Cannot infer type for a because class composite needs an explicit target type */ +/* @@@ label1 Error TypeError: Cannot infer type for a because class composite needs an explicit target type */ +/* @@@ label2 Error SyntaxError: Object pattern can't contain methods. */ +/* @@@ label3 Error SyntaxError: Unexpected token, expected ']'. */