Skip to content

Commit b946f16

Browse files
committed
Merge branch 'main' into umeshma/dev
2 parents 22e875b + 79814e7 commit b946f16

File tree

2 files changed

+46
-50
lines changed

2 files changed

+46
-50
lines changed

Diff for: ts/packages/agents/spelunker/src/typescriptChunker.ts

+40-50
Original file line numberDiff line numberDiff line change
@@ -45,61 +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 getFunctionsAndClasses(): (
52-
| ts.FunctionDeclaration
53-
| ts.ClassDeclaration
54-
)[] {
55-
return tsCode.getStatements(
56-
sourceFile,
57-
(s) => ts.isFunctionDeclaration(s) || ts.isClassDeclaration(s),
58-
);
59-
}
60-
61-
const things = getFunctionsAndClasses();
62-
for (const thing of things) {
63-
const treeName = ts.SyntaxKind[thing.kind];
64-
const codeName = tsCode.getStatementName(thing) ?? "";
65-
// console.log(` ${treeName}: ${codeName}`);
66-
try {
67-
// console.log(
68-
// "--------------------------------------------------------",
69-
// );
70-
// console.log(`Name: ${thing.name?.escapedText}`);
71-
// console.log(
72-
// `Parameters: ${thing.parameters.map((p) => p.name?.getFullText(sourceFile))}`,
73-
// );
74-
// console.log(`Return type: ${thing.type?.getText(sourceFile)}`);
75-
76-
const chunk: Chunk = {
77-
chunkId: generate_id(),
78-
treeName,
79-
codeName,
80-
blobs: makeBlobs(
81-
sourceFile,
82-
thing.getFullStart(),
83-
thing.getEnd(),
84-
),
85-
parentId: rootChunk.chunkId,
86-
children: [],
87-
fileName,
88-
};
89-
chunks.push(chunk);
90-
} catch (e: any) {
91-
results.push({
92-
error: `${thing.name?.escapedText}: ${e.message}`,
93-
filename: fileName,
94-
});
95-
}
96-
}
97-
// console.log("========================================================");
48+
chunks.push(...recursivelyChunkify(sourceFile, rootChunk));
9849
const chunkedFile: ChunkedFile = {
9950
fileName,
10051
chunks,
10152
};
10253
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+
}
10393
}
10494

10595
return results;

Diff for: ts/packages/codeProcessor/src/tsCode.ts

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ export function getStatementName(statement: ts.Statement): string | undefined {
8787
) {
8888
return statement.name?.text;
8989
}
90+
if (
91+
ts.isInterfaceDeclaration(statement) ||
92+
ts.isTypeAliasDeclaration(statement)
93+
) {
94+
return statement.name?.escapedText.toString();
95+
}
9096
return undefined;
9197
}
9298

0 commit comments

Comments
 (0)