omnicoach 0.1.0

Lightweight, high-signal static analysis and command linter for Python and Rust
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Rule catalog: renders the rule list and one rule's documentation as plain Markdown.

architecture_component!(RuleCatalog);

use strum::IntoEnumIterator as _;

use crate::rule_declaration::{
    DeclaredOptions, EnforcementMode, FilterListDefaults, LanguageDefaults, OptionSpec,
};
use crate::rule_selection::{
    ConfigError, Facet, RegisteredRule, UnknownLabel, find_rule, load_rule_status,
    registered_rules, registered_topics, rules_tagged,
};

/// Why a discovery request failed. The message is what the user sees.
#[derive(Debug, thiserror::Error)]
pub enum CatalogError {
    /// A `--tag` or `--explain` label that was not recognized.
    #[error(transparent)]
    Label(#[from] UnknownLabel),
    /// A configuration error when reading status.
    #[error(transparent)]
    Config(#[from] ConfigError),
}

/// Command-line flags for rule discovery: `--list-rules`, `--tag`, `--list-tags` and `--explain`.
#[derive(Debug, clap::Args)]
pub struct DiscoveryArgs {
    /// List every rule with its languages and summary, as Markdown
    #[arg(long, global = true)]
    pub list_rules: bool,

    /// With --list-rules, keep only the rules a `select` label selects (tag, synonym or rule)
    #[arg(long, global = true, value_name = "LABEL", requires = "list_rules")]
    pub tag: Option<String>,

    /// List the topic tree with each topic's parent, synonyms, description and scope note
    #[arg(long, global = true, conflicts_with_all = ["list_rules", "explain"])]
    pub list_tags: bool,

    /// Print one rule's documentation as Markdown
    #[arg(long, global = true, value_name = "RULE", conflicts_with_all = ["list_rules", "list_tags"])]
    pub explain: Option<String>,
}

impl DiscoveryArgs {
    /// Renders the requested discovery output, or `None` when no discovery flag is given.
    #[must_use]
    pub fn render(&self) -> Option<Result<String, CatalogError>> {
        if let Some(name) = &self.explain {
            return Some(explain(name));
        }
        if self.list_tags {
            return Some(Ok(list_tags()));
        }
        self.list_rules.then(|| list_rules(self.tag.as_deref()))
    }
}

/// The line printed after plain diagnostics, pointing at `--explain`.
#[must_use]
pub fn explain_footer(binary: &str) -> String {
    format!("For details on a rule, run: {binary} --explain <rule>")
}

/// One Markdown bullet per rule, sorted by name: name, languages (or input) and summary.
///
/// # Errors
///
/// Returns [`CatalogError::Label`] if `tag` is not a label `select` accepts.
pub fn list_rules(tag: Option<&str>) -> Result<String, CatalogError> {
    let mut rules = match tag {
        Some(label) => rules_tagged(label)?,
        None => registered_rules().iter().collect(),
    };
    rules.sort_by_key(|rule| rule.name);
    let lines: Vec<String> = rules
        .iter()
        .map(|rule| {
            let languages = Some(facet_tags(rule, Facet::Languages))
                .filter(|languages| !languages.is_empty())
                .unwrap_or_else(|| facet_tags(rule, Facet::Input));
            format!("- `{}` ({languages}): {}", rule.name, rule.doc.summary)
        })
        .collect();
    Ok(lines.join("\n") + "\n")
}

/// The topic tree as a Markdown table: each topic's label, parent, synonyms, description and
/// scope note, with children listed under their parent.
#[must_use]
pub fn list_tags() -> String {
    let mut lines = vec![
        "| Topic | Parent | Synonyms | Description | Scope note |".to_owned(),
        "|---|---|---|---|---|".to_owned(),
    ];
    for topic in registered_topics() {
        let parent = topic.parent.map_or("—", |parent| parent.label);
        let synonyms: Vec<String> = topic
            .synonyms
            .iter()
            .map(|synonym| format!("`{synonym}`"))
            .collect();
        lines.push(format!(
            "| `{}` | {parent} | {} | {} | {} |",
            topic.label,
            synonyms.join(", "),
            topic.description,
            topic.scope_note,
        ));
    }
    lines.join("\n") + "\n"
}

/// The rule's full documentation as Markdown, including its active status.
///
/// # Errors
///
/// Returns [`CatalogError::Label`] if no rule is named `name`.
pub fn explain(name: &str) -> Result<String, CatalogError> {
    let rule = find_rule(name)?;
    Ok(render_rule(rule))
}

fn render_rule(rule: &RegisteredRule) -> String {
    let doc = rule.doc;
    let template = rule.template;
    let status_line = match load_rule_status(rule) {
        Ok(status) => format!("Status: {status}"),
        Err(error) => format!("Status: <config error: {error}>"),
    };
    let mut lines = vec![
        format!("# {}", rule.name),
        String::new(),
        status_line,
        String::new(),
        "## What it does".to_owned(),
        String::new(),
        doc.what_it_does.to_owned(),
        String::new(),
        "## Why is this bad?".to_owned(),
        String::new(),
        doc.why_is_this_bad.trim_end().to_owned(),
        String::new(),
    ];
    if let Some(known_problems) = doc.known_problems {
        lines.extend([
            "## Known problems".to_owned(),
            String::new(),
            known_problems.to_owned(),
            String::new(),
        ]);
    }
    if !doc.examples.is_empty() {
        lines.extend(["## Example".to_owned(), String::new()]);
        for example in doc.examples {
            let fence = format!("```{}", example.language.as_str());
            lines.extend([
                fence.clone(),
                example.flagged.trim_end().to_owned(),
                "```".to_owned(),
                String::new(),
                "Use instead:".to_owned(),
                String::new(),
                fence,
                example.fixed.trim_end().to_owned(),
                "```".to_owned(),
                String::new(),
            ]);
        }
    }
    lines.extend(["## Message".to_owned(), String::new()]);
    for (label, text) in [
        ("Summary", template.summary),
        ("Rationale", template.rationale),
        ("Suggestion", template.suggestion),
    ] {
        lines.push(text.base.map_or_else(
            || format!("- {label}:"),
            |base| format!("- {label}: {base}"),
        ));
        lines.extend(
            text.overrides
                .iter()
                .map(|(language, override_text)| format!("  - {language}: {override_text}")),
        );
    }
    let configuration = configuration_lines(&rule.options);
    if !configuration.is_empty() {
        lines.extend([
            String::new(),
            "## Configuration".to_owned(),
            String::new(),
            format!(
                "Keys under `[rules.{0}]`, or `[rules.{0}.<language>]` for one language:",
                rule.name
            ),
            String::new(),
        ]);
        lines.extend(configuration);
        if let Some(toml_block) = rule.options.default_toml(rule.name, rule.languages) {
            lines.extend([
                String::new(),
                "```toml".to_owned(),
                toml_block.trim_end().to_owned(),
                "```".to_owned(),
            ]);
        }
    }
    if !doc.references.is_empty() {
        lines.extend([String::new(), "## References".to_owned(), String::new()]);
        lines.extend(
            doc.references
                .iter()
                .map(|reference| format!("- [{}]({})", reference.title, reference.url)),
        );
    }
    lines.extend([String::new(), "## Tags".to_owned(), String::new()]);
    lines.extend(
        Facet::iter()
            .map(|facet| (facet, facet_tags(rule, facet)))
            .filter(|(_, tags)| !tags.is_empty())
            .map(|(facet, tags)| format!("- {}: {tags}", facet.label())),
    );
    lines.join("\n") + "\n"
}

/// One bullet per accepted key: its type, default and meaning, as declared by the rule.
fn configuration_lines(options: &DeclaredOptions) -> Vec<String> {
    let mut lines = Vec::new();
    if let Some(default) = options.enforcement_mode {
        let modes: Vec<&str> = EnforcementMode::iter().map(Into::into).collect();
        lines.push(format!(
            "- `{}` (`{}`, default {}): {}",
            EnforcementMode::KEY,
            modes.join("` or `"),
            per_language(default, |mode| format!("`{}`", <&str>::from(mode))),
            EnforcementMode::DOC
        ));
    }
    for option in &options.options {
        match option {
            OptionSpec::Count(count) => lines.push(format!(
                "- `{}` (integer, default {}): {}",
                count.key,
                per_language(count.default, |value| value.to_string()),
                count.doc
            )),
            OptionSpec::List(list) => lines.extend([
                format!(
                    "- `{}` (list of strings, default {}): {}",
                    list.kind.replace_key(),
                    list_default(&list.default),
                    list.doc
                ),
                format!(
                    "- `{}` (list of strings): items added to the default.",
                    list.kind.extend_key()
                ),
                format!(
                    "- `{}` (list of strings): items removed from the default.",
                    list.kind.remove_key()
                ),
            ]),
        }
    }
    lines
}

/// A default and its per-language overrides, e.g. `4, rust: 6`.
fn per_language<T: Copy>(defaults: LanguageDefaults<T>, render: impl Fn(T) -> String) -> String {
    let overrides = defaults
        .overrides
        .iter()
        .map(|&(language, value)| format!(", {}: {}", language.as_str(), render(value)));
    std::iter::once(render(defaults.base))
        .chain(overrides)
        .collect()
}

/// A list default and its per-language changes, e.g. `` `a`, `b`; rust removes `b` ``.
fn list_default(defaults: &FilterListDefaults) -> String {
    let quoted = |items: &[&str]| {
        let items: Vec<String> = items.iter().map(|item| format!("`{item}`")).collect();
        items.join(", ")
    };
    let base = if defaults.base.is_empty() {
        "empty".to_owned()
    } else {
        quoted(defaults.base)
    };
    let added = defaults
        .extend
        .iter()
        .map(|&(language, items)| format!("{} adds {}", language.as_str(), quoted(items)));
    let removed = defaults
        .remove
        .iter()
        .map(|&(language, items)| format!("{} removes {}", language.as_str(), quoted(items)));
    let parts: Vec<String> = std::iter::once(base).chain(added).chain(removed).collect();
    parts.join("; ")
}

/// The rule's tags in `facet`, comma-separated; an ancestor topic names the topic it comes
/// from, e.g. `testing [via test-timing]`.
fn facet_tags(rule: &RegisteredRule, facet: Facet) -> String {
    let mut labels = Vec::new();
    for branch in rule.branches() {
        let Some((leaf, ancestors)) = branch.split_last() else {
            continue;
        };
        if leaf.facet() != facet {
            continue;
        }
        labels.push(leaf.label().to_owned());
        labels.extend(
            ancestors
                .iter()
                .rev()
                .map(|ancestor| format!("{} [via {}]", ancestor.label(), leaf.label())),
        );
    }
    labels.join(", ")
}

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

    use super::*;
    use crate::rule_declaration::{Reference, RuleDoc};

    /// The style problems of one doc.
    fn doc_problems(doc: &RuleDoc) -> Vec<String> {
        let mut problems = Vec::new();
        let summary = doc.summary;
        if summary.contains('\n')
            || !summary.ends_with('.')
            || summary.trim_end_matches('.').contains(". ")
        {
            problems.push("`summary` must be one sentence on one line, ending with `.`".into());
        }
        let texts = [
            ("summary", summary),
            ("what_it_does", doc.what_it_does),
            ("why_is_this_bad", doc.why_is_this_bad),
        ]
        .into_iter()
        .chain(doc.known_problems.map(|text| ("known_problems", text)))
        .chain(doc.references.iter().flat_map(|reference| {
            [
                ("reference title", reference.title),
                ("reference url", reference.url),
            ]
        }));
        for (field, text) in texts {
            if text.trim().is_empty() {
                problems.push(format!("`{field}` is empty"));
            } else if text.trim() != text {
                problems.push(format!(
                    "`{field}` has leading or trailing whitespace; close the `indoc!` string on \
                     its last line"
                ));
            }
            if text.lines().any(|line| line.trim_start().starts_with('#')) {
                problems.push(format!(
                    "`{field}` has a `#` heading; the renderer owns headings"
                ));
            }
        }
        problems
    }

    #[test]
    fn documented_rules_follow_the_doc_style() {
        let problems: Vec<String> = registered_rules()
            .iter()
            .filter(|rule| !rule.doc.is_placeholder())
            .flat_map(|rule| {
                doc_problems(&rule.doc)
                    .into_iter()
                    .map(|problem| format!("{}: {problem}", rule.name))
            })
            .collect();
        assert!(problems.is_empty(), "{}", problems.join("\n"));
    }

    const GOOD: RuleDoc = RuleDoc {
        summary: "Flags things.",
        what_it_does: "Flags things when they happen.",
        why_is_this_bad: "Things are bad.",
        known_problems: None,
        references: &[Reference {
            title: "Title",
            url: "https://example.com",
        }],
        examples: &[],
    };

    #[rstest]
    #[case::good(GOOD, &[])]
    #[case::two_sentences(
        RuleDoc { summary: "Flags things. Also others.", ..GOOD },
        &["`summary` must be one sentence on one line, ending with `.`"]
    )]
    #[case::no_period(
        RuleDoc { summary: "Flags things", ..GOOD },
        &["`summary` must be one sentence on one line, ending with `.`"]
    )]
    #[case::empty_why(
        RuleDoc { why_is_this_bad: " ", ..GOOD },
        &["`why_is_this_bad` is empty"]
    )]
    #[case::heading(
        RuleDoc { why_is_this_bad: "Bad.\n\n## Example", ..GOOD },
        &["`why_is_this_bad` has a `#` heading; the renderer owns headings"]
    )]
    #[case::empty_known_problems(
        RuleDoc { known_problems: Some(" "), ..GOOD },
        &["`known_problems` is empty"]
    )]
    #[case::trailing_newline(
        RuleDoc { what_it_does: "Flags things.\n", ..GOOD },
        &["`what_it_does` has leading or trailing whitespace; close the `indoc!` string on its last line"]
    )]
    fn doc_problems_name_each_problem(#[case] doc: RuleDoc, #[case] expected: &[&str]) {
        assert_eq!(doc_problems(&doc), expected);
    }

    #[test]
    fn explain_rejects_unknown_rules() {
        assert_eq!(
            explain("testing").unwrap_err().to_string(),
            "unknown rule `testing`"
        );
    }

    fn configuration_toml_block(rendered: &str) -> Option<&str> {
        let (_, after_heading) = rendered.split_once("\n## Configuration\n")?;
        let section = after_heading.split("\n## ").next().unwrap_or(after_heading);
        let (_, after_fence) = section
            .split_once("\n```toml\n")
            .expect("`## Configuration` section must contain a ```toml block");
        let (block, _) = after_fence
            .split_once("\n```\n")
            .expect("```toml block must be closed");
        Some(block)
    }

    type ResolvedLanguageOptions = (
        crate::diagnostic::Language,
        EnforcementMode,
        Vec<usize>,
        Vec<std::collections::BTreeSet<String>>,
    );

    fn resolved_rule_options(
        rule: &RegisteredRule,
        overrides: Option<&crate::rule_declaration::RuleOverrides>,
    ) -> Option<Vec<ResolvedLanguageOptions>> {
        use crate::rule_declaration::OptionsDeclaration as _;

        if rule.options.keys().is_empty() {
            return None;
        }
        Some(
            rule.languages
                .iter()
                .map(|&language| {
                    let mode = rule.options.enforcement_mode(language, overrides);
                    let mut counts = Vec::new();
                    let mut lists = Vec::new();
                    for option in &rule.options.options {
                        match option {
                            OptionSpec::Count(count) => {
                                counts.push(count.resolve(language, overrides));
                            }
                            OptionSpec::List(list) => {
                                lists.push(list.resolve(language, overrides).into_iter().collect());
                            }
                        }
                    }
                    (language, mode, counts, lists)
                })
                .collect(),
        )
    }

    #[test]
    fn explain_toml_block_round_trips_to_default_options_for_every_rule() {
        use crate::rule_selection::parse_config;

        for rule in registered_rules() {
            let rendered = explain(rule.name.0).unwrap();
            let round_tripped = configuration_toml_block(&rendered).map(|toml_block| {
                let config = parse_config(toml_block).unwrap_or_else(|error| {
                    panic!(
                        "Rule {} rendered invalid TOML block:\n{toml_block}\nError: {error}",
                        rule.name
                    )
                });
                let overrides = config.rule_overrides.get(&rule.name).unwrap_or_else(|| {
                    panic!(
                        "Rule {} TOML block did not populate `rule_overrides`",
                        rule.name
                    )
                });
                resolved_rule_options(rule, Some(overrides)).unwrap()
            });
            assert_eq!(
                round_tripped,
                resolved_rule_options(rule, None),
                "Rule {} `--explain` TOML block did not round-trip to its declared defaults",
                rule.name
            );
        }
    }
}