All files diagnostics.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 8/8
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                    239x     21x       15x       44x       37x       2x 2x 2x   2x 2x 2x 1x        
export interface Diagnostic {
    file: string;
    line: number;
    message: string;
    severity: 'error' | 'warning';
    /** Optional stable identifier — consumed by editor tooling to dispatch quick-fixes. */
    code?: string;
}
 
export class DiagnosticCollector {
    private diagnostics: Diagnostic[] = [];
 
    error(file: string, line: number, message: string, code?: string): void {
        this.diagnostics.push({ file, line, message, severity: 'error', code });
    }
 
    warn(file: string, line: number, message: string, code?: string): void {
        this.diagnostics.push({ file, line, message, severity: 'warning', code });
    }
 
    hasErrors(): boolean {
        return this.diagnostics.some(d => d.severity === 'error');
    }
 
    getAll(): Diagnostic[] {
        return [...this.diagnostics];
    }
 
    report(): void {
        for (const d of this.diagnostics) {
            const prefix = d.severity === 'error' ? '\x1b[31mERROR\x1b[0m' : '\x1b[33mWARN\x1b[0m';
            console.error(`${prefix}  ${d.file}:${d.line}  ${d.message}`);
        }
        const errors = this.diagnostics.filter(d => d.severity === 'error').length;
        const warns = this.diagnostics.filter(d => d.severity === 'warning').length;
        if (errors > 0 || warns > 0) {
            console.error(`\n${errors} error(s), ${warns} warning(s)`);
        }
    }
}