Skip to main content

fallow_core/plugins/
used_export_names.rs

1//! A path lookup over the plugin used-export rules.
2
3use std::collections::BTreeSet;
4
5use super::{AggregatedPluginResult, PathRuleIndex};
6
7/// The plugin used-export rules of one plugin run, compiled for path lookups.
8///
9/// A framework plugin marks some exports of a file as used by the framework,
10/// for example `revalidate` in a Next.js page. The unused-export analysis
11/// reads the same rules with the same path matcher.
12#[derive(Debug, Clone)]
13pub struct UsedExportNames {
14    rules: PathRuleIndex<Vec<String>>,
15}
16
17impl UsedExportNames {
18    /// The export names that a plugin marks as used in the file at the
19    /// project-root-relative `rel_path`, with forward slashes. The name `*`
20    /// means each export of the file.
21    #[must_use]
22    pub fn names_for(&self, rel_path: &str) -> BTreeSet<&str> {
23        self.rules
24            .matching(rel_path)
25            .into_iter()
26            .flat_map(|exports| exports.iter().map(String::as_str))
27            .collect()
28    }
29}
30
31impl AggregatedPluginResult {
32    /// Compiles the used-export rules for path lookups. A rule with an
33    /// invalid glob matches no file, as in the unused-export analysis.
34    #[must_use]
35    pub fn used_export_names(&self) -> UsedExportNames {
36        UsedExportNames {
37            rules: PathRuleIndex::build(
38                self.used_exports
39                    .iter()
40                    .map(|rule| (&rule.rule.path, rule.rule.exports.clone())),
41                "used_exports pattern",
42            ),
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::super::{PluginUsedExportRule, UsedExportRule};
50    use super::*;
51
52    fn result(rules: Vec<PluginUsedExportRule>) -> AggregatedPluginResult {
53        AggregatedPluginResult {
54            used_exports: rules,
55            ..AggregatedPluginResult::default()
56        }
57    }
58
59    #[test]
60    fn names_for_a_path_join_each_matching_rule() {
61        let names = result(vec![
62            PluginUsedExportRule::new(
63                "nextjs",
64                UsedExportRule::new("app/**/page.{ts,tsx}", ["default", "revalidate"]),
65            ),
66            PluginUsedExportRule::new("nextjs", UsedExportRule::new("app/**/*.tsx", ["dynamic"])),
67            PluginUsedExportRule::new(
68                "tanstack-router",
69                UsedExportRule::new("src/routes/**/*.tsx", ["Route"]),
70            ),
71        ])
72        .used_export_names();
73
74        assert_eq!(
75            names.names_for("app/blog/page.tsx"),
76            BTreeSet::from(["default", "dynamic", "revalidate"])
77        );
78        assert_eq!(
79            names.names_for("src/routes/index.tsx"),
80            BTreeSet::from(["Route"])
81        );
82        assert!(names.names_for("src/lib/math.ts").is_empty());
83    }
84
85    #[test]
86    fn an_excluded_glob_and_an_invalid_glob_match_no_file() {
87        let names = result(vec![
88            PluginUsedExportRule::new(
89                "tanstack-router",
90                UsedExportRule::new("src/routes/**/*.tsx", ["Route"])
91                    .with_excluded_globs(["src/routes/**/-*.tsx"]),
92            ),
93            PluginUsedExportRule::new("broken", UsedExportRule::new("src/[", ["config"])),
94        ])
95        .used_export_names();
96
97        assert_eq!(
98            names.names_for("src/routes/posts.tsx"),
99            BTreeSet::from(["Route"])
100        );
101        assert!(names.names_for("src/routes/-helper.tsx").is_empty());
102        assert!(names.names_for("src/[").is_empty());
103    }
104}