proa_source_tools 0.1.1

Source formatting, linting, and inspection engines for Proa
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
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::fmt;
use std::path::{Path, PathBuf};

use serde::Serialize;
use unicode_width::UnicodeWidthStr;

/// Authored source range. Lines and Unicode scalar columns are one-based;
/// the end is exclusive (display widths and editor UTF-16 offsets differ).
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceRange {
    pub path: PathBuf,
    pub line: usize,
    pub column: usize,
    pub end_line: usize,
    pub end_column: usize,
}

impl SourceRange {
    pub fn from_span(path: &Path, span: proc_macro2::Span) -> Self {
        let start = span.start();
        let end = span.end();
        Self {
            path: path.to_owned(),
            line: start.line.max(1),
            column: start.column + 1,
            end_line: end.line.max(1),
            end_column: end.column + 1,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct SourceLine {
    pub number: usize,
    pub text: String,
}

/// Evidence retained with a diagnostic, so output never needs to reread a file.
#[derive(Debug, Clone, Serialize)]
pub struct SourceLabel {
    pub range: SourceRange,
    pub message: String,
    pub primary: bool,
    pub source: Vec<SourceLine>,
}

impl SourceLabel {
    pub fn new(
        range: SourceRange,
        source: &str,
        message: impl Into<String>,
        primary: bool,
    ) -> Self {
        let lines: Vec<_> = source
            .lines()
            .enumerate()
            .filter_map(|(index, text)| {
                let number = index + 1;
                let near_start = number >= range.line.saturating_sub(1)
                    && number <= range.line.saturating_add(2);
                let near_end = number >= range.end_line.saturating_sub(1)
                    && number <= range.end_line.saturating_add(1);
                (near_start || near_end).then(|| SourceLine {
                    number,
                    text: text.to_owned(),
                })
            })
            .collect();
        Self {
            range,
            message: message.into(),
            primary,
            source: lines,
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum SuggestionApplicability {
    MachineApplicable,
    MaybeIncorrect,
}

#[derive(Debug, Clone, Serialize)]
pub struct Suggestion {
    pub message: String,
    pub range: SourceRange,
    pub replacement: String,
    pub applicability: SuggestionApplicability,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Severity {
    Error,
    Warning,
    Info,
}

impl Severity {
    /// Ordering rank for severity filtering (higher = more severe).
    pub const fn rank(self) -> u8 {
        match self {
            Self::Error => 2,
            Self::Warning => 1,
            Self::Info => 0,
        }
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Error => "error",
            Self::Warning => "warning",
            Self::Info => "info",
        }
    }
}

impl Serialize for Severity {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct Diagnostic {
    pub path: PathBuf,
    pub line: usize,
    pub column: usize,
    pub severity: Severity,
    pub code: &'static str,
    pub message: String,
    pub help: Option<String>,
    pub labels: Vec<SourceLabel>,
    pub notes: Vec<String>,
    pub suggestions: Vec<Suggestion>,
}

impl Diagnostic {
    pub fn new(
        path: impl AsRef<Path>,
        line: usize,
        column: usize,
        severity: Severity,
        code: &'static str,
        message: impl Into<String>,
    ) -> Self {
        Self {
            path: path.as_ref().to_path_buf(),
            line,
            column,
            severity,
            code,
            message: message.into(),
            help: None,
            labels: Vec::new(),
            notes: Vec::new(),
            suggestions: Vec::new(),
        }
    }

    pub fn with_help(mut self, help: impl Into<String>) -> Self {
        self.help = Some(help.into());
        self
    }

    pub fn with_label(mut self, label: SourceLabel) -> Self {
        self.labels.push(label);
        self
    }

    pub fn with_note(mut self, note: impl Into<String>) -> Self {
        self.notes.push(note.into());
        self
    }

    pub fn with_suggestion(mut self, suggestion: Suggestion) -> Self {
        self.suggestions.push(suggestion);
        self
    }
}

impl fmt::Display for Diagnostic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.labels.is_empty() {
            writeln!(
                f,
                "{}[{}]: {}",
                self.severity.as_str(),
                self.code,
                self.message
            )?;
            for label in &self.labels {
                render_label(f, label)?;
            }
            for note in &self.notes {
                writeln!(f, "   = note: {note}")?;
            }
            if let Some(help) = &self.help {
                writeln!(f, "   = help: {help}")?;
            }
            for suggestion in &self.suggestions {
                writeln!(f, "   = help: {}", suggestion.message)?;
                for line in suggestion.replacement.lines() {
                    writeln!(f, "           {line}")?;
                }
            }
            return Ok(());
        }
        writeln!(
            f,
            "{}:{}:{}: {}[{}]: {}",
            self.path.display(),
            self.line,
            self.column,
            self.severity.as_str(),
            self.code,
            self.message
        )?;
        if let Some(help) = &self.help {
            writeln!(f, "  help: {help}")?;
        }
        for note in &self.notes {
            writeln!(f, "  note: {note}")?;
        }
        for suggestion in &self.suggestions {
            writeln!(
                f,
                "  help: {}: {}",
                suggestion.message, suggestion.replacement
            )?;
        }
        Ok(())
    }
}

fn render_label(f: &mut fmt::Formatter<'_>, label: &SourceLabel) -> fmt::Result {
    let range = &label.range;
    let arrow = if label.primary { "-->" } else { ":::" };
    writeln!(
        f,
        "  {arrow} {}:{}:{}",
        crate::source::display_path(&range.path).display(),
        range.line,
        range.column
    )?;
    let width = label
        .source
        .last()
        .map_or(range.line, |line| line.number)
        .to_string()
        .len();
    writeln!(f, " {:width$} |", "")?;
    let mut previous = None;
    for line in &label.source {
        if previous.is_some_and(|number| line.number > number + 1) {
            writeln!(f, " {:width$} | ...", "")?;
        }
        writeln!(f, " {:>width$} | {}", line.number, line.text)?;
        if line.number == range.line {
            let column = range
                .column
                .saturating_sub(1)
                .min(line.text.chars().count());
            let authored_prefix: String = line.text.chars().take(column).collect();
            let prefix: String = authored_prefix
                .split_inclusive('\t')
                .map(|segment| {
                    let text = segment.trim_end_matches('\t');
                    let mut spaces = " ".repeat(text.width());
                    if segment.ends_with('\t') {
                        spaces.push('\t');
                    }
                    spaces
                })
                .collect();
            let available = line.text.chars().count().saturating_sub(column).max(1);
            let length = if range.end_line == range.line {
                range
                    .end_column
                    .saturating_sub(range.column)
                    .max(1)
                    .min(available)
            } else {
                available
            };
            let highlighted: String = line.text.chars().skip(column).take(length).collect();
            let underline = if label.primary { '^' } else { '-' }
                .to_string()
                .repeat(highlighted.width().max(1));
            writeln!(f, " {:width$} | {prefix}{underline} {}", "", label.message)?;
        }
        previous = Some(line.number);
    }
    if !label.source.iter().any(|line| line.number == range.line) {
        writeln!(f, " {:width$} | {}", "", label.message)?;
    }
    writeln!(f, " {:width$} |", "")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn severity_rank_orders_error_over_warning_over_info() {
        assert!(Severity::Error.rank() > Severity::Warning.rank());
        assert!(Severity::Warning.rank() > Severity::Info.rank());
    }

    #[test]
    fn severity_serializes_as_lowercase_string() {
        assert_eq!(
            serde_json::to_string(&Severity::Warning).unwrap(),
            "\"warning\""
        );
        assert_eq!(
            serde_json::to_string(&Severity::Error).unwrap(),
            "\"error\""
        );
    }

    #[test]
    fn display_includes_location_code_and_help() {
        let diagnostic = Diagnostic::new(
            Path::new("src/page.rs"),
            4,
            9,
            Severity::Warning,
            "perf/vec",
            "Vec in a render path",
        )
        .with_help("use stack storage");

        let rendered = diagnostic.to_string();
        assert_eq!(
            rendered,
            "src/page.rs:4:9: warning[perf/vec]: Vec in a render path\n  help: use stack storage\n"
        );
    }

    #[test]
    fn display_omits_help_line_when_absent() {
        let diagnostic = Diagnostic::new(
            Path::new("src/page.rs"),
            1,
            1,
            Severity::Info,
            "render/advisory",
            "advisory",
        );

        assert_eq!(
            diagnostic.to_string(),
            "src/page.rs:1:1: info[render/advisory]: advisory\n"
        );
    }
}

#[cfg(test)]
mod frame_tests {
    use super::*;
    #[test]
    fn source_frames_have_primary_related_evidence_and_repairs() {
        let range = SourceRange {
            path: "src/product.md.rs".into(),
            line: 2,
            column: 5,
            end_line: 2,
            end_column: 13,
        };
        let related = SourceRange {
            path: "src/product.html.rs".into(),
            line: 1,
            column: 1,
            end_line: 1,
            end_column: 8,
        };
        let diag = Diagnostic::new(
            &range.path,
            2,
            5,
            Severity::Error,
            "render/content-coverage",
            "Product Markdown omits RestockNotice",
        )
        .with_label(SourceLabel::new(
            range.clone(),
            "fn render_md() {\n    md_sync! {}\n}",
            "the notice is absent here",
            true,
        ))
        .with_label(SourceLabel::new(
            related,
            "notice.render(cx)",
            "rendered in HTML",
            false,
        ))
        .with_note("constructing a child does not render it")
        .with_suggestion(Suggestion {
            message: "acknowledge an intentional omission inside render_md".into(),
            range,
            replacement: "proa_macros::omit!(RestockNotice);".into(),
            applicability: SuggestionApplicability::MaybeIncorrect,
        });
        assert_eq!(diag.to_string(),"error[render/content-coverage]: Product Markdown omits RestockNotice\n  --> src/product.md.rs:2:5\n   |\n 1 | fn render_md() {\n 2 |     md_sync! {}\n   |     ^^^^^^^^ the notice is absent here\n 3 | }\n   |\n  ::: src/product.html.rs:1:1\n   |\n 1 | notice.render(cx)\n   | ------- rendered in HTML\n   |\n   = note: constructing a child does not render it\n   = help: acknowledge an intentional omission inside render_md\n           proa_macros::omit!(RestockNotice);\n");
    }
    #[test]
    fn rust_span_columns_are_unicode_scalar_offsets() {
        let expr: syn::Expr = syn::parse_str("(\"🦀\", value)").unwrap();
        let syn::Expr::Tuple(tuple) = expr else {
            panic!()
        };
        let range = SourceRange::from_span(
            Path::new("test.rs"),
            syn::spanned::Spanned::span(&tuple.elems[1]),
        );
        assert_eq!(range.column, 7);
        assert_eq!(range.end_column, 12);
    }
}

#[cfg(test)]
mod unicode_frame_tests {
    use super::*;
    #[test]
    fn source_underlines_use_display_width_after_wide_characters() {
        let r = SourceRange {
            path: "md.rs".into(),
            line: 1,
            column: 3,
            end_line: 1,
            end_column: 8,
        };
        let d = Diagnostic::new("md.rs", 1, 3, Severity::Info, "test/frame", "frame")
            .with_label(SourceLabel::new(r, "🦀 value", "value", true));
        assert!(d.to_string().contains("   |    ^^^^^ value"));
    }
}