panache 3.2.0

Language server, formatter, and linter for Markdown, Quarto, and R Markdown
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Shared parser for the jolars family of linters (arity, fatou).
//!
//! Both tools emit a top-level JSON array of diagnostics with 0-indexed byte
//! ranges into the linted file plus optional fix data. The schemas differ only
//! in severity casing (arity: `Warning`, fatou: `warning`) and fix shape
//! (arity: single optional `fix` object, fatou: `fixes` array), so one parser
//! handles both.

use rowan::TextRange;
use serde::Deserialize;

use super::{
    ExternalLinterParser, LinterError, ParseContext,
    map_concatenated_offset_to_original_with_end_boundary,
};
use crate::linter::diagnostics::{
    Diagnostic, DiagnosticNoteKind, DiagnosticOrigin, Edit, Fix, Location,
};

#[derive(Debug, Deserialize)]
struct FamilyDiagnostic {
    rule: String,
    severity: String,
    range: FamilyRange,
    message: FamilyMessage,
    #[serde(default)]
    fix: Option<FamilyFix>,
    #[serde(default)]
    fixes: Vec<FamilyFix>,
}

#[derive(Debug, Deserialize)]
struct FamilyRange {
    start: usize,
    end: usize,
}

#[derive(Debug, Deserialize)]
struct FamilyMessage {
    #[allow(dead_code)]
    name: String,
    body: String,
    #[serde(default)]
    suggestion: Option<String>,
}

#[derive(Debug, Deserialize)]
struct FamilyFix {
    content: String,
    start: usize,
    end: usize,
    applicability: String,
    description: String,
}

/// Map a byte offset in the linted (possibly concatenated) input back to the
/// original document, clamped to the input like the other parsers do.
fn map_diagnostic_offset(offset: usize, ctx: &ParseContext<'_>) -> usize {
    match ctx.mappings {
        Some(mappings) => map_concatenated_offset_to_original_with_end_boundary(offset, mappings)
            .unwrap_or(ctx.original_input.len()),
        None => offset.min(ctx.original_input.len()),
    }
}

fn parse_family(ctx: &ParseContext<'_>, tool: &str) -> Result<Vec<Diagnostic>, LinterError> {
    let output: Vec<FamilyDiagnostic> = serde_json::from_str(ctx.output)
        .map_err(|e| LinterError::ParseError(format!("invalid {} JSON: {}", tool, e)))?;

    let mut diagnostics = Vec::new();
    for family_diag in output {
        let start_offset = map_diagnostic_offset(family_diag.range.start, ctx);
        let end_offset = map_diagnostic_offset(family_diag.range.end, ctx).max(start_offset);
        let range = TextRange::new((start_offset as u32).into(), (end_offset as u32).into());
        let location = Location::from_range(range, ctx.original_input);

        let fix = if let Some(mappings) = ctx.mappings {
            family_diag
                .fix
                .as_ref()
                .or_else(|| family_diag.fixes.first())
                .and_then(|family_fix| {
                    let fix_start = map_concatenated_offset_to_original_with_end_boundary(
                        family_fix.start,
                        mappings,
                    )?;
                    let fix_end = map_concatenated_offset_to_original_with_end_boundary(
                        family_fix.end,
                        mappings,
                    )?;
                    let edits = vec![Edit {
                        range: TextRange::new((fix_start as u32).into(), (fix_end as u32).into()),
                        replacement: family_fix.content.clone(),
                    }];
                    Some(if family_fix.applicability.eq_ignore_ascii_case("unsafe") {
                        Fix::unsafe_fix(family_fix.description.clone(), edits)
                    } else {
                        Fix::safe(family_fix.description.clone(), edits)
                    })
                })
        } else {
            None
        };

        let severity = family_diag.severity.to_ascii_lowercase();
        let mut diagnostic = match severity.as_str() {
            "error" => Diagnostic::error(location, family_diag.rule, family_diag.message.body),
            "warning" => Diagnostic::warning(location, family_diag.rule, family_diag.message.body),
            _ => Diagnostic::info(location, family_diag.rule, family_diag.message.body),
        }
        .with_origin(DiagnosticOrigin::External);

        if let Some(suggestion) = family_diag.message.suggestion.as_ref()
            && !suggestion.trim().is_empty()
        {
            diagnostic = diagnostic.with_note(DiagnosticNoteKind::Help, suggestion.clone());
        }
        diagnostics.push(if let Some(fix) = fix {
            diagnostic.with_fix(fix)
        } else {
            diagnostic
        });
    }
    Ok(diagnostics)
}

pub(crate) struct ArityParser;

impl ExternalLinterParser for ArityParser {
    const NAME: &'static str = "arity";

    fn parse(ctx: &ParseContext<'_>) -> Result<Vec<Diagnostic>, LinterError> {
        parse_family(ctx, Self::NAME)
    }
}

pub(crate) struct FatouParser;

impl ExternalLinterParser for FatouParser {
    const NAME: &'static str = "fatou";

    fn parse(ctx: &ParseContext<'_>) -> Result<Vec<Diagnostic>, LinterError> {
        parse_family(ctx, Self::NAME)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::linter::code_block_collector::BlockMapping;
    use crate::linter::diagnostics::{FixSafety, Severity};

    // Captured from `arity lint --no-config --output json` on `any(is.na(x))\n`.
    const ARITY_OUTPUT: &str = r#"[
  {
    "rule": "any-is-na",
    "severity": "Warning",
    "path": "input.R",
    "range": { "start": 0, "end": 13 },
    "message": {
      "name": "any-is-na",
      "body": "`any(is.na(x))` is the faster, clearer `anyNA(x)`",
      "suggestion": "Use `anyNA(x)`."
    },
    "fix": {
      "content": "anyNA(x)",
      "start": 0,
      "end": 13,
      "applicability": "safe",
      "description": "Replace `any(is.na(x))` with `anyNA(x)`"
    }
  },
  {
    "rule": "undefined-symbol",
    "severity": "Warning",
    "path": "input.R",
    "range": { "start": 10, "end": 11 },
    "message": {
      "name": "undefined-symbol",
      "body": "no in-scope binding or attached package exports `x`",
      "suggestion": null
    }
  }
]"#;

    // Captured from `fatou lint --no-config --output json`.
    const FATOU_OUTPUT: &str = r#"[
  {
    "rule": "nothing-comparison",
    "severity": "warning",
    "path": "fix.jl",
    "range": { "start": 3, "end": 15 },
    "message": {
      "name": "nothing-comparison",
      "body": "comparison against `nothing` by value; use `===` or `isnothing`",
      "suggestion": null
    },
    "fixes": [
      {
        "description": "Replace `==` with `===`",
        "content": "===",
        "start": 5,
        "end": 7,
        "applicability": "safe"
      }
    ]
  }
]"#;

    #[test]
    fn parses_arity_diagnostics_without_mappings() {
        let input = "any(is.na(x))\n";
        let ctx = ParseContext {
            output: ARITY_OUTPUT,
            linted_input: input,
            original_input: input,
            mappings: None,
        };
        let diagnostics = ArityParser::parse(&ctx).unwrap();
        assert_eq!(diagnostics.len(), 2);

        let diag = &diagnostics[0];
        assert_eq!(diag.code, "any-is-na");
        assert_eq!(diag.severity, Severity::Warning);
        assert_eq!(diag.origin, DiagnosticOrigin::External);
        assert_eq!(diag.location.line, 1);
        assert_eq!(diag.location.column, 1);
        assert_eq!(diag.location.range, TextRange::new(0.into(), 13.into()));
        assert_eq!(diag.notes.len(), 1);
        assert_eq!(diag.notes[0].kind, DiagnosticNoteKind::Help);
        assert_eq!(diag.notes[0].message, "Use `anyNA(x)`.");
        // Fixes require block mappings, matching the jarl/ruff parsers.
        assert!(diag.fix.is_none());

        let diag = &diagnostics[1];
        assert_eq!(diag.code, "undefined-symbol");
        assert!(diag.notes.is_empty());
        assert!(diag.fix.is_none());
    }

    #[test]
    fn maps_arity_ranges_and_fix_through_block_mappings() {
        // Document: heading, blank line, then an r code block on line 4.
        let original = "# Title\n\n```r\nany(is.na(x))\n```\n";
        // Concatenated file: three blank lines, then the block content.
        let linted = "\n\n\nany(is.na(x))\n";
        let mappings = vec![BlockMapping {
            concatenated_range: 3..17,
            original_range: 14..28,
            start_line: 4,
        }];
        // Offsets are relative to the concatenated file (block starts at 3).
        let output = r#"[
  {
    "rule": "any-is-na",
    "severity": "Warning",
    "path": "input.R",
    "range": { "start": 3, "end": 16 },
    "message": {
      "name": "any-is-na",
      "body": "`any(is.na(x))` is the faster, clearer `anyNA(x)`",
      "suggestion": "Use `anyNA(x)`."
    },
    "fix": {
      "content": "anyNA(x)",
      "start": 3,
      "end": 16,
      "applicability": "safe",
      "description": "Replace `any(is.na(x))` with `anyNA(x)`"
    }
  }
]"#;
        let ctx = ParseContext {
            output,
            linted_input: linted,
            original_input: original,
            mappings: Some(&mappings),
        };
        let diagnostics = ArityParser::parse(&ctx).unwrap();
        assert_eq!(diagnostics.len(), 1);

        let diag = &diagnostics[0];
        assert_eq!(diag.location.range, TextRange::new(14.into(), 27.into()));
        assert_eq!(diag.location.line, 4);
        assert_eq!(diag.location.column, 1);

        let fix = diag.fix.as_ref().expect("fix should map");
        assert_eq!(fix.safety, FixSafety::Safe);
        assert_eq!(fix.message, "Replace `any(is.na(x))` with `anyNA(x)`");
        assert_eq!(fix.edits.len(), 1);
        assert_eq!(fix.edits[0].range, TextRange::new(14.into(), 27.into()));
        assert_eq!(fix.edits[0].replacement, "anyNA(x)");
    }

    #[test]
    fn parses_fatou_diagnostics_with_fixes_array() {
        let original = "if x == nothing\n    y = 1\nend\n";
        let mappings = vec![BlockMapping {
            concatenated_range: 0..30,
            original_range: 0..30,
            start_line: 1,
        }];
        let ctx = ParseContext {
            output: FATOU_OUTPUT,
            linted_input: original,
            original_input: original,
            mappings: Some(&mappings),
        };
        let diagnostics = FatouParser::parse(&ctx).unwrap();
        assert_eq!(diagnostics.len(), 1);

        let diag = &diagnostics[0];
        assert_eq!(diag.code, "nothing-comparison");
        assert_eq!(diag.severity, Severity::Warning);
        assert!(diag.notes.is_empty());
        assert_eq!(diag.location.range, TextRange::new(3.into(), 15.into()));

        let fix = diag.fix.as_ref().expect("fix from fixes array");
        assert_eq!(fix.message, "Replace `==` with `===`");
        assert_eq!(fix.edits[0].range, TextRange::new(5.into(), 7.into()));
        assert_eq!(fix.edits[0].replacement, "===");
    }

    #[test]
    fn empty_fixes_array_yields_no_fix() {
        let input = "import Printf\nx = 1\n";
        let output = r#"[
  {
    "rule": "unused-import",
    "severity": "warning",
    "path": "input.jl",
    "range": { "start": 7, "end": 13 },
    "message": {
      "name": "unused-import",
      "body": "`Printf` is imported but never used",
      "suggestion": null
    },
    "fixes": []
  }
]"#;
        let mappings = vec![BlockMapping {
            concatenated_range: 0..20,
            original_range: 0..20,
            start_line: 1,
        }];
        let ctx = ParseContext {
            output,
            linted_input: input,
            original_input: input,
            mappings: Some(&mappings),
        };
        let diagnostics = FatouParser::parse(&ctx).unwrap();
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].fix.is_none());
    }

    #[test]
    fn maps_severities_case_insensitively() {
        let input = "x\n";
        for (severity, expected) in [
            ("Error", Severity::Error),
            ("error", Severity::Error),
            ("Warning", Severity::Warning),
            ("warning", Severity::Warning),
            ("Info", Severity::Info),
            ("info", Severity::Info),
            ("Hint", Severity::Info),
            ("hint", Severity::Info),
        ] {
            let output = format!(
                r#"[{{"rule":"r","severity":"{}","range":{{"start":0,"end":1}},"message":{{"name":"r","body":"b","suggestion":null}}}}]"#,
                severity
            );
            let ctx = ParseContext {
                output: &output,
                linted_input: input,
                original_input: input,
                mappings: None,
            };
            let diagnostics = ArityParser::parse(&ctx).unwrap();
            assert_eq!(diagnostics[0].severity, expected, "severity {}", severity);
        }
    }

    #[test]
    fn unsafe_applicability_yields_unsafe_fix() {
        let input = "x <- 1\n";
        let output = r#"[
  {
    "rule": "some-rule",
    "severity": "warning",
    "range": { "start": 0, "end": 1 },
    "message": { "name": "some-rule", "body": "b", "suggestion": null },
    "fix": {
      "content": "y",
      "start": 0,
      "end": 1,
      "applicability": "unsafe",
      "description": "Rename `x` to `y`"
    }
  }
]"#;
        let mappings = vec![BlockMapping {
            concatenated_range: 0..7,
            original_range: 0..7,
            start_line: 1,
        }];
        let ctx = ParseContext {
            output,
            linted_input: input,
            original_input: input,
            mappings: Some(&mappings),
        };
        let diagnostics = ArityParser::parse(&ctx).unwrap();
        let fix = diagnostics[0].fix.as_ref().expect("fix expected");
        assert_eq!(fix.safety, FixSafety::Unsafe);
    }

    #[test]
    fn drops_unmappable_fix_but_keeps_diagnostic() {
        let original = "# Title\n\n```r\nany(is.na(x))\n```\n";
        let linted = "\n\n\nany(is.na(x))\n";
        let mappings = vec![BlockMapping {
            concatenated_range: 3..17,
            original_range: 14..28,
            start_line: 4,
        }];
        // Fix offsets far outside every mapping.
        let output = r#"[
  {
    "rule": "some-rule",
    "severity": "warning",
    "range": { "start": 3, "end": 16 },
    "message": { "name": "some-rule", "body": "b", "suggestion": null },
    "fix": {
      "content": "anyNA(x)",
      "start": 100,
      "end": 113,
      "applicability": "safe",
      "description": "d"
    }
  }
]"#;
        let ctx = ParseContext {
            output,
            linted_input: linted,
            original_input: original,
            mappings: Some(&mappings),
        };
        let diagnostics = ArityParser::parse(&ctx).unwrap();
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].fix.is_none());
        assert_eq!(
            diagnostics[0].location.range,
            TextRange::new(14.into(), 27.into())
        );
    }

    #[test]
    fn malformed_json_is_a_parse_error() {
        let ctx = ParseContext {
            output: "not json",
            linted_input: "x\n",
            original_input: "x\n",
            mappings: None,
        };
        let result = ArityParser::parse(&ctx);
        assert!(matches!(result, Err(LinterError::ParseError(_))));
    }
}