|
| 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 | +} |