Skip to content

Commit 4374a98

Browse files
committed
Address PR feedback
1 parent 7416fa2 commit 4374a98

File tree

1 file changed

+64
-80
lines changed

1 file changed

+64
-80
lines changed

packages/firestore/scripts/assertion-id-tool.ts

Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ function getTsFilesRecursive(dirPath: string): string[] {
6464
if (entry.isDirectory()) {
6565
// Ignore node_modules for performance and relevance
6666
if (entry.name === 'node_modules') {
67-
// log(`Skipping node_modules directory: ${fullPath}`);
6867
continue;
6968
}
7069
// Recursively scan subdirectories
@@ -78,11 +77,11 @@ function getTsFilesRecursive(dirPath: string): string[] {
7877
}
7978
} catch (error: any) {
8079
console.error(`Error reading directory ${dirPath}: ${error.message}`);
80+
throw error;
8181
}
8282
return tsFiles;
8383
}
8484

85-
8685
/**
8786
* Analyzes TypeScript source files to find calls to specific functions.
8887
* @param filePaths An array of absolute paths to the TypeScript files to scan.
@@ -92,87 +91,79 @@ function findFunctionCalls(filePaths: string[]): CallSiteInfo[] {
9291
const foundCalls: CallSiteInfo[] = [];
9392

9493
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;
119112

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+
}
156140
});
157141
}
158-
}
159142

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+
}
163155

164-
// Start traversal from the root SourceFile node
165-
visit(sourceFile);
156+
// Continue traversing down the AST
157+
ts.forEachChild(node, visit);
158+
};
166159

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);
170162
} // End loop through filePaths
171163

172164
return foundCalls;
173165
}
174166

175-
176167
// --- Action Handlers ---
177168

178169
function handleList(occurrences: CallSiteInfo[]): void {
@@ -362,11 +353,4 @@ async function main(): Promise<void> {
362353
}
363354

364355
// 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

Comments
 (0)