1
+ /* eslint-disable no-case-declarations, regexp/no-contradiction-with-assertion */
1
2
import type { Declaration } from './types'
2
3
import * as ts from 'typescript'
3
4
@@ -80,12 +81,12 @@ export function extractDeclarations(sourceCode: string, filePath: string, keepCo
80
81
}
81
82
82
83
visitTopLevel ( sourceFile )
83
-
84
+
84
85
// Second pass: Find referenced types that aren't imported or declared
85
86
const referencedTypes = findReferencedTypes ( declarations , sourceCode )
86
87
const additionalDeclarations = extractReferencedTypeDeclarations ( sourceFile , referencedTypes , sourceCode )
87
88
declarations . push ( ...additionalDeclarations )
88
-
89
+
89
90
return declarations
90
91
}
91
92
@@ -1016,49 +1017,51 @@ function getNodeText(node: ts.Node, sourceCode: string): string {
1016
1017
*/
1017
1018
function extractJSDocComments ( node : ts . Node , sourceFile : ts . SourceFile ) : string [ ] {
1018
1019
const comments : string [ ] = [ ]
1019
-
1020
+
1020
1021
// Get leading trivia (comments before the node)
1021
1022
const fullStart = node . getFullStart ( )
1022
1023
const start = node . getStart ( sourceFile )
1023
-
1024
+
1024
1025
if ( fullStart !== start ) {
1025
1026
const triviaText = sourceFile . text . substring ( fullStart , start )
1026
-
1027
+
1027
1028
// Extract JSDoc comments (/** ... */) and single-line comments (// ...)
1028
1029
const jsDocMatches = triviaText . match ( / \/ \* \* [ \s \S ] * ?\* \/ / g)
1029
1030
if ( jsDocMatches ) {
1030
1031
comments . push ( ...jsDocMatches )
1031
1032
}
1032
-
1033
+
1033
1034
// Also capture regular block comments (/* ... */) that might be documentation
1034
1035
const blockCommentMatches = triviaText . match ( / \/ \* (? ! \* ) [ \s \S ] * ?\* \/ / g)
1035
1036
if ( blockCommentMatches ) {
1036
1037
comments . push ( ...blockCommentMatches )
1037
1038
}
1038
-
1039
+
1039
1040
// Capture single-line comments that appear right before the declaration
1040
1041
const lines = triviaText . split ( '\n' )
1041
1042
const commentLines : string [ ] = [ ]
1042
-
1043
+
1043
1044
// Look for consecutive comment lines at the end of the trivia
1044
1045
for ( let i = lines . length - 1 ; i >= 0 ; i -- ) {
1045
1046
const line = lines [ i ] . trim ( )
1046
1047
if ( line . startsWith ( '//' ) ) {
1047
1048
commentLines . unshift ( line )
1048
- } else if ( line === '' ) {
1049
+ }
1050
+ else if ( line === '' ) {
1049
1051
// Empty line is okay, continue
1050
1052
continue
1051
- } else {
1053
+ }
1054
+ else {
1052
1055
// Non-comment, non-empty line - stop
1053
1056
break
1054
1057
}
1055
1058
}
1056
-
1059
+
1057
1060
if ( commentLines . length > 0 ) {
1058
1061
comments . push ( commentLines . join ( '\n' ) )
1059
1062
}
1060
1063
}
1061
-
1064
+
1062
1065
return comments
1063
1066
}
1064
1067
@@ -1119,7 +1122,7 @@ function hasAsyncModifier(node: ts.FunctionDeclaration): boolean {
1119
1122
/**
1120
1123
* Check if a non-exported function should be included (e.g., if it's referenced by exported items)
1121
1124
*/
1122
- function shouldIncludeNonExportedFunction ( functionName : string , sourceCode : string ) : boolean {
1125
+ function shouldIncludeNonExportedFunction ( _functionName ? : string , _sourceCode ? : string ) : boolean {
1123
1126
// For now, don't include non-exported functions
1124
1127
// In the future, we could analyze if they're referenced by exported functions
1125
1128
return false
@@ -1139,7 +1142,7 @@ function shouldIncludeNonExportedInterface(interfaceName: string, sourceCode: st
1139
1142
/**
1140
1143
* Find types that are referenced in declarations but not imported or declared
1141
1144
*/
1142
- function findReferencedTypes ( declarations : Declaration [ ] , sourceCode : string ) : Set < string > {
1145
+ function findReferencedTypes ( declarations : Declaration [ ] , _sourceCode : string ) : Set < string > {
1143
1146
const referencedTypes = new Set < string > ( )
1144
1147
const importedTypes = new Set < string > ( )
1145
1148
const declaredTypes = new Set < string > ( )
@@ -1260,43 +1263,43 @@ function extractReferencedTypeDeclarations(sourceFile: ts.SourceFile, referenced
1260
1263
*/
1261
1264
function extractTypesFromModuleText ( moduleText : string ) : string [ ] {
1262
1265
const types : string [ ] = [ ]
1263
-
1266
+
1264
1267
// Look for interface declarations
1265
1268
const interfaceMatches = moduleText . match ( / (?: e x p o r t \s + ) ? i n t e r f a c e \s + ( [ A - Z ] [ a - z A - Z 0 - 9 ] * ) / g)
1266
1269
if ( interfaceMatches ) {
1267
- interfaceMatches . forEach ( match => {
1270
+ interfaceMatches . forEach ( ( match ) => {
1268
1271
const name = match . replace ( / (?: e x p o r t \s + ) ? i n t e r f a c e \s + / , '' )
1269
1272
types . push ( name )
1270
1273
} )
1271
1274
}
1272
-
1275
+
1273
1276
// Look for type alias declarations
1274
1277
const typeMatches = moduleText . match ( / (?: e x p o r t \s + ) ? t y p e \s + ( [ A - Z ] [ a - z A - Z 0 - 9 ] * ) / g)
1275
1278
if ( typeMatches ) {
1276
- typeMatches . forEach ( match => {
1279
+ typeMatches . forEach ( ( match ) => {
1277
1280
const name = match . replace ( / (?: e x p o r t \s + ) ? t y p e \s + / , '' )
1278
1281
types . push ( name )
1279
1282
} )
1280
1283
}
1281
-
1284
+
1282
1285
// Look for class declarations
1283
1286
const classMatches = moduleText . match ( / (?: e x p o r t \s + ) ? (?: d e c l a r e \s + ) ? c l a s s \s + ( [ A - Z ] [ a - z A - Z 0 - 9 ] * ) / g)
1284
1287
if ( classMatches ) {
1285
- classMatches . forEach ( match => {
1288
+ classMatches . forEach ( ( match ) => {
1286
1289
const name = match . replace ( / (?: e x p o r t \s + ) ? (?: d e c l a r e \s + ) ? c l a s s \s + / , '' )
1287
1290
types . push ( name )
1288
1291
} )
1289
1292
}
1290
-
1293
+
1291
1294
// Look for enum declarations
1292
1295
const enumMatches = moduleText . match ( / (?: e x p o r t \s + ) ? (?: d e c l a r e \s + ) ? (?: c o n s t \s + ) ? e n u m \s + ( [ A - Z ] [ a - z A - Z 0 - 9 ] * ) / g)
1293
1296
if ( enumMatches ) {
1294
- enumMatches . forEach ( match => {
1297
+ enumMatches . forEach ( ( match ) => {
1295
1298
const name = match . replace ( / (?: e x p o r t \s + ) ? (?: d e c l a r e \s + ) ? (?: c o n s t \s + ) ? e n u m \s + / , '' )
1296
1299
types . push ( name )
1297
1300
} )
1298
1301
}
1299
-
1302
+
1300
1303
return types
1301
1304
}
1302
1305
@@ -1305,13 +1308,66 @@ function extractTypesFromModuleText(moduleText: string): string[] {
1305
1308
*/
1306
1309
function isBuiltInType ( typeName : string ) : boolean {
1307
1310
const builtInTypes = new Set ( [
1308
- 'string' , 'number' , 'boolean' , 'object' , 'any' , 'unknown' , 'never' , 'void' ,
1309
- 'undefined' , 'null' , 'Array' , 'Promise' , 'Record' , 'Partial' , 'Required' ,
1310
- 'Pick' , 'Omit' , 'Exclude' , 'Extract' , 'NonNullable' , 'ReturnType' ,
1311
- 'Parameters' , 'ConstructorParameters' , 'InstanceType' , 'ThisType' ,
1312
- 'Function' , 'Date' , 'RegExp' , 'Error' , 'Map' , 'Set' , 'WeakMap' , 'WeakSet' ,
1311
+ 'string' ,
1312
+ 'number' ,
1313
+ 'boolean' ,
1314
+ 'object' ,
1315
+ 'any' ,
1316
+ 'unknown' ,
1317
+ 'never' ,
1318
+ 'void' ,
1319
+ 'undefined' ,
1320
+ 'null' ,
1321
+ 'Array' ,
1322
+ 'Promise' ,
1323
+ 'Record' ,
1324
+ 'Partial' ,
1325
+ 'Required' ,
1326
+ 'Pick' ,
1327
+ 'Omit' ,
1328
+ 'Exclude' ,
1329
+ 'Extract' ,
1330
+ 'NonNullable' ,
1331
+ 'ReturnType' ,
1332
+ 'Parameters' ,
1333
+ 'ConstructorParameters' ,
1334
+ 'InstanceType' ,
1335
+ 'ThisType' ,
1336
+ 'Function' ,
1337
+ 'Date' ,
1338
+ 'RegExp' ,
1339
+ 'Error' ,
1340
+ 'Map' ,
1341
+ 'Set' ,
1342
+ 'WeakMap' ,
1343
+ 'WeakSet' ,
1313
1344
// Common generic type parameters
1314
- 'T' , 'K' , 'V' , 'U' , 'R' , 'P' , 'E' , 'A' , 'B' , 'C' , 'D' , 'F' , 'G' , 'H' , 'I' , 'J' , 'L' , 'M' , 'N' , 'O' , 'Q' , 'S' , 'W' , 'X' , 'Y' , 'Z'
1345
+ 'T' ,
1346
+ 'K' ,
1347
+ 'V' ,
1348
+ 'U' ,
1349
+ 'R' ,
1350
+ 'P' ,
1351
+ 'E' ,
1352
+ 'A' ,
1353
+ 'B' ,
1354
+ 'C' ,
1355
+ 'D' ,
1356
+ 'F' ,
1357
+ 'G' ,
1358
+ 'H' ,
1359
+ 'I' ,
1360
+ 'J' ,
1361
+ 'L' ,
1362
+ 'M' ,
1363
+ 'N' ,
1364
+ 'O' ,
1365
+ 'Q' ,
1366
+ 'S' ,
1367
+ 'W' ,
1368
+ 'X' ,
1369
+ 'Y' ,
1370
+ 'Z' ,
1315
1371
] )
1316
1372
return builtInTypes . has ( typeName )
1317
1373
}
0 commit comments