diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index d1dc137a335d7826eddc28f5a1818ab58c1449e0..7a70ba30413367cd8bf5d2465ca3a438e9e7c3ea 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -563,6 +563,11 @@ ir::ScriptFunction *ETSParser::ParseFunction(ParserStatus newStatus) LogError(diagnostic::ASYNC_IN_AMBIENT_CONTEXT); } + if (isDeclare && signature.ReturnType() != nullptr) { + // Note: if the return type annotation is null, the error handler will set later. + endLoc = signature.ReturnType()->Range().end; + } + // clang-format off ir::ModifierFlags mFlags = isDeclare ? ir::ModifierFlags::DECLARE : ir::ModifierFlags::NONE; ir::ScriptFunctionFlags funcFlags = diff --git a/ets2panda/test/compiler/ets/ambient_declaration-expected.txt b/ets2panda/test/compiler/ets/ambient_declaration-expected.txt index f7106d19ffa27a6cc09c2c9b85b34666c0c7e000..a8179e6b22d9142cc9da38ad1c5a57408728d0d2 100644 --- a/ets2panda/test/compiler/ets/ambient_declaration-expected.txt +++ b/ets2panda/test/compiler/ets/ambient_declaration-expected.txt @@ -342,7 +342,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -359,7 +359,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -372,7 +372,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -385,7 +385,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -398,7 +398,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -418,7 +418,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -434,7 +434,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -454,7 +454,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -468,7 +468,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -482,7 +482,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -495,7 +495,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } @@ -510,7 +510,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 50, "program": "ambient_declaration.ets" } } diff --git a/ets2panda/test/parser/ets/ambient_call_signature-expected.txt b/ets2panda/test/parser/ets/ambient_call_signature-expected.txt index cac03c53accbad9b91c8b279b277ba1c22d8a477..1ac57e4dae489621ec8d28686dd9770746d1c1e9 100644 --- a/ets2panda/test/parser/ets/ambient_call_signature-expected.txt +++ b/ets2panda/test/parser/ets/ambient_call_signature-expected.txt @@ -209,7 +209,7 @@ }, "end": { "line": 17, - "column": 5, + "column": 22, "program": "ambient_call_signature.ets" } } @@ -222,7 +222,7 @@ }, "end": { "line": 17, - "column": 5, + "column": 22, "program": "ambient_call_signature.ets" } } @@ -237,7 +237,7 @@ }, "end": { "line": 17, - "column": 5, + "column": 22, "program": "ambient_call_signature.ets" } } diff --git a/ets2panda/test/parser/ets/ambient_object_iterable-expected.txt b/ets2panda/test/parser/ets/ambient_object_iterable-expected.txt index b63cb1f23429598d1e245dbaa99854938a4270b4..f72f09baaafe1e4a5bed3f9d0f9bc7c03116fe9e 100644 --- a/ets2panda/test/parser/ets/ambient_object_iterable-expected.txt +++ b/ets2panda/test/parser/ets/ambient_object_iterable-expected.txt @@ -633,7 +633,7 @@ }, "end": { "line": 23, - "column": 16, + "column": 42, "program": "ambient_object_iterable.ets" } } @@ -646,7 +646,7 @@ }, "end": { "line": 23, - "column": 16, + "column": 42, "program": "ambient_object_iterable.ets" } } @@ -661,7 +661,7 @@ }, "end": { "line": 23, - "column": 16, + "column": 42, "program": "ambient_object_iterable.ets" } } @@ -972,7 +972,7 @@ }, "end": { "line": 29, - "column": 20, + "column": 33, "program": "ambient_object_iterable.ets" } } @@ -985,7 +985,7 @@ }, "end": { "line": 29, - "column": 20, + "column": 33, "program": "ambient_object_iterable.ets" } } @@ -1000,7 +1000,7 @@ }, "end": { "line": 29, - "column": 20, + "column": 33, "program": "ambient_object_iterable.ets" } } diff --git a/ets2panda/test/parser/ets/declare_class-expected.txt b/ets2panda/test/parser/ets/declare_class-expected.txt index 3d702f66a6158571c7819d50dba55febb89281ed..de2146e91036722eb2c862183b348d391dcf281d 100644 --- a/ets2panda/test/parser/ets/declare_class-expected.txt +++ b/ets2panda/test/parser/ets/declare_class-expected.txt @@ -319,7 +319,7 @@ }, "end": { "line": 19, - "column": 8, + "column": 27, "program": "declare_class.ets" } } @@ -332,7 +332,7 @@ }, "end": { "line": 19, - "column": 8, + "column": 27, "program": "declare_class.ets" } } @@ -347,7 +347,7 @@ }, "end": { "line": 19, - "column": 8, + "column": 27, "program": "declare_class.ets" } } @@ -473,7 +473,7 @@ }, "end": { "line": 20, - "column": 15, + "column": 32, "program": "declare_class.ets" } } @@ -486,7 +486,7 @@ }, "end": { "line": 20, - "column": 15, + "column": 32, "program": "declare_class.ets" } } @@ -501,7 +501,7 @@ }, "end": { "line": 20, - "column": 15, + "column": 32, "program": "declare_class.ets" } } diff --git a/ets2panda/test/parser/ets/declare_func-expected.txt b/ets2panda/test/parser/ets/declare_func-expected.txt index 383ca52e3ad8606d9ef1605becf85661f11540b9..16e244d829d316348d41598c1f70265364181cb8 100644 --- a/ets2panda/test/parser/ets/declare_func-expected.txt +++ b/ets2panda/test/parser/ets/declare_func-expected.txt @@ -177,7 +177,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 38, "program": "declare_func.ets" } } @@ -190,7 +190,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 38, "program": "declare_func.ets" } } @@ -205,7 +205,7 @@ }, "end": { "line": 16, - "column": 21, + "column": 38, "program": "declare_func.ets" } } diff --git a/ets2panda/test/parser/ets/dynamic_import_tests/modules/module-expected.txt b/ets2panda/test/parser/ets/dynamic_import_tests/modules/module-expected.txt index 8e6226d91a7bd7c1acf4e92c8e3d689c94fa9109..5bdd4b315aedc072e3d5e0db126775178a8097c2 100644 --- a/ets2panda/test/parser/ets/dynamic_import_tests/modules/module-expected.txt +++ b/ets2panda/test/parser/ets/dynamic_import_tests/modules/module-expected.txt @@ -629,7 +629,7 @@ }, "end": { "line": 25, - "column": 8, + "column": 27, "program": "module.ets" } } @@ -642,7 +642,7 @@ }, "end": { "line": 25, - "column": 8, + "column": 27, "program": "module.ets" } } @@ -657,7 +657,7 @@ }, "end": { "line": 25, - "column": 8, + "column": 27, "program": "module.ets" } } @@ -735,7 +735,7 @@ }, "end": { "line": 26, - "column": 15, + "column": 25, "program": "module.ets" } } @@ -748,7 +748,7 @@ }, "end": { "line": 26, - "column": 15, + "column": 25, "program": "module.ets" } } @@ -763,7 +763,7 @@ }, "end": { "line": 26, - "column": 15, + "column": 25, "program": "module.ets" } } @@ -933,7 +933,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -950,7 +950,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -963,7 +963,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -976,7 +976,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -989,7 +989,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1009,7 +1009,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1025,7 +1025,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1045,7 +1045,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1059,7 +1059,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1073,7 +1073,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1086,7 +1086,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1101,7 +1101,7 @@ }, "end": { "line": 27, - "column": 17, + "column": 37, "program": "module.ets" } } @@ -1959,7 +1959,7 @@ }, "end": { "line": 36, - "column": 18, + "column": 26, "program": "module.ets" } } @@ -1972,7 +1972,7 @@ }, "end": { "line": 36, - "column": 18, + "column": 26, "program": "module.ets" } } @@ -1987,7 +1987,7 @@ }, "end": { "line": 36, - "column": 18, + "column": 26, "program": "module.ets" } } @@ -2685,7 +2685,7 @@ }, "end": { "line": 16, - "column": 28, + "column": 40, "program": "module.ets" } } @@ -2698,7 +2698,7 @@ }, "end": { "line": 16, - "column": 28, + "column": 40, "program": "module.ets" } } @@ -2713,7 +2713,7 @@ }, "end": { "line": 16, - "column": 28, + "column": 40, "program": "module.ets" } } @@ -2883,7 +2883,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2900,7 +2900,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2913,7 +2913,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2926,7 +2926,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2939,7 +2939,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2959,7 +2959,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2975,7 +2975,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -2995,7 +2995,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -3009,7 +3009,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -3023,7 +3023,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -3036,7 +3036,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } @@ -3051,7 +3051,7 @@ }, "end": { "line": 17, - "column": 37, + "column": 50, "program": "module.ets" } } diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_parse_and_verifier_position.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_parse_and_verifier_position.cpp index 664bd9ab176781412b32e12eb78cb87c4b586a6b..5146723f04266ccf7d3c241d12ec81166a9a6d88 100644 --- a/ets2panda/test/unit/plugin/plugin_proceed_to_state_parse_and_verifier_position.cpp +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_parse_and_verifier_position.cpp @@ -85,6 +85,11 @@ export { ClassOutside, val, foo } export { PI, E } from "std/math" export NS + +export declare function fooDecl( + options: number, + myop: string +): void )"; constexpr size_t NS_START_LINE = 11; @@ -114,6 +119,7 @@ constexpr size_t INTERFACE_FOO4_START_LINE = 37; constexpr size_t FUNCTION_PARAM1_START_LINE = 55; constexpr size_t FUNCTION_PARAM2_START_LINE = 56; constexpr size_t FUNCTION_PARAM3_START_LINE = 43; +constexpr size_t FUNCTION_DECL_START_LINE = 66; constexpr size_t NS_END_LINE = 30; constexpr size_t INNERNS_END_LINE = 29; @@ -142,6 +148,7 @@ constexpr size_t INTERFACE_FOO4_END_LINE = 37; constexpr size_t FUNCTION_PARAM1_END_LINE = 55; constexpr size_t FUNCTION_PARAM2_END_LINE = 56; constexpr size_t FUNCTION_PARAM3_END_LINE = 43; +constexpr size_t FUNCTION_DECL_END_LINE = 69; constexpr size_t NS_START_COL = 1; constexpr size_t INNERNS_START_COL = 3; @@ -170,6 +177,7 @@ constexpr size_t INTERFACE_FOO4_START_COL = 5; constexpr size_t FUNCTION_PARAM1_START_COL = 3; constexpr size_t FUNCTION_PARAM2_START_COL = 3; constexpr size_t FUNCTION_PARAM3_START_COL = 11; +constexpr size_t FUNCTION_DECL_START_COL = 8; constexpr size_t NS_END_COL = 2; constexpr size_t INNERNS_END_COL = 23; @@ -198,6 +206,7 @@ constexpr size_t INTERFACE_FOO4_END_COL = 25; constexpr size_t FUNCTION_PARAM1_END_COL = 16; constexpr size_t FUNCTION_PARAM2_END_COL = 16; constexpr size_t FUNCTION_PARAM3_END_COL = 21; +constexpr size_t FUNCTION_DECL_END_COL = 8; static std::map startLineMap = {{"exportNamedDecl", EXPORT_NAMED_DECL_START_LINE}, {"exportSingleNamedDecl", EXPORT_SINGLE_NAMED_DECL_START_LINE}, @@ -225,7 +234,8 @@ static std::map startLineMap = {{"exportNamedDecl", EXPORT_ {"interfaceFoo4", INTERFACE_FOO4_START_LINE}, {"fooP1", FUNCTION_PARAM1_START_LINE}, {"fooP2", FUNCTION_PARAM2_START_LINE}, - {"p3", FUNCTION_PARAM3_START_LINE}}; + {"p3", FUNCTION_PARAM3_START_LINE}, + {"fooDecl", FUNCTION_DECL_START_LINE}}; static std::map startColMap = {{"exportNamedDecl", EXPORT_NAMED_DECL_START_COL}, {"exportSingleNamedDecl", EXPORT_SINGLE_NAMED_DECL_START_COL}, @@ -253,7 +263,8 @@ static std::map startColMap = {{"exportNamedDecl", EXPORT_N {"interfaceFoo4", INTERFACE_FOO4_START_COL}, {"fooP1", FUNCTION_PARAM1_START_COL}, {"fooP2", FUNCTION_PARAM2_START_COL}, - {"p3", FUNCTION_PARAM3_START_COL}}; + {"p3", FUNCTION_PARAM3_START_COL}, + {"fooDecl", FUNCTION_DECL_START_COL}}; static std::map endLineMap = {{"exportNamedDecl", EXPORT_NAMED_DECL_END_LINE}, {"exportSingleNamedDecl", EXPORT_SINGLE_NAMED_DECL_END_LINE}, @@ -281,7 +292,8 @@ static std::map endLineMap = {{"exportNamedDecl", EXPORT_NA {"interfaceFoo4", INTERFACE_FOO4_END_LINE}, {"fooP1", FUNCTION_PARAM1_END_LINE}, {"fooP2", FUNCTION_PARAM2_END_LINE}, - {"p3", FUNCTION_PARAM3_END_LINE}}; + {"p3", FUNCTION_PARAM3_END_LINE}, + {"fooDecl", FUNCTION_DECL_END_LINE}}; static std::map endColMap = {{"exportNamedDecl", EXPORT_NAMED_DECL_END_COL}, {"exportSingleNamedDecl", EXPORT_SINGLE_NAMED_DECL_END_COL}, @@ -309,11 +321,29 @@ static std::map endColMap = {{"exportNamedDecl", EXPORT_NAM {"interfaceFoo4", INTERFACE_FOO4_END_COL}, {"fooP1", FUNCTION_PARAM1_END_COL}, {"fooP2", FUNCTION_PARAM2_END_COL}, - {"p3", FUNCTION_PARAM3_END_COL}}; + {"p3", FUNCTION_PARAM3_END_COL}, + {"fooDecl", FUNCTION_DECL_END_COL}}; static es2panda_Impl *impl = nullptr; es2panda_Context *context = nullptr; es2panda_AstNode *fooDecl = nullptr; +static void FindFunctionDecl(es2panda_AstNode *ast, [[maybe_unused]] void *ctx) +{ + if (!impl->IsFunctionDeclaration(ast)) { + return; + } + auto scriptFunc = impl->FunctionDeclarationFunction(context, ast); + + auto *ident = impl->ScriptFunctionId(context, scriptFunc); + if (ident == nullptr) { + return; + } + + auto name = std::string(impl->IdentifierName(context, ident)); + if (name == "fooDecl") { + fooDecl = ast; + } +} static std::map namespaceDecl = {{"NS", nullptr}, {"InnerNS", nullptr}}; static void FindNamespaceDecl(es2panda_AstNode *ast, [[maybe_unused]] void *ctx) @@ -475,12 +505,18 @@ static void FindTargetAst(es2panda_AstNode *ast, [[maybe_unused]] void *ctx) impl->AstNodeForEach(ast, FindMethodDef, context); impl->AstNodeForEach(ast, FindETSParamDecl, context); impl->AstNodeForEach(ast, FindEnumMember, context); + impl->AstNodeForEach(ast, FindFunctionDecl, context); } static bool CheckLineAndCol(es2panda_AstNode *ast, std::string name) { auto start = impl->AstNodeStartConst(context, ast); auto end = impl->AstNodeEndConst(context, ast); + [[maybe_unused]] auto a = impl->SourcePositionLine(context, start); + [[maybe_unused]] auto b = impl->SourcePositionCol(context, start); + [[maybe_unused]] auto c = impl->SourcePositionLine(context, end); + [[maybe_unused]] auto d = impl->SourcePositionCol(context, end); + auto res = startLineMap[name] == impl->SourcePositionLine(context, start); ASSERT(startLineMap[name] == impl->SourcePositionLine(context, start)); @@ -501,6 +537,7 @@ static bool CheckAllNode() res &= CheckLineAndCol(exportSingleNamedDecl, "exportSingleNamedDecl"); res &= CheckLineAndCol(reExportedDecl, "reExportedDecl"); res &= CheckLineAndCol(importDecl, "importDecl"); + res &= CheckLineAndCol(fooDecl, "fooDecl"); for (const auto &[name, targetAst] : namespaceDecl) { res &= CheckLineAndCol(targetAst, name); }