fallow_api/explain_examples.rs
1//! The example project of each rule: a small project that makes the rule
2//! report a finding.
3//!
4//! The files are in `crates/api/rule-examples/<rule>/`, where `<rule>` is the
5//! rule id without its namespace prefix:
6//!
7//! - `files/` holds the project. For a rule of the `audit` command, it holds
8//! the project after the change.
9//! - `base/` holds the project before the change. Only an `audit` rule has it.
10//! A file of `base/` that `files/` does not have stays as it is.
11//! - `example.json` is optional. `args` are the arguments after the command
12//! that the finding needs. `hidden` lists the files that a reader does not
13//! need to understand the finding, such as a plain `package.json`.
14//! `anchor` is `"file-level"` for a rule whose finding is on a whole file.
15//! Without it, the finding is on a line of the example code.
16//!
17//! `explain_examples_table.rs` embeds the files. The CLI integration test `rule_example_tests`
18//! runs fallow on each project and checks that the rule reports it, so an
19//! example cannot go stale.
20
21use crate::explain::{RuleDef, bare_rule_id};
22
23#[path = "explain_examples_table.rs"]
24mod table;
25
26/// One file of an example project.
27pub struct ExampleFile {
28 /// The path from the project root, with `/` separators.
29 pub path: &'static str,
30 /// The text of the file.
31 pub code: &'static str,
32 /// Whether the explain output leaves the file out. The file is still part
33 /// of the project that reports the finding.
34 pub hidden: bool,
35}
36
37/// Where the finding of an example is in its file.
38#[derive(Clone, Copy, Debug, PartialEq, Eq)]
39pub enum ExampleAnchor {
40 /// The finding is on the line of the code that causes it. The example
41 /// keeps that code away from line 1, column 0, so a location that a
42 /// format fills in for a missing span cannot pass as a real one.
43 Line,
44 /// The finding is on the whole file, such as an unused file or a route
45 /// file. A format reports no line, or line 1.
46 FileLevel,
47}
48
49/// The example project of one rule.
50pub struct RuleExample {
51 /// The rule id without its namespace prefix, such as `unused-export`.
52 pub rule: &'static str,
53 /// The arguments after the command that the finding needs.
54 pub args: &'static [&'static str],
55 /// Where the finding is in its file.
56 pub anchor: ExampleAnchor,
57 /// The project before the change. Empty unless the rule reports on a change.
58 pub base: &'static [ExampleFile],
59 /// The project. For a rule that reports on a change, the files after it.
60 pub files: &'static [ExampleFile],
61}
62
63/// Every example project, in order of rule id.
64#[must_use]
65pub fn rule_examples() -> &'static [RuleExample] {
66 table::RULE_EXAMPLES
67}
68
69/// The example project of `rule`. `None` when the rule needs data that a
70/// small project does not have, such as production runtime data.
71#[must_use]
72pub fn rule_example(rule: &RuleDef) -> Option<&'static RuleExample> {
73 let bare = bare_rule_id(rule);
74 rule_examples().iter().find(|example| example.rule == bare)
75}
76
77fn visible(files: &[ExampleFile]) -> Vec<fallow_output::ExplainExampleFile> {
78 files
79 .iter()
80 .filter(|file| !file.hidden)
81 .map(|file| fallow_output::ExplainExampleFile {
82 path: file.path.to_string(),
83 code: file.code.to_string(),
84 })
85 .collect()
86}
87
88/// The visible files of the example of `rule`: the project, then the project
89/// before the change. Both are empty when the rule has no example.
90pub(crate) fn visible_example_files(
91 rule: &RuleDef,
92) -> (
93 Vec<fallow_output::ExplainExampleFile>,
94 Vec<fallow_output::ExplainExampleFile>,
95) {
96 rule_example(rule).map_or_else(
97 || (Vec::new(), Vec::new()),
98 |example| (visible(example.files), visible(example.base)),
99 )
100}
101
102#[cfg(test)]
103#[path = "explain_examples_tests.rs"]
104mod tests;