diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index f8425e77904eaf9590a4c419c9bb0edf3bacdc9e..7790230f0dce6024016e4495e20c46985d90b43c 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -364,6 +364,11 @@ ir::Expression *ETSParser::ParsePrimaryExpression(ExpressionParseFlags flags) ParseTypeAliasDeclaration(); // Try to parse type alias and drop the result. return AllocBrokenExpression(rangeToken); } + case lexer::TokenType::KEYW_FUNCTION: { + LogError(diagnostic::FUNC_EXPR); + ParseFunctionDeclaration(true, ir::ModifierFlags::NONE); + return AllocBrokenExpression(Lexer()->GetToken().Loc()); + } case lexer::TokenType::PUNCTUATOR_FORMAT: { return ParseExpressionFormatPlaceholder(); } @@ -685,6 +690,11 @@ ir::Expression *ETSParser::ParseNewExpression() ir::Expression *ETSParser::ParseAsyncExpression() { Lexer()->NextToken(); // eat 'async' + if (Lexer()->GetToken().Type() == lexer::TokenType::KEYW_FUNCTION) { + LogError(diagnostic::FUNC_EXPR); + ParseFunctionDeclaration(true, ir::ModifierFlags::NONE); + return AllocBrokenExpression(Lexer()->GetToken().Loc()); + } if (Lexer()->GetToken().Type() != lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS || !IsArrowFunctionExpressionStart()) { LogExpectedToken(lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS); diff --git a/ets2panda/test/ast/compiler/ets/arrow_function_call_as_record_property_key.ets b/ets2panda/test/ast/compiler/ets/arrow_function_call_as_record_property_key.ets index 041edd9bf57c48566d18bb9426596382a8e2868c..b989234f62ac33da449ea53cd390b7fe8df7dad7 100644 --- a/ets2panda/test/ast/compiler/ets/arrow_function_call_as_record_property_key.ets +++ b/ets2panda/test/ast/compiler/ets/arrow_function_call_as_record_property_key.ets @@ -24,29 +24,6 @@ const b: Record number> = { [(():number => +("bar"))()]: (y: string):number => y.length }; -/* @@? 21:21 Error TypeError: Bad operand type, the type of the operand must be numeric type. */ -/* @@? 21:34 Error SyntaxError: Unexpected token 'function'. */ -/* @@? 21:45 Error SyntaxError: Unexpected token, expected ',' or ')'. */ -/* @@? 21:45 Error SyntaxError: Unexpected token. */ -/* @@? 21:47 Error TypeError: Type 'String' is not compatible with type 'Double' at index 1 */ -/* @@? 21:47 Error TypeError: Type name 'string' used in the wrong context */ -/* @@? 21:53 Error SyntaxError: Unexpected token, expected ':'. */ -/* @@? 21:54 Error SyntaxError: Unexpected token ':'. */ -/* @@? 21:56 Error SyntaxError: Unexpected token. */ -/* @@? 22:9 Error SyntaxError: return keyword should be used in function body. */ -/* @@? 22:16 Error TypeError: All return statements in the function should be empty or have a value. */ -/* @@? 22:16 Error TypeError: Unresolved reference y */ -/* @@? 23:5 Error SyntaxError: Unexpected token '}'. */ -/* @@? 23:6 Error SyntaxError: Unexpected token ','. */ -/* @@? 23:6 Error TypeError: Indexed access is not supported for such expression type. */ -/* @@? 24:32 Error SyntaxError: Unexpected token ':'. */ -/* @@? 24:36 Error SyntaxError: Unexpected token ':'. */ -/* @@? 24:36 Error SyntaxError: Unexpected token, expected ',' or ')'. */ -/* @@? 24:36 Error SyntaxError: Unexpected token ':'. */ -/* @@? 24:38 Error SyntaxError: Unexpected token 'string'. */ -/* @@? 24:38 Error TypeError: Type name 'string' used in the wrong context */ -/* @@? 24:44 Error SyntaxError: Unexpected token ')'. */ -/* @@? 24:45 Error SyntaxError: Unexpected token ':'. */ -/* @@? 24:46 Error SyntaxError: Unexpected token 'number'. */ -/* @@? 24:46 Error TypeError: The type of parameter 'number' cannot be inferred */ -/* @@? 25:1 Error SyntaxError: Unexpected token '}'. */ +/* @@? 21:21 Error TypeError: Bad operand type, the type of the operand must be numeric type. */ +/* @@? 21:34 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 24:21 Error TypeError: Bad operand type, the type of the operand must be numeric type. */ diff --git a/ets2panda/test/ast/compiler/ets/async-function-expression1.ets b/ets2panda/test/ast/compiler/ets/async-function-expression1.ets new file mode 100644 index 0000000000000000000000000000000000000000..de02a742451ceee41853731115e253ec22a218be --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/async-function-expression1.ets @@ -0,0 +1,54 @@ +/* + * 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. + */ + +function relationalStoreCustomDirTest() { + it(async function () {}) + + it(async function () { + let storestore = null; + let u8 = new Uint8Array([1, 2, 3]); + const valueBucket = { + "name": "", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + let ret = await storestore.insert("test", valueBucket); + }) + + it(async function () { + const STORE_CONFIG = { + name: "", + securityLevel: data_Rdb.SecurityLevel.S1, + customDir: "" + } + let storestore = await data_Rdb.getRdbStore(context, STORE_CONFIG); + await storestore.executeSql(CREATE_TABLE_TEST, null); + let u8 = new Uint8Array([1, 2, 3]); + const valueBucket = { + "name": "", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + let ret = await storestore.insert("test", valueBucket); + }) +} +/* @@? 17:5 Error TypeError: Unresolved reference it */ +/* @@? 17:14 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 19:5 Error TypeError: This expression is not callable. */ +/* @@? 19:14 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 31:5 Error TypeError: This expression is not callable. */ +/* @@? 31:14 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ diff --git a/ets2panda/test/ast/compiler/ets/async-function-expression2.ets b/ets2panda/test/ast/compiler/ets/async-function-expression2.ets new file mode 100644 index 0000000000000000000000000000000000000000..c4fef79dcf25a122cf68f790e788854816ff7fc9 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/async-function-expression2.ets @@ -0,0 +1,66 @@ +/* + * 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. + */ + +function relationalStoreCustomDirTest() { + it(async function function() {}) + + it(async function function() { + let storestore = null; + let u8 = new Uint8Array([1, 2, 3]); + const valueBucket = { + "name": "", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + let ret = await storestore.insert("test", valueBucket); + }) + + it(async function function() { + const STORE_CONFIG = { + name: "", + securityLevel: data_Rdb.SecurityLevel.S1, + customDir: "" + } + let storestore = await data_Rdb.getRdbStore(context, STORE_CONFIG); + await storestore.executeSql(CREATE_TABLE_TEST, null); + let u8 = new Uint8Array([1, 2, 3]); + const valueBucket = { + "name": "", + "age": 18, + "salary": 100.5, + "blobType": u8, + } + let ret = await storestore.insert("test", valueBucket); + }) +} +/* @@? 17:5 Error TypeError: Unresolved reference it */ +/* @@? 17:14 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 17:23 Error SyntaxError: Unexpected token, expected '('. */ +/* @@? 17:31 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 17:31 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 17:31 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 19:5 Error TypeError: This expression is not callable. */ +/* @@? 19:14 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 19:23 Error SyntaxError: Unexpected token, expected '('. */ +/* @@? 19:31 Error SyntaxError: Unexpected token, expected ',' or ')'. */ +/* @@? 19:31 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 19:31 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 31:5 Error TypeError: This expression is not callable. */ +/* @@? 31:14 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 31:23 Error SyntaxError: Unexpected token, expected '('. */ +/* @@? 31:31 Error SyntaxError: Unexpected token, expected an identifier. */ +/* @@? 31:31 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ +/* @@? 31:31 Error SyntaxError: Unexpected token, expected ',' or ')'. */ diff --git a/ets2panda/test/ast/compiler/ets/invalid_two_functions.ets b/ets2panda/test/ast/compiler/ets/invalid_two_functions.ets index 62d52f75f5cb2155a2d76209841e60a88aca9c41..f4850751e54e1f9d72894560bd436e6a932569d9 100644 --- a/ets2panda/test/ast/compiler/ets/invalid_two_functions.ets +++ b/ets2panda/test/ast/compiler/ets/invalid_two_functions.ets @@ -20,5 +20,6 @@ const b = a.map(function (e) { return e * 2; }); -/* @@? 16:2 Error SyntaxError: Unexpected token 'function'. */ -/* @@? 19:17 Error SyntaxError: Unexpected token 'function'. */ +/* @@? 16:2 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 19:17 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 19:28 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ diff --git a/ets2panda/test/ast/parser/ets/empty_array_map_inference_fail.ets b/ets2panda/test/ast/parser/ets/empty_array_map_inference_fail.ets index 0d648f4ea72508aa1f13ee99f58bafac51877434..e085a3efaffb4ed91f7fcca39ce993d05794b0f6 100644 --- a/ets2panda/test/ast/parser/ets/empty_array_map_inference_fail.ets +++ b/ets2panda/test/ast/parser/ets/empty_array_map_inference_fail.ets @@ -21,6 +21,7 @@ return x+1; }); -/* @@? 16:2 Error SyntaxError: Unexpected token 'function'. */ +/* @@? 16:2 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ /* @@? 20:1 Error TypeError: Can't resolve array type */ -/* @@? 20:8 Error SyntaxError: Unexpected token 'function'. */ +/* @@? 20:8 Error SyntaxError: Function expressions are not supported, use arrow functions instead */ +/* @@? 20:18 Error SyntaxError: Parameter declaration should have an explicit type annotation. */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index a412c35ab549ada14459b3f41c431bdf0075ed09..015b0a355c14fb617ef27710857762ea9810001d 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -1230,4 +1230,8 @@ syntax: - name: DEEP_NESTING id: 305 - message: "Maximum allowed nesting level exceeded." \ No newline at end of file + message: "Maximum allowed nesting level exceeded." + +- name: FUNC_EXPR + id: 306 + message: "Function expressions are not supported, use arrow functions instead" \ No newline at end of file diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index d9253e6b53d02f151452dda4d40ed4f699a8850d..35b894e041414bf35872d00ccfccb2ecc943daca 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -878,7 +878,7 @@ static ir::AstNode *GetSpecifier(const util::StringView &importedLocal, ir::ETSI return decl->Specifiers()[0]; } ES2PANDA_UNREACHABLE(); -}; +} std::pair ETSBinder::FindImportDeclInReExports( const ir::ETSImportDeclaration *const import, const util::StringView &imported,