-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts_ast_utils.py
More file actions
39 lines (37 loc) · 1.23 KB
/
ts_ast_utils.py
File metadata and controls
39 lines (37 loc) · 1.23 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
import subprocess
import json
TS_PARSER_JS = r"""
const { Project } = require("ts-morph");
let code = "";
process.stdin.setEncoding("utf8");
process.stdin.on("data", chunk => { code += chunk; });
process.stdin.on("end", () => {
const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("temp.ts", code);
const functions = [];
sourceFile.getFunctions().forEach(fn => {
let name = fn.getName() || "<anonymous>";
let body = fn.getBodyText() || "";
let calls = [];
fn.forEachDescendant(n => {
if(n.getKindName() === "CallExpression") {
try {
let callee = n.getExpression().getText();
calls.push(callee);
} catch(e) {}
}
});
functions.push({name: name, code: body, calls: Array.from(new Set(calls))});
});
console.log(JSON.stringify(functions));
});
"""
def extract_ts_function_info(code):
try:
result = subprocess.run([
"node", "-e", TS_PARSER_JS
], input=code, text=True, capture_output=True, check=True)
return json.loads(result.stdout)
except (subprocess.CalledProcessError, json.JSONDecodeError) as e:
print("[DEBUG] TypeScript AST parsing error:", e)
return []