From 2c0833d776a785662fab301efa1dde762d79b396 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Tue, 12 Mar 2024 14:42:59 +0100 Subject: [PATCH 1/2] WIP Bogi frontend of PR1454 for CI debug Change-Id: Ifa3b2082371296877be21641c6905a4079a68639 --- es2panda/ir/base/scriptFunction.h | 2 +- ets2panda/checker/types/globalTypesHolder.cpp | 6 +- ets2panda/checker/types/globalTypesHolder.h | 4 +- ets2panda/ir/astNodeFlags.h | 3 +- ets2panda/ir/base/scriptFunction.h | 5 + .../import_name_conflict_10-expected.txt | 348 +++++++++++++++++ .../import_tests/import_name_conflict_10.ets | 20 + .../import_name_conflict_11-expected.txt | 317 +++++++++++++++ .../import_tests/import_name_conflict_11.ets | 20 + .../import_name_conflict_12-expected.txt | 346 +++++++++++++++++ .../import_tests/import_name_conflict_12.ets | 22 ++ .../import_name_conflict_13-expected.txt | 317 +++++++++++++++ .../import_tests/import_name_conflict_13.ets | 20 + .../import_name_conflict_14-expected.txt | 346 +++++++++++++++++ .../import_tests/import_name_conflict_14.ets | 22 ++ .../import_name_conflict_15-expected.txt | 332 ++++++++++++++++ .../import_tests/import_name_conflict_15.ets | 20 + .../import_name_conflict_16-expected.txt | 361 +++++++++++++++++ .../import_tests/import_name_conflict_16.ets | 22 ++ .../import_name_conflict_17-expected.txt | 363 ++++++++++++++++++ .../import_tests/import_name_conflict_17.ets | 20 + .../import_name_conflict_18-expected.txt | 363 ++++++++++++++++++ .../import_tests/import_name_conflict_18.ets | 20 + .../import_name_conflict_19-expected.txt | 363 ++++++++++++++++++ .../import_tests/import_name_conflict_19.ets | 20 + .../import_name_conflict_20-expected.txt | 332 ++++++++++++++++ .../import_tests/import_name_conflict_20.ets | 20 + .../import_name_conflict_21-expected.txt | 361 +++++++++++++++++ .../import_tests/import_name_conflict_21.ets | 22 ++ .../import_name_conflict_22-expected.txt | 332 ++++++++++++++++ .../import_tests/import_name_conflict_22.ets | 20 + .../import_name_conflict_23-expected.txt | 361 +++++++++++++++++ .../import_tests/import_name_conflict_23.ets | 22 ++ .../import_name_conflict_6-expected.txt | 317 +++++++++++++++ .../import_tests/import_name_conflict_6.ets | 20 + .../import_name_conflict_7-expected.txt | 346 +++++++++++++++++ .../import_tests/import_name_conflict_7.ets | 22 ++ .../import_name_conflict_8-expected.txt | 348 +++++++++++++++++ .../import_tests/import_name_conflict_8.ets | 20 + .../import_name_conflict_9-expected.txt | 348 +++++++++++++++++ .../import_tests/import_name_conflict_9.ets | 20 + .../ets/lambdaExpressionWithRestParameter.ets | 6 +- ets2panda/varbinder/ETSBinder.cpp | 81 ++-- 43 files changed, 6645 insertions(+), 35 deletions(-) create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_10-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_10.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_11-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_11.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_12-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_12.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_13-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_13.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_14-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_14.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_15-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_15.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_16-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_16.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_17-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_17.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_18-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_18.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_19-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_19.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_20-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_20.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_21-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_21.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_22-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_22.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_23-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_23.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_6-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_6.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_7-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_7.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_8-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_8.ets create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_9-expected.txt create mode 100644 ets2panda/test/parser/ets/import_tests/import_name_conflict_9.ets diff --git a/es2panda/ir/base/scriptFunction.h b/es2panda/ir/base/scriptFunction.h index 72931dde7a..703c8b8ea0 100644 --- a/es2panda/ir/base/scriptFunction.h +++ b/es2panda/ir/base/scriptFunction.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 diff --git a/ets2panda/checker/types/globalTypesHolder.cpp b/ets2panda/checker/types/globalTypesHolder.cpp index 08cfc89bd1..93e903bf24 100644 --- a/ets2panda/checker/types/globalTypesHolder.cpp +++ b/ets2panda/checker/types/globalTypesHolder.cpp @@ -145,7 +145,7 @@ GlobalTypesHolder::GlobalTypesHolder(ArenaAllocator *allocator) : builtinNameMap builtinNameMappings_.emplace("AssertionError", GlobalTypeId::ETS_ASSERTION_ERROR_BUILTIN); builtinNameMappings_.emplace("DivideByZeroError", GlobalTypeId::ETS_DIVIDE_BY_ZERO_ERROR_BUILTIN); builtinNameMappings_.emplace("NullPointerError", GlobalTypeId::ETS_NULL_POINTER_ERROR_BUILTIN); - builtinNameMappings_.emplace("UncatchedExceptionError", GlobalTypeId::ETS_UNCATCHED_EXCEPTION_ERROR_BUILTIN); + builtinNameMappings_.emplace("UncaughtExceptionError", GlobalTypeId::ETS_UNCAUGHT_EXCEPTION_ERROR_BUILTIN); builtinNameMappings_.emplace("Map", GlobalTypeId::ETS_MAP_BUILTIN); builtinNameMappings_.emplace("RegExp", GlobalTypeId::ETS_REGEXP_BUILTIN); builtinNameMappings_.emplace("Set", GlobalTypeId::ETS_SET_BUILTIN); @@ -502,9 +502,9 @@ Type *GlobalTypesHolder::GlobalNullPointerErrorBuiltinType() return globalTypes_.at(static_cast(GlobalTypeId::ETS_NULL_POINTER_ERROR_BUILTIN)); } -Type *GlobalTypesHolder::GlobalUncatchedExceptionErrorBuiltinType() +Type *GlobalTypesHolder::GlobalUncaughtExceptionErrorBuiltinType() { - return globalTypes_.at(static_cast(GlobalTypeId::ETS_UNCATCHED_EXCEPTION_ERROR_BUILTIN)); + return globalTypes_.at(static_cast(GlobalTypeId::ETS_UNCAUGHT_EXCEPTION_ERROR_BUILTIN)); } Type *GlobalTypesHolder::GlobalETSStringBuiltinType() diff --git a/ets2panda/checker/types/globalTypesHolder.h b/ets2panda/checker/types/globalTypesHolder.h index 2e6e2badaf..f806aa9434 100644 --- a/ets2panda/checker/types/globalTypesHolder.h +++ b/ets2panda/checker/types/globalTypesHolder.h @@ -88,7 +88,7 @@ enum class GlobalTypeId { ETS_ASSERTION_ERROR_BUILTIN, ETS_DIVIDE_BY_ZERO_ERROR_BUILTIN, ETS_NULL_POINTER_ERROR_BUILTIN, - ETS_UNCATCHED_EXCEPTION_ERROR_BUILTIN, + ETS_UNCAUGHT_EXCEPTION_ERROR_BUILTIN, ETS_STRING_BUILTIN, ETS_STRING_BUILDER_BUILTIN, ETS_TYPE_BUILTIN, @@ -232,7 +232,7 @@ public: Type *GlobalAssertionErrorBuiltinType(); Type *GlobalDivideByZeroErrorBuiltinType(); Type *GlobalNullPointerErrorBuiltinType(); - Type *GlobalUncatchedExceptionErrorBuiltinType(); + Type *GlobalUncaughtExceptionErrorBuiltinType(); Type *GlobalMapBuiltinType(); Type *GlobalRegExpBuiltinType(); Type *GlobalSetBuiltinType(); diff --git a/ets2panda/ir/astNodeFlags.h b/ets2panda/ir/astNodeFlags.h index 171c8a5591..66176b384e 100644 --- a/ets2panda/ir/astNodeFlags.h +++ b/ets2panda/ir/astNodeFlags.h @@ -80,7 +80,8 @@ enum class ScriptFunctionFlags : uint32_t { SETTER = 1U << 16U, ENTRY_POINT = 1U << 17U, INSTANCE_EXTENSION_METHOD = 1U << 18U, - HAS_RETURN = 1U << 19U + HAS_RETURN = 1U << 19U, + EXTERNAL_OVERLOAD = 1U << 20U }; enum class TSOperatorType { READONLY, KEYOF, UNIQUE }; diff --git a/ets2panda/ir/base/scriptFunction.h b/ets2panda/ir/base/scriptFunction.h index 6101c12aeb..55de4281f0 100644 --- a/ets2panda/ir/base/scriptFunction.h +++ b/ets2panda/ir/base/scriptFunction.h @@ -150,6 +150,11 @@ public: return (funcFlags_ & ir::ScriptFunctionFlags::OVERLOAD) != 0; } + [[nodiscard]] bool IsExternalOverload() const + { + return (funcFlags_ & ir::ScriptFunctionFlags::EXTERNAL_OVERLOAD) != 0; + } + [[nodiscard]] bool IsConstructor() const noexcept { return (funcFlags_ & ir::ScriptFunctionFlags::CONSTRUCTOR) != 0; diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_10-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_10-expected.txt new file mode 100644 index 0000000000..46f101e574 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_10-expected.txt @@ -0,0 +1,348 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./check_exported_3", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 35 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 42 + } + } +} +SyntaxError: Function 'bar' is already defined with different type. [import_name_conflict_10.ets:16:16] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_10.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_10.ets new file mode 100644 index 0000000000..45fc0cf1f0 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_10.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "./check_exported_3"; + +class bar{}; + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_11-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_11-expected.txt new file mode 100644 index 0000000000..eb0fa94776 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_11-expected.txt @@ -0,0 +1,317 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/relative_import/Point", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 52 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 42 + } + } +} +SyntaxError: Class 'Point' is already defined with different type. [import_name_conflict_11.ets:16:15] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_11.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_11.ets new file mode 100644 index 0000000000..afa06bfc54 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_11.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "import_tests/relative_import/Point"; + +let Point: int = 1; + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_12-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_12-expected.txt new file mode 100644 index 0000000000..85ff0e226d --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_12-expected.txt @@ -0,0 +1,346 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/packages/subpackage-1", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 52 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 50 + } + } +} +SyntaxError: Variable 'foo' is already defined with different type. [import_name_conflict_12.ets:18:3] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_12.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_12.ets new file mode 100644 index 0000000000..6bc116caf8 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_12.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "import_tests/packages/subpackage-1"; + +function foo(): int { + return 42; +} + +//Throw error: Variable 'foo' is already defined. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_13-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_13-expected.txt new file mode 100644 index 0000000000..f636ec1cfc --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_13-expected.txt @@ -0,0 +1,317 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./check_exported_3", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 35 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 42 + } + } +} +SyntaxError: Function 'bar' is already defined with different type. [import_name_conflict_13.ets:16:16] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_13.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_13.ets new file mode 100644 index 0000000000..3a25e0da4f --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_13.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "./check_exported_3"; + +let bar: int = 1; + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_14-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_14-expected.txt new file mode 100644 index 0000000000..c8104cf52f --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_14-expected.txt @@ -0,0 +1,346 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/relative_import/Point", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 52 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 42 + } + } +} +SyntaxError: Class 'Point' is already defined with different type. [import_name_conflict_14.ets:16:15] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_14.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_14.ets new file mode 100644 index 0000000000..cd9d03c0e5 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_14.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "import_tests/relative_import/Point"; + +function Point(): int { + return 42; +} + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_15-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_15-expected.txt new file mode 100644 index 0000000000..cdf8389c14 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_15-expected.txt @@ -0,0 +1,332 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/packages/subpackage-1", + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 55 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 56 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 50 + } + } +} +SyntaxError: Variable 'foo' is already defined. [import_name_conflict_15.ets:16:19] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_15.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_15.ets new file mode 100644 index 0000000000..bee0d7e790 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_15.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import {foo} from "import_tests/packages/subpackage-1"; + +let foo: int = 1; + +//Throw error: Variable 'foo' is already defined. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_16-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_16-expected.txt new file mode 100644 index 0000000000..dd81bc1933 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_16-expected.txt @@ -0,0 +1,361 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./check_exported_3", + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 39 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "imported": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 42 + } + } +} +SyntaxError: Function 'bar' is already defined. [import_name_conflict_16.ets:16:19] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_16.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_16.ets new file mode 100644 index 0000000000..1e83a642cc --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_16.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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. + */ + +import {bar} from "./check_exported_3"; + +function bar(): int { + return 42; +} + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_17-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_17-expected.txt new file mode 100644 index 0000000000..7b96d815f0 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_17-expected.txt @@ -0,0 +1,363 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/relative_import/Point", + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 16, + "column": 57 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "imported": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 58 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 43 + } + } +} +SyntaxError: Class 'Point' is already defined. [import_name_conflict_17.ets:16:21] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_17.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_17.ets new file mode 100644 index 0000000000..100fb20867 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_17.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import {Point} from "import_tests/relative_import/Point"; + +class Point {} + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_18-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_18-expected.txt new file mode 100644 index 0000000000..af7868ba66 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_18-expected.txt @@ -0,0 +1,363 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/packages/subpackage-1", + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 55 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 56 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 50 + } + } +} +SyntaxError: Variable 'foo' is already defined with different type. [import_name_conflict_18.ets:16:19] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_18.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_18.ets new file mode 100644 index 0000000000..88479ddac1 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_18.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import {foo} from "import_tests/packages/subpackage-1"; + +class foo{}; + +//Throw error: Variable 'foo' is already defined. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_19-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_19-expected.txt new file mode 100644 index 0000000000..ec9a2c8dd8 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_19-expected.txt @@ -0,0 +1,363 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./check_exported_3", + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 39 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "imported": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 42 + } + } +} +SyntaxError: Function 'bar' is already defined with different type. [import_name_conflict_19.ets:16:19] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_19.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_19.ets new file mode 100644 index 0000000000..4fe39ebe2f --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_19.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import {bar} from "./check_exported_3"; + +class bar{}; + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_20-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_20-expected.txt new file mode 100644 index 0000000000..a738668091 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_20-expected.txt @@ -0,0 +1,332 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/relative_import/Point", + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 16, + "column": 57 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "imported": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 58 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 20 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 42 + } + } +} +SyntaxError: Class 'Point' is already defined with different type. [import_name_conflict_20.ets:16:21] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_20.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_20.ets new file mode 100644 index 0000000000..cabdd45231 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_20.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import {Point} from "import_tests/relative_import/Point"; + +let Point: int = 1; + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_21-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_21-expected.txt new file mode 100644 index 0000000000..c9c984faaa --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_21-expected.txt @@ -0,0 +1,361 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/packages/subpackage-1", + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 55 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "imported": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 56 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 50 + } + } +} +SyntaxError: Variable 'foo' is already defined with different type. [import_name_conflict_21.ets:16:19] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_21.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_21.ets new file mode 100644 index 0000000000..4dfc9aa6d5 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_21.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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. + */ + +import {foo} from "import_tests/packages/subpackage-1"; + +function foo(): int { + return 42; +} + +//Throw error: Variable 'foo' is already defined. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_22-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_22-expected.txt new file mode 100644 index 0000000000..8c9b2d887e --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_22-expected.txt @@ -0,0 +1,332 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./check_exported_3", + "loc": { + "start": { + "line": 16, + "column": 19 + }, + "end": { + "line": 16, + "column": 39 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "imported": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 40 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 42 + } + } +} +SyntaxError: Function 'bar' is already defined with different type. [import_name_conflict_22.ets:16:19] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_22.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_22.ets new file mode 100644 index 0000000000..2734690853 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_22.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import {bar} from "./check_exported_3"; + +let bar: int = 1; + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_23-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_23-expected.txt new file mode 100644 index 0000000000..32df1b1dc6 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_23-expected.txt @@ -0,0 +1,361 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/relative_import/Point", + "loc": { + "start": { + "line": 16, + "column": 21 + }, + "end": { + "line": 16, + "column": 57 + } + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "local": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "imported": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 58 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 22 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 42 + } + } +} +SyntaxError: Class 'Point' is already defined with different type. [import_name_conflict_23.ets:16:21] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_23.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_23.ets new file mode 100644 index 0000000000..d8af89cb24 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_23.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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. + */ + +import {Point} from "import_tests/relative_import/Point"; + +function Point(): int { + return 42; +} + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_6-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_6-expected.txt new file mode 100644 index 0000000000..136b813337 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_6-expected.txt @@ -0,0 +1,317 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/packages/subpackage-1", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 52 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 18, + "column": 16 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 18 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 8 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 13 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 50 + } + } +} +SyntaxError: Variable 'foo' is already defined. [import_name_conflict_6.ets:18:3] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_6.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_6.ets new file mode 100644 index 0000000000..c2d1c7738d --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_6.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "import_tests/packages/subpackage-1"; + +let foo: int = 1; + +//Throw error: Variable 'foo' is already defined. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_7-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_7-expected.txt new file mode 100644 index 0000000000..3491fb34b0 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_7-expected.txt @@ -0,0 +1,346 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "./check_exported_3", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 35 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 36 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "bar", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 22, + "column": 42 + } + } +} +TypeError: Function already declared. [import_name_conflict_7.ets:16:7] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_7.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_7.ets new file mode 100644 index 0000000000..8308115a8a --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_7.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "./check_exported_3"; + +function bar(): int { + return 42; +} + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_8-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_8-expected.txt new file mode 100644 index 0000000000..e96afebf14 --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_8-expected.txt @@ -0,0 +1,348 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/relative_import/Point", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 52 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Point", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 15 + }, + "end": { + "line": 18, + "column": 15 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 15 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 42 + } + } +} +SyntaxError: Class 'Point' is already defined. [import_name_conflict_8.ets:16:15] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_8.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_8.ets new file mode 100644 index 0000000000..31758023fb --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_8.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "import_tests/relative_import/Point"; + +class Point {} + +//Throw error: Function already declared. \ No newline at end of file diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_9-expected.txt b/ets2panda/test/parser/ets/import_tests/import_name_conflict_9-expected.txt new file mode 100644 index 0000000000..146ec60e3c --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_9-expected.txt @@ -0,0 +1,348 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ImportDeclaration", + "source": { + "type": "StringLiteral", + "value": "import_tests/packages/subpackage-1", + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 51 + } + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "local": { + "type": "Identifier", + "name": "", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 14 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 52 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 7 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 12 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 10 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 18, + "column": 12 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 20, + "column": 50 + } + } +} +SyntaxError: Variable 'foo' is already defined with different type. [import_name_conflict_9.ets:18:3] diff --git a/ets2panda/test/parser/ets/import_tests/import_name_conflict_9.ets b/ets2panda/test/parser/ets/import_tests/import_name_conflict_9.ets new file mode 100644 index 0000000000..8a9b2ba15d --- /dev/null +++ b/ets2panda/test/parser/ets/import_tests/import_name_conflict_9.ets @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 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. + */ + +import * from "import_tests/packages/subpackage-1"; + +class foo{}; + +//Throw error: Variable 'foo' is already defined. \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/lambdaExpressionWithRestParameter.ets b/ets2panda/test/runtime/ets/lambdaExpressionWithRestParameter.ets index b6f03111fa..942e971223 100644 --- a/ets2panda/test/runtime/ets/lambdaExpressionWithRestParameter.ets +++ b/ets2panda/test/runtime/ets/lambdaExpressionWithRestParameter.ets @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 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 @@ -13,12 +13,12 @@ * limitations under the License. */ -type LambdaType = (prefix: string, ...args: string[]) => string; +type LambdaTypeTest = (prefix: string, ...args: string[]) => string; function main(): void { - let lam: LambdaType = (prefix: string, ...args: string[]): string => { + let lam: LambdaTypeTest = (prefix: string, ...args: string[]): string => { let ret = prefix; for (let arg of args) { ret += arg; diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 13cef4418b..2e64deb302 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -14,6 +14,7 @@ */ #include "ETSBinder.h" +#include #include "ir/expressions/blockExpression.h" #include "ir/expressions/identifier.h" @@ -60,6 +61,7 @@ #include "util/ustring.h" #include "checker/types/type.h" #include "checker/types/ets/types.h" +#include "variable.h" namespace ark::es2panda::varbinder { @@ -473,7 +475,23 @@ void ETSBinder::ImportAllForeignBindings(ir::AstNode *const specifier, continue; } - if (!importGlobalScope->IsForeignBinding(bindingName) && !var->Declaration()->Node()->IsDefaultExported()) { + const bool exported = var->Declaration()->Node()->IsClassDefinition() + ? var->Declaration()->Node()->Parent()->IsExported() + : var->Declaration()->Node()->IsExported(); + if (!importGlobalScope->IsForeignBinding(bindingName) && !var->Declaration()->Node()->IsDefaultExported() && + exported) { + auto variable = Program()->GlobalClassScope()->FindLocal(bindingName, ResolveBindingOptions::ALL); + if (variable != nullptr && var != variable) { + auto type = var->Declaration()->Node()->IsClassDefinition() ? "Class '" + : var->Declaration()->IsFunctionDecl() ? "Function '" + : "Variable '"; + auto str = util::Helpers::AppendAll(type, bindingName.Utf8(), "'"); + + str += variable->Declaration()->Type() == var->Declaration()->Type() + ? " is already defined." + : " is already defined with different type."; + ThrowError(import->Source()->Start(), str); + } InsertForeignBinding(specifier, import, bindingName, var); } } @@ -615,7 +633,6 @@ bool ETSBinder::AddImportSpecifiersToTopBindings(ir::AstNode *const specifier, TopScope()->InsertForeignBinding(name, var); }; - const auto *const importSpecifier = specifier->AsImportSpecifier(); if (!importSpecifier->Imported()->IsIdentifier()) { @@ -713,20 +730,10 @@ void ETSBinder::AddSpecifiersToTopBindings(ir::AstNode *const specifier, const i const auto *const importGlobalScope = importProgram->GlobalScope(); const auto &globalBindings = importGlobalScope->Bindings(); - auto insertForeignBinding = [this, specifier, import](const util::StringView &name, Variable *var) { - if (import->Language().IsDynamic()) { - dynamicImportVars_.emplace(var, DynamicImportData {import, specifier, var}); - } - - TopScope()->InsertForeignBinding(name, var); - }; - - if (AddImportNamespaceSpecifiersToTopBindings(specifier, globalBindings, importProgram, importGlobalScope, - import)) { - return; - } - - if (AddImportSpecifiersToTopBindings(specifier, globalBindings, import, record, std::move(viewedReExport))) { + if (importProgram->SourceFilePath() == Program()->SourceFilePath() || + AddImportNamespaceSpecifiersToTopBindings(specifier, globalBindings, importProgram, importGlobalScope, + import) || + AddImportSpecifiersToTopBindings(specifier, globalBindings, import, record, std::move(viewedReExport))) { return; } @@ -735,12 +742,12 @@ void ETSBinder::AddSpecifiersToTopBindings(ir::AstNode *const specifier, const i auto item = std::find_if(globalBindings.begin(), globalBindings.end(), predicateFunc); if (item == globalBindings.end()) { - insertForeignBinding(specifier->AsImportDefaultSpecifier()->Local()->Name(), + InsertForeignBinding(specifier, import, specifier->AsImportDefaultSpecifier()->Local()->Name(), FindStaticBinding(record, importPath)); return; } - insertForeignBinding(specifier->AsImportDefaultSpecifier()->Local()->Name(), item->second); + InsertForeignBinding(specifier, import, specifier->AsImportDefaultSpecifier()->Local()->Name(), item->second); } void ETSBinder::HandleCustomNodes(ir::AstNode *childNode) @@ -1001,6 +1008,31 @@ bool ETSBinder::ImportGlobalPropertiesForNotDefaultedExports(varbinder::Variable return false; } + auto variable = Program()->GlobalClassScope()->FindLocal(name, ResolveBindingOptions::ALL); + if (variable != nullptr && var != variable) { + if (variable->Declaration()->IsFunctionDecl() && var->Declaration()->IsFunctionDecl()) { + auto *const currentNode = variable->Declaration()->Node(); + auto *const method = var->Declaration()->Node()->AsMethodDefinition(); + + if (!currentNode->AsMethodDefinition()->HasOverload(method)) { + currentNode->AsMethodDefinition()->AddOverload(method); + method->Function()->Id()->SetVariable(variable); + method->Function()->AddFlag(ir::ScriptFunctionFlags::OVERLOAD); + method->Function()->AddFlag(ir::ScriptFunctionFlags::EXTERNAL_OVERLOAD); + } + + return true; + } + + auto type = var->Declaration()->Node()->IsClassDefinition() ? "Class '" + : var->Declaration()->IsFunctionDecl() ? "Function '" + : "Variable '"; + auto str = util::Helpers::AppendAll(type, name.Utf8(), "'"); + str += variable->Declaration()->Type() == var->Declaration()->Type() + ? " is already defined." + : " is already defined with different type."; + ThrowError(classElement->Id()->Start(), str); + } const auto insRes = TopScope()->InsertForeignBinding(name, var); if (!(!insRes.second && insRes.first != TopScope()->Bindings().end()) || !(insRes.first->second != var)) { return true; @@ -1012,17 +1044,16 @@ bool ETSBinder::ImportGlobalPropertiesForNotDefaultedExports(varbinder::Variable currentNode->AsMethodDefinition()->AddOverload(method); method->Function()->Id()->SetVariable(insRes.first->second); method->Function()->AddFlag(ir::ScriptFunctionFlags::OVERLOAD); + method->Function()->AddFlag(ir::ScriptFunctionFlags::EXTERNAL_OVERLOAD); } return true; } - auto str = util::Helpers::AppendAll("Variable '", name.Utf8(), "'"); - if (insRes.first->second->Declaration()->Type() == var->Declaration()->Type()) { - str += " is already defined."; - } else { - str += " is already defined with different type."; - } - ThrowError(classElement->Id()->Start(), str); + ThrowError(classElement->Id()->Start(), + util::Helpers::AppendAll("Variable '", name.Utf8(), + insRes.first->second->Declaration()->Type() == var->Declaration()->Type() + ? "' is already defined." + : "' is already defined with different type.")); } void ETSBinder::ImportGlobalProperties(const ir::ClassDefinition *const classDef) -- Gitee From db4b54a6f4a346a214cc527ac2c1d188564e55d4 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Tue, 12 Mar 2024 14:54:25 +0100 Subject: [PATCH 2/2] Siprob path fix patch --- ets2panda/BUILD.gn | 2 +- ets2panda/CMakeLists.txt | 2 +- ets2panda/checker/ETSAnalyzer.cpp | 2 +- ets2panda/checker/ets/helpers.cpp | 2 +- .../ets/topLevelStmts/importExportDecls.cpp | 6 +- .../ets/topLevelStmts/importExportDecls.h | 2 +- ets2panda/parser/ETSparser.cpp | 65 ++-- ets2panda/parser/ETSparser.h | 20 +- ets2panda/util/helpers.cpp | 88 +----- ets2panda/util/helpers.h | 8 +- ets2panda/util/importPathManager.cpp | 269 +++++++++++++++++ ets2panda/util/importPathManager.h | 80 +++++ ets2panda/util/pathHandler.cpp | 283 ------------------ ets2panda/util/pathHandler.h | 200 ------------- ets2panda/varbinder/ETSBinder.cpp | 2 +- ets2panda/varbinder/ETSBinder.h | 20 +- 16 files changed, 433 insertions(+), 618 deletions(-) create mode 100644 ets2panda/util/importPathManager.cpp create mode 100644 ets2panda/util/importPathManager.h delete mode 100644 ets2panda/util/pathHandler.cpp delete mode 100644 ets2panda/util/pathHandler.h diff --git a/ets2panda/BUILD.gn b/ets2panda/BUILD.gn index 8d5a3a07dc..ddf4980bb9 100644 --- a/ets2panda/BUILD.gn +++ b/ets2panda/BUILD.gn @@ -376,8 +376,8 @@ libes2panda_sources = [ "util/declgenEts2Ts.cpp", "util/errorHandler.cpp", "util/helpers.cpp", + "util/importPathManager.cpp", "util/path.cpp", - "util/pathHandler.cpp", "util/plugin.cpp", "util/ustring.cpp", "varbinder/ASBinder.cpp", diff --git a/ets2panda/CMakeLists.txt b/ets2panda/CMakeLists.txt index a4868c6a84..3008e14e60 100644 --- a/ets2panda/CMakeLists.txt +++ b/ets2panda/CMakeLists.txt @@ -454,8 +454,8 @@ set(ES2PANDA_LIB_SRC util/declgenEts2Ts.cpp util/errorHandler.cpp util/helpers.cpp + util/importPathManager.cpp util/path.cpp - util/pathHandler.cpp util/ustring.cpp ) diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 0abbc2ebf2..ad9b64c0f7 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1569,7 +1569,7 @@ checker::Type *ETSAnalyzer::Check(ir::ImportNamespaceSpecifier *st) const return type; } - auto [moduleName, isPackageModule] = checker->VarBinder()->AsETSBinder()->GetModuleNameFromSource(importPath); + auto [moduleName, isPackageModule] = checker->VarBinder()->AsETSBinder()->GetModuleInfo(importPath); std::vector syntheticNames = checker->GetNameForSynteticObjectType(moduleName); diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index e8565e50ba..86afb94e7b 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1479,7 +1479,7 @@ void ETSChecker::SetPropertiesForModuleObject(checker::ETSObjectType *moduleObjT auto *etsBinder = static_cast(VarBinder()); auto extRecords = etsBinder->GetGlobalRecordTable()->Program()->ExternalSources(); - auto [name, isPackageModule] = etsBinder->GetModuleNameFromSource(importPath); + auto [name, isPackageModule] = etsBinder->GetModuleInfo(importPath); auto res = extRecords.find(name); ASSERT(res != extRecords.end()); diff --git a/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.cpp b/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.cpp index 3a75d17ee5..eb13cef95e 100644 --- a/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.cpp +++ b/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.cpp @@ -15,14 +15,14 @@ #include "compiler/lowering/ets/topLevelStmts/importExportDecls.h" #include "ir/ets/etsReExportDeclaration.h" +#include "util/importPathManager.h" namespace ark::es2panda::compiler { void ImportExportDecls::ParseDefaultSources() { - auto imports = - parser_->ParseDefaultSources(DEFAULT_IMPORT_SOURCE_FILE, defaultImportSource_, util::PathHandler::StdLib()); - varbinder_->FillSourceList(parser_->GetPathes()); + auto imports = parser_->ParseDefaultSources(DEFAULT_IMPORT_SOURCE_FILE, defaultImportSource_); + varbinder_->SetModuleList(parser_->ModuleList()); varbinder_->SetDefaultImports(std::move(imports)); } diff --git a/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.h b/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.h index 1bcead43e3..950558ea54 100644 --- a/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.h +++ b/ets2panda/compiler/lowering/ets/topLevelStmts/importExportDecls.h @@ -37,7 +37,7 @@ class ImportExportDecls : ir::visitor::EmptyAstVisitor { return importStdlibFile; } - const std::string defaultImportSource_ = CreateDefaultImportSource(util::PathHandler::StdLib()); + const std::string defaultImportSource_ = CreateDefaultImportSource(util::Helpers::StdLib()); public: ImportExportDecls() = default; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 66aa029d38..8dff96c7f8 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -153,7 +153,9 @@ void ETSParser::ParseProgram(ScriptKind kind) statements.emplace_back(decl); } auto script = ParseETSGlobalScript(startLoc, statements); - GetProgram()->VarBinder()->AsETSBinder()->FillSourceList(this->GetPathes()); + + AddExternalSource(ParseSources()); + GetProgram()->VarBinder()->AsETSBinder()->SetModuleList(this->ModuleList()); GetProgram()->SetAst(script); } @@ -165,8 +167,6 @@ ir::ETSScript *ETSParser::ParseETSGlobalScript(lexer::SourcePosition startLoc, A auto topLevelStatements = ParseTopLevelDeclaration(); statements.insert(statements.end(), topLevelStatements.begin(), topLevelStatements.end()); - AddExternalSource(ParseSources(pathHandler_->GetParseList())); - auto *etsScript = AllocNode(Allocator(), std::move(statements), GetProgram()); etsScript->SetRange({startLoc, Lexer()->GetToken().End()}); return etsScript; @@ -187,8 +187,7 @@ void ETSParser::AddExternalSource(const std::vector &programs) } ArenaVector ETSParser::ParseDefaultSources(std::string_view srcFile, - std::string_view importSrc, - const std::vector &paths) + std::string_view importSrc) { auto isp = InnerSourceParser(this); SourceFile source(srcFile, importSrc); @@ -200,33 +199,35 @@ ArenaVector ETSParser::ParseDefaultSources(std::stri auto statements = ParseImportDeclarations(); GetContext().Status() &= ~ParserStatus::IN_DEFAULT_IMPORTS; - pathHandler_->CollectDefaultSources(paths); - AddExternalSource(ParseSources(pathHandler_->GetParseList())); + AddExternalSource(ParseSources()); return statements; } -std::vector ETSParser::ParseSources(const ArenaVector &paths) +std::vector ETSParser::ParseSources() { std::vector programs; - for (const auto &path : paths) { - pathHandler_->MarkAsParsed(path); - } + auto &parseList = importPathManager_->ParseList(); - for (const auto &path : paths) { - std::ifstream inputStream(path.Mutf8()); - - const auto data = pathHandler_->GetImportData(path, Extension()); + // This parse list `paths` can grow in the meantime, so keep this index-based iteration + // NOLINTNEXTLINE(modernize-loop-convert) + for (size_t idx = 0; idx < parseList.size(); idx++) { + // check if already parsed + if (parseList[idx].isParsed) { + continue; + } + std::ifstream inputStream(parseList[idx].sourcePath.Mutf8()); + const auto data = importPathManager_->GetImportData(parseList[idx].sourcePath, Extension()); if (!data.hasDecl) { continue; } - if (GetProgram()->SourceFilePath().Is(path.Mutf8())) { + if (GetProgram()->SourceFilePath().Is(parseList[idx].sourcePath.Mutf8())) { break; } if (inputStream.fail()) { - ThrowSyntaxError({"Failed to open file: ", path.Mutf8()}); + ThrowSyntaxError({"Failed to open file: ", parseList[idx].sourcePath.Mutf8()}); } std::stringstream ss; @@ -235,16 +236,19 @@ std::vector ETSParser::ParseSources(const ArenaVectorNew(externalSource, Allocator()); - pathHandler_->MarkAsParsed(path); - auto newProg = ParseSource({path.Utf8(), extSrc->View().Utf8(), path.Utf8(), false}); + auto newProg = ParseSource( + {parseList[idx].sourcePath.Utf8(), extSrc->View().Utf8(), parseList[idx].sourcePath.Utf8(), false}); + programs.emplace_back(newProg); GetContext().SetLanguage(currentLang); } + return programs; } parser::Program *ETSParser::ParseSource(const SourceFile &sourceFile) { + importPathManager_->MarkAsParsed(sourceFile.filePath); auto *program = Allocator()->New(Allocator(), GetProgram()->VarBinder()); auto esp = ExternalSourceParser(this, program); auto lexer = InitLexer(sourceFile); @@ -2367,11 +2371,13 @@ ir::ETSPackageDeclaration *ETSParser::ParsePackageDeclaration() if (Lexer()->GetToken().Type() != lexer::TokenType::KEYW_PACKAGE) { if (!IsETSModule() && GetProgram()->IsEntryPoint()) { - pathHandler_->SetModuleName(GetProgram()->AbsoluteName(), util::StringView(""), false); + // NOTE(rsipka): consider adding a filename name as module name to entry points as well + importPathManager_->InsertModuleInfo(GetProgram()->AbsoluteName(), + util::ImportPathManager::ModuleInfo {util::StringView(""), false}); return nullptr; } - pathHandler_->SetModuleName(GetProgram()->AbsoluteName(), GetProgram()->FileName(), false); - // NOTE(rsipka): setPackage should be eliminated from here, and should be reconsider its usage from program + importPathManager_->InsertModuleInfo(GetProgram()->AbsoluteName(), + util::ImportPathManager::ModuleInfo {GetProgram()->FileName(), false}); GetProgram()->SetPackageName(GetProgram()->FileName()); return nullptr; } @@ -2389,8 +2395,12 @@ ir::ETSPackageDeclaration *ETSParser::ParsePackageDeclaration() name->IsIdentifier() ? name->AsIdentifier()->Name() : name->AsTSQualifiedName()->ToString(Allocator()); GetProgram()->SetPackageName(packageName); - pathHandler_->SetModuleName(GetProgram()->AbsoluteName(), packageName, true); // NOTE: rid of it - pathHandler_->SetModuleName(GetProgram()->ResolvedFilePath(), packageName, true); + // NOTE(rsipka): handle these two cases, check that is it really required + importPathManager_->InsertModuleInfo(GetProgram()->AbsoluteName(), + util::ImportPathManager::ModuleInfo {packageName, true}); + importPathManager_->InsertModuleInfo(GetProgram()->ResolvedFilePath(), + util::ImportPathManager::ModuleInfo {packageName, true}); + return packageDeclaration; } @@ -2410,10 +2420,13 @@ ir::ImportSource *ETSParser::ParseSourceFromClause(bool requireFrom) ASSERT(Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_STRING); auto importPath = Lexer()->GetToken().Ident(); - auto resolvedImportPath = pathHandler_->AddPath(GetProgram()->AbsoluteName(), importPath); + + auto resolvedImportPath = importPathManager_->ResolvePath(GetProgram()->AbsoluteName(), importPath); + importPathManager_->AddToParseList(resolvedImportPath, + (GetContext().Status() & ParserStatus::IN_DEFAULT_IMPORTS) != 0U); auto *resolvedSource = AllocNode(resolvedImportPath); - auto importData = pathHandler_->GetImportData(resolvedImportPath, Extension()); + auto importData = importPathManager_->GetImportData(resolvedImportPath, Extension()); auto *source = AllocNode(importPath); source->SetRange(Lexer()->GetToken().Loc()); diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index b01b9c51f9..6ef244e519 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -19,7 +19,7 @@ #include #include "parserFlags.h" #include "util/arktsconfig.h" -#include "util/pathHandler.h" +#include "util/importPathManager.h" #include "TypedParser.h" namespace ark::es2panda::ir { @@ -44,9 +44,7 @@ public: ETSParser(Program *program, const CompilerOptions &options, ParserStatus status = ParserStatus::NO_OPTS) : TypedParser(program, options, status), globalProgram_(GetProgram()) { - pathHandler_ = std::make_unique(Allocator()); - pathHandler_->SetArkTsConfig(ArkTSConfig()); - pathHandler_->SetStdLib(GetOptions().stdLib); + importPathManager_ = std::make_unique(Allocator(), ArkTSConfig(), GetOptions().stdLib); } ETSParser() = delete; @@ -60,9 +58,9 @@ public: return true; } - ArenaUnorderedMap GetPathes() const + const ArenaMap &ModuleList() const { - return pathHandler_->GetPathes(); + return importPathManager_->ModuleList(); } // Methods to create AST node(s) from the specified string (part of valid ETS-code!) @@ -109,8 +107,7 @@ public: return CreateFormattedStatements(sourceCode, insertingNodes, fileName); } - ArenaVector ParseDefaultSources(std::string_view srcFile, std::string_view importSrc, - const std::vector &paths); + ArenaVector ParseDefaultSources(std::string_view srcFile, std::string_view importSrc); private: void ParseProgram(ScriptKind kind) override; @@ -125,8 +122,7 @@ private: void ParseNamedExportSpecifiers(ArenaVector *specifiers, bool defaultExport); void ParseUserSources(std::vector userParths); ArenaVector ParseTopLevelDeclaration(); - std::tuple, bool> CollectUserSources(const std::string &path); - std::vector ParseSources(const ArenaVector &paths); + std::vector ParseSources(); std::tuple> ParseFromClause(bool requireFrom); ArenaVector ParseNamedSpecifiers(); ArenaVector ParseImportDeclarations(); @@ -319,7 +315,7 @@ private: bool IsExternal() const override { - return pathHandler_->IsStdLib(GetProgram()); + return util::Helpers::IsStdLib(GetProgram()); } // NOLINTNEXTLINE(google-default-arguments) @@ -357,7 +353,7 @@ private: private: parser::Program *globalProgram_; std::vector insertingNodes_ {}; - std::unique_ptr pathHandler_ {nullptr}; + std::unique_ptr importPathManager_ {nullptr}; }; class ExternalSourceParser { diff --git a/ets2panda/util/helpers.cpp b/ets2panda/util/helpers.cpp index 8a22728336..4c20f7511a 100644 --- a/ets2panda/util/helpers.cpp +++ b/ets2panda/util/helpers.cpp @@ -160,24 +160,6 @@ util::StringView Helpers::ToStringView(ArenaAllocator *allocator, int32_t number return str.View(); } -bool Helpers::IsRelativePath(const std::string &path) -{ - auto pathDelimiter = ark::os::file::File::GetPathDelim(); - - std::string currentDirReference = "."; - std::string parentDirReference = ".."; - - currentDirReference.append(pathDelimiter); - parentDirReference.append(pathDelimiter); - - return ((path.find(currentDirReference) == 0) || (path.find(parentDirReference) == 0)); -} - -bool Helpers::IsCompatibleExtension(const std::string &extension) -{ - return extension == ".ets" || extension == ".ts"; -} - bool Helpers::EndsWith(const std::string &str, const std::string &suffix) { if (str.length() < suffix.length()) { @@ -187,61 +169,6 @@ bool Helpers::EndsWith(const std::string &str, const std::string &suffix) return str.find(suffix, expectPos) == expectPos; } -std::string Helpers::GetSourcePath(const std::string &path) -{ - std::string fullFilePath = path; - std::string importExtension; - if (!ark::os::file::File::IsRegularFile(path) && (ark::os::GetAbsolutePath(path).empty())) { - importExtension = ".ets"; - fullFilePath = path + importExtension; - if (!ark::os::file::File::IsRegularFile(fullFilePath)) { - importExtension = ".ts"; - fullFilePath = path + importExtension; - if (!ark::os::file::File::IsRegularFile(fullFilePath)) { - return path; - } - } - } - std::string absFilePath = ark::os::GetAbsolutePath(fullFilePath); - absFilePath.erase(absFilePath.find(importExtension), importExtension.size()); - return absFilePath; -} - -std::string Helpers::TruncateCompatibleExtension(const std::string &path) -{ - const size_t extensionIndex = path.find_last_of('.'); - if (extensionIndex != std::string::npos && IsCompatibleExtension(path.substr(extensionIndex, path.length()))) { - return path.substr(0, extensionIndex); - } - - return path; -} - -util::StringView Helpers::TruncateCompatibleExtension(const util::StringView &path) -{ - const size_t extensionIndex = path.Mutf8().find_last_of('.'); - if (extensionIndex != std::string::npos && - IsCompatibleExtension(path.Mutf8().substr(extensionIndex, path.Mutf8().length()))) { - return path.Substr(0, extensionIndex); - } - - return path; -} - -bool Helpers::IsRealPath(const std::string &path) -{ - if (!ark::os::file::File::IsRegularFile(path) && (ark::os::GetAbsolutePath(path).empty())) { - auto importExtension = ".ets"; - if (!ark::os::file::File::IsRegularFile(path + importExtension)) { - importExtension = ".ts"; - if (!ark::os::file::File::IsRegularFile(path + importExtension)) { - return false; - } - } - } - return true; -} - const ir::ScriptFunction *Helpers::GetContainingConstructor(const ir::AstNode *node) { const ir::ScriptFunction *iter = GetContainingFunction(node); @@ -722,4 +649,19 @@ std::pair Helpers::SplitSignature(std::strin return {className, methodName}; } +std::vector &Helpers::StdLib() +{ + static std::vector stdlib {"std/core", "std/math", "std/containers", "std/time", + "std/interop/js", "std/debug", "std/debug/concurrency", "escompat"}; + return stdlib; +} + +bool Helpers::IsStdLib(const parser::Program *program) +{ + const auto &stdlib = StdLib(); + auto fileFolder = program->GetPackageName().Mutf8(); + std::replace(fileFolder.begin(), fileFolder.end(), '.', '/'); + return std::count(stdlib.begin(), stdlib.end(), fileFolder) != 0; +} + } // namespace ark::es2panda::util diff --git a/ets2panda/util/helpers.h b/ets2panda/util/helpers.h index 0c6342fa18..1a8754b970 100644 --- a/ets2panda/util/helpers.h +++ b/ets2panda/util/helpers.h @@ -20,6 +20,7 @@ #include "mem/pool_manager.h" #include "util/ustring.h" #include "ir/module/importSpecifier.h" +#include "parser/program/program.h" #include #include @@ -115,11 +116,7 @@ public: static util::StringView ToStringView(ArenaAllocator *allocator, double number); static util::StringView ToStringView(ArenaAllocator *allocator, int32_t number); static util::StringView ToStringView(ArenaAllocator *allocator, uint32_t number); - static bool IsRelativePath(const std::string &path); - static bool IsRealPath(const std::string &path); - static bool IsCompatibleExtension(const std::string &extension); static bool EndsWith(const std::string &str, const std::string &suffix); - static std::string GetSourcePath(const std::string &path); static std::string TruncateCompatibleExtension(const std::string &path); static util::StringView TruncateCompatibleExtension(const util::StringView &path); @@ -206,6 +203,9 @@ public: static std::pair SplitSignature(std::string_view signature); + static std::vector &StdLib(); + static bool IsStdLib(const parser::Program *program); + private: template static void Log(Elements &&...elems); diff --git a/ets2panda/util/importPathManager.cpp b/ets2panda/util/importPathManager.cpp new file mode 100644 index 0000000000..e7cea026ab --- /dev/null +++ b/ets2panda/util/importPathManager.cpp @@ -0,0 +1,269 @@ +/** + * Copyright (c) 2024 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. + */ + +#include "importPathManager.h" +#include + +#if defined PANDA_TARGET_MOBILE +#define USE_UNIX_SYSCALL +#endif + +#ifdef USE_UNIX_SYSCALL +#include +#include +#include +#else +#if __has_include() +#include +namespace fs = std::filesystem; +#elif __has_include() +#include +namespace fs = std::experimental::filesystem; +#endif +#endif +namespace ark::es2panda::util { + +static bool IsCompitableExtension(const std::string &extension) +{ + return extension == ".ets" || extension == ".ts"; +} + +StringView ImportPathManager::ResolvePath(const StringView ¤tModulePath, const StringView &importPath) const +{ + if (importPath.Empty()) { + throw Error(ErrorType::GENERIC, "", "Import path cannot be empty"); + } + + if (IsRelativePath(importPath)) { + const size_t pos = currentModulePath.Mutf8().find_last_of(pathDelimiter_); + ASSERT(pos != std::string::npos); + + auto currentDirectory = currentModulePath.Mutf8().substr(0, pos); + auto resolvedPath = UString(currentDirectory, allocator_); + resolvedPath.Append(pathDelimiter_); + resolvedPath.Append(importPath.Mutf8()); + + return AppendExtensionOrIndexFileIfOmitted(resolvedPath.View()); + } + + std::string baseUrl; + if (importPath.Mutf8()[0] == pathDelimiter_.at(0)) { + baseUrl = arktsConfig_->BaseUrl(); + baseUrl.append(importPath.Mutf8(), 0, importPath.Mutf8().length()); + return AppendExtensionOrIndexFileIfOmitted(UString(baseUrl, allocator_).View()); + } + + auto &dynamicPaths = arktsConfig_->DynamicPaths(); + if (auto it = dynamicPaths.find(importPath.Mutf8()); it != dynamicPaths.cend() && !it->second.HasDecl()) { + return AppendExtensionOrIndexFileIfOmitted(importPath); + } + + const size_t pos = importPath.Mutf8().find(pathDelimiter_); + bool containsDelim = (pos != std::string::npos); + auto rootPart = containsDelim ? importPath.Substr(0, pos) : importPath; + if (!stdLib_.empty() && + (rootPart.Is("std") || rootPart.Is("escompat"))) { // Get std or escompat path from CLI if provided + baseUrl = stdLib_ + pathDelimiter_.at(0) + rootPart.Mutf8(); + } else { + ASSERT(arktsConfig_ != nullptr); + auto resolvedPath = arktsConfig_->ResolvePath(importPath.Mutf8()); + if (!resolvedPath) { + throw Error(ErrorType::GENERIC, "", + "Can't find prefix for '" + importPath.Mutf8() + "' in " + arktsConfig_->ConfigPath()); + } + + return AppendExtensionOrIndexFileIfOmitted(UString(resolvedPath.value(), allocator_).View()); + } + + if (containsDelim) { + baseUrl.append(1, pathDelimiter_.at(0)); + baseUrl.append(importPath.Mutf8(), rootPart.Mutf8().length() + 1, importPath.Mutf8().length()); + } + + return UString(baseUrl, allocator_).View(); +} + +void ImportPathManager::AddToParseList(const StringView &resolvedPath, bool isDefaultImport) +{ + if (ark::os::file::File::IsDirectory(resolvedPath.Mutf8())) { +#ifdef USE_UNIX_SYSCALL + DIR *dir = opendir(resolvedPath.Mutf8().c_str()); + if (dir == nullptr) { + throw Error(ErrorType::GENERIC, "", "Cannot open folder: " + resolvedPath.Mutf8()); + } + + struct dirent *entry; + while ((entry = readdir(dir)) != nullptr) { + if (entry->d_type != DT_REG) { + continue; + } + + std::string fileName = entry->d_name; + std::string::size_type pos = fileName.find_last_of('.'); + if (pos == std::string::npos || !IsCompitableExtension(fileName.substr(pos))) { + continue; + } + + std::string filePath = resolvedPath.Mutf8() + "/" + entry->d_name; + AddToParseList(UString(filePath, allocator_).View(), isDefaultImport); + } + + closedir(dir); + return; +#else + for (auto const &entry : fs::directory_iterator(resolvedPath.Mutf8())) { + if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { + continue; + } + + AddToParseList(UString(entry.path().string(), allocator_).View(), isDefaultImport); + } + return; +#endif + } + + if (!ark::os::file::File::IsRegularFile(resolvedPath.Mutf8())) { + throw Error(ErrorType::GENERIC, "", "Not an available source path: " + resolvedPath.Mutf8()); + } + + for (const auto &parseInfo : parseList_) { + if (parseInfo.sourcePath == resolvedPath) { + return; + } + } + + if (isDefaultImport) { + int position = resolvedPath.Mutf8().find_last_of(pathDelimiter_); + if (resolvedPath.Substr(position + 1, resolvedPath.Length()).Is("Object.ets")) { + parseList_.emplace(parseList_.begin(), ParseInfo {resolvedPath, false}); + return; + } + } + + parseList_.emplace_back(ParseInfo {resolvedPath, false}); +} + +const ArenaVector &ImportPathManager::ParseList() +{ + return parseList_; +} + +ImportPathManager::ImportData ImportPathManager::GetImportData(const util::StringView &path, + const ScriptExtension &extension) const +{ + const auto &dynamicPaths = arktsConfig_->DynamicPaths(); + auto key = ark::os::NormalizePath(path.Mutf8()); + + auto it = dynamicPaths.find(key); + if (it == dynamicPaths.cend()) { + key = ark::os::RemoveExtension(key); + } + + while (it == dynamicPaths.cend() && !key.empty()) { + it = dynamicPaths.find(key); + if (it != dynamicPaths.cend()) { + break; + } + key = ark::os::GetParentDir(key); + } + + if (it != dynamicPaths.cend()) { + return {it->second.GetLanguage(), key, it->second.HasDecl()}; + } + + return {ToLanguage(extension), path.Mutf8(), true}; +} + +void ImportPathManager::InsertModuleInfo(const util::StringView &path, + const util::ImportPathManager::ModuleInfo &moduleInfo) +{ + moduleList_.insert({path, moduleInfo}); +} + +const ArenaMap &ImportPathManager::ModuleList() const +{ + return moduleList_; +} + +void ImportPathManager::MarkAsParsed(const StringView &path) +{ + for (auto &parseInfo : parseList_) { + if (parseInfo.sourcePath == path) { + parseInfo.isParsed = true; + return; + } + } +} + +bool ImportPathManager::IsRelativePath(const StringView &path) const +{ + std::string currentDirReference = "."; + std::string parentDirReference = ".."; + + currentDirReference.append(pathDelimiter_); + parentDirReference.append(pathDelimiter_); + + return ((path.Mutf8().find(currentDirReference) == 0) || (path.Mutf8().find(parentDirReference) == 0)); +} + +StringView ImportPathManager::GetRealPath(const StringView &path) const +{ + const std::string realPath = ark::os::GetAbsolutePath(path.Mutf8()); + if (realPath.empty() || realPath == path.Mutf8()) { + return path; + } + + return UString(realPath, allocator_).View(); +} + +StringView ImportPathManager::AppendExtensionOrIndexFileIfOmitted(const StringView &path) const +{ + StringView realPath = GetRealPath(path); + if (ark::os::file::File::IsRegularFile(realPath.Mutf8())) { + return realPath; + } + + if (ark::os::file::File::IsDirectory(realPath.Mutf8())) { + // Supported index files: keep this checking order + std::array supportedIndexFiles = {"index.ets", "index.ts"}; + for (const auto &indexFile : supportedIndexFiles) { + std::string indexFilePath = realPath.Mutf8() + pathDelimiter_.data() + indexFile; + if (ark::os::file::File::IsRegularFile(indexFilePath)) { + return GetRealPath(UString(indexFilePath, allocator_).View()); + } + } + + return realPath; + } + + // Supported extensions: keep this checking order + std::array supportedExtensions = {".ets", ".ts"}; + + for (const auto &extension : supportedExtensions) { + if (ark::os::file::File::IsRegularFile(path.Mutf8() + extension)) { + return GetRealPath(UString(path.Mutf8().append(extension), allocator_).View()); + } + } + + auto &dynamicPaths = arktsConfig_->DynamicPaths(); + if (auto it = dynamicPaths.find(path.Mutf8()); it != dynamicPaths.cend()) { + return path; + } + + throw Error(ErrorType::GENERIC, "", "Not supported path: " + path.Mutf8()); +} + +} // namespace ark::es2panda::util +#undef USE_UNIX_SYSCALL diff --git a/ets2panda/util/importPathManager.h b/ets2panda/util/importPathManager.h new file mode 100644 index 0000000000..7f75b83463 --- /dev/null +++ b/ets2panda/util/importPathManager.h @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2024 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. + */ + +#ifndef ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H +#define ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H + +#include "util/arktsconfig.h" +#include "util/ustring.h" +#include "es2panda.h" + +namespace ark::es2panda::util { + +class ImportPathManager { +public: + struct ImportData { + Language lang; + std::string module; + bool hasDecl; + }; + + struct ParseInfo { + StringView sourcePath; + bool isParsed; + }; + + struct ModuleInfo { + StringView moduleName; + bool isPackageModule; + }; + + ImportPathManager(ark::ArenaAllocator *allocator, std::shared_ptr arktsConfig, std::string stdLib) + : allocator_(allocator), + arktsConfig_(std::move(arktsConfig)), + stdLib_(std::move(stdLib)), + parseList_(allocator->Adapter()), + moduleList_(allocator->Adapter()) + { + } + + NO_COPY_SEMANTIC(ImportPathManager); + NO_MOVE_SEMANTIC(ImportPathManager); + ImportPathManager() = delete; + ~ImportPathManager() = default; + + StringView ResolvePath(const StringView ¤tModulePath, const StringView &importPath) const; + void AddToParseList(const StringView &path, bool isDefaultImport); + const ArenaVector &ParseList(); + ImportData GetImportData(const util::StringView &path, const ScriptExtension &extension) const; + void InsertModuleInfo(const util::StringView &path, const ModuleInfo &moduleInfo); + const ArenaMap &ModuleList() const; + void MarkAsParsed(const StringView &path); + +private: + bool IsRelativePath(const StringView &path) const; + StringView GetRealPath(const StringView &path) const; + StringView AppendExtensionOrIndexFileIfOmitted(const StringView &path) const; + + ArenaAllocator *allocator_ {nullptr}; + std::shared_ptr arktsConfig_ {nullptr}; + std::string stdLib_ {}; + ArenaVector parseList_; + ArenaMap moduleList_; + std::string_view pathDelimiter_ {ark::os::file::File::GetPathDelim()}; +}; + +} // namespace ark::es2panda::util + +#endif // ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H diff --git a/ets2panda/util/pathHandler.cpp b/ets2panda/util/pathHandler.cpp deleted file mode 100644 index 27b566e7c0..0000000000 --- a/ets2panda/util/pathHandler.cpp +++ /dev/null @@ -1,283 +0,0 @@ -/** - * Copyright (c) 2024 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. - */ - -#include "pathHandler.h" -#include "libpandabase/os/filesystem.h" -#include - -#if defined PANDA_TARGET_MOBILE -#define USE_UNIX_SYSCALL -#endif - -#ifdef USE_UNIX_SYSCALL -#include -#include -#include -#else -#if __has_include() -#include -namespace fs = std::filesystem; -#elif __has_include() -#include -namespace fs = std::experimental::filesystem; -#endif -#endif - -namespace ark::es2panda::util { - -static bool IsCompitableExtension(const std::string &extension) -{ - return extension == ".ets" || extension == ".ts"; -} - -void PathHandler::UnixWalkThroughDirectory([[maybe_unused]] const StringView &directory) -{ -#ifdef USE_UNIX_SYSCALL - DIR *dir = opendir(directory.Mutf8().c_str()); - if (dir == nullptr) { - throw Error(ErrorType::GENERIC, "", "Cannot open folder: " + directory.Mutf8()); - } - - struct dirent *entry; - while ((entry = readdir(dir)) != nullptr) { - if (entry->d_type != DT_REG) { - continue; - } - - std::string fileName = entry->d_name; - std::string::size_type pos = fileName.find_last_of('.'); - if (pos == std::string::npos || !IsCompitableExtension(fileName.substr(pos))) { - continue; - } - - std::string filePath = directory.Mutf8() + "/" + entry->d_name; - StringView sourcePath = util::UString(filePath, allocator_).View(); - if (fileName == "Object.ets") { - pathes_.insert({sourcePath, ParseInfo(allocator_, true)}); - } else { - pathes_.insert({sourcePath, ParseInfo(allocator_)}); - } - } - - closedir(dir); -#endif -} - -StringView PathHandler::AddPath(const StringView &callerPath, const StringView &path) -{ - auto resolvedPath = ResolveSourcePath(callerPath, path); - if (!ark::os::file::File::IsDirectory(resolvedPath.Mutf8())) { - pathes_.insert({resolvedPath, ParseInfo(allocator_)}); - return resolvedPath; - } - - pathes_.insert({resolvedPath, ParseInfo(allocator_)}); - - bool hasIndexFile = false; - std::string indexFile = resolvedPath.Mutf8() + pathDelimiter_.data() + "index.ets"; - if (ark::os::file::File::IsRegularFile(indexFile)) { - hasIndexFile = true; - } else { - indexFile = resolvedPath.Mutf8() + pathDelimiter_.data() + "index.ts"; - if (ark::os::file::File::IsRegularFile(indexFile)) { - hasIndexFile = true; - } - } - - if (hasIndexFile) { - StringView indexFilePath = util::UString(indexFile, allocator_).View(); - pathes_.insert({indexFilePath, ParseInfo(allocator_)}); - return indexFilePath; - } - -#ifdef USE_UNIX_SYSCALL - UnixWalkThroughDirectory(resolvedPath); -#else - for (auto const &entry : fs::directory_iterator(resolvedPath.Mutf8())) { - if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { - continue; - } - - StringView sourcePath = util::UString(entry.path().string(), allocator_).View(); - if (entry.path().filename().string() == "Object.ets") { - pathes_.insert({sourcePath, ParseInfo(allocator_, true)}); - } else { - pathes_.insert({sourcePath, ParseInfo(allocator_)}); - } - } -#endif - return resolvedPath; -} - -void PathHandler::CollectDefaultSources(const std::vector &stdlib) -{ - for (auto const &path : stdlib) { - StringView callerPath = util::UString(allocator_).View(); - StringView stdlibPath = ResolveSourcePath(callerPath, util::UString(path, allocator_).View()); - pathes_.insert({stdlibPath, ParseInfo(allocator_)}); -#ifdef USE_UNIX_SYSCALL - UnixWalkThroughDirectory(stdlibPath); -#else - for (auto const &entry : fs::directory_iterator(stdlibPath.Mutf8())) { - if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { - continue; - } - - // NOTE(rsipka): seems to me a duplicated check, since it was already in pathes_ - StringView sourcePath = util::UString(entry.path().string(), allocator_).View(); - if (entry.path().filename().string() == "Object.ets") { - pathes_.insert({sourcePath, ParseInfo(allocator_, true)}); - } else { - pathes_.insert({sourcePath, ParseInfo(allocator_)}); - } - } -#endif - } -} - -ArenaVector PathHandler::GetParseList() const -{ - ArenaVector parseableSources(allocator_->Adapter()); - for (const auto [path, info] : pathes_) { - if (!info.IsParsed() && !ark::os::file::File::IsDirectory(path.Mutf8())) { - // NOTE(rsipka): it should be handled in a better way - if (info.IsObjectfile()) { - parseableSources.emplace(parseableSources.begin(), path); - } else { - parseableSources.emplace_back(path); - } - } - } - return parseableSources; -} - -bool PathHandler::IsRelativePath(const StringView &path) const -{ - std::string currentDirReference = "."; - std::string parentDirReference = ".."; - - currentDirReference.append(pathDelimiter_); - parentDirReference.append(pathDelimiter_); - - return ((path.Mutf8().find(currentDirReference) == 0) || (path.Mutf8().find(parentDirReference) == 0)); -} - -StringView PathHandler::GetParentFolder(const StringView &path) const -{ - const size_t pos = path.Mutf8().find_last_of(pathDelimiter_); - if (pos != std::string::npos) { - return util::UString(path.Mutf8().substr(0, pos + 1), allocator_).View(); - } - - return util::UString(allocator_).View(); -} - -StringView PathHandler::AppendExtension(const StringView &path) const -{ - StringView realPath = GetRealPath(path); - if (ark::os::file::File::IsDirectory(realPath.Mutf8()) || ark::os::file::File::IsRegularFile(realPath.Mutf8())) { - return realPath; - } - - std::string importExtension = ".ets"; - if (!ark::os::file::File::IsRegularFile(path.Mutf8() + importExtension)) { - importExtension = ".ts"; - if (!ark::os::file::File::IsRegularFile(path.Mutf8() + importExtension)) { - // NOTE(rsipka): this check should be eliminated - auto &dynamicPaths = arktsConfig_->DynamicPaths(); - if (auto it = dynamicPaths.find(path.Mutf8()); it != dynamicPaths.cend()) { - return path; - } - - throw Error(ErrorType::GENERIC, "", "Not supported path: " + path.Mutf8()); - } - } - - return GetRealPath(util::UString(path.Mutf8().append(importExtension), allocator_).View()); -} - -StringView PathHandler::GetRealPath(const StringView &path) const -{ - const std::string realPath = ark::os::GetAbsolutePath(path.Mutf8()); - if (realPath.empty()) { - return path; - } - - if (realPath == path.Mutf8()) { - return path; - } - - return util::UString(realPath, allocator_).View(); -} - -StringView PathHandler::ResolveSourcePath(const StringView &callerPath, const StringView &path) const -{ - if (IsRelativePath(path)) { - const size_t pos = callerPath.Mutf8().find_last_of(pathDelimiter_); - ASSERT(pos != std::string::npos); - auto parentFolder = callerPath.Mutf8().substr(0, pos); - auto resolvedPath = util::UString(parentFolder, allocator_); - resolvedPath.Append(pathDelimiter_); - resolvedPath.Append(path.Mutf8()); - return AppendExtension(resolvedPath.View()); - } - - std::string baseUrl; - if (path.Mutf8().find('/') == 0) { - baseUrl = arktsConfig_->BaseUrl(); - baseUrl.append(path.Mutf8(), 0, path.Mutf8().length()); - return AppendExtension(util::UString(baseUrl, allocator_).View()); - } - - auto &dynamicPaths = arktsConfig_->DynamicPaths(); - if (auto it = dynamicPaths.find(path.Mutf8()); it != dynamicPaths.cend() && !it->second.HasDecl()) { - return AppendExtension(path); - } - - const size_t pos = path.Mutf8().find(pathDelimiter_); - bool containsDelim = (pos != std::string::npos); - auto rootPart = containsDelim ? path.Substr(0, pos) : path; - if (rootPart.Is("std") && !stdLib_.empty()) { // Get std path from CLI if provided - baseUrl = stdLib_ + "/std"; - } else if (rootPart.Is("escompat") && !stdLib_.empty()) { // Get escompat path from CLI if provided - baseUrl = stdLib_ + "/escompat"; - } else { - ASSERT(arktsConfig_ != nullptr); - auto resolvedPath = arktsConfig_->ResolvePath(path.Mutf8()); - if (!resolvedPath) { - throw Error(ErrorType::GENERIC, "", - "Can't find prefix for '" + path.Mutf8() + "' in " + arktsConfig_->ConfigPath()); - } - - return AppendExtension(util::UString(resolvedPath.value(), allocator_).View()); - } - - if (containsDelim) { - baseUrl.append(1, pathDelimiter_.at(0)); - baseUrl.append(path.Mutf8(), rootPart.Mutf8().length() + 1, path.Mutf8().length()); - } - - return util::UString(baseUrl, allocator_).View(); -} - -std::vector &PathHandler::StdLib() -{ - static std::vector stdlib {"std/core", "std/math", "std/containers", "std/time", - "std/interop/js", "std/debug", "std/debug/concurrency", "escompat"}; - return stdlib; -} - -} // namespace ark::es2panda::util -#undef USE_UNIX_SYSCALL diff --git a/ets2panda/util/pathHandler.h b/ets2panda/util/pathHandler.h deleted file mode 100644 index b850cc23e8..0000000000 --- a/ets2panda/util/pathHandler.h +++ /dev/null @@ -1,200 +0,0 @@ -/** - * Copyright (c) 2024 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. - */ - -#ifndef ES2PANDA_UTIL_PATH_HANDLER_H -#define ES2PANDA_UTIL_PATH_HANDLER_H - -#include -#include - -#include "es2panda.h" -#include "parser/program/program.h" -#include "util/arktsconfig.h" -#include "util/ustring.h" -#include - -namespace ark::es2panda::util { - -class ParseInfo { -public: - explicit ParseInfo(ark::ArenaAllocator *allocator, bool isObjectFile = false) - : isObjectFile_(isObjectFile), isParsed_(false), moduleName_(allocator), isPackageModule_(false) - { - } - - ParseInfo() = delete; - - bool IsParsed() const - { - return isParsed_; - } - - void MarkAsParsed() - { - isParsed_ = true; - } - - StringView ModuleName() const - { - return moduleName_.View(); - } - - bool IsObjectfile() const - { - return isObjectFile_; - } - - bool IsPackageModule() const - { - return isPackageModule_; - } - - void SetModuleName(const StringView &moduleName, bool isPackageModule) - { - if (moduleName_.View().Empty()) { - moduleName_.Append(moduleName); - isPackageModule_ = isPackageModule; - } - } - -private: - bool isObjectFile_; - bool isParsed_; - util::UString moduleName_; - bool isPackageModule_; -}; - -class PathHandler { -public: - struct ImportData { - Language lang; - std::string module; - bool hasDecl; - }; - - static std::vector &StdLib(); - - ImportData GetImportData(util::StringView path, ScriptExtension extension) - { - const auto &dynamicPaths = arktsConfig_->DynamicPaths(); - auto key = ark::os::NormalizePath(path.Mutf8()); - - auto it = dynamicPaths.find(key); - if (it == dynamicPaths.cend()) { - key = ark::os::RemoveExtension(key); - } - - while (it == dynamicPaths.cend() && !key.empty()) { - it = dynamicPaths.find(key); - if (it != dynamicPaths.cend()) { - break; - } - key = ark::os::GetParentDir(key); - } - - if (it != dynamicPaths.cend()) { - return {it->second.GetLanguage(), key, it->second.HasDecl()}; - } - return {ToLanguage(extension), path.Mutf8(), true}; - } - - bool IsStdLib(const parser::Program *program) const - { - const auto &stdlib = StdLib(); - auto fileFolder = program->GetPackageName().Mutf8(); - std::replace(fileFolder.begin(), fileFolder.end(), '.', '/'); - return std::count(stdlib.begin(), stdlib.end(), fileFolder) != 0; - } - - explicit PathHandler(ark::ArenaAllocator *allocator) : allocator_(allocator), pathes_(allocator->Adapter()) {} - - StringView AddPath(const StringView &callerPath, const StringView &path); - ArenaVector GetParseList() const; - void CollectDefaultSources(const std::vector &stdlib); - - void MarkAsParsed(const StringView &path) - { - auto it = pathes_.find(path); - if (it != pathes_.end()) { - it->second.MarkAsParsed(); - } - } - - bool IsParsed(const std::string &path) - { - auto pathView = util::UString(path, allocator_).View(); - auto it = pathes_.find(pathView); - if (it != pathes_.end()) { - return it->second.IsParsed(); - } - - return false; - } - - void MarkAsParsed(const std::string &path) - { - auto pathView = util::UString(path, allocator_).View(); - auto it = pathes_.find(pathView); - if (it != pathes_.end()) { - it->second.MarkAsParsed(); - } - } - - void SetModuleName(const StringView &path, const StringView &moduleName, bool isPackageModule) - { - auto it = pathes_.find(path); - if (it != pathes_.end()) { - it->second.SetModuleName(moduleName, isPackageModule); - } - } - - void SetStdLib(const std::string &stdLib) - { - stdLib_ = stdLib; - } - - void SetArkTsConfig(std::shared_ptr arktsConfig) - { - arktsConfig_ = std::move(arktsConfig); - } - - ArenaUnorderedMap &GetPathes() - { - return pathes_; - } - - NO_COPY_SEMANTIC(PathHandler); - NO_MOVE_SEMANTIC(PathHandler); - PathHandler() = delete; - ~PathHandler() = default; - -private: - bool IsRelativePath(const StringView &path) const; - StringView GetParentFolder(const StringView &path) const; - StringView ResolveSourcePath(const StringView &callerPath, const StringView &path) const; - StringView AppendExtension(const StringView &path) const; - StringView GetRealPath(const StringView &path) const; - void UnixWalkThroughDirectory(const StringView &directory); - - ArenaAllocator *allocator_; - ArenaUnorderedMap pathes_; - std::string stdLib_ = {}; - std::shared_ptr arktsConfig_ = {nullptr}; - std::string_view pathDelimiter_ = ark::os::file::File::GetPathDelim(); -}; - -} // namespace ark::es2panda::util - -#endif // ES2PANDA_UTIL_PATH_HANDLER_H diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 2e64deb302..94ba6ac82f 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -703,7 +703,7 @@ ArenaVector ETSBinder::GetExternalProgram(const util::StringV { const auto &extRecords = globalRecordTable_.Program()->ExternalSources(); - auto [name, _] = GetModuleNameFromSource(sourceName); + auto [name, _] = GetModuleInfo(sourceName); auto res = extRecords.find(name); if (res == extRecords.end()) { ThrowError(importPath->Start(), "Cannot find import: " + importPath->Str().Mutf8()); diff --git a/ets2panda/varbinder/ETSBinder.h b/ets2panda/varbinder/ETSBinder.h index 496a87b176..128fedd761 100644 --- a/ets2panda/varbinder/ETSBinder.h +++ b/ets2panda/varbinder/ETSBinder.h @@ -20,7 +20,7 @@ #include "varbinder/recordTable.h" #include "ir/ets/etsImportDeclaration.h" #include "ir/ets/etsReExportDeclaration.h" -#include "util/pathHandler.h" +#include "util/importPathManager.h" namespace ark::es2panda::varbinder { @@ -47,7 +47,7 @@ public: lambdaObjects_(Allocator()->Adapter()), dynamicImportVars_(Allocator()->Adapter()), importSpecifiers_(Allocator()->Adapter()), - sourceList_(Allocator()->Adapter()) + moduleList_(Allocator()->Adapter()) { InitImplicitThisParam(); } @@ -207,18 +207,16 @@ public: defaultExport_ = defaultExport; } - void FillSourceList(const ArenaUnorderedMap &pathes) + void SetModuleList(const ArenaMap &moduleList) { - for (const auto path : pathes) { - sourceList_.emplace(path.first, std::tuple(path.second.ModuleName(), - path.second.IsPackageModule())); - } + moduleList_ = moduleList; } - std::tuple GetModuleNameFromSource(const util::StringView &path) const + util::ImportPathManager::ModuleInfo GetModuleInfo(const util::StringView &path) const { - auto it = sourceList_.find(path); - ASSERT(it != sourceList_.end()); + auto it = moduleList_.find(path); + + ASSERT(it != moduleList_.end()); return it->second; } @@ -253,7 +251,7 @@ private: DynamicImportVariables dynamicImportVars_; ir::Identifier *thisParam_ {}; ArenaVector> importSpecifiers_; - ArenaUnorderedMap> sourceList_; + ArenaMap moduleList_; ir::AstNode *defaultExport_ {}; }; -- Gitee