Skip to main content

fallow_core/plugins/
mod.rs

1//! Plugin system for framework-aware codebase analysis.
2//!
3//! Unlike knip's JavaScript plugin system that evaluates config files at runtime,
4//! fallow's plugin system uses Oxc's parser to extract configuration values from
5//! JS/TS/JSON config files via AST walking, no JavaScript evaluation needed.
6//!
7//! Each plugin implements the [`Plugin`] trait with:
8//! - **Static defaults**: Entry patterns, config file patterns, used exports
9//! - **Dynamic resolution**: Parse tool config files to discover additional entries,
10//!   referenced dependencies, and setup files
11
12use std::path::{Path, PathBuf};
13
14use fallow_config::{AutoImportRule, EntryPointRole, PackageJson, UsedClassMemberRule};
15use regex::Regex;
16
17const TEST_ENTRY_POINT_PLUGINS: &[&str] = &[
18    "ava",
19    "bun",
20    "cucumber",
21    "cypress",
22    "jest",
23    "k6",
24    "mocha",
25    "playwright",
26    "tap",
27    "tsd",
28    "vitest",
29    "webdriverio",
30];
31
32const RUNTIME_ENTRY_POINT_PLUGINS: &[&str] = &[
33    "adonis",
34    "angular",
35    "astro",
36    "browser-extension",
37    "convex",
38    "docusaurus",
39    "electron",
40    "ember",
41    "expo",
42    "expo-router",
43    "gatsby",
44    "hardhat",
45    "nestjs",
46    "next-intl",
47    "nextjs",
48    "nitro",
49    "nuxt",
50    "obsidian",
51    "parcel",
52    "qwik",
53    "react-native",
54    "react-router",
55    "redwoodsdk",
56    "remix",
57    "rolldown",
58    "rollup",
59    "rsbuild",
60    "rspack",
61    "sanity",
62    "supabase",
63    "sveltekit",
64    "tanstack-router",
65    "tsdown",
66    "tsup",
67    "vite",
68    "vitepress",
69    "webpack",
70    "wrangler",
71    "wxt",
72];
73
74#[cfg(test)]
75const SUPPORT_ENTRY_POINT_PLUGINS: &[&str] = &[
76    "content-collections",
77    "contentlayer",
78    "danger",
79    "drizzle",
80    "fumadocs",
81    "i18next",
82    "knex",
83    "kysely",
84    "mintlify",
85    "msw",
86    "opencode",
87    "prisma",
88    "storybook",
89    "stryker",
90    "typeorm",
91    "velite",
92];
93
94/// Result of resolving a plugin's config file.
95#[derive(Debug, Default)]
96pub struct PluginResult {
97    /// Additional entry point glob patterns discovered from config.
98    pub entry_patterns: Vec<PathRule>,
99    /// When true, `entry_patterns` from config replace the plugin's static
100    /// `entry_patterns()` defaults instead of adding to them. Tools like Vitest
101    /// and Jest treat their config's include/testMatch as a replacement for built-in
102    /// defaults, so when the config is explicit the static patterns must be dropped.
103    pub replace_entry_patterns: bool,
104    /// When true, `used_exports` from config replace the plugin's static
105    /// `used_export_rules()` defaults instead of adding to them.
106    pub replace_used_export_rules: bool,
107    /// Additional export-usage rules discovered from config.
108    pub used_exports: Vec<UsedExportRule>,
109    /// Class member rules that should never be flagged as unused. Contributed
110    /// by plugins that know their framework invokes these methods at runtime
111    /// and may scope suppression via `extends` / `implements` constraints when
112    /// the method name is too common to allowlist globally.
113    pub used_class_members: Vec<UsedClassMemberRule>,
114    /// Dependencies referenced in config files (should not be flagged as unused).
115    pub referenced_dependencies: Vec<String>,
116    /// Additional files that are always considered used.
117    pub always_used_files: Vec<String>,
118    /// Path alias mappings discovered from config (prefix -> replacement directory).
119    pub path_aliases: Vec<(String, String)>,
120    /// Setup/helper files referenced from config.
121    pub setup_files: Vec<PathBuf>,
122    /// Test fixture glob patterns discovered from config.
123    pub fixture_patterns: Vec<String>,
124    /// Absolute directories to include when resolving SCSS/Sass `@import` and
125    /// `@use` specifiers. Contributed by framework plugins that read their
126    /// tool's equivalent of `includePaths` (e.g. Angular's
127    /// `stylePreprocessorOptions.includePaths` from `angular.json` /
128    /// `project.json`). Bare SCSS specifiers that fail to resolve relative to
129    /// the importing file retry against each include path using the SCSS
130    /// partial / directory-index conventions.
131    pub scss_include_paths: Vec<PathBuf>,
132    /// URL-to-filesystem static directory mappings discovered from tool config.
133    /// Each tuple is `(absolute_source_dir, normalized_url_mount)`.
134    pub static_dir_mappings: Vec<(PathBuf, String)>,
135    /// File-scoped dependency providers. Matching imports are considered
136    /// available from the framework runtime and are not unlisted dependencies.
137    pub provided_dependencies: Vec<ProvidedDependencyRule>,
138}
139
140impl PluginResult {
141    pub fn push_entry_pattern(&mut self, pattern: impl Into<String>) {
142        self.entry_patterns
143            .push(PathRule::new(normalize_entry_pattern(pattern.into())));
144    }
145
146    pub fn extend_entry_patterns<I, S>(&mut self, patterns: I)
147    where
148        I: IntoIterator<Item = S>,
149        S: Into<String>,
150    {
151        self.entry_patterns.extend(
152            patterns
153                .into_iter()
154                .map(|pat| PathRule::new(normalize_entry_pattern(pat.into()))),
155        );
156    }
157
158    pub fn push_used_export_rule(
159        &mut self,
160        pattern: impl Into<String>,
161        exports: impl IntoIterator<Item = impl Into<String>>,
162    ) {
163        self.used_exports
164            .push(UsedExportRule::new(pattern, exports));
165    }
166
167    #[must_use]
168    pub const fn is_empty(&self) -> bool {
169        self.entry_patterns.is_empty()
170            && self.used_exports.is_empty()
171            && self.used_class_members.is_empty()
172            && self.referenced_dependencies.is_empty()
173            && self.always_used_files.is_empty()
174            && self.path_aliases.is_empty()
175            && self.setup_files.is_empty()
176            && self.fixture_patterns.is_empty()
177            && self.scss_include_paths.is_empty()
178            && self.static_dir_mappings.is_empty()
179            && self.provided_dependencies.is_empty()
180    }
181}
182
183fn normalize_entry_pattern(pattern: String) -> String {
184    pattern
185        .strip_prefix("./")
186        .map(str::to_owned)
187        .unwrap_or(pattern)
188}
189
190/// A file-pattern rule with optional exclusion globs plus path-level or
191/// segment-level regex filters.
192///
193/// Exclusion regexes are matched against the project-relative path and should be
194/// anchored when generated dynamically so they can be safely workspace-prefixed.
195#[derive(Debug, Clone, Default, PartialEq, Eq)]
196pub struct PathRule {
197    pub pattern: String,
198    pub exclude_globs: Vec<String>,
199    pub exclude_regexes: Vec<String>,
200    /// Regexes matched against individual path segments. These are not prefixed
201    /// for workspaces because they intentionally operate on segment names rather
202    /// than the full project-relative path.
203    pub exclude_segment_regexes: Vec<String>,
204}
205
206impl PathRule {
207    #[must_use]
208    pub fn new(pattern: impl Into<String>) -> Self {
209        Self {
210            pattern: pattern.into(),
211            exclude_globs: Vec::new(),
212            exclude_regexes: Vec::new(),
213            exclude_segment_regexes: Vec::new(),
214        }
215    }
216
217    #[must_use]
218    pub fn from_static(pattern: &'static str) -> Self {
219        Self::new(pattern)
220    }
221
222    #[must_use]
223    pub fn with_excluded_globs<I, S>(mut self, patterns: I) -> Self
224    where
225        I: IntoIterator<Item = S>,
226        S: Into<String>,
227    {
228        self.exclude_globs
229            .extend(patterns.into_iter().map(Into::into));
230        self
231    }
232
233    #[must_use]
234    pub fn with_excluded_regexes<I, S>(mut self, patterns: I) -> Self
235    where
236        I: IntoIterator<Item = S>,
237        S: Into<String>,
238    {
239        self.exclude_regexes
240            .extend(patterns.into_iter().map(Into::into));
241        self
242    }
243
244    #[must_use]
245    pub fn with_excluded_segment_regexes<I, S>(mut self, patterns: I) -> Self
246    where
247        I: IntoIterator<Item = S>,
248        S: Into<String>,
249    {
250        self.exclude_segment_regexes
251            .extend(patterns.into_iter().map(Into::into));
252        self
253    }
254
255    #[must_use]
256    pub fn prefixed(&self, ws_prefix: &str) -> Self {
257        Self {
258            pattern: prefix_workspace_pattern(&self.pattern, ws_prefix),
259            exclude_globs: self
260                .exclude_globs
261                .iter()
262                .map(|pattern| prefix_workspace_pattern(pattern, ws_prefix))
263                .collect(),
264            exclude_regexes: self
265                .exclude_regexes
266                .iter()
267                .map(|pattern| prefix_workspace_regex(pattern, ws_prefix))
268                .collect(),
269            exclude_segment_regexes: self.exclude_segment_regexes.clone(),
270        }
271    }
272}
273
274/// A used-export rule bound to a file-pattern rule.
275#[derive(Debug, Clone, Default, PartialEq, Eq)]
276pub struct UsedExportRule {
277    pub path: PathRule,
278    pub exports: Vec<String>,
279}
280
281impl UsedExportRule {
282    #[must_use]
283    pub fn new(
284        pattern: impl Into<String>,
285        exports: impl IntoIterator<Item = impl Into<String>>,
286    ) -> Self {
287        Self {
288            path: PathRule::new(pattern),
289            exports: exports.into_iter().map(Into::into).collect(),
290        }
291    }
292
293    #[must_use]
294    pub fn from_static(pattern: &'static str, exports: &'static [&'static str]) -> Self {
295        Self::new(pattern, exports.iter().copied())
296    }
297
298    #[must_use]
299    pub fn with_excluded_globs<I, S>(mut self, patterns: I) -> Self
300    where
301        I: IntoIterator<Item = S>,
302        S: Into<String>,
303    {
304        self.path = self.path.with_excluded_globs(patterns);
305        self
306    }
307
308    #[must_use]
309    pub fn with_excluded_regexes<I, S>(mut self, patterns: I) -> Self
310    where
311        I: IntoIterator<Item = S>,
312        S: Into<String>,
313    {
314        self.path = self.path.with_excluded_regexes(patterns);
315        self
316    }
317
318    #[must_use]
319    pub fn with_excluded_segment_regexes<I, S>(mut self, patterns: I) -> Self
320    where
321        I: IntoIterator<Item = S>,
322        S: Into<String>,
323    {
324        self.path = self.path.with_excluded_segment_regexes(patterns);
325        self
326    }
327
328    #[must_use]
329    pub fn prefixed(&self, ws_prefix: &str) -> Self {
330        Self {
331            path: self.path.prefixed(ws_prefix),
332            exports: self.exports.clone(),
333        }
334    }
335}
336
337/// A used-export rule tagged with the plugin that contributed it.
338#[derive(Debug, Clone, PartialEq, Eq)]
339pub struct PluginUsedExportRule {
340    pub plugin_name: String,
341    pub rule: UsedExportRule,
342}
343
344impl PluginUsedExportRule {
345    #[must_use]
346    pub fn new(plugin_name: impl Into<String>, rule: UsedExportRule) -> Self {
347        Self {
348            plugin_name: plugin_name.into(),
349            rule,
350        }
351    }
352
353    #[must_use]
354    pub fn prefixed(&self, ws_prefix: &str) -> Self {
355        Self {
356            plugin_name: self.plugin_name.clone(),
357            rule: self.rule.prefixed(ws_prefix),
358        }
359    }
360}
361
362/// A file-scoped dependency provider rule contributed by a framework plugin.
363#[derive(Debug, Clone, Default, PartialEq, Eq)]
364pub struct ProvidedDependencyRule {
365    pub path: PathRule,
366    pub exact_specifiers: Vec<String>,
367    pub specifier_prefixes: Vec<String>,
368}
369
370impl ProvidedDependencyRule {
371    #[must_use]
372    pub fn new(
373        pattern: impl Into<String>,
374        exact_specifiers: impl IntoIterator<Item = impl Into<String>>,
375        specifier_prefixes: impl IntoIterator<Item = impl Into<String>>,
376    ) -> Self {
377        Self {
378            path: PathRule::new(pattern),
379            exact_specifiers: exact_specifiers.into_iter().map(Into::into).collect(),
380            specifier_prefixes: specifier_prefixes.into_iter().map(Into::into).collect(),
381        }
382    }
383
384    #[must_use]
385    pub fn prefixed(&self, ws_prefix: &str) -> Self {
386        Self {
387            path: self.path.prefixed(ws_prefix),
388            exact_specifiers: self.exact_specifiers.clone(),
389            specifier_prefixes: self.specifier_prefixes.clone(),
390        }
391    }
392
393    #[must_use]
394    pub fn may_cover_package(&self, package_name: &str) -> bool {
395        self.exact_specifiers
396            .iter()
397            .chain(self.specifier_prefixes.iter())
398            .any(|specifier| crate::resolve::extract_package_name(specifier) == package_name)
399    }
400
401    #[must_use]
402    pub fn covers_specifier(&self, specifier: &str) -> bool {
403        self.exact_specifiers
404            .iter()
405            .any(|allowed| allowed == specifier)
406            || self
407                .specifier_prefixes
408                .iter()
409                .any(|prefix| specifier.starts_with(prefix))
410    }
411}
412
413/// A compiled path rule matcher shared by entry-point and used-export matching.
414#[derive(Debug, Clone)]
415pub(crate) struct CompiledPathRule {
416    include: globset::GlobMatcher,
417    exclude_globs: Vec<globset::GlobMatcher>,
418    exclude_regexes: Vec<Regex>,
419    exclude_segment_regexes: Vec<Regex>,
420}
421
422impl CompiledPathRule {
423    pub(crate) fn for_entry_rule(rule: &PathRule, rule_kind: &str) -> Option<Self> {
424        let include = match globset::GlobBuilder::new(&rule.pattern)
425            .literal_separator(true)
426            .build()
427        {
428            Ok(glob) => glob.compile_matcher(),
429            Err(err) => {
430                tracing::warn!("invalid {rule_kind} '{}': {err}", rule.pattern);
431                return None;
432            }
433        };
434        Some(Self {
435            include,
436            exclude_globs: compile_excluded_globs(&rule.exclude_globs, rule_kind, &rule.pattern),
437            exclude_regexes: compile_excluded_regexes(
438                &rule.exclude_regexes,
439                rule_kind,
440                &rule.pattern,
441            ),
442            exclude_segment_regexes: compile_excluded_segment_regexes(
443                &rule.exclude_segment_regexes,
444                rule_kind,
445                &rule.pattern,
446            ),
447        })
448    }
449
450    pub(crate) fn for_used_export_rule(rule: &PathRule, rule_kind: &str) -> Option<Self> {
451        let include = match globset::Glob::new(&rule.pattern) {
452            Ok(glob) => glob.compile_matcher(),
453            Err(err) => {
454                tracing::warn!("invalid {rule_kind} '{}': {err}", rule.pattern);
455                return None;
456            }
457        };
458        Some(Self {
459            include,
460            exclude_globs: compile_excluded_globs(&rule.exclude_globs, rule_kind, &rule.pattern),
461            exclude_regexes: compile_excluded_regexes(
462                &rule.exclude_regexes,
463                rule_kind,
464                &rule.pattern,
465            ),
466            exclude_segment_regexes: compile_excluded_segment_regexes(
467                &rule.exclude_segment_regexes,
468                rule_kind,
469                &rule.pattern,
470            ),
471        })
472    }
473
474    #[must_use]
475    pub(crate) fn matches(&self, path: &str) -> bool {
476        self.include.is_match(path)
477            && !self.exclude_globs.iter().any(|glob| glob.is_match(path))
478            && !self
479                .exclude_regexes
480                .iter()
481                .any(|regex| regex.is_match(path))
482            && !matches_segment_regex(path, &self.exclude_segment_regexes)
483    }
484}
485
486fn prefix_workspace_pattern(pattern: &str, ws_prefix: &str) -> String {
487    if pattern.starts_with(ws_prefix) || pattern.starts_with('/') {
488        pattern.to_string()
489    } else {
490        format!("{ws_prefix}/{pattern}")
491    }
492}
493
494fn prefix_workspace_regex(pattern: &str, ws_prefix: &str) -> String {
495    if let Some(pattern) = pattern.strip_prefix('^') {
496        format!("^{}/{}", regex::escape(ws_prefix), pattern)
497    } else {
498        format!("^{}/(?:{})", regex::escape(ws_prefix), pattern)
499    }
500}
501
502fn compile_excluded_globs(
503    patterns: &[String],
504    rule_kind: &str,
505    rule_pattern: &str,
506) -> Vec<globset::GlobMatcher> {
507    patterns
508        .iter()
509        .filter_map(|pattern| {
510            match globset::GlobBuilder::new(pattern)
511                .literal_separator(true)
512                .build()
513            {
514                Ok(glob) => Some(glob.compile_matcher()),
515                Err(err) => {
516                    tracing::warn!(
517                        "skipping invalid excluded glob '{}' for {} '{}': {err}",
518                        pattern,
519                        rule_kind,
520                        rule_pattern
521                    );
522                    None
523                }
524            }
525        })
526        .collect()
527}
528
529fn compile_excluded_regexes(
530    patterns: &[String],
531    rule_kind: &str,
532    rule_pattern: &str,
533) -> Vec<Regex> {
534    patterns
535        .iter()
536        .filter_map(|pattern| match Regex::new(pattern) {
537            Ok(regex) => Some(regex),
538            Err(err) => {
539                tracing::warn!(
540                    "skipping invalid excluded regex '{}' for {} '{}': {err}",
541                    pattern,
542                    rule_kind,
543                    rule_pattern
544                );
545                None
546            }
547        })
548        .collect()
549}
550
551fn compile_excluded_segment_regexes(
552    patterns: &[String],
553    rule_kind: &str,
554    rule_pattern: &str,
555) -> Vec<Regex> {
556    patterns
557        .iter()
558        .filter_map(|pattern| match Regex::new(pattern) {
559            Ok(regex) => Some(regex),
560            Err(err) => {
561                tracing::warn!(
562                    "skipping invalid excluded segment regex '{}' for {} '{}': {err}",
563                    pattern,
564                    rule_kind,
565                    rule_pattern
566                );
567                None
568            }
569        })
570        .collect()
571}
572
573fn matches_segment_regex(path: &str, regexes: &[Regex]) -> bool {
574    path.split('/')
575        .any(|segment| regexes.iter().any(|regex| regex.is_match(segment)))
576}
577
578impl From<String> for PathRule {
579    fn from(pattern: String) -> Self {
580        Self::new(pattern)
581    }
582}
583
584impl From<&str> for PathRule {
585    fn from(pattern: &str) -> Self {
586        Self::new(pattern)
587    }
588}
589
590impl std::ops::Deref for PathRule {
591    type Target = str;
592
593    fn deref(&self) -> &Self::Target {
594        &self.pattern
595    }
596}
597
598impl PartialEq<&str> for PathRule {
599    fn eq(&self, other: &&str) -> bool {
600        self.pattern == *other
601    }
602}
603
604impl PartialEq<str> for PathRule {
605    fn eq(&self, other: &str) -> bool {
606        self.pattern == other
607    }
608}
609
610impl PartialEq<String> for PathRule {
611    fn eq(&self, other: &String) -> bool {
612        &self.pattern == other
613    }
614}
615
616/// A framework/tool plugin that contributes to dead code analysis.
617pub trait Plugin: Send + Sync {
618    /// Human-readable plugin name.
619    fn name(&self) -> &'static str;
620
621    /// Package names that activate this plugin when found in package.json.
622    /// Supports exact matches and prefix patterns (ending with `/`).
623    fn enablers(&self) -> &'static [&'static str] {
624        &[]
625    }
626
627    /// Check if this plugin should be active for the given project.
628    /// Default implementation checks `enablers()` against package.json dependencies.
629    fn is_enabled(&self, pkg: &PackageJson, root: &Path) -> bool {
630        let deps = pkg.all_dependency_names();
631        self.is_enabled_with_deps(&deps, root)
632    }
633
634    /// Fast variant of `is_enabled` that accepts a pre-computed deps list.
635    /// Avoids repeated `all_dependency_names()` allocation when checking many plugins.
636    fn is_enabled_with_deps(&self, deps: &[String], _root: &Path) -> bool {
637        let enablers = self.enablers();
638        if enablers.is_empty() {
639            return false;
640        }
641        enablers.iter().any(|enabler| {
642            if enabler.ends_with('/') {
643                // Prefix match (e.g., "@storybook/" matches "@storybook/react")
644                deps.iter().any(|d| d.starts_with(enabler))
645            } else {
646                deps.iter().any(|d| d == enabler)
647            }
648        })
649    }
650
651    /// Check whether this plugin should be active with source discovery available.
652    ///
653    /// Most plugins only need dependency/config activation. Convention-only tools
654    /// can override this to activate from discovered source filenames without
655    /// forcing a separate filesystem walk.
656    ///
657    /// `candidate_index` is the discovery walk's in-memory listing of source +
658    /// non-source config-candidate files (`Some` outside production mode, `None`
659    /// in production). A plugin that activates on a non-source sentinel file
660    /// (`manifest.json`, `.env.schema`) can consult it to avoid a per-directory
661    /// filesystem probe; when it is `None`, the plugin falls back to the
662    /// filesystem.
663    fn is_enabled_with_files(
664        &self,
665        deps: &[String],
666        root: &Path,
667        _discovered_files: &[PathBuf],
668        _candidate_index: Option<&registry::ConfigCandidateIndex>,
669    ) -> bool {
670        self.is_enabled_with_deps(deps, root)
671    }
672
673    /// Package-script binary/package names that can activate this plugin.
674    fn script_enablers(&self) -> &'static [&'static str] {
675        &[]
676    }
677
678    /// Check whether this plugin should be active from package.json scripts.
679    fn is_enabled_with_scripts(
680        &self,
681        script_packages: &rustc_hash::FxHashSet<String>,
682        _root: &Path,
683    ) -> bool {
684        let enablers = self.script_enablers();
685        if enablers.is_empty() {
686            return false;
687        }
688        enablers.iter().any(|enabler| {
689            if enabler.ends_with('/') {
690                script_packages
691                    .iter()
692                    .any(|package| package.starts_with(enabler))
693            } else {
694                script_packages.contains(*enabler)
695            }
696        })
697    }
698
699    /// Default glob patterns for entry point files.
700    fn entry_patterns(&self) -> &'static [&'static str] {
701        &[]
702    }
703
704    /// Entry point rules with optional exclusions.
705    fn entry_pattern_rules(&self) -> Vec<PathRule> {
706        self.entry_patterns()
707            .iter()
708            .map(|pattern| PathRule::from_static(pattern))
709            .collect()
710    }
711
712    /// How this plugin's entry patterns should contribute to coverage reachability.
713    ///
714    /// `Support` roots keep files alive for dead-code analysis but do not count
715    /// as runtime or test reachability for static coverage gaps.
716    fn entry_point_role(&self) -> EntryPointRole {
717        builtin_entry_point_role(self.name())
718    }
719
720    /// Glob patterns for config files this plugin can parse.
721    fn config_patterns(&self) -> &'static [&'static str] {
722        &[]
723    }
724
725    /// Files that are always considered "used" when this plugin is active.
726    fn always_used(&self) -> &'static [&'static str] {
727        &[]
728    }
729
730    /// Exports that are always considered used for matching file patterns.
731    fn used_exports(&self) -> Vec<(&'static str, &'static [&'static str])> {
732        vec![]
733    }
734
735    /// Used-export rules with optional exclusions.
736    fn used_export_rules(&self) -> Vec<UsedExportRule> {
737        self.used_exports()
738            .into_iter()
739            .map(|(pattern, exports)| UsedExportRule::from_static(pattern, exports))
740            .collect()
741    }
742
743    /// Class member names the framework invokes at runtime. Matching members
744    /// are skipped during `unused-class-members` analysis. Intended for
745    /// interface/contract patterns where the library calls methods on consumer
746    /// classes (e.g. ag-Grid's `agInit`, Web Components' `connectedCallback`).
747    fn used_class_members(&self) -> &'static [&'static str] {
748        &[]
749    }
750
751    /// Heritage-scoped class member rules. Each rule applies only to classes
752    /// matching its `extends` and/or `implements` clause. Used for frameworks
753    /// where lifecycle members are runtime-invoked only on classes that extend
754    /// a known base (e.g. Lit's `render`/`updated` on classes extending
755    /// `LitElement`, native Web Components' `connectedCallback` on classes
756    /// extending `HTMLElement`). Default: empty. Plugins override when they
757    /// need scoping; flat names should still come from `used_class_members`.
758    fn used_class_member_rules(&self) -> Vec<UsedClassMemberRule> {
759        Vec::new()
760    }
761
762    /// Glob patterns for test fixture files consumed by this framework.
763    /// These files are implicitly used by the test runner and should not be
764    /// flagged as unused. Unlike `always_used()`, this carries semantic intent
765    /// for reporting purposes.
766    fn fixture_glob_patterns(&self) -> &'static [&'static str] {
767        &[]
768    }
769
770    /// Hidden directory names that should be traversed when this plugin is active.
771    ///
772    /// These are consulted before normal plugin execution because source discovery
773    /// runs first. Keep entries static and package-convention scoped.
774    fn discovery_hidden_dirs(&self) -> &'static [&'static str] {
775        &[]
776    }
777
778    /// Dependencies that are tooling (used via CLI/config, not source imports).
779    /// These should not be flagged as unused devDependencies.
780    fn tooling_dependencies(&self) -> &'static [&'static str] {
781        &[]
782    }
783
784    /// Import prefixes that are virtual modules provided by this framework at build time.
785    /// Imports matching these prefixes should not be flagged as unlisted dependencies.
786    /// Each entry is matched as a prefix against the extracted package name
787    /// (e.g., `"@theme/"` matches `@theme/Layout`).
788    fn virtual_module_prefixes(&self) -> &'static [&'static str] {
789        &[]
790    }
791
792    /// Package name suffixes that are virtual modules provided by this framework
793    /// at build time (e.g., test runner mock conventions).
794    /// Imports matching these suffixes should not be flagged as unlisted dependencies.
795    /// Each entry is matched as a suffix against the extracted package name
796    /// (e.g., `"/__mocks__"` matches `@aws-sdk/__mocks__` and `some-pkg/__mocks__`).
797    fn virtual_package_suffixes(&self) -> &'static [&'static str] {
798        &[]
799    }
800
801    /// Import suffixes for build-time generated relative imports.
802    ///
803    /// Unresolved relative imports whose specifier ends with one of these suffixes
804    /// will not be flagged as unresolved. For example, SvelteKit generates
805    /// `./$types` imports in route files, returning `"/$types"` suppresses those.
806    fn generated_import_patterns(&self) -> &'static [&'static str] {
807        &[]
808    }
809
810    /// Import prefixes for generated type-only relative imports.
811    ///
812    /// Unresolved type-only imports whose specifier starts with one of these prefixes
813    /// will not be flagged as unresolved. Runtime imports are still reported.
814    fn generated_type_import_prefixes(&self) -> &'static [&'static str] {
815        &[]
816    }
817
818    /// Path alias mappings provided by this framework at build time.
819    ///
820    /// Returns a list of `(prefix, replacement_dir)` tuples. When an import starting
821    /// with `prefix` fails to resolve, the resolver will substitute the prefix with
822    /// `replacement_dir` (relative to the project root) and retry.
823    ///
824    /// Called once when plugins are activated. The project `root` is provided so
825    /// plugins can inspect the filesystem (e.g., Nuxt checks whether `app/` exists
826    /// to determine the `srcDir`).
827    fn path_aliases(&self, _root: &Path) -> Vec<(&'static str, String)> {
828        vec![]
829    }
830
831    /// Convention-based auto-imports provided by this framework.
832    ///
833    /// Returns the names this framework exposes to user code by filesystem
834    /// convention with no explicit `import` statement (e.g. Nuxt `components/`
835    /// resolved by `<Card001 />` template tags), each mapped to the source file
836    /// providing the export. When a file references one of these names without an
837    /// import, the resolver synthesizes a graph edge to `source`.
838    ///
839    /// Called once when plugins are activated. The project `root` is provided so
840    /// plugins can scan the convention directories on the filesystem. The table is
841    /// a function of which files exist on disk, so it is rebuilt every run and is
842    /// never folded into per-file extraction caching. See issue #704.
843    fn auto_imports(&self, _root: &Path) -> Vec<AutoImportRule> {
844        Vec::new()
845    }
846
847    /// File-scoped dependency providers contributed by this framework.
848    fn provided_dependencies(&self) -> Vec<ProvidedDependencyRule> {
849        Vec::new()
850    }
851
852    /// Check whether parsed package.json metadata activates this plugin.
853    fn is_enabled_with_package_json(&self, _pkg: &PackageJson, _root: &Path) -> bool {
854        false
855    }
856
857    /// Resolve parsed package.json metadata into dynamic plugin facts.
858    fn resolve_package_json(&self, _pkg: &PackageJson, _root: &Path) -> PluginResult {
859        PluginResult::default()
860    }
861
862    /// Dependencies referenced by the package's own package.json metadata.
863    ///
864    /// Unlike config-derived dependencies, these credits apply only to the
865    /// package.json that produced them.
866    fn package_json_referenced_dependencies(
867        &self,
868        _pkg: &PackageJson,
869        _root: &Path,
870    ) -> Vec<String> {
871        Vec::new()
872    }
873
874    /// Parse a config file's AST to discover additional entries, dependencies, etc.
875    ///
876    /// Called for each config file matching `config_patterns()`. The source code
877    /// and parsed AST are provided, use [`config_parser`] utilities to extract values.
878    fn resolve_config(&self, _config_path: &Path, _source: &str, _root: &Path) -> PluginResult {
879        PluginResult::default()
880    }
881
882    /// The key name in package.json that holds inline configuration for this tool.
883    /// When set (e.g., `"jest"` for the `"jest"` key in package.json), the plugin
884    /// system will extract that key's value and call `resolve_config` with its
885    /// JSON content if no standalone config file was found.
886    fn package_json_config_key(&self) -> Option<&'static str> {
887        None
888    }
889}
890
891fn builtin_entry_point_role(name: &str) -> EntryPointRole {
892    if TEST_ENTRY_POINT_PLUGINS.contains(&name) {
893        EntryPointRole::Test
894    } else if RUNTIME_ENTRY_POINT_PLUGINS.contains(&name) {
895        EntryPointRole::Runtime
896    } else {
897        EntryPointRole::Support
898    }
899}
900
901/// Macro to eliminate boilerplate in plugin implementations.
902///
903/// Generates a struct and a `Plugin` trait impl with the standard static methods
904/// (`name`, `enablers`, `entry_patterns`, `config_patterns`, `always_used`, `tooling_dependencies`,
905/// `fixture_glob_patterns`, `virtual_module_prefixes`, `virtual_package_suffixes`,
906/// `generated_type_import_prefixes`, `used_exports`).
907///
908/// For plugins that need custom `resolve_config()` or `is_enabled()`, keep those as
909/// manual `impl Plugin for ...` blocks instead of using this macro.
910///
911/// # Usage
912///
913/// ```ignore
914/// // Simple plugin (most common):
915/// define_plugin! {
916///     struct VitePlugin => "vite",
917///     enablers: ENABLERS,
918///     entry_patterns: ENTRY_PATTERNS,
919///     config_patterns: CONFIG_PATTERNS,
920///     always_used: ALWAYS_USED,
921///     tooling_dependencies: TOOLING_DEPENDENCIES,
922/// }
923///
924/// // Plugin with used_exports:
925/// define_plugin! {
926///     struct RemixPlugin => "remix",
927///     enablers: ENABLERS,
928///     entry_patterns: ENTRY_PATTERNS,
929///     always_used: ALWAYS_USED,
930///     tooling_dependencies: TOOLING_DEPENDENCIES,
931///     used_exports: [("app/routes/**/*.{ts,tsx}", ROUTE_EXPORTS)],
932/// }
933///
934/// // Plugin with imports-only resolve_config (extracts imports from config as deps):
935/// define_plugin! {
936///     struct CypressPlugin => "cypress",
937///     enablers: ENABLERS,
938///     entry_patterns: ENTRY_PATTERNS,
939///     config_patterns: CONFIG_PATTERNS,
940///     always_used: ALWAYS_USED,
941///     tooling_dependencies: TOOLING_DEPENDENCIES,
942///     resolve_config: imports_only,
943/// }
944///
945/// // Plugin with custom resolve_config body:
946/// define_plugin! {
947///     struct RollupPlugin => "rollup",
948///     enablers: ENABLERS,
949///     config_patterns: CONFIG_PATTERNS,
950///     always_used: ALWAYS_USED,
951///     tooling_dependencies: TOOLING_DEPENDENCIES,
952///     resolve_config(config_path, source, _root) {
953///         let mut result = PluginResult::default();
954///         // custom config parsing...
955///         result
956///     }
957/// }
958/// ```
959///
960/// All fields except `struct` and `enablers` are optional and default to `&[]` / `vec![]`.
961macro_rules! define_plugin {
962    (
963        struct $name:ident => $display:expr,
964        enablers: $enablers:expr
965        $(, entry_patterns: $entry:expr)?
966        $(, config_patterns: $config:expr)?
967        $(, always_used: $always:expr)?
968        $(, tooling_dependencies: $tooling:expr)?
969        $(, fixture_glob_patterns: $fixtures:expr)?
970        $(, discovery_hidden_dirs: $hidden_dirs:expr)?
971        $(, virtual_module_prefixes: $virtual:expr)?
972        $(, virtual_package_suffixes: $virtual_suffixes:expr)?
973        $(, generated_type_import_prefixes: $generated_type_prefixes:expr)?
974        $(, provided_dependencies: $provided_dependencies:expr)?
975        $(, used_exports: [$( ($pat:expr, $exports:expr) ),* $(,)?])?
976        , resolve_config: imports_only
977        $(,)?
978    ) => {
979        pub struct $name;
980
981        impl Plugin for $name {
982            fn name(&self) -> &'static str {
983                $display
984            }
985
986            fn enablers(&self) -> &'static [&'static str] {
987                $enablers
988            }
989
990            $( fn entry_patterns(&self) -> &'static [&'static str] { $entry } )?
991            $( fn config_patterns(&self) -> &'static [&'static str] { $config } )?
992            $( fn always_used(&self) -> &'static [&'static str] { $always } )?
993            $( fn tooling_dependencies(&self) -> &'static [&'static str] { $tooling } )?
994            $( fn fixture_glob_patterns(&self) -> &'static [&'static str] { $fixtures } )?
995            $( fn discovery_hidden_dirs(&self) -> &'static [&'static str] { $hidden_dirs } )?
996            $( fn virtual_module_prefixes(&self) -> &'static [&'static str] { $virtual } )?
997            $( fn virtual_package_suffixes(&self) -> &'static [&'static str] { $virtual_suffixes } )?
998            $( fn generated_type_import_prefixes(&self) -> &'static [&'static str] { $generated_type_prefixes } )?
999            $( fn provided_dependencies(&self) -> Vec<ProvidedDependencyRule> { $provided_dependencies } )?
1000
1001            $(
1002                fn used_exports(&self) -> Vec<(&'static str, &'static [&'static str])> {
1003                    vec![$( ($pat, $exports) ),*]
1004                }
1005            )?
1006
1007            fn resolve_config(
1008                &self,
1009                config_path: &std::path::Path,
1010                source: &str,
1011                _root: &std::path::Path,
1012            ) -> PluginResult {
1013                let mut result = PluginResult::default();
1014                crate::plugins::add_import_referenced_dependencies(
1015                    &mut result,
1016                    source,
1017                    config_path,
1018                );
1019                result
1020            }
1021        }
1022    };
1023
1024    (
1025        struct $name:ident => $display:expr,
1026        enablers: $enablers:expr
1027        $(, entry_patterns: $entry:expr)?
1028        $(, config_patterns: $config:expr)?
1029        $(, always_used: $always:expr)?
1030        $(, tooling_dependencies: $tooling:expr)?
1031        $(, fixture_glob_patterns: $fixtures:expr)?
1032        $(, discovery_hidden_dirs: $hidden_dirs:expr)?
1033        $(, virtual_module_prefixes: $virtual:expr)?
1034        $(, virtual_package_suffixes: $virtual_suffixes:expr)?
1035        $(, generated_type_import_prefixes: $generated_type_prefixes:expr)?
1036        $(, provided_dependencies: $provided_dependencies:expr)?
1037        $(, package_json_config_key: $pkg_key:expr)?
1038        $(, used_exports: [$( ($pat:expr, $exports:expr) ),* $(,)?])?
1039        , resolve_config($cp:ident, $src:ident, $root:ident) $body:block
1040        $(,)?
1041    ) => {
1042        pub struct $name;
1043
1044        impl Plugin for $name {
1045            fn name(&self) -> &'static str {
1046                $display
1047            }
1048
1049            fn enablers(&self) -> &'static [&'static str] {
1050                $enablers
1051            }
1052
1053            $( fn entry_patterns(&self) -> &'static [&'static str] { $entry } )?
1054            $( fn config_patterns(&self) -> &'static [&'static str] { $config } )?
1055            $( fn always_used(&self) -> &'static [&'static str] { $always } )?
1056            $( fn tooling_dependencies(&self) -> &'static [&'static str] { $tooling } )?
1057            $( fn fixture_glob_patterns(&self) -> &'static [&'static str] { $fixtures } )?
1058            $( fn discovery_hidden_dirs(&self) -> &'static [&'static str] { $hidden_dirs } )?
1059            $( fn virtual_module_prefixes(&self) -> &'static [&'static str] { $virtual } )?
1060            $( fn virtual_package_suffixes(&self) -> &'static [&'static str] { $virtual_suffixes } )?
1061            $( fn generated_type_import_prefixes(&self) -> &'static [&'static str] { $generated_type_prefixes } )?
1062            $( fn provided_dependencies(&self) -> Vec<ProvidedDependencyRule> { $provided_dependencies } )?
1063
1064            $(
1065                fn package_json_config_key(&self) -> Option<&'static str> {
1066                    Some($pkg_key)
1067                }
1068            )?
1069
1070            $(
1071                fn used_exports(&self) -> Vec<(&'static str, &'static [&'static str])> {
1072                    vec![$( ($pat, $exports) ),*]
1073                }
1074            )?
1075
1076            fn resolve_config(
1077                &self,
1078                $cp: &std::path::Path,
1079                $src: &str,
1080                $root: &std::path::Path,
1081            ) -> PluginResult
1082            $body
1083        }
1084    };
1085
1086    (
1087        struct $name:ident => $display:expr,
1088        enablers: $enablers:expr
1089        $(, entry_patterns: $entry:expr)?
1090        $(, config_patterns: $config:expr)?
1091        $(, always_used: $always:expr)?
1092        $(, tooling_dependencies: $tooling:expr)?
1093        $(, fixture_glob_patterns: $fixtures:expr)?
1094        $(, discovery_hidden_dirs: $hidden_dirs:expr)?
1095        $(, virtual_module_prefixes: $virtual:expr)?
1096        $(, virtual_package_suffixes: $virtual_suffixes:expr)?
1097        $(, generated_type_import_prefixes: $generated_type_prefixes:expr)?
1098        $(, provided_dependencies: $provided_dependencies:expr)?
1099        $(, used_exports: [$( ($pat:expr, $exports:expr) ),* $(,)?])?
1100        $(,)?
1101    ) => {
1102        pub struct $name;
1103
1104        impl Plugin for $name {
1105            fn name(&self) -> &'static str {
1106                $display
1107            }
1108
1109            fn enablers(&self) -> &'static [&'static str] {
1110                $enablers
1111            }
1112
1113            $( fn entry_patterns(&self) -> &'static [&'static str] { $entry } )?
1114            $( fn config_patterns(&self) -> &'static [&'static str] { $config } )?
1115            $( fn always_used(&self) -> &'static [&'static str] { $always } )?
1116            $( fn tooling_dependencies(&self) -> &'static [&'static str] { $tooling } )?
1117            $( fn fixture_glob_patterns(&self) -> &'static [&'static str] { $fixtures } )?
1118            $( fn discovery_hidden_dirs(&self) -> &'static [&'static str] { $hidden_dirs } )?
1119            $( fn virtual_module_prefixes(&self) -> &'static [&'static str] { $virtual } )?
1120            $( fn virtual_package_suffixes(&self) -> &'static [&'static str] { $virtual_suffixes } )?
1121            $( fn generated_type_import_prefixes(&self) -> &'static [&'static str] { $generated_type_prefixes } )?
1122            $( fn provided_dependencies(&self) -> Vec<ProvidedDependencyRule> { $provided_dependencies } )?
1123
1124            $(
1125                fn used_exports(&self) -> Vec<(&'static str, &'static [&'static str])> {
1126                    vec![$( ($pat, $exports) ),*]
1127                }
1128            )?
1129        }
1130    };
1131}
1132
1133pub mod config_parser;
1134mod manifest;
1135pub mod registry;
1136mod tooling;
1137
1138pub use registry::{AggregatedPluginResult, PluginRegistry};
1139pub use tooling::is_known_tooling_dependency;
1140
1141fn add_import_referenced_dependencies(result: &mut PluginResult, source: &str, config_path: &Path) {
1142    let imports = config_parser::extract_imports(source, config_path);
1143    for import in &imports {
1144        result
1145            .referenced_dependencies
1146            .push(crate::resolve::extract_package_name(import));
1147    }
1148}
1149
1150mod adonis;
1151mod angular;
1152mod astro;
1153mod ava;
1154mod babel;
1155mod biome;
1156mod browser_extension;
1157mod bun;
1158mod c8;
1159mod capacitor;
1160mod changesets;
1161mod commit_and_tag_version;
1162mod commitizen;
1163mod commitlint;
1164mod content_collections;
1165mod contentlayer;
1166mod convex;
1167mod cspell;
1168mod cucumber;
1169mod cypress;
1170mod danger;
1171mod dependency_cruiser;
1172mod docusaurus;
1173mod drizzle;
1174mod electron;
1175mod ember;
1176mod eslint;
1177mod expo;
1178mod expo_router;
1179mod firebase;
1180mod fumadocs;
1181mod gatsby;
1182mod graphql_codegen;
1183mod hardhat;
1184mod husky;
1185mod i18next;
1186mod ionic;
1187mod jest;
1188mod k6;
1189mod karma;
1190mod knex;
1191mod kysely;
1192mod lefthook;
1193mod lexical;
1194mod lint_staged;
1195mod lit;
1196mod markdownlint;
1197mod mintlify;
1198mod mocha;
1199mod msw;
1200mod napi_rs;
1201mod nestjs;
1202mod next_intl;
1203mod nextjs;
1204mod nitro;
1205mod nodemon;
1206pub(crate) mod nuxt;
1207mod nx;
1208mod nyc;
1209mod obsidian;
1210mod openapi_ts;
1211mod opencode;
1212mod opennext_cloudflare;
1213mod oxlint;
1214mod pandacss;
1215mod parcel;
1216mod pinia;
1217mod pkg_utils;
1218mod playwright;
1219mod plop;
1220mod pm2;
1221mod pnpm;
1222mod postcss;
1223mod prettier;
1224mod prisma;
1225mod qwik;
1226mod react_compiler;
1227mod react_native;
1228mod react_router;
1229mod redwoodsdk;
1230mod relay;
1231mod remark;
1232mod remix;
1233mod rolldown;
1234mod rollup;
1235mod rsbuild;
1236mod rspack;
1237mod rspress;
1238mod sanity;
1239mod semantic_release;
1240mod sentry;
1241mod simple_git_hooks;
1242mod storybook;
1243mod stryker;
1244mod stylelint;
1245mod supabase;
1246mod sveltekit;
1247mod svgo;
1248mod svgr;
1249mod swc;
1250mod syncpack;
1251mod tailwind;
1252mod tanstack_router;
1253mod tap;
1254mod test_alias;
1255mod tsd;
1256mod tsdown;
1257mod tsup;
1258mod turborepo;
1259mod typedoc;
1260mod typeorm;
1261mod typescript;
1262mod unocss;
1263mod varlock;
1264mod velite;
1265mod vercel;
1266mod vite;
1267mod vitepress;
1268mod vitest;
1269mod vscode;
1270mod webdriverio;
1271mod webpack;
1272mod wrangler;
1273mod wuchale;
1274mod wxt;
1275
1276#[cfg(test)]
1277mod tests {
1278    use super::*;
1279    use std::path::Path;
1280
1281    #[test]
1282    fn is_enabled_with_deps_exact_match() {
1283        let plugin = nextjs::NextJsPlugin;
1284        let deps = vec!["next".to_string()];
1285        assert!(plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1286    }
1287
1288    #[test]
1289    fn is_enabled_with_deps_no_match() {
1290        let plugin = nextjs::NextJsPlugin;
1291        let deps = vec!["react".to_string()];
1292        assert!(!plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1293    }
1294
1295    #[test]
1296    fn is_enabled_with_deps_empty_deps() {
1297        let plugin = nextjs::NextJsPlugin;
1298        let deps: Vec<String> = vec![];
1299        assert!(!plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1300    }
1301
1302    #[test]
1303    fn entry_point_role_defaults_are_centralized() {
1304        assert_eq!(vite::VitePlugin.entry_point_role(), EntryPointRole::Runtime);
1305        assert_eq!(
1306            vitest::VitestPlugin.entry_point_role(),
1307            EntryPointRole::Test
1308        );
1309        assert_eq!(
1310            storybook::StorybookPlugin.entry_point_role(),
1311            EntryPointRole::Support
1312        );
1313        assert_eq!(
1314            obsidian::ObsidianPlugin.entry_point_role(),
1315            EntryPointRole::Runtime
1316        );
1317        assert_eq!(knex::KnexPlugin.entry_point_role(), EntryPointRole::Support);
1318    }
1319
1320    #[test]
1321    fn plugins_with_entry_patterns_have_explicit_role_intent() {
1322        let runtime_or_test_or_support: rustc_hash::FxHashSet<&'static str> =
1323            TEST_ENTRY_POINT_PLUGINS
1324                .iter()
1325                .chain(RUNTIME_ENTRY_POINT_PLUGINS.iter())
1326                .chain(SUPPORT_ENTRY_POINT_PLUGINS.iter())
1327                .copied()
1328                .collect();
1329
1330        for plugin in crate::plugins::registry::builtin::create_builtin_plugins() {
1331            if plugin.entry_patterns().is_empty() {
1332                continue;
1333            }
1334            assert!(
1335                runtime_or_test_or_support.contains(plugin.name()),
1336                "plugin '{}' exposes entry patterns but is missing from the entry-point role map",
1337                plugin.name()
1338            );
1339        }
1340    }
1341
1342    #[test]
1343    fn plugin_result_is_empty_when_default() {
1344        let r = PluginResult::default();
1345        assert!(r.is_empty());
1346    }
1347
1348    #[test]
1349    fn plugin_result_not_empty_with_entry_patterns() {
1350        let r = PluginResult {
1351            entry_patterns: vec!["*.ts".into()],
1352            ..Default::default()
1353        };
1354        assert!(!r.is_empty());
1355    }
1356
1357    #[test]
1358    fn plugin_result_not_empty_with_referenced_deps() {
1359        let r = PluginResult {
1360            referenced_dependencies: vec!["lodash".to_string()],
1361            ..Default::default()
1362        };
1363        assert!(!r.is_empty());
1364    }
1365
1366    #[test]
1367    fn plugin_result_not_empty_with_setup_files() {
1368        let r = PluginResult {
1369            setup_files: vec![PathBuf::from("/setup.ts")],
1370            ..Default::default()
1371        };
1372        assert!(!r.is_empty());
1373    }
1374
1375    #[test]
1376    fn plugin_result_not_empty_with_always_used_files() {
1377        let r = PluginResult {
1378            always_used_files: vec!["**/*.stories.tsx".to_string()],
1379            ..Default::default()
1380        };
1381        assert!(!r.is_empty());
1382    }
1383
1384    #[test]
1385    fn plugin_result_not_empty_with_fixture_patterns() {
1386        let r = PluginResult {
1387            fixture_patterns: vec!["**/__fixtures__/**/*".to_string()],
1388            ..Default::default()
1389        };
1390        assert!(!r.is_empty());
1391    }
1392
1393    #[test]
1394    fn is_enabled_with_deps_prefix_match() {
1395        let plugin = storybook::StorybookPlugin;
1396        let deps = vec!["@storybook/react".to_string()];
1397        assert!(plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1398    }
1399
1400    #[test]
1401    fn is_enabled_with_deps_prefix_no_match_without_slash() {
1402        let plugin = storybook::StorybookPlugin;
1403        let deps = vec!["@storybookish".to_string()];
1404        assert!(!plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1405    }
1406
1407    #[test]
1408    fn is_enabled_with_deps_multiple_enablers() {
1409        let plugin = vitest::VitestPlugin;
1410        let deps_vitest = vec!["vitest".to_string()];
1411        let deps_none = vec!["mocha".to_string()];
1412        assert!(plugin.is_enabled_with_deps(&deps_vitest, Path::new("/project")));
1413        assert!(!plugin.is_enabled_with_deps(&deps_none, Path::new("/project")));
1414    }
1415
1416    #[test]
1417    fn plugin_default_methods_return_empty() {
1418        let plugin = commitizen::CommitizenPlugin;
1419        assert!(
1420            plugin.tooling_dependencies().is_empty() || !plugin.tooling_dependencies().is_empty()
1421        );
1422        assert!(plugin.virtual_module_prefixes().is_empty());
1423        assert!(plugin.virtual_package_suffixes().is_empty());
1424        assert!(plugin.path_aliases(Path::new("/project")).is_empty());
1425        assert!(
1426            plugin.package_json_config_key().is_none()
1427                || plugin.package_json_config_key().is_some()
1428        );
1429    }
1430
1431    #[test]
1432    fn plugin_resolve_config_default_returns_empty() {
1433        let plugin = commitizen::CommitizenPlugin;
1434        let result = plugin.resolve_config(
1435            Path::new("/project/config.js"),
1436            "const x = 1;",
1437            Path::new("/project"),
1438        );
1439        assert!(result.is_empty());
1440    }
1441
1442    #[test]
1443    fn is_enabled_with_deps_exact_and_prefix_both_work() {
1444        let plugin = storybook::StorybookPlugin;
1445        let deps_exact = vec!["storybook".to_string()];
1446        assert!(plugin.is_enabled_with_deps(&deps_exact, Path::new("/project")));
1447        let deps_prefix = vec!["@storybook/vue3".to_string()];
1448        assert!(plugin.is_enabled_with_deps(&deps_prefix, Path::new("/project")));
1449    }
1450
1451    #[test]
1452    fn is_enabled_with_deps_multiple_enablers_remix() {
1453        let plugin = remix::RemixPlugin;
1454        let deps_node = vec!["@remix-run/node".to_string()];
1455        assert!(plugin.is_enabled_with_deps(&deps_node, Path::new("/project")));
1456        let deps_react = vec!["@remix-run/react".to_string()];
1457        assert!(plugin.is_enabled_with_deps(&deps_react, Path::new("/project")));
1458        let deps_cf = vec!["@remix-run/cloudflare".to_string()];
1459        assert!(plugin.is_enabled_with_deps(&deps_cf, Path::new("/project")));
1460    }
1461
1462    struct MinimalPlugin;
1463    impl Plugin for MinimalPlugin {
1464        fn name(&self) -> &'static str {
1465            "minimal"
1466        }
1467    }
1468
1469    #[test]
1470    fn default_enablers_is_empty() {
1471        assert!(MinimalPlugin.enablers().is_empty());
1472    }
1473
1474    #[test]
1475    fn default_entry_patterns_is_empty() {
1476        assert!(MinimalPlugin.entry_patterns().is_empty());
1477    }
1478
1479    #[test]
1480    fn default_config_patterns_is_empty() {
1481        assert!(MinimalPlugin.config_patterns().is_empty());
1482    }
1483
1484    #[test]
1485    fn default_always_used_is_empty() {
1486        assert!(MinimalPlugin.always_used().is_empty());
1487    }
1488
1489    #[test]
1490    fn default_used_exports_is_empty() {
1491        assert!(MinimalPlugin.used_exports().is_empty());
1492    }
1493
1494    #[test]
1495    fn default_tooling_dependencies_is_empty() {
1496        assert!(MinimalPlugin.tooling_dependencies().is_empty());
1497    }
1498
1499    #[test]
1500    fn default_fixture_glob_patterns_is_empty() {
1501        assert!(MinimalPlugin.fixture_glob_patterns().is_empty());
1502    }
1503
1504    #[test]
1505    fn default_virtual_module_prefixes_is_empty() {
1506        assert!(MinimalPlugin.virtual_module_prefixes().is_empty());
1507    }
1508
1509    #[test]
1510    fn default_virtual_package_suffixes_is_empty() {
1511        assert!(MinimalPlugin.virtual_package_suffixes().is_empty());
1512    }
1513
1514    #[test]
1515    fn default_path_aliases_is_empty() {
1516        assert!(MinimalPlugin.path_aliases(Path::new("/")).is_empty());
1517    }
1518
1519    #[test]
1520    fn default_resolve_config_returns_empty() {
1521        let r = MinimalPlugin.resolve_config(
1522            Path::new("config.js"),
1523            "export default {}",
1524            Path::new("/"),
1525        );
1526        assert!(r.is_empty());
1527    }
1528
1529    #[test]
1530    fn default_package_json_metadata_hooks_are_empty() {
1531        let pkg = PackageJson::default();
1532        assert!(!MinimalPlugin.is_enabled_with_package_json(&pkg, Path::new("/")));
1533        assert!(
1534            MinimalPlugin
1535                .resolve_package_json(&pkg, Path::new("/"))
1536                .is_empty()
1537        );
1538    }
1539
1540    #[test]
1541    fn default_package_json_config_key_is_none() {
1542        assert!(MinimalPlugin.package_json_config_key().is_none());
1543    }
1544
1545    #[test]
1546    fn default_is_enabled_returns_false_when_no_enablers() {
1547        let deps = vec!["anything".to_string()];
1548        assert!(!MinimalPlugin.is_enabled_with_deps(&deps, Path::new("/")));
1549    }
1550
1551    #[test]
1552    fn all_builtin_plugin_names_are_unique() {
1553        let plugins = registry::builtin::create_builtin_plugins();
1554        let mut seen = std::collections::BTreeSet::new();
1555        for p in &plugins {
1556            let name = p.name();
1557            assert!(seen.insert(name), "duplicate plugin name: {name}");
1558        }
1559    }
1560
1561    #[test]
1562    fn all_builtin_plugins_have_activation_signals() {
1563        const PACKAGE_JSON_METADATA_PLUGINS: &[&str] = &["napi-rs"];
1564        let plugins = registry::builtin::create_builtin_plugins();
1565        for p in &plugins {
1566            assert!(
1567                !p.enablers().is_empty()
1568                    || !p.script_enablers().is_empty()
1569                    || PACKAGE_JSON_METADATA_PLUGINS.contains(&p.name()),
1570                "plugin '{}' has no activation signal",
1571                p.name()
1572            );
1573        }
1574    }
1575
1576    #[test]
1577    fn plugins_with_config_patterns_have_always_used() {
1578        let plugins = registry::builtin::create_builtin_plugins();
1579        for p in &plugins {
1580            if !p.config_patterns().is_empty() {
1581                assert!(
1582                    !p.always_used().is_empty(),
1583                    "plugin '{}' has config_patterns but no always_used",
1584                    p.name()
1585                );
1586            }
1587        }
1588    }
1589
1590    #[test]
1591    fn framework_plugins_enablers() {
1592        let cases: Vec<(&dyn Plugin, &[&str])> = vec![
1593            (&nextjs::NextJsPlugin, &["next"]),
1594            (&nuxt::NuxtPlugin, &["nuxt"]),
1595            (&angular::AngularPlugin, &["@angular/core"]),
1596            (&ionic::IonicPlugin, &["@ionic/angular"]),
1597            (&sveltekit::SvelteKitPlugin, &["@sveltejs/kit"]),
1598            (&gatsby::GatsbyPlugin, &["gatsby"]),
1599        ];
1600        for (plugin, expected_enablers) in cases {
1601            let enablers = plugin.enablers();
1602            for expected in expected_enablers {
1603                assert!(
1604                    enablers.contains(expected),
1605                    "plugin '{}' should have '{}'",
1606                    plugin.name(),
1607                    expected
1608                );
1609            }
1610        }
1611    }
1612
1613    #[test]
1614    fn testing_plugins_enablers() {
1615        let cases: Vec<(&dyn Plugin, &str)> = vec![
1616            (&jest::JestPlugin, "jest"),
1617            (&vitest::VitestPlugin, "vitest"),
1618            (&playwright::PlaywrightPlugin, "@playwright/test"),
1619            (&cypress::CypressPlugin, "cypress"),
1620            (&mocha::MochaPlugin, "mocha"),
1621            (&stryker::StrykerPlugin, "@stryker-mutator/core"),
1622        ];
1623        for (plugin, enabler) in cases {
1624            assert!(
1625                plugin.enablers().contains(&enabler),
1626                "plugin '{}' should have '{}'",
1627                plugin.name(),
1628                enabler
1629            );
1630        }
1631    }
1632
1633    #[test]
1634    fn bundler_plugins_enablers() {
1635        let cases: Vec<(&dyn Plugin, &str)> = vec![
1636            (&vite::VitePlugin, "vite"),
1637            (&webpack::WebpackPlugin, "webpack"),
1638            (&rollup::RollupPlugin, "rollup"),
1639        ];
1640        for (plugin, enabler) in cases {
1641            assert!(
1642                plugin.enablers().contains(&enabler),
1643                "plugin '{}' should have '{}'",
1644                plugin.name(),
1645                enabler
1646            );
1647        }
1648    }
1649
1650    #[test]
1651    fn test_plugins_have_test_entry_patterns() {
1652        let test_plugins: Vec<&dyn Plugin> = vec![
1653            &bun::BunPlugin,
1654            &jest::JestPlugin,
1655            &vitest::VitestPlugin,
1656            &mocha::MochaPlugin,
1657            &tap::TapPlugin,
1658            &tsd::TsdPlugin,
1659        ];
1660        for plugin in test_plugins {
1661            let patterns = plugin.entry_patterns();
1662            assert!(
1663                !patterns.is_empty(),
1664                "test plugin '{}' should have entry patterns",
1665                plugin.name()
1666            );
1667            assert!(
1668                patterns
1669                    .iter()
1670                    .any(|p| p.contains("test") || p.contains("spec") || p.contains("__tests__")),
1671                "test plugin '{}' should have test/spec patterns",
1672                plugin.name()
1673            );
1674        }
1675    }
1676
1677    #[test]
1678    fn framework_plugins_have_entry_patterns() {
1679        let plugins: Vec<&dyn Plugin> = vec![
1680            &nextjs::NextJsPlugin,
1681            &nuxt::NuxtPlugin,
1682            &angular::AngularPlugin,
1683            &sveltekit::SvelteKitPlugin,
1684        ];
1685        for plugin in plugins {
1686            assert!(
1687                !plugin.entry_patterns().is_empty(),
1688                "framework plugin '{}' should have entry patterns",
1689                plugin.name()
1690            );
1691        }
1692    }
1693
1694    #[test]
1695    fn plugins_with_resolve_config_have_config_patterns() {
1696        let plugins: Vec<&dyn Plugin> = vec![
1697            &jest::JestPlugin,
1698            &vitest::VitestPlugin,
1699            &babel::BabelPlugin,
1700            &eslint::EslintPlugin,
1701            &webpack::WebpackPlugin,
1702            &storybook::StorybookPlugin,
1703            &typescript::TypeScriptPlugin,
1704            &postcss::PostCssPlugin,
1705            &nextjs::NextJsPlugin,
1706            &nuxt::NuxtPlugin,
1707            &angular::AngularPlugin,
1708            &nx::NxPlugin,
1709            &stryker::StrykerPlugin,
1710            &wuchale::WuchalePlugin,
1711            &rollup::RollupPlugin,
1712            &sveltekit::SvelteKitPlugin,
1713            &prettier::PrettierPlugin,
1714            &contentlayer::ContentlayerPlugin,
1715        ];
1716        for plugin in plugins {
1717            assert!(
1718                !plugin.config_patterns().is_empty(),
1719                "plugin '{}' with resolve_config should have config_patterns",
1720                plugin.name()
1721            );
1722        }
1723    }
1724
1725    #[test]
1726    fn plugin_tooling_deps_include_enabler_package() {
1727        let plugins: Vec<&dyn Plugin> = vec![
1728            &jest::JestPlugin,
1729            &vitest::VitestPlugin,
1730            &webpack::WebpackPlugin,
1731            &typescript::TypeScriptPlugin,
1732            &eslint::EslintPlugin,
1733            &prettier::PrettierPlugin,
1734            &danger::DangerPlugin,
1735            &stryker::StrykerPlugin,
1736            &wuchale::WuchalePlugin,
1737            &contentlayer::ContentlayerPlugin,
1738        ];
1739        for plugin in plugins {
1740            let tooling = plugin.tooling_dependencies();
1741            let enablers = plugin.enablers();
1742            assert!(
1743                enablers
1744                    .iter()
1745                    .any(|e| !e.ends_with('/') && tooling.contains(e)),
1746                "plugin '{}': at least one non-prefix enabler should be in tooling_dependencies",
1747                plugin.name()
1748            );
1749        }
1750    }
1751
1752    #[test]
1753    fn nextjs_has_used_exports_for_pages() {
1754        let plugin = nextjs::NextJsPlugin;
1755        let exports = plugin.used_exports();
1756        assert!(!exports.is_empty());
1757        assert!(exports.iter().any(|(_, names)| names.contains(&"default")));
1758    }
1759
1760    #[test]
1761    fn remix_has_used_exports_for_routes() {
1762        let plugin = remix::RemixPlugin;
1763        let exports = plugin.used_exports();
1764        assert!(!exports.is_empty());
1765        let route_entry = exports.iter().find(|(pat, _)| pat.contains("routes"));
1766        assert!(route_entry.is_some());
1767        let (_, names) = route_entry.unwrap();
1768        assert!(names.contains(&"loader"));
1769        assert!(names.contains(&"action"));
1770        assert!(names.contains(&"default"));
1771    }
1772
1773    #[test]
1774    fn sveltekit_has_used_exports_for_routes() {
1775        let plugin = sveltekit::SvelteKitPlugin;
1776        let exports = plugin.used_exports();
1777        assert!(!exports.is_empty());
1778        assert!(exports.iter().any(|(_, names)| names.contains(&"GET")));
1779    }
1780
1781    #[test]
1782    fn nuxt_has_hash_virtual_prefix() {
1783        assert!(nuxt::NuxtPlugin.virtual_module_prefixes().contains(&"#"));
1784    }
1785
1786    #[test]
1787    fn sveltekit_has_dollar_virtual_prefixes() {
1788        let prefixes = sveltekit::SvelteKitPlugin.virtual_module_prefixes();
1789        assert!(prefixes.contains(&"$app/"));
1790        assert!(prefixes.contains(&"$env/"));
1791        assert!(prefixes.contains(&"$lib/"));
1792    }
1793
1794    #[test]
1795    fn sveltekit_has_lib_path_alias() {
1796        let aliases = sveltekit::SvelteKitPlugin.path_aliases(Path::new("/project"));
1797        assert!(aliases.iter().any(|(prefix, _)| *prefix == "$lib/"));
1798    }
1799
1800    #[test]
1801    fn nuxt_has_tilde_path_alias() {
1802        let aliases = nuxt::NuxtPlugin.path_aliases(Path::new("/nonexistent"));
1803        assert!(aliases.iter().any(|(prefix, _)| *prefix == "~/"));
1804        assert!(aliases.iter().any(|(prefix, _)| *prefix == "~~/"));
1805    }
1806
1807    #[test]
1808    fn jest_has_package_json_config_key() {
1809        assert_eq!(jest::JestPlugin.package_json_config_key(), Some("jest"));
1810    }
1811
1812    #[test]
1813    fn tsd_has_package_json_config_key() {
1814        assert_eq!(tsd::TsdPlugin.package_json_config_key(), Some("tsd"));
1815    }
1816
1817    #[test]
1818    fn babel_has_package_json_config_key() {
1819        assert_eq!(babel::BabelPlugin.package_json_config_key(), Some("babel"));
1820    }
1821
1822    #[test]
1823    fn eslint_has_package_json_config_key() {
1824        assert_eq!(
1825            eslint::EslintPlugin.package_json_config_key(),
1826            Some("eslintConfig")
1827        );
1828    }
1829
1830    #[test]
1831    fn prettier_has_package_json_config_key() {
1832        assert_eq!(
1833            prettier::PrettierPlugin.package_json_config_key(),
1834            Some("prettier")
1835        );
1836    }
1837
1838    #[test]
1839    fn macro_generated_plugin_basic_properties() {
1840        let plugin = msw::MswPlugin;
1841        assert_eq!(plugin.name(), "msw");
1842        assert!(plugin.enablers().contains(&"msw"));
1843        assert!(!plugin.entry_patterns().is_empty());
1844        assert!(plugin.config_patterns().is_empty());
1845        assert!(!plugin.always_used().is_empty());
1846        assert!(!plugin.tooling_dependencies().is_empty());
1847    }
1848
1849    #[test]
1850    fn macro_generated_plugin_with_used_exports() {
1851        let plugin = remix::RemixPlugin;
1852        assert_eq!(plugin.name(), "remix");
1853        assert!(!plugin.used_exports().is_empty());
1854    }
1855
1856    #[test]
1857    fn macro_passes_through_virtual_package_suffixes() {
1858        define_plugin! {
1859            struct MacroSuffixSmokePlugin => "macro-suffix-smoke",
1860            enablers: &["macro-suffix-smoke"],
1861            virtual_package_suffixes: &["/__macro_smoke__"],
1862        }
1863
1864        let plugin = MacroSuffixSmokePlugin;
1865        assert_eq!(
1866            plugin.virtual_package_suffixes(),
1867            &["/__macro_smoke__"],
1868            "macro-declared virtual_package_suffixes must propagate to the trait method"
1869        );
1870    }
1871
1872    #[test]
1873    fn macro_generated_plugin_imports_only_resolve_config() {
1874        let plugin = cypress::CypressPlugin;
1875        let source = r"
1876            import { defineConfig } from 'cypress';
1877            import coveragePlugin from '@cypress/code-coverage';
1878            export default defineConfig({});
1879        ";
1880        let result = plugin.resolve_config(
1881            Path::new("cypress.config.ts"),
1882            source,
1883            Path::new("/project"),
1884        );
1885        assert!(
1886            result
1887                .referenced_dependencies
1888                .contains(&"cypress".to_string())
1889        );
1890        assert!(
1891            result
1892                .referenced_dependencies
1893                .contains(&"@cypress/code-coverage".to_string())
1894        );
1895    }
1896
1897    #[test]
1898    fn builtin_plugin_count_is_expected() {
1899        let plugins = registry::builtin::create_builtin_plugins();
1900        assert!(
1901            plugins.len() >= 110,
1902            "expected at least 110 built-in plugins, got {}",
1903            plugins.len()
1904        );
1905    }
1906}