pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
Documentation
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// SATD extraction: constructors, comment scanning, and context hashing.
//
// The comment scanner is the fix for #944: a comment was only recognised when
// it STARTED the line, so `pub fn a() -> i32 { 1 } // TODO: trailing one` was
// invisible and a 407-line file with 43 markers reported 3. It is also half the
// fix for #925: the scanner knows which leaders a file's language actually has
// and where its string literals are, so pmat's own `--help` text (a `#`-prefixed
// line inside a Rust raw string) is no longer read as a Python comment.

impl Default for SATDDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl SATDDetector {
    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new() -> Self {
        Self::with_classifier(DebtClassifier::new())
    }

    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// New strict.
    pub fn new_strict() -> Self {
        Self::with_classifier(DebtClassifier::new_strict())
    }

    /// Extended mode: detects euphemisms like placeholder, stub, "for now"
    /// See issue #149
    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub fn new_extended() -> Self {
        Self::with_classifier(DebtClassifier::new_extended())
    }

    fn with_classifier(debt_classifier: DebtClassifier) -> Self {
        Self { debt_classifier }
    }

    /// Extract technical debt from source code content
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
    pub fn extract_from_content(
        &self,
        content: &str,
        file_path: &Path,
    ) -> Result<Vec<TechnicalDebt>, TemplateError> {
        let mut debts = Vec::new();

        // Analyzer-owned, generated and vendored files are out of scope
        // wholesale; the per-line heuristics that used to guess at this are
        // gone (#925).
        if self.should_exclude_file(file_path) {
            return Ok(debts);
        }

        let mut test_tracker = TestBlockTracker::new(self.is_rust_file(file_path));
        let mut scanner = CommentScanner::for_path(file_path);

        for (line_num, line) in content.lines().enumerate() {
            if line.len() > MAX_LINE_LEN {
                return Err(TemplateError::ValidationError {
                    parameter: "line".to_string(),
                    reason: "Line too long for comment extraction (>10000 chars)".to_string(),
                });
            }

            test_tracker.update_from_line(line.trim());
            let comment = scanner.scan_line(line);

            if test_tracker.is_in_test_block() {
                continue;
            }
            if let Some(debt) = self.debt_of(comment, file_path, line_num as u32 + 1) {
                debts.push(debt);
            }
        }

        self.sort_debts(&mut debts);
        Ok(debts)
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
    pub(crate) fn is_rust_file(&self, file_path: &Path) -> bool {
        file_path.extension().and_then(|s| s.to_str()) == Some("rs")
    }

    fn sort_debts(&self, debts: &mut [TechnicalDebt]) {
        debts.sort_by_key(|d| (d.file.clone(), d.line, d.column));
    }

    /// The debt admitted by one scanned comment, if any.
    fn debt_of(
        &self,
        comment: Option<CommentSpan>,
        file_path: &Path,
        line_num: u32,
    ) -> Option<TechnicalDebt> {
        let comment = comment?;
        let (category, severity) = self.debt_classifier.classify_comment(&comment.text)?;

        // Basic context (could be enhanced with actual AST analysis)
        let context = AstContext {
            node_type: AstNodeType::Regular,
            parent_function: "unknown".to_string(),
            complexity: 1,
            siblings_count: 0,
            nesting_depth: 0,
            surrounding_statements: vec![],
        };

        Some(TechnicalDebt {
            category,
            severity: self.debt_classifier.adjust_severity(severity, &context),
            context_hash: self.hash_context(file_path, line_num, &comment.text),
            text: comment.text,
            file: file_path.to_path_buf(),
            line: line_num,
            column: comment.column,
        })
    }

    /// Generate context hash for debt identity tracking
    fn hash_context(&self, file_path: &Path, line_num: u32, content: &str) -> [u8; 16] {
        let mut hasher = Hasher::new();

        // Hash structural elements for stability across refactorings
        hasher.update(file_path.to_string_lossy().as_bytes());
        hasher.update(&line_num.to_le_bytes());
        hasher.update(content.as_bytes());

        let hash = hasher.finalize();
        hash.as_bytes()[..16].try_into().expect("internal error")
    }
}

/// Longest line the scanner will look at. A longer line is minified or
/// generated, not commented source.
const MAX_LINE_LEN: usize = 10_000;

/// One comment found on one line: where it starts (1-based column) and its text
/// with the leader stripped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CommentSpan {
    pub(crate) column: u32,
    pub(crate) text: String,
}

/// Which comment leaders a file's language actually has.
///
/// #925: the extractor used to try `//`, `#`, `/*` and `<!--` on every file,
/// so a `#`-prefixed line inside a Rust raw string — pmat's own `--help` block
/// in `cli/commands/cli_struct.rs` — was read as a shell comment and reported
/// as `Medium / Design` debt. `#` in Rust is an attribute, never a comment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct CommentSyntax {
    /// `//` line comments and `/* … */` block comments.
    slash: bool,
    /// `#` line comments.
    hash: bool,
    /// `<!-- … -->` comments.
    html: bool,
    /// `'…'` delimits a string (shell/python) rather than a char literal.
    quote_is_string: bool,
}

impl CommentSyntax {
    /// The syntax of the language `path` is written in. An unknown extension
    /// gets the permissive union of the line-comment styles, since guessing
    /// wrong there can only be a missed comment or an unclassified one — never
    /// a wrong verdict, because classification still requires a marker.
    pub(crate) fn for_path(path: &Path) -> Self {
        let extension = path
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or_default()
            .to_ascii_lowercase();

        match extension.as_str() {
            // C-family and friends: `//`, `/* */`, char literals in single quotes.
            "rs" | "c" | "h" | "cc" | "cpp" | "cxx" | "hpp" | "hh" | "cs" | "java" | "js"
            | "jsx" | "ts" | "tsx" | "mjs" | "cjs" | "go" | "kt" | "kts" | "swift" | "scala"
            | "dart" | "proto" | "sol" | "zig" | "groovy" | "m" | "mm" | "css" | "scss"
            | "less" | "v" | "sv" => Self {
                slash: true,
                hash: false,
                html: false,
                quote_is_string: false,
            },
            // PHP takes both `//` and `#`.
            "php" => Self {
                slash: true,
                hash: true,
                html: false,
                quote_is_string: true,
            },
            // Hash-comment languages and configuration formats.
            "py" | "rb" | "sh" | "bash" | "zsh" | "fish" | "pl" | "pm" | "r" | "jl" | "nim"
            | "cr" | "ex" | "exs" | "yaml" | "yml" | "toml" | "ini" | "cfg" | "conf" | "tf"
            | "mk" | "cmake" | "ps1" | "awk" | "gradle" => Self {
                slash: false,
                hash: true,
                html: false,
                quote_is_string: true,
            },
            // Markup: only `<!-- -->` is a comment. A leading `#` is a heading.
            "html" | "htm" | "xml" | "xhtml" | "svg" | "md" | "markdown" | "vue" | "svelte" => {
                Self {
                    slash: false,
                    hash: false,
                    html: true,
                    quote_is_string: true,
                }
            }
            // Functional languages whose block/line comments are not modelled
            // here; `--` is not treated as a comment because `i--` is not one.
            "hs" | "elm" | "ml" | "clj" | "lua" | "sql" => Self {
                slash: false,
                hash: false,
                html: false,
                quote_is_string: true,
            },
            _ => Self {
                slash: true,
                hash: true,
                html: false,
                quote_is_string: true,
            },
        }
    }
}

/// Finds the comment on each line of ONE file, carrying the multi-line state
/// (block comments, raw strings) that a per-line regex cannot have.
pub(crate) struct CommentScanner {
    syntax: CommentSyntax,
    /// Inside a `/* … */` that opened on an earlier line.
    in_block_comment: bool,
    /// The block comment that is open is a doc comment (`/** … */`).
    block_is_doc: bool,
    /// Column the open block comment's `/*` sat at, on the line it opened.
    block_start: Option<usize>,
    /// Inside a raw string that opened on an earlier line; holds its closing
    /// delimiter (`"`, `"#`, `"##`, …). Test fixtures embedded in `.rs` files
    /// live here, and their `// TODO` lines are string data, not comments.
    raw_string_close: Option<String>,
}

impl CommentScanner {
    pub(crate) fn for_path(path: &Path) -> Self {
        Self::new(CommentSyntax::for_path(path))
    }

    pub(crate) fn new(syntax: CommentSyntax) -> Self {
        Self {
            syntax,
            in_block_comment: false,
            block_is_doc: false,
            block_start: None,
            raw_string_close: None,
        }
    }

    /// The first comment on `line`, at any column, or `None`.
    ///
    /// Doc comments (`///`, `//!`, `/** … */`) deliberately yield `None`: the
    /// long-standing policy is that documentation is not debt.
    pub(crate) fn scan_line(&mut self, line: &str) -> Option<CommentSpan> {
        let mut found: Option<CommentSpan> = None;
        let mut idx = 0usize;

        while idx < line.len() {
            if let Some(close) = self.raw_string_close.take() {
                match line[idx..].find(close.as_str()) {
                    Some(pos) => idx += pos + close.len(),
                    None => {
                        self.raw_string_close = Some(close);
                        break;
                    }
                }
                continue;
            }

            if self.in_block_comment {
                let (chunk, next) = match line[idx..].find("*/") {
                    Some(pos) => (&line[idx..idx + pos], Some(idx + pos + 2)),
                    None => (&line[idx..], None),
                };
                if !self.block_is_doc {
                    let column = self.block_start.unwrap_or(idx);
                    record(&mut found, column, block_comment_text(chunk));
                }
                match next {
                    Some(next) => {
                        self.in_block_comment = false;
                        self.block_is_doc = false;
                        self.block_start = None;
                        idx = next;
                        continue;
                    }
                    None => {
                        // Continuation lines start at their own column.
                        self.block_start = None;
                        break;
                    }
                }
            }

            let rest = &line[idx..];

            if let Some(consumed) = self.consume_string(line, idx) {
                idx += consumed;
                continue;
            }

            if self.syntax.slash && rest.starts_with("//") {
                // A line comment owns the rest of the line.
                if !rest.starts_with("///") && !rest.starts_with("//!") {
                    record(&mut found, idx, rest[2..].trim().to_string());
                }
                break;
            }

            if self.syntax.slash && rest.starts_with("/*") {
                self.in_block_comment = true;
                self.block_is_doc = rest.starts_with("/**") && !rest.starts_with("/**/");
                self.block_start = Some(idx);
                idx += 2;
                continue;
            }

            if self.syntax.hash && rest.starts_with('#') {
                record(&mut found, idx, rest[1..].trim().to_string());
                break;
            }

            if self.syntax.html && rest.starts_with("<!--") {
                let body = &rest[4..];
                let text = match body.find("-->") {
                    Some(pos) => &body[..pos],
                    None => body,
                };
                record(&mut found, idx, text.trim().to_string());
                break;
            }

            idx += next_char_len(rest);
        }

        found
    }

    /// If a string literal starts at `idx`, how many bytes of `line` it covers
    /// (to the end of the line when it is unterminated). `None` when `idx` is
    /// not the start of one.
    fn consume_string(&mut self, line: &str, idx: usize) -> Option<usize> {
        let rest = &line[idx..];

        // Rust raw string: r"…", r#"…"#, r##"…"##
        if rest.starts_with('r') && !preceded_by_ident_char(line, idx) {
            let hashes = rest[1..].len() - rest[1..].trim_start_matches('#').len();
            if rest[1 + hashes..].starts_with('"') {
                let close = format!("\"{}", "#".repeat(hashes));
                let body_at = 1 + hashes + 1;
                return Some(match rest[body_at..].find(close.as_str()) {
                    Some(pos) => body_at + pos + close.len(),
                    None => {
                        self.raw_string_close = Some(close);
                        rest.len()
                    }
                });
            }
        }

        if rest.starts_with('"') {
            return Some(quoted_len(rest, '"'));
        }

        if rest.starts_with('\'') {
            if self.syntax.quote_is_string {
                return Some(quoted_len(rest, '\''));
            }
            // A char literal — but `'a` in `&'a str` is a lifetime, which has
            // no closing quote.
            let len = quoted_len(rest, '\'');
            if rest[..len].ends_with('\'') && len > 1 {
                return Some(len);
            }
            return Some(1);
        }

        None
    }
}

/// Bytes covered by a `quote`-delimited literal starting at the front of
/// `rest`, honouring backslash escapes. An unterminated literal covers the
/// whole slice.
fn quoted_len(rest: &str, quote: char) -> usize {
    let mut chars = rest.char_indices();
    chars.next(); // opening quote
    let mut escaped = false;
    for (offset, ch) in chars {
        if escaped {
            escaped = false;
            continue;
        }
        if ch == '\\' {
            escaped = true;
            continue;
        }
        if ch == quote {
            return offset + ch.len_utf8();
        }
    }
    rest.len()
}

fn preceded_by_ident_char(line: &str, idx: usize) -> bool {
    line[..idx]
        .chars()
        .next_back()
        .is_some_and(|c| c.is_alphanumeric() || c == '_')
}

/// A block comment's continuation lines conventionally start with `*`.
fn block_comment_text(chunk: &str) -> String {
    chunk
        .trim()
        .trim_start_matches('*')
        .trim_start()
        .trim_end()
        .to_string()
}

fn record(found: &mut Option<CommentSpan>, idx: usize, text: String) {
    if found.is_none() && !text.is_empty() {
        *found = Some(CommentSpan {
            column: idx as u32 + 1,
            text,
        });
    }
}

fn next_char_len(rest: &str) -> usize {
    rest.chars().next().map_or(1, char::len_utf8)
}