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 <cstddef>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19 #include <string>
20 #include <vector>
21 #include "lsp_api_test.h"
22 #include "lsp/include/internal_api.h"
23 #include "lsp/include/get_name_or_dotted_name_span.h"
24
25 namespace {
26 const size_t IDENTIFIER_ONLY_IDX = 24;
27 const size_t PROPERTY_ACCESS_IDX = 36;
28 const size_t FULL_DOTTED_EXPR_IDX = 56;
29 const size_t INVALID_TOKEN_IDX = 31;
30 const size_t MODULE_DECLARATION_QUALIFIED_NAME_IDX = 72;
31
ExtractSpanText(const std::string & source,const TextSpan * span)32 std::string ExtractSpanText(const std::string &source, const TextSpan *span)
33 {
34 if (span == nullptr || span->start + span->length > source.size()) {
35 return {};
36 }
37
38 return source.substr(span->start, span->length);
39 }
40
41 class GetNameOrDottedNameSpanTests : public LSPAPITests {};
TEST_F(GetNameOrDottedNameSpanTests,GetNameSpanFromSimpleIdentifier)42 TEST_F(GetNameOrDottedNameSpanTests, GetNameSpanFromSimpleIdentifier)
43 {
44 std::vector<std::string> fileNames = {"get_name_span_from_simple_identifier.ets"};
45 std::vector<std::string> fileContents = {
46 R"(
47 function myMethod() {}
48 myMethod();
49 )"};
50
51 auto filePaths = CreateTempFile(fileNames, fileContents);
52 ASSERT_EQ(filePaths.size(), 1);
53
54 ark::es2panda::lsp::Initializer initializer;
55 auto context = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
56 auto content = fileContents[0];
57 auto span = ark::es2panda::lsp::GetNameOrDottedNameSpanImpl(context, IDENTIFIER_ONLY_IDX);
58 ASSERT_NE(span, nullptr);
59 EXPECT_EQ(ExtractSpanText(content, span), "myMethod");
60 initializer.DestroyContext(context);
61 }
62
TEST_F(GetNameOrDottedNameSpanTests,GetNameSpanFromPropertyAccess)63 TEST_F(GetNameOrDottedNameSpanTests, GetNameSpanFromPropertyAccess)
64 {
65 std::vector<std::string> fileNames = {"get_name_span_from_property_access.ets"};
66 std::vector<std::string> fileContents = {
67 R"(
68 class A {
69 static myMethod() {}
70 }
71 A.myMethod();
72 )"};
73
74 auto filePaths = CreateTempFile(fileNames, fileContents);
75 ASSERT_EQ(filePaths.size(), 1);
76
77 ark::es2panda::lsp::Initializer initializer;
78 auto context = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
79 auto content = fileContents[0];
80 auto span = ark::es2panda::lsp::GetNameOrDottedNameSpanImpl(context, PROPERTY_ACCESS_IDX);
81 ASSERT_NE(span, nullptr);
82 EXPECT_EQ(ExtractSpanText(content, span), "A.myMethod");
83 initializer.DestroyContext(context);
84 }
85
TEST_F(GetNameOrDottedNameSpanTests,GetNameSpanFromNestedNamespaceClassCall)86 TEST_F(GetNameOrDottedNameSpanTests, GetNameSpanFromNestedNamespaceClassCall)
87 {
88 std::vector<std::string> fileNames = {"get_name_span_from_nested_namespace_class_call.ets"};
89 std::vector<std::string> fileContents = {
90 R"(
91 namespace A {
92 export class B {
93 static foo() {}
94 }
95 }
96 A.B.foo();
97 )"};
98
99 auto filePaths = CreateTempFile(fileNames, fileContents);
100 ASSERT_EQ(filePaths.size(), 1);
101
102 ark::es2panda::lsp::Initializer initializer;
103 auto context = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
104 auto content = fileContents[0];
105 auto span = ark::es2panda::lsp::GetNameOrDottedNameSpanImpl(context, FULL_DOTTED_EXPR_IDX);
106 ASSERT_NE(span, nullptr);
107 EXPECT_EQ(ExtractSpanText(content, span), "A.B.foo");
108 initializer.DestroyContext(context);
109 }
110
TEST_F(GetNameOrDottedNameSpanTests,GetNameSpanFromInvalidToken)111 TEST_F(GetNameOrDottedNameSpanTests, GetNameSpanFromInvalidToken)
112 {
113 std::vector<std::string> fileNames = {"get_name_span_from_invalid_token.ets"};
114 std::vector<std::string> fileContents = {
115 R"(
116 let message = "hello";
117 console.log(message);
118 )"};
119
120 auto filePaths = CreateTempFile(fileNames, fileContents);
121 ASSERT_EQ(filePaths.size(), 1);
122
123 ark::es2panda::lsp::Initializer initializer;
124 auto context = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
125 auto content = fileContents[0];
126 auto span = ark::es2panda::lsp::GetNameOrDottedNameSpanImpl(context, INVALID_TOKEN_IDX);
127 EXPECT_EQ(span, nullptr);
128 initializer.DestroyContext(context);
129 }
130
TEST_F(GetNameOrDottedNameSpanTests,GetNameSpanFromQualifiedNamespaceFunction)131 TEST_F(GetNameOrDottedNameSpanTests, GetNameSpanFromQualifiedNamespaceFunction)
132 {
133 std::vector<std::string> fileNames = {"get_name_span_from_qualified_namespace_function.ets"};
134 std::vector<std::string> fileContents = {
135 R"(
136 namespace Outer.Inner {
137 export function greet() {}
138 }
139
140 Outer.Inner.greet();
141 )"};
142
143 auto filePaths = CreateTempFile(fileNames, fileContents);
144 ASSERT_EQ(filePaths.size(), 1);
145 ark::es2panda::lsp::Initializer initializer;
146 auto context = initializer.CreateContext(filePaths[0].c_str(), ES2PANDA_STATE_CHECKED);
147 auto content = fileContents[0];
148 auto span = ark::es2panda::lsp::GetNameOrDottedNameSpanImpl(context, MODULE_DECLARATION_QUALIFIED_NAME_IDX);
149 ASSERT_NE(span, nullptr);
150 EXPECT_EQ(ExtractSpanText(content, span), "Outer.Inner.greet");
151 initializer.DestroyContext(context);
152 }
153 } // namespace