Skip to main content

fallow_core/plugins/
used_export_rule.rs

1//! Used-export rules: the export names that a plugin marks as used in the
2//! files that a path rule matches.
3
4use super::{AggregatedPluginResult, PathRule, PathRuleIndex, PluginResult};
5
6impl PluginResult {
7    pub(super) fn push_used_export_rule(
8        &mut self,
9        pattern: impl Into<String>,
10        exports: impl IntoIterator<Item = impl Into<String>>,
11    ) {
12        self.used_exports
13            .push(UsedExportRule::new(pattern, exports));
14    }
15
16    /// Credit `exports` of the entry files that `pattern` matches, under
17    /// `includeEntryExports` only.
18    pub(super) fn push_entry_export_credit(
19        &mut self,
20        pattern: impl Into<String>,
21        exports: impl IntoIterator<Item = impl Into<String>>,
22    ) {
23        self.entry_export_credits
24            .push(UsedExportRule::new(pattern, exports));
25    }
26}
27
28/// A used-export rule bound to a file-pattern rule.
29#[derive(Debug, Clone, Default, PartialEq, Eq)]
30pub struct UsedExportRule {
31    pub(crate) path: PathRule,
32    pub(crate) exports: Vec<String>,
33}
34
35impl UsedExportRule {
36    #[must_use]
37    pub(crate) fn new(
38        pattern: impl Into<String>,
39        exports: impl IntoIterator<Item = impl Into<String>>,
40    ) -> Self {
41        Self {
42            path: PathRule::new(pattern),
43            exports: exports.into_iter().map(Into::into).collect(),
44        }
45    }
46
47    #[must_use]
48    pub(super) fn from_static(pattern: &'static str, exports: &'static [&'static str]) -> Self {
49        Self::new(pattern, exports.iter().copied())
50    }
51
52    #[must_use]
53    pub(super) fn with_excluded_globs<I, S>(mut self, patterns: I) -> Self
54    where
55        I: IntoIterator<Item = S>,
56        S: Into<String>,
57    {
58        self.path = self.path.with_excluded_globs(patterns);
59        self
60    }
61
62    #[must_use]
63    pub(super) fn with_excluded_regexes<I, S>(mut self, patterns: I) -> Self
64    where
65        I: IntoIterator<Item = S>,
66        S: Into<String>,
67    {
68        self.path = self.path.with_excluded_regexes(patterns);
69        self
70    }
71
72    #[must_use]
73    pub(super) fn with_excluded_segment_regexes<I, S>(mut self, patterns: I) -> Self
74    where
75        I: IntoIterator<Item = S>,
76        S: Into<String>,
77    {
78        self.path = self.path.with_excluded_segment_regexes(patterns);
79        self
80    }
81
82    #[must_use]
83    pub(super) fn prefixed(&self, ws_prefix: &str) -> Self {
84        Self {
85            path: self.path.prefixed(ws_prefix),
86            exports: self.exports.clone(),
87        }
88    }
89}
90
91/// A used-export rule tagged with the plugin that contributed it.
92#[derive(Debug, Clone, PartialEq, Eq)]
93pub struct PluginUsedExportRule {
94    pub(crate) plugin_name: String,
95    pub(crate) rule: UsedExportRule,
96}
97
98impl PluginUsedExportRule {
99    #[must_use]
100    pub(crate) fn new(plugin_name: impl Into<String>, rule: UsedExportRule) -> Self {
101        Self {
102            plugin_name: plugin_name.into(),
103            rule,
104        }
105    }
106
107    #[must_use]
108    pub(super) fn prefixed(&self, ws_prefix: &str) -> Self {
109        Self {
110            plugin_name: self.plugin_name.clone(),
111            rule: self.rule.prefixed(ws_prefix),
112        }
113    }
114}
115
116/// The export credit for the config files of active plugins.
117///
118/// A tool loads its config file and reads some of its exports. Under
119/// `includeEntryExports`, these exports count as used. The rules come from the
120/// config patterns of each active plugin (the `default` export) and from the
121/// anchored `config_file_exports` of each plugin (the export names that the
122/// tool reads, or `*` for every export).
123///
124/// The credit has its own matcher set. Without `includeEntryExports`, fallow
125/// skips an entry point as a whole, and the plugin `used_exports` matchers
126/// decide whether to analyse it. This credit never feeds that decision, so
127/// output without `includeEntryExports` does not change.
128pub struct ConfigExportCredit<'a> {
129    matchers: PathRuleIndex<&'a [String]>,
130}
131
132impl<'a> ConfigExportCredit<'a> {
133    /// Compile the credit. It is empty unless `include_entry_exports` is on.
134    pub fn new(
135        plugin_result: Option<&'a AggregatedPluginResult>,
136        include_entry_exports: bool,
137    ) -> Self {
138        let matchers = plugin_result
139            .filter(|_| include_entry_exports)
140            .map(|pr| {
141                PathRuleIndex::build(
142                    pr.config_export_credits
143                        .iter()
144                        .map(|rule| (&rule.rule.path, rule.rule.exports.as_slice())),
145                    "config export credit pattern",
146                )
147            })
148            .unwrap_or_default();
149        Self { matchers }
150    }
151
152    /// Whether no config pattern gets the credit.
153    pub fn is_empty(&self) -> bool {
154        self.matchers.is_empty()
155    }
156
157    /// The credited export names of each rule that matches the
158    /// project-relative `path`.
159    pub fn exports_for(&self, path: &str) -> Vec<&'a [String]> {
160        self.matchers.matching(path).into_iter().copied().collect()
161    }
162
163    /// Whether the export lists from [`Self::exports_for`] credit
164    /// `export_name`. The comparison is by name, so
165    /// `export { config as default }` matches `default`.
166    pub fn credits(credited: &[&[String]], export_name: &str) -> bool {
167        credited
168            .iter()
169            .any(|exports| exports.iter().any(|e| e == "*" || e == export_name))
170    }
171}
172
173/// Check whether an export name is covered by config ignore rules or plugin rules.
174pub fn is_export_ignored(
175    export_name: &str,
176    matching_ignore: &[&[String]],
177    matching_plugin: &[&[&str]],
178) -> bool {
179    matching_ignore
180        .iter()
181        .any(|exports| exports.iter().any(|e| e == "*" || e == export_name))
182        || matching_plugin
183            .iter()
184            .any(|exports| exports.contains(&"*") || exports.contains(&export_name))
185}