• 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 <gtest/gtest.h>
17 #include <cstddef>
18 #include <string>
19 #include "lsp_api_test.h"
20 #include "lsp/include/applicable_refactors.h"
21 
22 const size_t REFACTOR_CHAIN_POSITION_OFFSET = 8;
23 
24 namespace {
25 using ark::es2panda::lsp::Initializer;
26 
27 class LspChainRefTests : public LSPAPITests {
28 public:
29     static constexpr std::string_view TO_NAMED_CHAIN_KIND = "refactor.rewrite.expression.optionalChain";
30     static constexpr std::string_view TO_NAMED_CHAIN_NAME = "Convert to optional chain expression";
31 };
32 
TEST_F(LspChainRefTests,ConvertChainRefactor1)33 TEST_F(LspChainRefTests, ConvertChainRefactor1)
34 {
35     std::vector<std::string> files = {"ConvertChainRefactor1.ets"};
36     std::vector<std::string> texts = {R"(let a = {b: ()=>{return () =>{c:0}}}/*1*/a&&a.b&&a.b()().c/*2*/;)"};
37 
38     auto filePaths = CreateTempFile(files, texts);
39     size_t const expectedFileCount = 1;
40     ASSERT_EQ(filePaths.size(), expectedFileCount);
41 
42     auto pos = texts[0].find("/*1*/");
43     ASSERT_NE(pos, std::string::npos);
44     Initializer initializer = Initializer();
45     auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
46     ark::es2panda::lsp::RefactorContext refactorContext;
47     refactorContext.context = ctx;
48     refactorContext.kind = std::string(TO_NAMED_CHAIN_KIND);
49     refactorContext.span.pos = pos + REFACTOR_CHAIN_POSITION_OFFSET;
50     auto result = GetApplicableRefactorsImpl(&refactorContext);
51 
52     initializer.DestroyContext(ctx);
53     ASSERT_EQ(1, result.size());
54     ASSERT_EQ(std::string(TO_NAMED_CHAIN_NAME), result[0].action.name);
55 }
56 
TEST_F(LspChainRefTests,ConvertChainRefactor2)57 TEST_F(LspChainRefTests, ConvertChainRefactor2)
58 {
59     std::vector<std::string> files = {"ConvertChainRefactor2.ets"};
60     std::vector<std::string> texts = {R"(interface A { baz?:string | null; }
61 interface Foo{
62 bar?: A
63 }
64 declare let foo: Foo;
65 let ccc = /*1*/foo.bar ? foo.bar.baz : "whenFalse";/*2*/
66  )"};
67 
68     auto filePaths = CreateTempFile(files, texts);
69     size_t const expectedFileCount = 1;
70     ASSERT_EQ(filePaths.size(), expectedFileCount);
71 
72     auto pos = texts[0].find("/*1*/");
73     ASSERT_NE(pos, std::string::npos);
74     Initializer initializer = Initializer();
75     auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
76     ark::es2panda::lsp::RefactorContext refactorContext;
77     refactorContext.context = ctx;
78     refactorContext.kind = std::string(TO_NAMED_CHAIN_KIND);
79     refactorContext.span.pos = pos + REFACTOR_CHAIN_POSITION_OFFSET;
80     auto result = GetApplicableRefactorsImpl(&refactorContext);
81     initializer.DestroyContext(ctx);
82     ASSERT_EQ(0, result.size());
83 }
84 
TEST_F(LspChainRefTests,ConvertChainRefactor3)85 TEST_F(LspChainRefTests, ConvertChainRefactor3)
86 {
87     std::vector<std::string> files = {"ConvertChainRefactor3.ets"};
88     std::vector<std::string> texts = {R"(interface A { baz: string }
89 interface Foo{
90 bar?: A
91 }
92 declare let foo: Foo;
93 let ccc = /*1*/foo.bar ? foo.bar.baz: "whenFalse";/*2*/
94  )"};
95 
96     auto filePaths = CreateTempFile(files, texts);
97     size_t const expectedFileCount = 1;
98     ASSERT_EQ(filePaths.size(), expectedFileCount);
99 
100     auto pos = texts[0].find("/*1*/");
101     ASSERT_NE(pos, std::string::npos);
102     Initializer initializer = Initializer();
103     auto ctx = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
104     ark::es2panda::lsp::RefactorContext refactorContext;
105     refactorContext.context = ctx;
106     refactorContext.kind = std::string(TO_NAMED_CHAIN_KIND);
107     refactorContext.span.pos = pos + REFACTOR_CHAIN_POSITION_OFFSET;
108     auto result = GetApplicableRefactorsImpl(&refactorContext);
109 
110     initializer.DestroyContext(ctx);
111     ASSERT_EQ(1, result.size());
112     ASSERT_EQ(std::string(TO_NAMED_CHAIN_NAME), result[0].action.name);
113 }
114 
115 }  // namespace