1/** 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#ifndef ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H 17#define ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H 18 19#if defined PANDA_TARGET_MOBILE 20#define USE_UNIX_SYSCALL 21#endif 22 23#include "util/arktsconfig.h" 24#include "util/ustring.h" 25#include "util/enumbitops.h" 26 27#include "es2panda.h" 28 29namespace ark::es2panda::util { 30 31using ENUMBITOPS_OPERATORS; 32 33enum class ImportFlags { 34 NONE = 0U, 35 DEFAULT_IMPORT = 1U << 1U, 36 IMPLICIT_PACKAGE_IMPORT = 1U << 2U, 37}; 38class ImportPathManager { 39public: 40 struct ImportData { 41 Language lang; 42 std::string module; 43 bool hasDecl; 44 }; 45 46 struct ParseInfo { 47 StringView sourcePath; 48 bool isParsed; 49 bool isImplicitPackageImported = false; 50 }; 51 52 ImportPathManager(ark::ArenaAllocator *allocator, std::shared_ptr<ArkTsConfig> arktsConfig, std::string stdLib) 53 : allocator_(allocator), 54 arktsConfig_(std::move(arktsConfig)), 55 stdLib_(std::move(stdLib)), 56 parseList_(allocator->Adapter()) 57 { 58 } 59 60 NO_COPY_SEMANTIC(ImportPathManager); 61 NO_MOVE_SEMANTIC(ImportPathManager); 62 ImportPathManager() = delete; 63 ~ImportPathManager() = default; 64 65 [[nodiscard]] const ArenaVector<ParseInfo> &ParseList() const 66 { 67 return parseList_; 68 } 69 70 StringView ResolvePath(const StringView ¤tModulePath, const StringView &importPath) const; 71 void AddToParseList(const StringView &resolvedPath, ImportFlags importFlags); 72 ImportData GetImportData(const util::StringView &path, const ScriptExtension &extension) const; 73 void MarkAsParsed(const StringView &path); 74 75private: 76 bool IsRelativePath(const StringView &path) const; 77 StringView GetRealPath(const StringView &path) const; 78 StringView AppendExtensionOrIndexFileIfOmitted(const StringView &path) const; 79#ifdef USE_UNIX_SYSCALL 80 void UnixWalkThroughDirectoryAndAddToParseList(const StringView &directoryPath, ImportFlags importFlags); 81#endif 82 83 ArenaAllocator *allocator_ {nullptr}; 84 std::shared_ptr<ArkTsConfig> arktsConfig_ {nullptr}; 85 std::string stdLib_ {}; 86 ArenaVector<ParseInfo> parseList_; 87 std::string_view pathDelimiter_ {ark::os::file::File::GetPathDelim()}; 88}; 89 90} // namespace ark::es2panda::util 91 92namespace enumbitops { 93 94template <> 95struct IsAllowedType<ark::es2panda::util::ImportFlags> : std::true_type { 96}; 97} // namespace enumbitops 98 99#endif // ES2PANDA_UTIL_IMPORT_PATH_MANAGER_H 100