• 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_function.h"
18 #include "refactor_provider.h"
19 #include "internal_api.h"
20 
21 namespace ark::es2panda::lsp {
22 
ConvertFunctionRefactor()23 ConvertFunctionRefactor::ConvertFunctionRefactor()
24 {
25     AddKind(std::string(TO_ANONYMOUS_FUNCTION_ACTION.kind));
26     AddKind(std::string(TO_NAMED_FUNCTION_ACTION.kind));
27     AddKind(std::string(TO_ARROW_FUNCTION_ACTION.kind));
28 }
29 
HasArrowFunction(ark::es2panda::ir::AstNode * node)30 bool HasArrowFunction(ark::es2panda::ir::AstNode *node)
31 {
32     if (!node->IsCallExpression() && !node->IsClassProperty() && !node->IsVariableDeclarator()) {
33         return false;
34     }
35     if ((node->IsClassProperty() && node->AsClassProperty()->Value() != nullptr &&
36          node->AsClassProperty()->Value()->IsArrowFunctionExpression()) ||
37         (node->IsVariableDeclarator() && node->AsVariableDeclarator()->Init() != nullptr &&
38          node->AsVariableDeclarator()->Init()->IsArrowFunctionExpression())) {
39         return true;
40     }
41     if (node->IsCallExpression()) {
42         auto arguments = node->AsCallExpression()->Arguments();
43         for (auto argument : arguments) {
44             if (argument->IsArrowFunctionExpression()) {
45                 return true;
46             }
47         }
48     }
49     return false;
50 }
51 
GetAvailableActions(const RefactorContext & refContext) const52 ApplicableRefactorInfo ConvertFunctionRefactor::GetAvailableActions(const RefactorContext &refContext) const
53 {
54     es2panda_Context *context = refContext.context;
55     size_t position = refContext.span.pos;
56 
57     ApplicableRefactorInfo res;
58 
59     if (!IsKind(refContext.kind)) {
60         return res;
61     }
62 
63     auto node = GetTouchingToken(context, position, false);
64     if (node == nullptr) {
65         return res;
66     }
67     auto cb = [](ir::AstNode *ancestorNode) { return HasArrowFunction(ancestorNode); };
68     auto ancestor = FindAncestor(node, cb);
69     if (ancestor != nullptr && ancestor->IsClassProperty()) {
70         res.name = refactor_name::CONVERT_FUNCTION_REFACTOR_NAME;
71         res.description = refactor_description::CONVERT_FUNCTION_REFACTOR_DESC;
72         res.action.kind = std::string(TO_NAMED_FUNCTION_ACTION.kind);
73         res.action.name = std::string(TO_NAMED_FUNCTION_ACTION.name);
74         res.action.description = std::string(TO_NAMED_FUNCTION_ACTION.description);
75     }
76 
77     return res;
78 }
79 
GetEditsForAction(const RefactorContext & context,const std::string & actionName) const80 std::unique_ptr<RefactorEditInfo> ConvertFunctionRefactor::GetEditsForAction(const RefactorContext &context,
81                                                                              const std::string &actionName) const
82 {
83     (void)context;
84     (void)actionName;
85     return std::make_unique<RefactorEditInfo>();
86 }
87 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects, cert-err58-cpp)
88 AutoRefactorRegister<ConvertFunctionRefactor> g_convertFunctionRefactorRegister("ConvertFunctionRefactor");
89 
90 }  // namespace ark::es2panda::lsp
91