@@ -64,7 +64,6 @@ function getTsFilesRecursive(dirPath: string): string[] {
64
64
if ( entry . isDirectory ( ) ) {
65
65
// Ignore node_modules for performance and relevance
66
66
if ( entry . name === 'node_modules' ) {
67
- // log(`Skipping node_modules directory: ${fullPath}`);
68
67
continue ;
69
68
}
70
69
// Recursively scan subdirectories
@@ -78,11 +77,11 @@ function getTsFilesRecursive(dirPath: string): string[] {
78
77
}
79
78
} catch ( error : any ) {
80
79
console . error ( `Error reading directory ${ dirPath } : ${ error . message } ` ) ;
80
+ throw error ;
81
81
}
82
82
return tsFiles ;
83
83
}
84
84
85
-
86
85
/**
87
86
* Analyzes TypeScript source files to find calls to specific functions.
88
87
* @param filePaths An array of absolute paths to the TypeScript files to scan.
@@ -92,87 +91,79 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
92
91
const foundCalls : CallSiteInfo [ ] = [ ] ;
93
92
94
93
for ( const filePath of filePaths ) {
95
- try {
96
- // Read the file content
97
- const sourceText = fs . readFileSync ( filePath , "utf8" ) ;
98
-
99
- // Create the SourceFile AST node
100
- const sourceFile = ts . createSourceFile (
101
- path . basename ( filePath ) , // Use basename for AST node name
102
- sourceText ,
103
- ts . ScriptTarget . ESNext , // Or your project's target
104
- true , // Set parent pointers
105
- ts . ScriptKind . Unknown // Detect TS vs TSX automatically
106
- ) ;
107
-
108
- // Define the visitor function
109
- const visit = ( node : ts . Node ) :void => {
110
- // Check if the node is a CallExpression (e.g., myFunction(...))
111
- if ( ts . isCallExpression ( node ) ) {
112
- let functionName : string | null = null ;
113
- const expression = node . expression ;
114
-
115
- // Check if the call is directly to an identifier (e.g., fail())
116
- if ( ts . isIdentifier ( expression ) ) {
117
- functionName = expression . text ;
118
- }
94
+ // Read the file content
95
+ const sourceText = fs . readFileSync ( filePath , 'utf8' ) ;
96
+
97
+ // Create the SourceFile AST node
98
+ const sourceFile = ts . createSourceFile (
99
+ path . basename ( filePath ) , // Use basename for AST node name
100
+ sourceText ,
101
+ ts . ScriptTarget . ESNext ,
102
+ true , // Set parent pointers
103
+ ts . ScriptKind . Unknown // Detect TS vs TSX automatically
104
+ ) ;
105
+
106
+ // Define the visitor function
107
+ const visit = ( node : ts . Node ) : void => {
108
+ // Check if the node is a CallExpression (e.g., myFunction(...))
109
+ if ( ts . isCallExpression ( node ) ) {
110
+ let functionName : string | null = null ;
111
+ const expression = node . expression ;
119
112
120
- // If we found a function name, and it's one we're looking for
121
- if ( functionName && targetFunctionNames . has ( functionName ) ) {
122
- // Get line and character number
123
- const { line, character } = ts . getLineAndCharacterOfPosition (
124
- sourceFile ,
125
- node . getStart ( ) // Get start position of the call expression
126
- ) ;
127
-
128
- // --- Extract Arguments ---
129
- const argsText : string [ ] = [ ] ;
130
- let errorMessage : string | undefined ;
131
- let assertionId : string | undefined ;
132
- if ( node . arguments && node . arguments . length > 0 ) {
133
- node . arguments . forEach ( ( arg : ts . Expression ) => {
134
- // Get the source text of the argument node
135
- argsText . push ( arg . getText ( sourceFile ) ) ;
136
-
137
- if ( ts . isStringLiteral ( arg ) ) {
138
- errorMessage = arg . getText ( sourceFile ) ;
139
- }
140
- else if ( ts . isNumericLiteral ( arg ) ) {
141
- assertionId = arg . getText ( sourceFile ) ;
142
- }
143
- } ) ;
144
- }
145
- // --- End Extract Arguments ---
146
-
147
- // Store the information (add 1 to line/char for 1-based indexing)
148
- foundCalls . push ( {
149
- fileName : filePath , // Store the full path
150
- functionName,
151
- line : line + 1 ,
152
- character : character + 1 ,
153
- argumentsText : argsText , // Store the extracted arguments,
154
- errorMessage,
155
- assertionId : assertionId ?? "INVALID" ,
113
+ // Check if the call is directly to an identifier (e.g., fail())
114
+ if ( ts . isIdentifier ( expression ) ) {
115
+ functionName = expression . text ;
116
+ }
117
+
118
+ // If we found a function name, and it's one we're looking for
119
+ if ( functionName && targetFunctionNames . has ( functionName ) ) {
120
+ // Get line and character number
121
+ const { line, character } = ts . getLineAndCharacterOfPosition (
122
+ sourceFile ,
123
+ node . getStart ( ) // Get start position of the call expression
124
+ ) ;
125
+
126
+ // --- Extract Arguments ---
127
+ const argsText : string [ ] = [ ] ;
128
+ let errorMessage : string | undefined ;
129
+ let assertionId : string | undefined ;
130
+ if ( node . arguments && node . arguments . length > 0 ) {
131
+ node . arguments . forEach ( ( arg : ts . Expression ) => {
132
+ // Get the source text of the argument node
133
+ argsText . push ( arg . getText ( sourceFile ) ) ;
134
+
135
+ if ( ts . isStringLiteral ( arg ) ) {
136
+ errorMessage = arg . getText ( sourceFile ) ;
137
+ } else if ( ts . isNumericLiteral ( arg ) ) {
138
+ assertionId = arg . getText ( sourceFile ) ;
139
+ }
156
140
} ) ;
157
141
}
158
- }
159
142
160
- // Continue traversing down the AST
161
- ts . forEachChild ( node , visit ) ;
162
- } ;
143
+ // Store the information (add 1 to line/char for 1-based indexing)
144
+ foundCalls . push ( {
145
+ fileName : filePath , // Store the full path
146
+ functionName,
147
+ line : line + 1 ,
148
+ character : character + 1 ,
149
+ argumentsText : argsText , // Store the extracted arguments,
150
+ errorMessage,
151
+ assertionId : assertionId ?? 'INVALID'
152
+ } ) ;
153
+ }
154
+ }
163
155
164
- // Start traversal from the root SourceFile node
165
- visit ( sourceFile ) ;
156
+ // Continue traversing down the AST
157
+ ts . forEachChild ( node , visit ) ;
158
+ } ;
166
159
167
- } catch ( error : any ) {
168
- console . error ( `Error processing file ${ filePath } : ${ error . message } ` ) ;
169
- }
160
+ // Start traversal from the root SourceFile node
161
+ visit ( sourceFile ) ;
170
162
} // End loop through filePaths
171
163
172
164
return foundCalls ;
173
165
}
174
166
175
-
176
167
// --- Action Handlers ---
177
168
178
169
function handleList ( occurrences : CallSiteInfo [ ] ) : void {
@@ -362,11 +353,4 @@ async function main(): Promise<void> {
362
353
}
363
354
364
355
// Run the main function
365
- main ( ) . catch ( error => {
366
- console . error ( "\nAn unexpected error occurred:" ) ;
367
- console . error ( error ) ;
368
- process . exit ( 1 ) ;
369
- } ) ;
370
-
371
-
372
-
356
+ main ( ) ;
0 commit comments