• 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 #ifndef ES2PANDA_LSP_INCLUDE_RENAME_H
17 #define ES2PANDA_LSP_INCLUDE_RENAME_H
18 
19 #include <optional>
20 #include <string>
21 #include <utility>
22 #include <variant>
23 #include <cstddef>
24 
25 #include "api.h"
26 #include "ir/astNode.h"
27 #include "public/es2panda_lib.h"
28 #include "checker/types/type.h"
29 #include "parser/program/program.h"
30 #include "util/helpers.h"
31 
32 namespace ark::es2panda::lsp {
33 
34 struct RenameInfoSuccess {
35 private:
36     bool canRenameSuccess_;
37     std::string fileToRename_;
38     std::string kind_;
39     std::string displayName_;
40     std::string fullDisplayName_;
41     std::string kindModifiers_;
42     TextSpan triggerSpan_;
43 
44 public:
RenameInfoSuccessRenameInfoSuccess45     RenameInfoSuccess(bool canRename, std::string fileToRename, std::string kind, std::string displayName,
46                       std::string fullDisplayName, std::string kindModifiers, TextSpan triggerSpan)
47         : canRenameSuccess_(canRename),
48           fileToRename_(std::move(fileToRename)),
49           kind_(std::move(kind)),
50           displayName_(std::move(displayName)),
51           fullDisplayName_(std::move(fullDisplayName)),
52           kindModifiers_(std::move(kindModifiers)),
53           triggerSpan_(triggerSpan)
54     {
55     }
GetCanRenameSuccessRenameInfoSuccess56     bool GetCanRenameSuccess() const
57     {
58         return canRenameSuccess_;
59     }
GetFileToRenameRenameInfoSuccess60     const std::string &GetFileToRename() const
61     {
62         return fileToRename_;
63     }
GetKindRenameInfoSuccess64     const std::string &GetKind() const
65     {
66         return kind_;
67     }
GetDisplayNameRenameInfoSuccess68     const std::string &GetDisplayName() const
69     {
70         return displayName_;
71     }
GetFullDisplayNameRenameInfoSuccess72     const std::string &GetFullDisplayName() const
73     {
74         return fullDisplayName_;
75     }
GetKindModifiersRenameInfoSuccess76     const std::string &GetKindModifiers() const
77     {
78         return kindModifiers_;
79     }
GetTriggerSpanRenameInfoSuccess80     const TextSpan &GetTriggerSpan() const
81     {
82         return triggerSpan_;
83     }
84 };
85 
86 struct RenameInfoFailure {
87 private:
88     bool canRenameFailure_;
89     std::string localizedErrorMessage_;
90 
91 public:
RenameInfoFailureRenameInfoFailure92     RenameInfoFailure(bool canRename, std::string localizedErrorMessage)
93         : canRenameFailure_(canRename), localizedErrorMessage_(std::move(localizedErrorMessage))
94     {
95     }
96 
GetCanRenameFailureRenameInfoFailure97     bool GetCanRenameFailure() const
98     {
99         return canRenameFailure_;
100     }
GetLocalizedErrorMessageRenameInfoFailure101     const std::string &GetLocalizedErrorMessage() const
102     {
103         return localizedErrorMessage_;
104     }
105 };
106 
107 using RenameInfoType = std::variant<RenameInfoSuccess, RenameInfoFailure>;
108 
109 RenameInfoType GetRenameInfo(es2panda_Context *context, size_t pos);
110 std::optional<RenameInfoType> GetRenameInfoForNode(ir::AstNode *node, checker::ETSChecker *checker,
111                                                    parser::Program *program);
112 std::optional<checker::VerifiedType> GetContextualTypeFromParentOrAncestorTypeNode(ir::AstNode *node,
113                                                                                    checker::ETSChecker *checker);
114 std::string GetTextOfNode(ir::AstNode *node, parser::Program *program);
115 
116 RenameInfoSuccess GetRenameInfoSuccess(std::string displayName, std::string fullDisplayName, std::string kind,
117                                        std::string kindModifiers, ir::AstNode *node);
118 RenameInfoFailure GetRenameInfoError(std::string diagnosticMessage);
119 
120 ir::AstNode *TryGetImportFromModuleSpecifier(ir::AstNode *node);
121 
122 bool IsValidAncestorType(ir::AstNodeType type);
123 bool NodeIsMissing(ir::AstNode *node);
124 
125 TextSpan CreateTriggerSpanForNode(ir::AstNode *node);
126 std::string GetSourceTextOfNodeFromSourceFile(util::StringView sourceCode, ir::AstNode *node);
127 
128 std::string StripQuotes(std::string name);
129 bool IsQuoteOrBacktick(int charCode);
130 std::optional<RenameInfoSuccess> GetRenameInfoForModule(ir::AstNode *node, parser::Program *program);
131 
132 std::string GetNodeKindForRenameInfo(ir::AstNode *node);
133 bool IsImportOrExportSpecifierName(ir::AstNode *node);
134 bool IsStringOrNumericLiteralLike(ir::AstNode *node);
135 
136 bool NodeIsEligibleForRename(ir::AstNode *node);
137 bool IsLiteralNameOfPropertyDeclarationOrIndexAccess(ir::AstNode *node);
138 ir::AstNode *GetNameOfDeclaration(ir::AstNode *node);
139 ir::AstNode *GetNonAssignedNameOfDeclaration(ir::AstNode *node);
140 ir::AstNode *GetAssignedName(ir::AstNode *node);
141 }  // namespace ark::es2panda::lsp
142 #endif