@@ -45,61 +45,51 @@ export async function chunkifyTypeScriptFiles(
45
45
} ;
46
46
const chunks : Chunk [ ] = [ rootChunk ] ;
47
47
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 ) ) ;
98
49
const chunkedFile : ChunkedFile = {
99
50
fileName,
100
51
chunks,
101
52
} ;
102
53
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
+ }
103
93
}
104
94
105
95
return results ;
0 commit comments