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_chain.h"
18 #include "refactor_provider.h"
19 #include "internal_api.h"
20 #include "public/public.h"
21
22 namespace ark::es2panda::lsp {
ConvertChainRefactor()23 ConvertChainRefactor::ConvertChainRefactor()
24 {
25 AddKind(std::string(TO_NAMED_CHAIN_ACTION.kind));
26 }
27
FindETSNullType(ark::es2panda::ir::AstNode * node,es2panda_Context * context)28 ark::es2panda::ir::AstNode *FindETSNullType(ark::es2panda::ir::AstNode *node, es2panda_Context *context)
29 {
30 auto consequent = node->AsConditionalExpression()->Consequent();
31 auto nodeproperty = consequent->AsMemberExpression()->Property();
32 auto ctx = reinterpret_cast<public_lib::Context *>(context);
33 auto targetNode = ctx->parserProgram->Ast()->FindChild([&nodeproperty](ir::AstNode *childNode) {
34 return childNode->IsScriptFunction() &&
35 childNode->AsScriptFunction()->Id()->AsIdentifier()->Name() == nodeproperty->AsIdentifier()->Name();
36 });
37 auto etc = targetNode->FindChild([](ir::AstNode *childNode) { return childNode->IsETSNullType(); });
38
39 return etc;
40 }
41
GetAvailableActions(const RefactorContext & refContext) const42 ApplicableRefactorInfo ConvertChainRefactor::GetAvailableActions(const RefactorContext &refContext) const
43 {
44 es2panda_Context *context = refContext.context;
45 size_t position = refContext.span.pos;
46
47 ApplicableRefactorInfo res;
48
49 if (!IsKind(refContext.kind)) {
50 return res;
51 }
52 auto node = GetTouchingToken(context, position, false);
53 if (node == nullptr) {
54 return res;
55 }
56
57 auto parent = node->Parent();
58 if (parent == nullptr) {
59 return res;
60 }
61 if (parent->IsExpression()) {
62 if (parent->IsConditionalExpression()) {
63 auto etc = FindETSNullType(parent, context);
64 if (etc != nullptr) {
65 return res;
66 }
67 }
68 res.name = refactor_name::CONVERT_CHAIN_REFACTOR_NAME;
69 res.description = refactor_description::CONVERT_CHAIN_REFACTOR_DESC;
70 res.action.kind = std::string(TO_NAMED_CHAIN_ACTION.kind);
71 res.action.name = std::string(TO_NAMED_CHAIN_ACTION.name);
72 res.action.description = std::string(TO_NAMED_CHAIN_ACTION.description);
73 }
74 return res;
75 }
76
GetEditsForAction(const RefactorContext & context,const std::string & actionName) const77 std::unique_ptr<RefactorEditInfo> ConvertChainRefactor::GetEditsForAction(const RefactorContext &context,
78 const std::string &actionName) const
79 {
80 (void)context;
81 (void)actionName;
82 return std::make_unique<RefactorEditInfo>();
83 }
84 // NOLINTNEXTLINE(fuchsia-statically-constructed-objects, cert-err58-cpp)
85 AutoRefactorRegister<ConvertChainRefactor> g_convertChainRefactorRegister("ConvertChainRefactor");
86
87 } // namespace ark::es2panda::lsp