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 commitizen;
1162mod commitlint;
1163mod content_collections;
1164mod contentlayer;
1165mod convex;
1166mod cspell;
1167mod cucumber;
1168mod cypress;
1169mod danger;
1170mod dependency_cruiser;
1171mod docusaurus;
1172mod drizzle;
1173mod electron;
1174mod ember;
1175mod eslint;
1176mod expo;
1177mod expo_router;
1178mod firebase;
1179mod fumadocs;
1180mod gatsby;
1181mod graphql_codegen;
1182mod hardhat;
1183mod husky;
1184mod i18next;
1185mod ionic;
1186mod jest;
1187mod k6;
1188mod karma;
1189mod knex;
1190mod kysely;
1191mod lefthook;
1192mod lexical;
1193mod lint_staged;
1194mod lit;
1195mod markdownlint;
1196mod mintlify;
1197mod mocha;
1198mod msw;
1199mod napi_rs;
1200mod nestjs;
1201mod next_intl;
1202mod nextjs;
1203mod nitro;
1204mod nodemon;
1205pub(crate) mod nuxt;
1206mod nx;
1207mod nyc;
1208mod obsidian;
1209mod openapi_ts;
1210mod opencode;
1211mod opennext_cloudflare;
1212mod oxlint;
1213mod pandacss;
1214mod parcel;
1215mod pinia;
1216mod pkg_utils;
1217mod playwright;
1218mod plop;
1219mod pm2;
1220mod pnpm;
1221mod postcss;
1222mod prettier;
1223mod prisma;
1224mod qwik;
1225mod react_compiler;
1226mod react_native;
1227mod react_router;
1228mod redwoodsdk;
1229mod relay;
1230mod remark;
1231mod remix;
1232mod rolldown;
1233mod rollup;
1234mod rsbuild;
1235mod rspack;
1236mod rspress;
1237mod sanity;
1238mod semantic_release;
1239mod sentry;
1240mod simple_git_hooks;
1241mod storybook;
1242mod stryker;
1243mod stylelint;
1244mod supabase;
1245mod sveltekit;
1246mod svgo;
1247mod svgr;
1248mod swc;
1249mod syncpack;
1250mod tailwind;
1251mod tanstack_router;
1252mod tap;
1253mod test_alias;
1254mod tsd;
1255mod tsdown;
1256mod tsup;
1257mod turborepo;
1258mod typedoc;
1259mod typeorm;
1260mod typescript;
1261mod unocss;
1262mod varlock;
1263mod velite;
1264mod vercel;
1265mod vite;
1266mod vitepress;
1267mod vitest;
1268mod vscode;
1269mod webdriverio;
1270mod webpack;
1271mod wrangler;
1272mod wuchale;
1273mod wxt;
1274
1275#[cfg(test)]
1276mod tests {
1277    use super::*;
1278    use std::path::Path;
1279
1280    #[test]
1281    fn is_enabled_with_deps_exact_match() {
1282        let plugin = nextjs::NextJsPlugin;
1283        let deps = vec!["next".to_string()];
1284        assert!(plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1285    }
1286
1287    #[test]
1288    fn is_enabled_with_deps_no_match() {
1289        let plugin = nextjs::NextJsPlugin;
1290        let deps = vec!["react".to_string()];
1291        assert!(!plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1292    }
1293
1294    #[test]
1295    fn is_enabled_with_deps_empty_deps() {
1296        let plugin = nextjs::NextJsPlugin;
1297        let deps: Vec<String> = vec![];
1298        assert!(!plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1299    }
1300
1301    #[test]
1302    fn entry_point_role_defaults_are_centralized() {
1303        assert_eq!(vite::VitePlugin.entry_point_role(), EntryPointRole::Runtime);
1304        assert_eq!(
1305            vitest::VitestPlugin.entry_point_role(),
1306            EntryPointRole::Test
1307        );
1308        assert_eq!(
1309            storybook::StorybookPlugin.entry_point_role(),
1310            EntryPointRole::Support
1311        );
1312        assert_eq!(
1313            obsidian::ObsidianPlugin.entry_point_role(),
1314            EntryPointRole::Runtime
1315        );
1316        assert_eq!(knex::KnexPlugin.entry_point_role(), EntryPointRole::Support);
1317    }
1318
1319    #[test]
1320    fn plugins_with_entry_patterns_have_explicit_role_intent() {
1321        let runtime_or_test_or_support: rustc_hash::FxHashSet<&'static str> =
1322            TEST_ENTRY_POINT_PLUGINS
1323                .iter()
1324                .chain(RUNTIME_ENTRY_POINT_PLUGINS.iter())
1325                .chain(SUPPORT_ENTRY_POINT_PLUGINS.iter())
1326                .copied()
1327                .collect();
1328
1329        for plugin in crate::plugins::registry::builtin::create_builtin_plugins() {
1330            if plugin.entry_patterns().is_empty() {
1331                continue;
1332            }
1333            assert!(
1334                runtime_or_test_or_support.contains(plugin.name()),
1335                "plugin '{}' exposes entry patterns but is missing from the entry-point role map",
1336                plugin.name()
1337            );
1338        }
1339    }
1340
1341    #[test]
1342    fn plugin_result_is_empty_when_default() {
1343        let r = PluginResult::default();
1344        assert!(r.is_empty());
1345    }
1346
1347    #[test]
1348    fn plugin_result_not_empty_with_entry_patterns() {
1349        let r = PluginResult {
1350            entry_patterns: vec!["*.ts".into()],
1351            ..Default::default()
1352        };
1353        assert!(!r.is_empty());
1354    }
1355
1356    #[test]
1357    fn plugin_result_not_empty_with_referenced_deps() {
1358        let r = PluginResult {
1359            referenced_dependencies: vec!["lodash".to_string()],
1360            ..Default::default()
1361        };
1362        assert!(!r.is_empty());
1363    }
1364
1365    #[test]
1366    fn plugin_result_not_empty_with_setup_files() {
1367        let r = PluginResult {
1368            setup_files: vec![PathBuf::from("/setup.ts")],
1369            ..Default::default()
1370        };
1371        assert!(!r.is_empty());
1372    }
1373
1374    #[test]
1375    fn plugin_result_not_empty_with_always_used_files() {
1376        let r = PluginResult {
1377            always_used_files: vec!["**/*.stories.tsx".to_string()],
1378            ..Default::default()
1379        };
1380        assert!(!r.is_empty());
1381    }
1382
1383    #[test]
1384    fn plugin_result_not_empty_with_fixture_patterns() {
1385        let r = PluginResult {
1386            fixture_patterns: vec!["**/__fixtures__/**/*".to_string()],
1387            ..Default::default()
1388        };
1389        assert!(!r.is_empty());
1390    }
1391
1392    #[test]
1393    fn is_enabled_with_deps_prefix_match() {
1394        let plugin = storybook::StorybookPlugin;
1395        let deps = vec!["@storybook/react".to_string()];
1396        assert!(plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1397    }
1398
1399    #[test]
1400    fn is_enabled_with_deps_prefix_no_match_without_slash() {
1401        let plugin = storybook::StorybookPlugin;
1402        let deps = vec!["@storybookish".to_string()];
1403        assert!(!plugin.is_enabled_with_deps(&deps, Path::new("/project")));
1404    }
1405
1406    #[test]
1407    fn is_enabled_with_deps_multiple_enablers() {
1408        let plugin = vitest::VitestPlugin;
1409        let deps_vitest = vec!["vitest".to_string()];
1410        let deps_none = vec!["mocha".to_string()];
1411        assert!(plugin.is_enabled_with_deps(&deps_vitest, Path::new("/project")));
1412        assert!(!plugin.is_enabled_with_deps(&deps_none, Path::new("/project")));
1413    }
1414
1415    #[test]
1416    fn plugin_default_methods_return_empty() {
1417        let plugin = commitizen::CommitizenPlugin;
1418        assert!(
1419            plugin.tooling_dependencies().is_empty() || !plugin.tooling_dependencies().is_empty()
1420        );
1421        assert!(plugin.virtual_module_prefixes().is_empty());
1422        assert!(plugin.virtual_package_suffixes().is_empty());
1423        assert!(plugin.path_aliases(Path::new("/project")).is_empty());
1424        assert!(
1425            plugin.package_json_config_key().is_none()
1426                || plugin.package_json_config_key().is_some()
1427        );
1428    }
1429
1430    #[test]
1431    fn plugin_resolve_config_default_returns_empty() {
1432        let plugin = commitizen::CommitizenPlugin;
1433        let result = plugin.resolve_config(
1434            Path::new("/project/config.js"),
1435            "const x = 1;",
1436            Path::new("/project"),
1437        );
1438        assert!(result.is_empty());
1439    }
1440
1441    #[test]
1442    fn is_enabled_with_deps_exact_and_prefix_both_work() {
1443        let plugin = storybook::StorybookPlugin;
1444        let deps_exact = vec!["storybook".to_string()];
1445        assert!(plugin.is_enabled_with_deps(&deps_exact, Path::new("/project")));
1446        let deps_prefix = vec!["@storybook/vue3".to_string()];
1447        assert!(plugin.is_enabled_with_deps(&deps_prefix, Path::new("/project")));
1448    }
1449
1450    #[test]
1451    fn is_enabled_with_deps_multiple_enablers_remix() {
1452        let plugin = remix::RemixPlugin;
1453        let deps_node = vec!["@remix-run/node".to_string()];
1454        assert!(plugin.is_enabled_with_deps(&deps_node, Path::new("/project")));
1455        let deps_react = vec!["@remix-run/react".to_string()];
1456        assert!(plugin.is_enabled_with_deps(&deps_react, Path::new("/project")));
1457        let deps_cf = vec!["@remix-run/cloudflare".to_string()];
1458        assert!(plugin.is_enabled_with_deps(&deps_cf, Path::new("/project")));
1459    }
1460
1461    struct MinimalPlugin;
1462    impl Plugin for MinimalPlugin {
1463        fn name(&self) -> &'static str {
1464            "minimal"
1465        }
1466    }
1467
1468    #[test]
1469    fn default_enablers_is_empty() {
1470        assert!(MinimalPlugin.enablers().is_empty());
1471    }
1472
1473    #[test]
1474    fn default_entry_patterns_is_empty() {
1475        assert!(MinimalPlugin.entry_patterns().is_empty());
1476    }
1477
1478    #[test]
1479    fn default_config_patterns_is_empty() {
1480        assert!(MinimalPlugin.config_patterns().is_empty());
1481    }
1482
1483    #[test]
1484    fn default_always_used_is_empty() {
1485        assert!(MinimalPlugin.always_used().is_empty());
1486    }
1487
1488    #[test]
1489    fn default_used_exports_is_empty() {
1490        assert!(MinimalPlugin.used_exports().is_empty());
1491    }
1492
1493    #[test]
1494    fn default_tooling_dependencies_is_empty() {
1495        assert!(MinimalPlugin.tooling_dependencies().is_empty());
1496    }
1497
1498    #[test]
1499    fn default_fixture_glob_patterns_is_empty() {
1500        assert!(MinimalPlugin.fixture_glob_patterns().is_empty());
1501    }
1502
1503    #[test]
1504    fn default_virtual_module_prefixes_is_empty() {
1505        assert!(MinimalPlugin.virtual_module_prefixes().is_empty());
1506    }
1507
1508    #[test]
1509    fn default_virtual_package_suffixes_is_empty() {
1510        assert!(MinimalPlugin.virtual_package_suffixes().is_empty());
1511    }
1512
1513    #[test]
1514    fn default_path_aliases_is_empty() {
1515        assert!(MinimalPlugin.path_aliases(Path::new("/")).is_empty());
1516    }
1517
1518    #[test]
1519    fn default_resolve_config_returns_empty() {
1520        let r = MinimalPlugin.resolve_config(
1521            Path::new("config.js"),
1522            "export default {}",
1523            Path::new("/"),
1524        );
1525        assert!(r.is_empty());
1526    }
1527
1528    #[test]
1529    fn default_package_json_metadata_hooks_are_empty() {
1530        let pkg = PackageJson::default();
1531        assert!(!MinimalPlugin.is_enabled_with_package_json(&pkg, Path::new("/")));
1532        assert!(
1533            MinimalPlugin
1534                .resolve_package_json(&pkg, Path::new("/"))
1535                .is_empty()
1536        );
1537    }
1538
1539    #[test]
1540    fn default_package_json_config_key_is_none() {
1541        assert!(MinimalPlugin.package_json_config_key().is_none());
1542    }
1543
1544    #[test]
1545    fn default_is_enabled_returns_false_when_no_enablers() {
1546        let deps = vec!["anything".to_string()];
1547        assert!(!MinimalPlugin.is_enabled_with_deps(&deps, Path::new("/")));
1548    }
1549
1550    #[test]
1551    fn all_builtin_plugin_names_are_unique() {
1552        let plugins = registry::builtin::create_builtin_plugins();
1553        let mut seen = std::collections::BTreeSet::new();
1554        for p in &plugins {
1555            let name = p.name();
1556            assert!(seen.insert(name), "duplicate plugin name: {name}");
1557        }
1558    }
1559
1560    #[test]
1561    fn all_builtin_plugins_have_activation_signals() {
1562        const PACKAGE_JSON_METADATA_PLUGINS: &[&str] = &["napi-rs"];
1563        let plugins = registry::builtin::create_builtin_plugins();
1564        for p in &plugins {
1565            assert!(
1566                !p.enablers().is_empty()
1567                    || !p.script_enablers().is_empty()
1568                    || PACKAGE_JSON_METADATA_PLUGINS.contains(&p.name()),
1569                "plugin '{}' has no activation signal",
1570                p.name()
1571            );
1572        }
1573    }
1574
1575    #[test]
1576    fn plugins_with_config_patterns_have_always_used() {
1577        let plugins = registry::builtin::create_builtin_plugins();
1578        for p in &plugins {
1579            if !p.config_patterns().is_empty() {
1580                assert!(
1581                    !p.always_used().is_empty(),
1582                    "plugin '{}' has config_patterns but no always_used",
1583                    p.name()
1584                );
1585            }
1586        }
1587    }
1588
1589    #[test]
1590    fn framework_plugins_enablers() {
1591        let cases: Vec<(&dyn Plugin, &[&str])> = vec![
1592            (&nextjs::NextJsPlugin, &["next"]),
1593            (&nuxt::NuxtPlugin, &["nuxt"]),
1594            (&angular::AngularPlugin, &["@angular/core"]),
1595            (&ionic::IonicPlugin, &["@ionic/angular"]),
1596            (&sveltekit::SvelteKitPlugin, &["@sveltejs/kit"]),
1597            (&gatsby::GatsbyPlugin, &["gatsby"]),
1598        ];
1599        for (plugin, expected_enablers) in cases {
1600            let enablers = plugin.enablers();
1601            for expected in expected_enablers {
1602                assert!(
1603                    enablers.contains(expected),
1604                    "plugin '{}' should have '{}'",
1605                    plugin.name(),
1606                    expected
1607                );
1608            }
1609        }
1610    }
1611
1612    #[test]
1613    fn testing_plugins_enablers() {
1614        let cases: Vec<(&dyn Plugin, &str)> = vec![
1615            (&jest::JestPlugin, "jest"),
1616            (&vitest::VitestPlugin, "vitest"),
1617            (&playwright::PlaywrightPlugin, "@playwright/test"),
1618            (&cypress::CypressPlugin, "cypress"),
1619            (&mocha::MochaPlugin, "mocha"),
1620            (&stryker::StrykerPlugin, "@stryker-mutator/core"),
1621        ];
1622        for (plugin, enabler) in cases {
1623            assert!(
1624                plugin.enablers().contains(&enabler),
1625                "plugin '{}' should have '{}'",
1626                plugin.name(),
1627                enabler
1628            );
1629        }
1630    }
1631
1632    #[test]
1633    fn bundler_plugins_enablers() {
1634        let cases: Vec<(&dyn Plugin, &str)> = vec![
1635            (&vite::VitePlugin, "vite"),
1636            (&webpack::WebpackPlugin, "webpack"),
1637            (&rollup::RollupPlugin, "rollup"),
1638        ];
1639        for (plugin, enabler) in cases {
1640            assert!(
1641                plugin.enablers().contains(&enabler),
1642                "plugin '{}' should have '{}'",
1643                plugin.name(),
1644                enabler
1645            );
1646        }
1647    }
1648
1649    #[test]
1650    fn test_plugins_have_test_entry_patterns() {
1651        let test_plugins: Vec<&dyn Plugin> = vec![
1652            &bun::BunPlugin,
1653            &jest::JestPlugin,
1654            &vitest::VitestPlugin,
1655            &mocha::MochaPlugin,
1656            &tap::TapPlugin,
1657            &tsd::TsdPlugin,
1658        ];
1659        for plugin in test_plugins {
1660            let patterns = plugin.entry_patterns();
1661            assert!(
1662                !patterns.is_empty(),
1663                "test plugin '{}' should have entry patterns",
1664                plugin.name()
1665            );
1666            assert!(
1667                patterns
1668                    .iter()
1669                    .any(|p| p.contains("test") || p.contains("spec") || p.contains("__tests__")),
1670                "test plugin '{}' should have test/spec patterns",
1671                plugin.name()
1672            );
1673        }
1674    }
1675
1676    #[test]
1677    fn framework_plugins_have_entry_patterns() {
1678        let plugins: Vec<&dyn Plugin> = vec![
1679            &nextjs::NextJsPlugin,
1680            &nuxt::NuxtPlugin,
1681            &angular::AngularPlugin,
1682            &sveltekit::SvelteKitPlugin,
1683        ];
1684        for plugin in plugins {
1685            assert!(
1686                !plugin.entry_patterns().is_empty(),
1687                "framework plugin '{}' should have entry patterns",
1688                plugin.name()
1689            );
1690        }
1691    }
1692
1693    #[test]
1694    fn plugins_with_resolve_config_have_config_patterns() {
1695        let plugins: Vec<&dyn Plugin> = vec![
1696            &jest::JestPlugin,
1697            &vitest::VitestPlugin,
1698            &babel::BabelPlugin,
1699            &eslint::EslintPlugin,
1700            &webpack::WebpackPlugin,
1701            &storybook::StorybookPlugin,
1702            &typescript::TypeScriptPlugin,
1703            &postcss::PostCssPlugin,
1704            &nextjs::NextJsPlugin,
1705            &nuxt::NuxtPlugin,
1706            &angular::AngularPlugin,
1707            &nx::NxPlugin,
1708            &stryker::StrykerPlugin,
1709            &wuchale::WuchalePlugin,
1710            &rollup::RollupPlugin,
1711            &sveltekit::SvelteKitPlugin,
1712            &prettier::PrettierPlugin,
1713            &contentlayer::ContentlayerPlugin,
1714        ];
1715        for plugin in plugins {
1716            assert!(
1717                !plugin.config_patterns().is_empty(),
1718                "plugin '{}' with resolve_config should have config_patterns",
1719                plugin.name()
1720            );
1721        }
1722    }
1723
1724    #[test]
1725    fn plugin_tooling_deps_include_enabler_package() {
1726        let plugins: Vec<&dyn Plugin> = vec![
1727            &jest::JestPlugin,
1728            &vitest::VitestPlugin,
1729            &webpack::WebpackPlugin,
1730            &typescript::TypeScriptPlugin,
1731            &eslint::EslintPlugin,
1732            &prettier::PrettierPlugin,
1733            &danger::DangerPlugin,
1734            &stryker::StrykerPlugin,
1735            &wuchale::WuchalePlugin,
1736            &contentlayer::ContentlayerPlugin,
1737        ];
1738        for plugin in plugins {
1739            let tooling = plugin.tooling_dependencies();
1740            let enablers = plugin.enablers();
1741            assert!(
1742                enablers
1743                    .iter()
1744                    .any(|e| !e.ends_with('/') && tooling.contains(e)),
1745                "plugin '{}': at least one non-prefix enabler should be in tooling_dependencies",
1746                plugin.name()
1747            );
1748        }
1749    }
1750
1751    #[test]
1752    fn nextjs_has_used_exports_for_pages() {
1753        let plugin = nextjs::NextJsPlugin;
1754        let exports = plugin.used_exports();
1755        assert!(!exports.is_empty());
1756        assert!(exports.iter().any(|(_, names)| names.contains(&"default")));
1757    }
1758
1759    #[test]
1760    fn remix_has_used_exports_for_routes() {
1761        let plugin = remix::RemixPlugin;
1762        let exports = plugin.used_exports();
1763        assert!(!exports.is_empty());
1764        let route_entry = exports.iter().find(|(pat, _)| pat.contains("routes"));
1765        assert!(route_entry.is_some());
1766        let (_, names) = route_entry.unwrap();
1767        assert!(names.contains(&"loader"));
1768        assert!(names.contains(&"action"));
1769        assert!(names.contains(&"default"));
1770    }
1771
1772    #[test]
1773    fn sveltekit_has_used_exports_for_routes() {
1774        let plugin = sveltekit::SvelteKitPlugin;
1775        let exports = plugin.used_exports();
1776        assert!(!exports.is_empty());
1777        assert!(exports.iter().any(|(_, names)| names.contains(&"GET")));
1778    }
1779
1780    #[test]
1781    fn nuxt_has_hash_virtual_prefix() {
1782        assert!(nuxt::NuxtPlugin.virtual_module_prefixes().contains(&"#"));
1783    }
1784
1785    #[test]
1786    fn sveltekit_has_dollar_virtual_prefixes() {
1787        let prefixes = sveltekit::SvelteKitPlugin.virtual_module_prefixes();
1788        assert!(prefixes.contains(&"$app/"));
1789        assert!(prefixes.contains(&"$env/"));
1790        assert!(prefixes.contains(&"$lib/"));
1791    }
1792
1793    #[test]
1794    fn sveltekit_has_lib_path_alias() {
1795        let aliases = sveltekit::SvelteKitPlugin.path_aliases(Path::new("/project"));
1796        assert!(aliases.iter().any(|(prefix, _)| *prefix == "$lib/"));
1797    }
1798
1799    #[test]
1800    fn nuxt_has_tilde_path_alias() {
1801        let aliases = nuxt::NuxtPlugin.path_aliases(Path::new("/nonexistent"));
1802        assert!(aliases.iter().any(|(prefix, _)| *prefix == "~/"));
1803        assert!(aliases.iter().any(|(prefix, _)| *prefix == "~~/"));
1804    }
1805
1806    #[test]
1807    fn jest_has_package_json_config_key() {
1808        assert_eq!(jest::JestPlugin.package_json_config_key(), Some("jest"));
1809    }
1810
1811    #[test]
1812    fn tsd_has_package_json_config_key() {
1813        assert_eq!(tsd::TsdPlugin.package_json_config_key(), Some("tsd"));
1814    }
1815
1816    #[test]
1817    fn babel_has_package_json_config_key() {
1818        assert_eq!(babel::BabelPlugin.package_json_config_key(), Some("babel"));
1819    }
1820
1821    #[test]
1822    fn eslint_has_package_json_config_key() {
1823        assert_eq!(
1824            eslint::EslintPlugin.package_json_config_key(),
1825            Some("eslintConfig")
1826        );
1827    }
1828
1829    #[test]
1830    fn prettier_has_package_json_config_key() {
1831        assert_eq!(
1832            prettier::PrettierPlugin.package_json_config_key(),
1833            Some("prettier")
1834        );
1835    }
1836
1837    #[test]
1838    fn macro_generated_plugin_basic_properties() {
1839        let plugin = msw::MswPlugin;
1840        assert_eq!(plugin.name(), "msw");
1841        assert!(plugin.enablers().contains(&"msw"));
1842        assert!(!plugin.entry_patterns().is_empty());
1843        assert!(plugin.config_patterns().is_empty());
1844        assert!(!plugin.always_used().is_empty());
1845        assert!(!plugin.tooling_dependencies().is_empty());
1846    }
1847
1848    #[test]
1849    fn macro_generated_plugin_with_used_exports() {
1850        let plugin = remix::RemixPlugin;
1851        assert_eq!(plugin.name(), "remix");
1852        assert!(!plugin.used_exports().is_empty());
1853    }
1854
1855    #[test]
1856    fn macro_passes_through_virtual_package_suffixes() {
1857        define_plugin! {
1858            struct MacroSuffixSmokePlugin => "macro-suffix-smoke",
1859            enablers: &["macro-suffix-smoke"],
1860            virtual_package_suffixes: &["/__macro_smoke__"],
1861        }
1862
1863        let plugin = MacroSuffixSmokePlugin;
1864        assert_eq!(
1865            plugin.virtual_package_suffixes(),
1866            &["/__macro_smoke__"],
1867            "macro-declared virtual_package_suffixes must propagate to the trait method"
1868        );
1869    }
1870
1871    #[test]
1872    fn macro_generated_plugin_imports_only_resolve_config() {
1873        let plugin = cypress::CypressPlugin;
1874        let source = r"
1875            import { defineConfig } from 'cypress';
1876            import coveragePlugin from '@cypress/code-coverage';
1877            export default defineConfig({});
1878        ";
1879        let result = plugin.resolve_config(
1880            Path::new("cypress.config.ts"),
1881            source,
1882            Path::new("/project"),
1883        );
1884        assert!(
1885            result
1886                .referenced_dependencies
1887                .contains(&"cypress".to_string())
1888        );
1889        assert!(
1890            result
1891                .referenced_dependencies
1892                .contains(&"@cypress/code-coverage".to_string())
1893        );
1894    }
1895
1896    #[test]
1897    fn builtin_plugin_count_is_expected() {
1898        let plugins = registry::builtin::create_builtin_plugins();
1899        assert!(
1900            plugins.len() >= 110,
1901            "expected at least 110 built-in plugins, got {}",
1902            plugins.len()
1903        );
1904    }
1905}