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//!
15//! `explain_examples_table.rs` embeds the files. The CLI integration test `rule_example_tests`
16//! runs fallow on each project and checks that the rule reports it, so an
17//! example cannot go stale.
18
19use crate::explain::{RuleDef, bare_rule_id};
20
21#[path = "explain_examples_table.rs"]
22mod table;
23
24/// One file of an example project.
25pub struct ExampleFile {
26 /// The path from the project root, with `/` separators.
27 pub path: &'static str,
28 /// The text of the file.
29 pub code: &'static str,
30 /// Whether the explain output leaves the file out. The file is still part
31 /// of the project that reports the finding.
32 pub hidden: bool,
33}
34
35/// The example project of one rule.
36pub struct RuleExample {
37 /// The rule id without its namespace prefix, such as `unused-export`.
38 pub rule: &'static str,
39 /// The arguments after the command that the finding needs.
40 pub args: &'static [&'static str],
41 /// The project before the change. Empty unless the rule reports on a change.
42 pub base: &'static [ExampleFile],
43 /// The project. For a rule that reports on a change, the files after it.
44 pub files: &'static [ExampleFile],
45}
46
47/// Every example project, in order of rule id.
48#[must_use]
49pub fn rule_examples() -> &'static [RuleExample] {
50 table::RULE_EXAMPLES
51}
52
53/// The example project of `rule`. `None` when the rule needs data that a
54/// small project does not have, such as production runtime data.
55#[must_use]
56pub fn rule_example(rule: &RuleDef) -> Option<&'static RuleExample> {
57 let bare = bare_rule_id(rule);
58 rule_examples().iter().find(|example| example.rule == bare)
59}
60
61fn visible(files: &[ExampleFile]) -> Vec<fallow_output::ExplainExampleFile> {
62 files
63 .iter()
64 .filter(|file| !file.hidden)
65 .map(|file| fallow_output::ExplainExampleFile {
66 path: file.path.to_string(),
67 code: file.code.to_string(),
68 })
69 .collect()
70}
71
72/// The visible files of the example of `rule`: the project, then the project
73/// before the change. Both are empty when the rule has no example.
74pub(crate) fn visible_example_files(
75 rule: &RuleDef,
76) -> (
77 Vec<fallow_output::ExplainExampleFile>,
78 Vec<fallow_output::ExplainExampleFile>,
79) {
80 rule_example(rule).map_or_else(
81 || (Vec::new(), Vec::new()),
82 |example| (visible(example.files), visible(example.base)),
83 )
84}
85
86#[cfg(test)]
87#[path = "explain_examples_tests.rs"]
88mod tests;