-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts-to-ast.js
125 lines (115 loc) · 4.26 KB
/
ts-to-ast.js
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
const ts = require("typescript");
function parseFile(filePath, search_value) {
let i = 0;
const fileContents = ts.sys.readFile(filePath);
if (!fileContents) throw new Error(`Failed to read file: ${filePath}`);
const sourceFile = ts.createSourceFile(
filePath,
fileContents,
ts.ScriptTarget.Latest,
true
);
function serializeAST(node, indentLevel = 0) {
let serializedAst = "";
function appendOutput(output) {
serializedAst += output + "\n";
}
(function serialize(node, indentLevel = 0) {
appendOutput(
`${" ".repeat(indentLevel * 2)}${ts.SyntaxKind[node.kind]}: ${node
.getText()
.trim()}`
);
node.forEachChild((child) => serialize(child, indentLevel + 1));
})(node);
return serializedAst;
}
function processNode(node) {
// console.log(node && node.code)
// console.log(Object.keys(node))
// console.log(node.getText())
// const found = text.startsWith(search_value) && text
// if(found){
// console.log(found)
// }
// const argument = node && node.arguments && node.arguments.map(a=>a.name && a.name.escapedText && console.log(a.name.escapedText))
// console.log()
// if (node && node.name && node.name.escapedText == search_value) {
const text = node.getText(sourceFile)
const found = text.startsWith(search_value) && text
if (found) {
const snippet = node.getText(sourceFile);
const astSegment = serializeAST(node);
console.log(
JSON.stringify({
ast: astSegment.trim(),
// astJSON: JSON.stringify(node),
code: snippet.trim(),
path: filePath
// routePath: const routePath = firstArg.text;,
})
);
}
// i++
// if (search_value && node.kind === ts.SyntaxKind.CallExpression) {
// const call = node;
// // Ensure the callee is an identifier named 'describe' or 'it'
// if (call.arguments && call.arguments.length > 0) {
// const firstArg = call.arguments[0];
// // Check if the first argument is a string literal
// if (firstArg.kind === ts.SyntaxKind.StringLiteral) {
// const routePath = firstArg.text; // Get the text of the string literal
// // Check if the route path matches '/session/documents/:id'
// if (routePath === search_value) {
// // Here, we've found the specific route
// const snippet = call.getText(sourceFile);
// const astSegment = serializeAST(call);
// console.log(
// JSON.stringify({ ast: astSegment.trim(), code: snippet.trim(), routePath })
// );
// }
// }
// }
// if (
// call.expression &&
// (call.expression.kind === ts.SyntaxKind.Identifier)
// ) {
// const calleeName = call.expression.escapedText;
// if (calleeName === "describe" || calleeName === "it" ) {
// const snippet = call.getText(sourceFile);
// const astSegment = serializeAST(call);
// // console.log(
// // JSON.stringify({ ast: astSegment.trim(), code: snippet.trim() })
// // );
// }
// }
// } else {
// // Define the node kinds you're interested in, e.g., FunctionDeclaration
// const significantNodeKinds = [
// ts.SyntaxKind.FunctionDeclaration,
// ts.SyntaxKind.ClassDeclaration,
// ts.SyntaxKind.InterfaceDeclaration,
// ts.SyntaxKind.EnumDeclaration,
// ts.SyntaxKind.TypeAliasDeclaration,
// ts.SyntaxKind.VariableStatement,
// ts.SyntaxKind.ModuleDeclaration,
// ts.SyntaxKind.ExportAssignment,
// ts.SyntaxKind.ExportDeclaration,
// ts.SyntaxKind.ImportDeclaration,
// ];
// if (significantNodeKinds.includes(node.kind)) {
// const snippet = node.getText();
// const astSegment = serializeAST(node);
// // console.log(
// // JSON.stringify({ ast: astSegment.trim(), code: snippet.trim() })
// // );
// }
// }
ts.forEachChild(node, processNode);
}
processNode(sourceFile);
}
// Example usage:
const path = process.argv[2];
const search_value = process.argv[3];
parseFile(path, search_value);