@@ -45,67 +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 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 ) ) ;
104
49
const chunkedFile : ChunkedFile = {
105
50
fileName,
106
51
chunks,
107
52
} ;
108
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
+ }
109
93
}
110
94
111
95
return results ;
0 commit comments