• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 #include <string_view>
17 #include "refactors/convert_import.h"
18 #include "refactor_provider.h"
19 #include "internal_api.h"
20 
21 namespace ark::es2panda::lsp {
22 
ConvertImportRefactor()23 ConvertImportRefactor::ConvertImportRefactor()
24 {
25     AddKind(std::string(TO_NAMED_IMPORT_ACTION.kind));
26     AddKind(std::string(TO_NAMESPACE_IMPORT_ACTION.kind));
27     AddKind(std::string(TO_DEFAULT_IMPORT_ACTION.kind));
28 }
29 
GetAvailableActions(const RefactorContext & refContext) const30 ApplicableRefactorInfo ConvertImportRefactor::GetAvailableActions(const RefactorContext &refContext) const
31 {
32     es2panda_Context *context = refContext.context;
33     size_t position = refContext.span.pos;
34 
35     ApplicableRefactorInfo res;
36 
37     if (!IsKind(refContext.kind)) {
38         return res;
39     }
40 
41     auto node = GetTouchingToken(context, position, false);
42     if (node == nullptr) {
43         return res;
44     }
45     auto cb = [](ir::AstNode *ancestorNode) {
46         return ancestorNode->IsETSImportDeclaration() || ancestorNode->IsImportNamespaceSpecifier() ||
47                ancestorNode->IsImportSpecifier();
48     };
49     auto ancestor = FindAncestor(node, cb);
50     if (ancestor != nullptr && ancestor->IsETSImportDeclaration()) {
51         auto specifiers = ancestor->AsETSImportDeclaration()->Specifiers();
52         if (!specifiers.empty()) {
53             ancestor = specifiers[0];
54         }
55     }
56     if (ancestor == nullptr) {
57         return res;
58     }
59     if (ancestor->IsImportNamespaceSpecifier()) {
60         res.name = refactor_name::CONVERT_IMPORT_REFACTOR_NAME;
61         res.description = std::string(TO_NAMED_IMPORT_ACTION.description);
62         res.action.kind = std::string(TO_NAMED_IMPORT_ACTION.kind);
63         res.action.name = std::string(TO_NAMED_IMPORT_ACTION.name);
64         res.action.description = std::string(TO_NAMED_IMPORT_ACTION.description);
65     } else if (ancestor->IsImportSpecifier()) {
66         res.name = refactor_name::CONVERT_IMPORT_REFACTOR_NAME;
67         res.description = std::string(TO_NAMESPACE_IMPORT_ACTION.description);
68         res.action.kind = std::string(TO_NAMESPACE_IMPORT_ACTION.kind);
69         res.action.name = std::string(TO_NAMESPACE_IMPORT_ACTION.name);
70         res.action.description = std::string(TO_NAMESPACE_IMPORT_ACTION.description);
71     }
72 
73     return res;
74 }
75 
GetEditsForAction(const RefactorContext & context,const std::string & actionName) const76 std::unique_ptr<RefactorEditInfo> ConvertImportRefactor::GetEditsForAction(const RefactorContext &context,
77                                                                            const std::string &actionName) const
78 {
79     (void)context;
80     (void)actionName;
81     return std::make_unique<RefactorEditInfo>();
82 }
83 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects, cert-err58-cpp)
84 AutoRefactorRegister<ConvertImportRefactor> g_convertImportRefactorRegister("ConvertImportRefactor");
85 
86 }  // namespace ark::es2panda::lsp
87