• 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 "lsp_api_test.h"
17 
18 #include <gtest/gtest.h>
19 #include <cstddef>
20 
21 #include "lsp/include/internal_api.h"
22 #include "public/es2panda_lib.h"
23 #include "public/public.h"
24 
25 namespace {
26 
27 using ark::es2panda::lsp::Initializer;
28 
TEST_F(LSPAPITests,GetFileReferencesImpl1)29 TEST_F(LSPAPITests, GetFileReferencesImpl1)
30 {
31     using ark::es2panda::public_lib::Context;
32     std::vector<std::string> files = {"lsp_api_test_export_1.ets", "lsp_api_test_file_1.ets"};
33     std::vector<std::string> texts = {
34         R"(export function A(a:number, b:number): number {
35   return a + b;
36 }
37 export function B(a:number, b:number): number {
38   return a + b;
39 })",
40         R"(import {A} from "./lsp_api_test_export_1";
41 import {B} from "./lsp_api_test_export_1.ets";
42 A(1, 2);
43 B(1, 2);)"};
44     auto filePaths = CreateTempFile(files, texts);
45     int const expectedFileCount = 2;
46     ASSERT_EQ(filePaths.size(), expectedFileCount);
47 
48     char const *searchFileName = filePaths[0].c_str();
49     char const *referenceFileName = filePaths[1].c_str();
50     Initializer initializer = Initializer();
51     auto ctx = initializer.CreateContext(searchFileName, ES2PANDA_STATE_CHECKED);
52     ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
53 
54     auto isPackageModule = reinterpret_cast<Context *>(ctx)->parserProgram->IsPackage();
55     ASSERT_FALSE(isPackageModule);
56     initializer.DestroyContext(ctx);
57 
58     auto ctx1 = initializer.CreateContext(referenceFileName, ES2PANDA_STATE_CHECKED);
59     ASSERT_EQ(ContextState(ctx1), ES2PANDA_STATE_CHECKED);
60 
61     auto result = References();
62     result = ark::es2panda::lsp::GetFileReferencesImpl(ctx1, searchFileName, isPackageModule);
63     auto expectedFileName1 = filePaths[1];
64     size_t const expectedStartPos1 = 16;
65     size_t const expectedLength1 = 25;
66     auto expectedFileName2 = filePaths[1];
67     size_t const expectedStartPos2 = 59;
68     size_t const expectedLength2 = 29;
69     ASSERT_EQ(result.referenceInfos.at(0).fileName, expectedFileName1);
70     ASSERT_EQ(result.referenceInfos.at(0).start, expectedStartPos1);
71     ASSERT_EQ(result.referenceInfos.at(0).length, expectedLength1);
72     ASSERT_EQ(result.referenceInfos.at(1).fileName, expectedFileName2);
73     ASSERT_EQ(result.referenceInfos.at(1).start, expectedStartPos2);
74     ASSERT_EQ(result.referenceInfos.at(1).length, expectedLength2);
75     initializer.DestroyContext(ctx1);
76 }
77 
TEST_F(LSPAPITests,GetFileReferencesImpl2)78 TEST_F(LSPAPITests, GetFileReferencesImpl2)
79 {
80     using ark::es2panda::public_lib::Context;
81     std::vector<std::string> files = {"lsp_api_test_export_2.ts", "lsp_api_test_file_2.ets"};
82     std::vector<std::string> texts = {
83         R"(export function A(a:number, b:number): number {
84   return a + b;
85 }
86 export function B(a:number, b:number): number {
87   return a + b;
88 })",
89         R"(import {A} from "./lsp_api_test_export_2";
90 import {B} from "./lsp_api_test_export_2.ts";
91 A(1, 2);
92 B(1, 2);)"};
93     auto filePaths = CreateTempFile(files, texts);
94     int const expectedFileCount = 2;
95     ASSERT_EQ(filePaths.size(), expectedFileCount);
96 
97     char const *searchFileName = filePaths[0].c_str();
98     char const *referenceFileName = filePaths[1].c_str();
99     Initializer initializer = Initializer();
100     auto ctx = initializer.CreateContext(searchFileName, ES2PANDA_STATE_CHECKED);
101     ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
102 
103     auto isPackageModule = reinterpret_cast<Context *>(ctx)->parserProgram->IsPackage();
104     ASSERT_FALSE(isPackageModule);
105     initializer.DestroyContext(ctx);
106 
107     auto ctx1 = initializer.CreateContext(referenceFileName, ES2PANDA_STATE_CHECKED);
108     ASSERT_EQ(ContextState(ctx1), ES2PANDA_STATE_CHECKED);
109 
110     auto result = References();
111     result = ark::es2panda::lsp::GetFileReferencesImpl(ctx1, searchFileName, isPackageModule);
112     auto expectedFileName1 = filePaths[1];
113     size_t const expectedStartPos1 = 16;
114     size_t const expectedLength1 = 25;
115     auto expectedFileName2 = filePaths[1];
116     size_t const expectedStartPos2 = 59;
117     size_t const expectedLength2 = 28;
118     ASSERT_EQ(result.referenceInfos.at(0).fileName, expectedFileName1);
119     ASSERT_EQ(result.referenceInfos.at(0).start, expectedStartPos1);
120     ASSERT_EQ(result.referenceInfos.at(0).length, expectedLength1);
121     ASSERT_EQ(result.referenceInfos.at(1).fileName, expectedFileName2);
122     ASSERT_EQ(result.referenceInfos.at(1).start, expectedStartPos2);
123     ASSERT_EQ(result.referenceInfos.at(1).length, expectedLength2);
124     initializer.DestroyContext(ctx1);
125 }
126 
TEST_F(LSPAPITests,GetFileReferencesImpl3)127 TEST_F(LSPAPITests, GetFileReferencesImpl3)
128 {
129     using ark::es2panda::public_lib::Context;
130     std::vector<std::string> files = {"package-module.ets"};
131     std::vector<std::string> texts = {R"(import { PI } from "std/math";
132 console.log(PI);)"};
133     auto filePaths = CreateTempFile(files, texts);
134     int const expectedFileCount = 1;
135     ASSERT_EQ(filePaths.size(), expectedFileCount);
136 
137     char const *referenceFileName = filePaths[0].c_str();
138     Initializer initializer = Initializer();
139     auto ctx = initializer.CreateContext(referenceFileName, ES2PANDA_STATE_CHECKED);
140     ASSERT_EQ(ContextState(ctx), ES2PANDA_STATE_CHECKED);
141 
142     auto baseUrl = reinterpret_cast<Context *>(ctx)->config->options->ArkTSConfig()->BaseUrl();
143     auto searchFileName = baseUrl + "/plugins/ets/stdlib/std/math/math.ets";
144     auto result = References();
145     result = ark::es2panda::lsp::GetFileReferencesImpl(ctx, searchFileName.c_str(), true);
146     auto expectedFileName = filePaths[0];
147     size_t const expectedStartPos = 19;
148     size_t const expectedLength = 10;
149 
150     ASSERT_EQ(result.referenceInfos.at(0).fileName, expectedFileName);
151     ASSERT_EQ(result.referenceInfos.at(0).start, expectedStartPos);
152     ASSERT_EQ(result.referenceInfos.at(0).length, expectedLength);
153     initializer.DestroyContext(ctx);
154 }
155 
156 }  // namespace
157