Skip to main content

perspt_coding/diag/
tsc.rs

1//! `tsc --pretty false` diagnostics (PSP-10 system 26).
2//!
3//! A versioned normalizer over the compiler's plain text form
4//! `path(line,col): error TSxxxx: message`. The PSP does not claim a JSON
5//! diagnostic protocol for `tsc`.
6
7use super::types::StructuredDiagnostic;
8use crate::lang::classify_ts_code;
9
10/// Parser identity; enters the sensor fingerprint.
11pub const PARSER_ID: &str = "tsc-text-v1";
12
13/// Parse `tsc --pretty false` error lines.
14pub fn parse(raw: &str) -> Vec<StructuredDiagnostic> {
15    raw.lines().filter_map(parse_line).collect()
16}
17
18fn parse_line(line: &str) -> Option<StructuredDiagnostic> {
19    // `src/index.ts(4,7): error TS2322: Type 'string' is not assignable...`
20    let marker = line.find(": error TS")?;
21    let (location, rest) = line.split_at(marker);
22    let rest = &rest[": error ".len()..];
23    let (code, message) = rest.split_once(": ")?;
24    let (path, position) = location.rsplit_once('(')?;
25    let position = position.strip_suffix(')')?;
26    let (line_no, column) = position.split_once(',')?;
27    Some(StructuredDiagnostic {
28        class: classify_ts_code(code),
29        code: Some(code.to_string()),
30        message: message.to_string(),
31        path: Some(path.to_string()),
32        line: line_no.trim().parse().ok(),
33        column: column.trim().parse().ok(),
34        primary: true,
35        suggestion: None,
36    })
37}