GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — docs: simplify README and consolidate documentation structure · s97712/TypeScriptParser@f2a0275

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • TypeScriptParser.Tests/tests
    • ExportExtractorTests.cs
  • USAGE_GUIDE.md
  • docs
    • API_REFERENCE.md
    • USAGE_GUIDE.md
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+4-17Lines changed: 4 additions & 17 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44

55
## 安装
66

7-
### 从NuGet安装
8-
97
```bash
108
dotnet add package TypeScriptParser
119
```
1210

13-
或通过包管理器控制台:
14-
15-
```powershell
16-
Install-Package TypeScriptParser
17-
```
11+
## 文档
1812

19-
也可以访问 [NuGet包页面](https://www.nuget.org/packages/TypeScriptParser) 查看更多安装选项。
13+
- [使用指南](docs/USAGE_GUIDE.md) - 快速开始和常用示例
14+
- [API参考](docs/API_REFERENCE.md) - 完整的API文档
2015

2116
## 构建项目
2217

@@ -48,15 +43,7 @@ dotnet test TypeScriptParser.TestPackage
4843
- [`TypeScriptParser.Native/`](TypeScriptParser.Native/) - 跨平台Native库包
4944
- [`TypeScriptParser.Tests/`](TypeScriptParser.Tests/) - 单元测试
5045

51-
## 开发指南
52-
53-
### Debug模式构建
54-
```bash
55-
dotnet build -c Debug
56-
dotnet test --configuration Debug
57-
```
58-
59-
### 支持平台
46+
## 支持平台
6047
- Linux x64
6148
- macOS ARM64
6249
- Windows x64
Collapse file

‎TypeScriptParser.Tests/tests/ExportExtractorTests.cs‎

Copy file name to clipboard
+197Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Xunit;
5+
using TypeScriptParser;
6+
using TypeScriptParser.TreeSitter;
7+
8+
namespace TypeScriptParser.Tests
9+
{
10+
public class ExportExtractorTests
11+
{
12+
[Fact]
13+
public void ExtractExportFunctions_SimpleExport_ShouldReturnFunctionNames()
14+
{
15+
// Arrange
16+
string code = @"
17+
export function add(a: number, b: number): number {
18+
return a + b;
19+
}
20+
21+
export function multiply(x: number, y: number): number {
22+
return x * y;
23+
}";
24+
25+
// Act
26+
var exportedFunctions = ExtractExportFunctions(code);
27+
28+
// Assert
29+
Assert.Equal(2, exportedFunctions.Count);
30+
Assert.Contains("add", exportedFunctions);
31+
Assert.Contains("multiply", exportedFunctions);
32+
}
33+
34+
[Fact]
35+
public void ExtractExportFunctions_MixedExports_ShouldOnlyReturnFunctions()
36+
{
37+
// Arrange
38+
string code = @"
39+
export function add(a: number, b: number): number {
40+
return a + b;
41+
}
42+
43+
function helper() {
44+
return 'not exported';
45+
}
46+
47+
export const PI = 3.14;
48+
49+
export function multiply(x: number, y: number): number {
50+
return x * y;
51+
}";
52+
53+
// Act
54+
var exportedFunctions = ExtractExportFunctions(code);
55+
56+
// Assert
57+
Assert.Equal(2, exportedFunctions.Count);
58+
Assert.Contains("add", exportedFunctions);
59+
Assert.Contains("multiply", exportedFunctions);
60+
Assert.DoesNotContain("helper", exportedFunctions);
61+
Assert.DoesNotContain("PI", exportedFunctions);
62+
}
63+
64+
[Fact]
65+
public void ExtractExportFunctions_NoExports_ShouldReturnEmpty()
66+
{
67+
// Arrange
68+
string code = @"
69+
function helper() {
70+
return 'not exported';
71+
}
72+
73+
const PI = 3.14;";
74+
75+
// Act
76+
var exportedFunctions = ExtractExportFunctions(code);
77+
78+
// Assert
79+
Assert.Empty(exportedFunctions);
80+
}
81+
82+
[Fact]
83+
public void ExtractExportFunctions_EmptyCode_ShouldReturnEmpty()
84+
{
85+
// Arrange
86+
string code = "";
87+
88+
// Act
89+
var exportedFunctions = ExtractExportFunctions(code);
90+
91+
// Assert
92+
Assert.Empty(exportedFunctions);
93+
}
94+
95+
[Fact]
96+
public void ExtractExportFunctions_ComplexExports_ShouldExtractAllFunctions()
97+
{
98+
// Arrange
99+
string code = @"
100+
export function calculate(x: number): number {
101+
return x * 2;
102+
}
103+
104+
export function processData(data: any[]): any[] {
105+
return data.filter(item => item != null);
106+
}
107+
108+
export function validateInput(input: string): boolean {
109+
return input && input.length > 0;
110+
}
111+
112+
// 非函数导出
113+
export const config = { api: 'https://api.example.com' };
114+
export let counter = 0;
115+
116+
// 非导出函数
117+
function internalHelper() {
118+
return 'internal';
119+
}";
120+
121+
// Act
122+
var exportedFunctions = ExtractExportFunctions(code);
123+
124+
// Assert
125+
Assert.Equal(3, exportedFunctions.Count);
126+
Assert.Contains("calculate", exportedFunctions);
127+
Assert.Contains("processData", exportedFunctions);
128+
Assert.Contains("validateInput", exportedFunctions);
129+
Assert.DoesNotContain("internalHelper", exportedFunctions);
130+
}
131+
132+
[Fact]
133+
public void ExtractExportFunctions_AsyncFunctions_ShouldExtractAsyncFunctions()
134+
{
135+
// Arrange
136+
string code = @"
137+
export async function fetchData(url: string): Promise<any> {
138+
const response = await fetch(url);
139+
return response.json();
140+
}
141+
142+
export function syncFunction(): string {
143+
return 'sync';
144+
}";
145+
146+
// Act
147+
var exportedFunctions = ExtractExportFunctions(code);
148+
149+
// Assert
150+
Assert.Equal(2, exportedFunctions.Count);
151+
Assert.Contains("fetchData", exportedFunctions);
152+
Assert.Contains("syncFunction", exportedFunctions);
153+
}
154+
155+
/// <summary>
156+
/// 提取TypeScript代码中所有导出的函数名称
157+
/// </summary>
158+
/// <param name="code">TypeScript源代码</param>
159+
/// <returns>导出函数名称列表</returns>
160+
private List<string> ExtractExportFunctions(string code)
161+
{
162+
var exportedFunctions = new List<string>();
163+
164+
using var parser = new Parser();
165+
using var tree = parser.ParseString(code);
166+
167+
// 创建查询匹配 export 函数
168+
var query = tree.language().query_new(@"
169+
(export_statement
170+
declaration: (function_declaration
171+
name: (identifier) @func_name))
172+
", out var offset, out var error);
173+
174+
if (query != null && error == TSQueryError.TSQueryErrorNone)
175+
{
176+
using var cursor = new TSQueryCursor();
177+
cursor.exec(query, tree.root_node());
178+
179+
while (cursor.next_match(out var match, out var captures))
180+
{
181+
foreach (var capture in captures)
182+
{
183+
var funcName = capture.node.text(code);
184+
if (!string.IsNullOrWhiteSpace(funcName))
185+
{
186+
exportedFunctions.Add(funcName);
187+
}
188+
}
189+
}
190+
191+
query.Dispose();
192+
}
193+
194+
return exportedFunctions;
195+
}
196+
}
197+
}
Collapse file

‎USAGE_GUIDE.md‎

Copy file name to clipboardExpand all lines: USAGE_GUIDE.md
-105Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.