Skip to content

Commit 0b8012a

Browse files
committed
Support functype/class/interface/type, also nested
1 parent 0184d32 commit 0b8012a

File tree

1 file changed

+40
-56
lines changed

1 file changed

+40
-56
lines changed

ts/packages/agents/spelunker/src/typescriptChunker.ts

+40-56
Original file line numberDiff line numberDiff line change
@@ -45,67 +45,51 @@ export async function chunkifyTypeScriptFiles(
4545
};
4646
const chunks: Chunk[] = [rootChunk];
4747
const sourceFile: ts.SourceFile = await tsCode.loadSourceFile(fileName);
48-
49-
// TODO: Also do nested functions, and classes, and interfaces, and modules.
50-
// TODO: For nested things, remove their text from the parent.
51-
function getChunkableStatements(): (
52-
| ts.FunctionDeclaration
53-
| ts.ClassDeclaration
54-
| ts.InterfaceDeclaration
55-
| ts.TypeAliasDeclaration
56-
)[] {
57-
return tsCode.getStatements(
58-
sourceFile,
59-
(s) =>
60-
ts.isFunctionDeclaration(s) ||
61-
ts.isClassDeclaration(s) ||
62-
ts.isInterfaceDeclaration(s) ||
63-
ts.isTypeAliasDeclaration(s),
64-
);
65-
}
66-
67-
const things = getChunkableStatements();
68-
for (const thing of things) {
69-
const treeName = ts.SyntaxKind[thing.kind];
70-
const codeName = tsCode.getStatementName(thing) ?? "";
71-
// console.log(` ${treeName}: ${codeName}`);
72-
try {
73-
// console.log(
74-
// "--------------------------------------------------------",
75-
// );
76-
// console.log(`Name: ${thing.name?.escapedText}`);
77-
// console.log(
78-
// `Parameters: ${thing.parameters.map((p) => p.name?.getFullText(sourceFile))}`,
79-
// );
80-
// console.log(`Return type: ${thing.type?.getText(sourceFile)}`);
81-
82-
const chunk: Chunk = {
83-
chunkId: generate_id(),
84-
treeName,
85-
codeName,
86-
blobs: makeBlobs(
87-
sourceFile,
88-
thing.getFullStart(),
89-
thing.getEnd(),
90-
),
91-
parentId: rootChunk.chunkId,
92-
children: [],
93-
fileName,
94-
};
95-
chunks.push(chunk);
96-
} catch (e: any) {
97-
results.push({
98-
error: `${thing.name?.escapedText}: ${e.message}`,
99-
filename: fileName,
100-
});
101-
}
102-
}
103-
// console.log("========================================================");
48+
chunks.push(...recursivelyChunkify(sourceFile, rootChunk));
10449
const chunkedFile: ChunkedFile = {
10550
fileName,
10651
chunks,
10752
};
10853
results.push(chunkedFile);
54+
55+
function recursivelyChunkify(
56+
parentNode: ts.Node,
57+
parentChunk: Chunk,
58+
): Chunk[] {
59+
const chunks: Chunk[] = [];
60+
for (const childNode of parentNode.getChildren(sourceFile)) {
61+
if (
62+
ts.isInterfaceDeclaration(childNode) ||
63+
ts.isTypeAliasDeclaration(childNode) ||
64+
ts.isFunctionDeclaration(childNode) ||
65+
ts.isClassDeclaration(childNode)
66+
) {
67+
// console.log(
68+
// ts.SyntaxKind[childNode.kind],
69+
// tsCode.getStatementName(childNode),
70+
// );
71+
const chunk: Chunk = {
72+
chunkId: generate_id(),
73+
treeName: ts.SyntaxKind[childNode.kind],
74+
codeName: tsCode.getStatementName(childNode) ?? "",
75+
blobs: makeBlobs(
76+
sourceFile,
77+
childNode.getFullStart(),
78+
childNode.getEnd(),
79+
),
80+
parentId: parentChunk.chunkId,
81+
children: [],
82+
fileName,
83+
};
84+
// TODO: Remove chunk.blobs from parentChunk.blobs.
85+
chunks.push(chunk);
86+
recursivelyChunkify(childNode, chunk);
87+
} else {
88+
recursivelyChunkify(childNode, parentChunk);
89+
}
90+
}
91+
return chunks;
92+
}
10993
}
11094

11195
return results;

0 commit comments

Comments
 (0)