-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeScriptAnalyzer.cs
More file actions
292 lines (250 loc) · 11.7 KB
/
TypeScriptAnalyzer.cs
File metadata and controls
292 lines (250 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using TypeScriptParser.TreeSitter;
using System.Runtime.InteropServices;
using TypeScriptParser;
namespace TypeScriptParser.Tests
{
// 参数信息结构
public class Parameter
{
public string Name { get; set; }
public string Type { get; set; }
public string DefaultValue { get; set; }
public bool IsOptional { get; set; }
public bool IsRestParameter { get; set; }
}
// 导出函数信息结构
public class ExportedFunction
{
public string Name { get; set; }
public List<Parameter> Parameters { get; set; } = new List<Parameter>();
public string ReturnType { get; set; }
public int LineNumber { get; set; }
public string SourceText { get; set; }
}
// TypeScript分析器模块
public class TypeScriptAnalyzer : IDisposable
{
private readonly Parser parser;
private readonly List<ExportedFunction> exportedFunctions = [];
private bool disposed = false;
public TypeScriptAnalyzer()
{
parser = new Parser();
}
public List<ExportedFunction> AnalyzeFile(string fileContent)
{
if (disposed)
throw new ObjectDisposedException(nameof(TypeScriptAnalyzer));
exportedFunctions.Clear();
using var tree = parser.ParseString(fileContent);
if (tree == null) {
return exportedFunctions;
}
using var cursor = new TSCursor(tree.root_node(), tree.language());
TraverseForExports(cursor, fileContent);
return [.. exportedFunctions];
}
private void TraverseForExports(TSCursor cursor, string filetext)
{
do {
var type = cursor.current_symbol();
int so = (int)cursor.current_node().start_offset();
int eo = (int)cursor.current_node().end_offset();
int line = (int)cursor.current_node().start_point().row + 1;
// 查找 export_statement 节点
if (type == "export_statement") {
ProcessExportStatement(cursor, filetext, line);
}
// 递归遍历子节点
if (cursor.goto_first_child()) {
TraverseForExports(cursor, filetext);
cursor.goto_parent();
}
} while (cursor.goto_next_sibling());
}
private void ProcessExportStatement(TSCursor cursor, string filetext, int line)
{
int so = (int)cursor.current_node().start_offset();
int eo = (int)cursor.current_node().end_offset();
var exportText = filetext.AsSpan(so, eo - so).ToString();
// 保存当前位置
var savedCursor = cursor;
if (cursor.goto_first_child()) {
do {
var childType = cursor.current_symbol();
// 直接导出函数: export function functionName()
if (childType == "function_declaration") {
ProcessFunctionDeclaration(cursor, filetext, line, exportText, true);
}
// 导出列表: export { functionName }
else if (childType == "export_clause") {
ProcessExportClause(cursor, filetext, line, exportText);
}
} while (cursor.goto_next_sibling());
cursor.goto_parent();
}
}
private void ProcessFunctionDeclaration(TSCursor cursor, string filetext, int line, string exportText, bool isDirectExport)
{
string functionName = "";
var parameters = new List<Parameter>();
string returnType = "";
if (cursor.goto_first_child()) {
do {
var childType = cursor.current_symbol();
int so = (int)cursor.current_node().start_offset();
int eo = (int)cursor.current_node().end_offset();
var text = filetext.AsSpan(so, eo - so).ToString();
if (childType == "identifier" && string.IsNullOrEmpty(functionName)) {
functionName = text;
}
else if (childType == "formal_parameters") {
parameters = ParseParameters(cursor, filetext);
}
else if (childType == "type_annotation") {
returnType = text.Substring(1).Trim(); // 移除前面的 ":"
}
} while (cursor.goto_next_sibling());
cursor.goto_parent();
}
if (!string.IsNullOrEmpty(functionName)) {
exportedFunctions.Add(new ExportedFunction {
Name = functionName,
Parameters = parameters,
ReturnType = returnType,
LineNumber = line,
SourceText = exportText.Trim()
});
}
}
private void ProcessExportClause(TSCursor cursor, string filetext, int line, string exportText)
{
// 这里处理 export { functionName } 形式
// 需要查找对应的函数声明
if (cursor.goto_first_child()) {
do {
var childType = cursor.current_symbol();
if (childType == "export_specifier") {
if (cursor.goto_first_child()) {
var identifierType = cursor.current_symbol();
if (identifierType == "identifier") {
int so = (int)cursor.current_node().start_offset();
int eo = (int)cursor.current_node().end_offset();
var functionName = filetext.AsSpan(so, eo - so).ToString();
// 添加导出函数(简化版本,不查找具体的函数声明)
var placeholderParameters = new List<Parameter>
{
new Parameter
{
Name = "// 从导出列表中识别",
Type = "// 需要查找原函数声明",
IsOptional = false,
IsRestParameter = false
}
};
exportedFunctions.Add(new ExportedFunction {
Name = functionName,
Parameters = placeholderParameters,
ReturnType = "// 需要查找原函数声明",
LineNumber = line,
SourceText = exportText.Trim()
});
}
cursor.goto_parent();
}
}
} while (cursor.goto_next_sibling());
cursor.goto_parent();
}
}
private List<Parameter> ParseParameters(TSCursor cursor, string filetext)
{
var parameters = new List<Parameter>();
// 保存当前位置
var originalCursor = cursor;
if (cursor.goto_first_child()) {
do {
var childType = cursor.current_symbol();
if (childType == "required_parameter" || childType == "optional_parameter" || childType == "rest_parameter") {
var parameter = ParseSingleParameter(cursor, filetext);
if (parameter != null) {
parameters.Add(parameter);
}
}
} while (cursor.goto_next_sibling());
cursor.goto_parent();
}
return parameters;
}
private Parameter ParseSingleParameter(TSCursor cursor, string filetext)
{
var parameter = new Parameter();
bool isRestParameter = cursor.current_symbol() == "rest_parameter";
bool isOptional = cursor.current_symbol() == "optional_parameter";
parameter.IsRestParameter = isRestParameter;
parameter.IsOptional = isOptional;
if (cursor.goto_first_child()) {
do {
var childType = cursor.current_symbol();
int so = (int)cursor.current_node().start_offset();
int eo = (int)cursor.current_node().end_offset();
var text = filetext.AsSpan(so, eo - so).ToString();
if (childType == "identifier" && string.IsNullOrEmpty(parameter.Name)) {
parameter.Name = text;
}
else if (childType == "type_annotation") {
parameter.Type = text.Substring(1).Trim(); // 移除前面的 ":"
}
else if (childType == "assignment_pattern") {
// 处理默认参数值
if (cursor.goto_first_child()) {
do {
var assignChildType = cursor.current_symbol();
if (assignChildType == "identifier" && string.IsNullOrEmpty(parameter.Name)) {
int assignSo = (int)cursor.current_node().start_offset();
int assignEo = (int)cursor.current_node().end_offset();
parameter.Name = filetext.AsSpan(assignSo, assignEo - assignSo).ToString();
}
else if (assignChildType != "identifier" && assignChildType != "=") {
// 默认值
int defaultSo = (int)cursor.current_node().start_offset();
int defaultEo = (int)cursor.current_node().end_offset();
parameter.DefaultValue = filetext.AsSpan(defaultSo, defaultEo - defaultSo).ToString();
}
} while (cursor.goto_next_sibling());
cursor.goto_parent();
}
}
else if (childType == "...") {
// Rest parameter spread operator
parameter.IsRestParameter = true;
}
} while (cursor.goto_next_sibling());
cursor.goto_parent();
}
return !string.IsNullOrEmpty(parameter.Name) ? parameter : null;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
parser?.Dispose();
}
disposed = true;
}
}
~TypeScriptAnalyzer()
{
Dispose(false);
}
}
}