Skip to main content

fallow_core/plugins/registry/
mod.rs

1//! Plugin registry: discovers active plugins, collects patterns, parses configs.
2
3use rustc_hash::FxHashSet;
4use std::path::{Path, PathBuf};
5
6use fallow_config::{EntryPointRole, ExternalPluginDef, PackageJson, UsedClassMemberRule};
7
8use super::{PathRule, Plugin, PluginUsedExportRule};
9
10pub(crate) mod builtin;
11mod helpers;
12
13use helpers::{
14    check_has_config_file, discover_config_files, process_config_result, process_external_plugins,
15    process_static_patterns,
16};
17
18fn must_parse_workspace_config_when_root_active(plugin_name: &str) -> bool {
19    matches!(
20        plugin_name,
21        "docusaurus" | "jest" | "tanstack-router" | "vitest"
22    )
23}
24
25/// Registry of all available plugins (built-in + external).
26pub struct PluginRegistry {
27    plugins: Vec<Box<dyn Plugin>>,
28    external_plugins: Vec<ExternalPluginDef>,
29}
30
31/// Aggregated results from all active plugins for a project.
32#[derive(Debug, Default)]
33pub struct AggregatedPluginResult {
34    /// All entry point patterns from active plugins: (rule, plugin_name).
35    pub entry_patterns: Vec<(PathRule, String)>,
36    /// Coverage role for each plugin contributing entry point patterns.
37    pub entry_point_roles: rustc_hash::FxHashMap<String, EntryPointRole>,
38    /// All config file patterns from active plugins.
39    pub config_patterns: Vec<String>,
40    /// All always-used file patterns from active plugins: (pattern, plugin_name).
41    pub always_used: Vec<(String, String)>,
42    /// All used export rules from active plugins.
43    pub used_exports: Vec<PluginUsedExportRule>,
44    /// Class member rules contributed by active plugins that should never be
45    /// flagged as unused. Extends the built-in Angular/React lifecycle allowlist
46    /// with framework-invoked method names, optionally scoped by class heritage.
47    pub used_class_members: Vec<UsedClassMemberRule>,
48    /// Dependencies referenced in config files (should not be flagged unused).
49    pub referenced_dependencies: Vec<String>,
50    /// Additional always-used files discovered from config parsing: (pattern, plugin_name).
51    pub discovered_always_used: Vec<(String, String)>,
52    /// Setup files discovered from config parsing: (path, plugin_name).
53    pub setup_files: Vec<(PathBuf, String)>,
54    /// Tooling dependencies (should not be flagged as unused devDeps).
55    pub tooling_dependencies: Vec<String>,
56    /// Package names discovered as used in package.json scripts (binary invocations).
57    pub script_used_packages: FxHashSet<String>,
58    /// Import prefixes for virtual modules provided by active frameworks.
59    /// Imports matching these prefixes should not be flagged as unlisted dependencies.
60    pub virtual_module_prefixes: Vec<String>,
61    /// Import suffixes for build-time generated relative imports.
62    /// Unresolved imports ending with these suffixes are suppressed.
63    pub generated_import_patterns: Vec<String>,
64    /// Path alias mappings from active plugins (prefix → replacement directory).
65    /// Used by the resolver to substitute import prefixes before re-resolving.
66    pub path_aliases: Vec<(String, String)>,
67    /// Names of active plugins.
68    pub active_plugins: Vec<String>,
69    /// Test fixture glob patterns from active plugins: (pattern, plugin_name).
70    pub fixture_patterns: Vec<(String, String)>,
71    /// Absolute directories contributed by plugins that should be searched
72    /// when resolving SCSS/Sass `@import`/`@use` specifiers. Populated from
73    /// Angular's `stylePreprocessorOptions.includePaths` and equivalent
74    /// framework settings. See issue #103.
75    pub scss_include_paths: Vec<PathBuf>,
76}
77
78impl PluginRegistry {
79    /// Create a registry with all built-in plugins and optional external plugins.
80    #[must_use]
81    pub fn new(external: Vec<ExternalPluginDef>) -> Self {
82        Self {
83            plugins: builtin::create_builtin_plugins(),
84            external_plugins: external,
85        }
86    }
87
88    /// Run all plugins against a project, returning aggregated results.
89    ///
90    /// This discovers which plugins are active, collects their static patterns,
91    /// then parses any config files to extract dynamic information.
92    pub fn run(
93        &self,
94        pkg: &PackageJson,
95        root: &Path,
96        discovered_files: &[PathBuf],
97    ) -> AggregatedPluginResult {
98        self.run_with_search_roots(pkg, root, discovered_files, &[root])
99    }
100
101    /// Run all plugins against a project with explicit config-file search roots.
102    ///
103    /// `config_search_roots` should stay narrowly focused to directories that are
104    /// already known to matter for this project. Broad recursive scans are
105    /// intentionally avoided because they become prohibitively expensive on
106    /// large monorepos with populated `node_modules` trees.
107    pub fn run_with_search_roots(
108        &self,
109        pkg: &PackageJson,
110        root: &Path,
111        discovered_files: &[PathBuf],
112        config_search_roots: &[&Path],
113    ) -> AggregatedPluginResult {
114        let _span = tracing::info_span!("run_plugins").entered();
115        let mut result = AggregatedPluginResult::default();
116
117        // Phase 1: Determine which plugins are active
118        // Compute deps once to avoid repeated Vec<String> allocation per plugin
119        let all_deps = pkg.all_dependency_names();
120        let active: Vec<&dyn Plugin> = self
121            .plugins
122            .iter()
123            .filter(|p| p.is_enabled_with_deps(&all_deps, root))
124            .map(AsRef::as_ref)
125            .collect();
126
127        tracing::info!(
128            plugins = active
129                .iter()
130                .map(|p| p.name())
131                .collect::<Vec<_>>()
132                .join(", "),
133            "active plugins"
134        );
135
136        // Warn when meta-frameworks are active but their generated configs are missing.
137        // Without these, tsconfig extends chains break and import resolution fails.
138        check_meta_framework_prerequisites(&active, root);
139
140        // Phase 2: Collect static patterns from active plugins
141        for plugin in &active {
142            process_static_patterns(*plugin, root, &mut result);
143        }
144
145        // Phase 2b: Process external plugins (includes inline framework definitions)
146        process_external_plugins(
147            &self.external_plugins,
148            &all_deps,
149            root,
150            discovered_files,
151            &mut result,
152        );
153
154        // Phase 3: Find and parse config files for dynamic resolution
155        // Pre-compile all config patterns
156        let config_matchers: Vec<(&dyn Plugin, Vec<globset::GlobMatcher>)> = active
157            .iter()
158            .filter(|p| !p.config_patterns().is_empty())
159            .map(|p| {
160                let matchers: Vec<globset::GlobMatcher> = p
161                    .config_patterns()
162                    .iter()
163                    .filter_map(|pat| globset::Glob::new(pat).ok().map(|g| g.compile_matcher()))
164                    .collect();
165                (*p, matchers)
166            })
167            .collect();
168
169        // Build relative paths lazily: only needed when config matchers exist
170        // or plugins have package_json_config_key. Skip entirely for projects
171        // with no config-parsing plugins (e.g., only React), avoiding O(files)
172        // String allocations.
173        let needs_relative_files = !config_matchers.is_empty()
174            || active.iter().any(|p| p.package_json_config_key().is_some());
175        let relative_files: Vec<(PathBuf, String)> = if needs_relative_files {
176            discovered_files
177                .iter()
178                .map(|f| {
179                    let rel = f
180                        .strip_prefix(root)
181                        .unwrap_or(f)
182                        .to_string_lossy()
183                        .into_owned();
184                    (f.clone(), rel)
185                })
186                .collect()
187        } else {
188            Vec::new()
189        };
190
191        if !config_matchers.is_empty() {
192            // Phase 3a: Match config files from discovered source files
193            let mut resolved_plugins: FxHashSet<&str> = FxHashSet::default();
194
195            for (plugin, matchers) in &config_matchers {
196                for (abs_path, rel_path) in &relative_files {
197                    if matchers.iter().any(|m| m.is_match(rel_path.as_str()))
198                        && let Ok(source) = std::fs::read_to_string(abs_path)
199                    {
200                        let plugin_result = plugin.resolve_config(abs_path, &source, root);
201                        if !plugin_result.is_empty() {
202                            resolved_plugins.insert(plugin.name());
203                            tracing::debug!(
204                                plugin = plugin.name(),
205                                config = rel_path.as_str(),
206                                entries = plugin_result.entry_patterns.len(),
207                                deps = plugin_result.referenced_dependencies.len(),
208                                "resolved config"
209                            );
210                            process_config_result(plugin.name(), plugin_result, &mut result);
211                        }
212                    }
213                }
214            }
215
216            // Phase 3b: Filesystem fallback for JSON config files.
217            // JSON files (angular.json, project.json) are not in the discovered file set
218            // because fallow only discovers JS/TS/CSS/Vue/etc. files.
219            let json_configs =
220                discover_config_files(&config_matchers, &resolved_plugins, config_search_roots);
221            for (abs_path, plugin) in &json_configs {
222                if let Ok(source) = std::fs::read_to_string(abs_path) {
223                    let plugin_result = plugin.resolve_config(abs_path, &source, root);
224                    if !plugin_result.is_empty() {
225                        let rel = abs_path
226                            .strip_prefix(root)
227                            .map(|p| p.to_string_lossy())
228                            .unwrap_or_default();
229                        tracing::debug!(
230                            plugin = plugin.name(),
231                            config = %rel,
232                            entries = plugin_result.entry_patterns.len(),
233                            deps = plugin_result.referenced_dependencies.len(),
234                            "resolved config (filesystem fallback)"
235                        );
236                        process_config_result(plugin.name(), plugin_result, &mut result);
237                    }
238                }
239            }
240        }
241
242        // Phase 4: Package.json inline config fallback
243        // For plugins that define `package_json_config_key()`, check if the root
244        // package.json contains that key and no standalone config file was found.
245        for plugin in &active {
246            if let Some(key) = plugin.package_json_config_key()
247                && !check_has_config_file(*plugin, &config_matchers, &relative_files)
248            {
249                // Try to extract the key from package.json
250                let pkg_path = root.join("package.json");
251                if let Ok(content) = std::fs::read_to_string(&pkg_path)
252                    && let Ok(json) = serde_json::from_str::<serde_json::Value>(&content)
253                    && let Some(config_value) = json.get(key)
254                {
255                    let config_json = serde_json::to_string(config_value).unwrap_or_default();
256                    let fake_path = root.join(format!("{key}.config.json"));
257                    let plugin_result = plugin.resolve_config(&fake_path, &config_json, root);
258                    if !plugin_result.is_empty() {
259                        tracing::debug!(
260                            plugin = plugin.name(),
261                            key = key,
262                            "resolved inline package.json config"
263                        );
264                        process_config_result(plugin.name(), plugin_result, &mut result);
265                    }
266                }
267            }
268        }
269
270        result
271    }
272
273    /// Fast variant of `run()` for workspace packages.
274    ///
275    /// Reuses pre-compiled config matchers and pre-computed relative files from the root
276    /// project run, avoiding repeated glob compilation and path computation per workspace.
277    /// Skips external plugins (they only activate at root level) and package.json inline
278    /// config (workspace packages rarely have inline configs).
279    pub fn run_workspace_fast(
280        &self,
281        pkg: &PackageJson,
282        root: &Path,
283        project_root: &Path,
284        precompiled_config_matchers: &[(&dyn Plugin, Vec<globset::GlobMatcher>)],
285        relative_files: &[(PathBuf, String)],
286        skip_config_plugins: &FxHashSet<&str>,
287    ) -> AggregatedPluginResult {
288        let _span = tracing::info_span!("run_plugins").entered();
289        let mut result = AggregatedPluginResult::default();
290
291        // Phase 1: Determine which plugins are active (with pre-computed deps)
292        let all_deps = pkg.all_dependency_names();
293        let active: Vec<&dyn Plugin> = self
294            .plugins
295            .iter()
296            .filter(|p| p.is_enabled_with_deps(&all_deps, root))
297            .map(AsRef::as_ref)
298            .collect();
299
300        tracing::info!(
301            plugins = active
302                .iter()
303                .map(|p| p.name())
304                .collect::<Vec<_>>()
305                .join(", "),
306            "active plugins"
307        );
308
309        // Early exit if no plugins are active (common for leaf workspace packages)
310        if active.is_empty() {
311            return result;
312        }
313
314        // Phase 2: Collect static patterns from active plugins
315        for plugin in &active {
316            process_static_patterns(*plugin, root, &mut result);
317        }
318
319        // Phase 3: Find and parse config files using pre-compiled matchers
320        // Only check matchers for plugins that are active in this workspace
321        let active_names: FxHashSet<&str> = active.iter().map(|p| p.name()).collect();
322        let workspace_matchers: Vec<_> = precompiled_config_matchers
323            .iter()
324            .filter(|(p, _)| {
325                active_names.contains(p.name())
326                    && (!skip_config_plugins.contains(p.name())
327                        || must_parse_workspace_config_when_root_active(p.name()))
328            })
329            .map(|(plugin, matchers)| (*plugin, matchers.clone()))
330            .collect();
331
332        let mut resolved_ws_plugins: FxHashSet<&str> = FxHashSet::default();
333        if !workspace_matchers.is_empty() {
334            for (plugin, matchers) in &workspace_matchers {
335                for (abs_path, rel_path) in relative_files {
336                    if matchers.iter().any(|m| m.is_match(rel_path.as_str()))
337                        && let Ok(source) = std::fs::read_to_string(abs_path)
338                    {
339                        let plugin_result = plugin.resolve_config(abs_path, &source, root);
340                        if !plugin_result.is_empty() {
341                            resolved_ws_plugins.insert(plugin.name());
342                            tracing::debug!(
343                                plugin = plugin.name(),
344                                config = rel_path.as_str(),
345                                entries = plugin_result.entry_patterns.len(),
346                                deps = plugin_result.referenced_dependencies.len(),
347                                "resolved config"
348                            );
349                            process_config_result(plugin.name(), plugin_result, &mut result);
350                        }
351                    }
352                }
353            }
354        }
355
356        // Phase 3b: Filesystem fallback for JSON config files at the project root.
357        // Config files like angular.json live at the monorepo root, but Angular is
358        // only active in workspace packages. Check the project root for unresolved
359        // config patterns.
360        let ws_json_configs = if root == project_root {
361            discover_config_files(&workspace_matchers, &resolved_ws_plugins, &[root])
362        } else {
363            discover_config_files(
364                &workspace_matchers,
365                &resolved_ws_plugins,
366                &[root, project_root],
367            )
368        };
369        // Parse discovered JSON config files
370        for (abs_path, plugin) in &ws_json_configs {
371            if let Ok(source) = std::fs::read_to_string(abs_path) {
372                let plugin_result = plugin.resolve_config(abs_path, &source, root);
373                if !plugin_result.is_empty() {
374                    let rel = abs_path
375                        .strip_prefix(project_root)
376                        .map(|p| p.to_string_lossy())
377                        .unwrap_or_default();
378                    tracing::debug!(
379                        plugin = plugin.name(),
380                        config = %rel,
381                        entries = plugin_result.entry_patterns.len(),
382                        deps = plugin_result.referenced_dependencies.len(),
383                        "resolved config (workspace filesystem fallback)"
384                    );
385                    process_config_result(plugin.name(), plugin_result, &mut result);
386                }
387            }
388        }
389
390        result
391    }
392
393    /// Pre-compile config pattern glob matchers for all plugins that have config patterns.
394    /// Returns a vec of (plugin, matchers) pairs that can be reused across multiple `run_workspace_fast` calls.
395    #[must_use]
396    pub fn precompile_config_matchers(&self) -> Vec<(&dyn Plugin, Vec<globset::GlobMatcher>)> {
397        self.plugins
398            .iter()
399            .filter(|p| !p.config_patterns().is_empty())
400            .map(|p| {
401                let matchers: Vec<globset::GlobMatcher> = p
402                    .config_patterns()
403                    .iter()
404                    .filter_map(|pat| globset::Glob::new(pat).ok().map(|g| g.compile_matcher()))
405                    .collect();
406                (p.as_ref(), matchers)
407            })
408            .collect()
409    }
410}
411
412impl Default for PluginRegistry {
413    fn default() -> Self {
414        Self::new(vec![])
415    }
416}
417
418/// Warn when meta-frameworks are active but their generated configs are missing.
419///
420/// Meta-frameworks like Nuxt and Astro generate tsconfig/types files during a
421/// "prepare" step. Without these, the tsconfig extends chain breaks and
422/// extensionless imports fail wholesale (e.g. 2000+ unresolved imports).
423fn check_meta_framework_prerequisites(active_plugins: &[&dyn Plugin], root: &Path) {
424    for plugin in active_plugins {
425        match plugin.name() {
426            "nuxt" if !root.join(".nuxt/tsconfig.json").exists() => {
427                tracing::warn!(
428                    "Nuxt project missing .nuxt/tsconfig.json: run `nuxt prepare` \
429                     before fallow for accurate analysis"
430                );
431            }
432            "astro" if !root.join(".astro").exists() => {
433                tracing::warn!(
434                    "Astro project missing .astro/ types: run `astro sync` \
435                     before fallow for accurate analysis"
436                );
437            }
438            _ => {}
439        }
440    }
441}
442
443#[cfg(test)]
444mod tests;