Skip to content

Commit 40bfb94

Browse files
committed
fix: improve type safety in report handling functions
1 parent f4cb6e3 commit 40bfb94

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to the "magento-log-viewer" extension will be documented in
1010
- fix: Caching for JSON reports: Avoids redundant reading and parsing of JSON files.
1111
- fix: Optimized line counting with caching: Reduces time spent counting lines in large files and avoids repeated calculations for unchanged files.
1212
- fix: Improved badge updates with throttling and debouncing: Prevents too frequent updates and implements more efficient counting methods.
13+
- fix: improve type safety in report handling functions
1314
- i18n: translations added
1415

1516
---

src/helpers.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,10 @@ export function getLogItems(dir: string, parseTitle: (filePath: string) => strin
411411
}
412412

413413
// Cache for JSON reports to avoid repeated parsing
414-
const reportCache = new Map<string, { content: any, timestamp: number }>();
414+
const reportCache = new Map<string, { content: unknown, timestamp: number }>();
415415

416416
// Helper function for reading and parsing JSON reports with caching
417-
function getReportContent(filePath: string): any | null {
417+
function getReportContent(filePath: string): unknown | null {
418418
try {
419419
const stats = fs.statSync(filePath);
420420
const cachedReport = reportCache.get(filePath);
@@ -447,10 +447,18 @@ export function parseReportTitle(filePath: string): string {
447447
if (filePath.includes('/api/')) {
448448
const folderName = path.basename(path.dirname(filePath));
449449
const capitalizedFolderName = folderName.charAt(0).toUpperCase() + folderName.slice(1);
450-
return `${capitalizedFolderName}: ${report}`;
450+
return `${capitalizedFolderName}: ${String(report)}`;
451451
}
452452

453-
return report['0'] || path.basename(filePath);
453+
// Type guard to check if report is a record type with string keys
454+
if (report && typeof report === 'object' && report !== null) {
455+
const reportObj = report as Record<string, unknown>;
456+
if ('0' in reportObj && typeof reportObj['0'] === 'string') {
457+
return reportObj['0'] || path.basename(filePath);
458+
}
459+
}
460+
461+
return path.basename(filePath);
454462
} catch (error) {
455463
return path.basename(filePath);
456464
}
@@ -467,8 +475,13 @@ export function getIconForReport(filePath: string): vscode.ThemeIcon {
467475
return new vscode.ThemeIcon('warning');
468476
}
469477

470-
if (report['0'] && report['0'].toLowerCase().includes('error')) {
471-
return new vscode.ThemeIcon('error');
478+
// Type guard to check if report is a record type with string keys
479+
if (report && typeof report === 'object' && report !== null) {
480+
const reportObj = report as Record<string, unknown>;
481+
if ('0' in reportObj && typeof reportObj['0'] === 'string' &&
482+
reportObj['0'].toLowerCase().includes('error')) {
483+
return new vscode.ThemeIcon('error');
484+
}
472485
}
473486

474487
return new vscode.ThemeIcon('file');

0 commit comments

Comments
 (0)