fallow-core 2.85.0

Analysis orchestration for fallow codebase intelligence (dead code, duplication, plugins, cross-reference)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! General tooling dependency detection.
//!
//! Known dev dependencies that are tooling (used by CLI/config, not imported in
//! application code). These complement the per-plugin `tooling_dependencies()`
//! lists with dependencies that aren't tied to any single plugin.
//!
//! The catalogue is community-maintainable: the prefix and exact lists live in
//! `crates/core/data/tooling.toml`, embedded via `include_str!` and parsed once
//! at startup. There is no regeneration step. To add a tool, edit one entry in
//! the TOML and open a PR. See `CONTRIBUTING.md`.

use rustc_hash::FxHashSet;

/// Embedded catalogue source. Because it is `include_str!`-embedded at compile
/// time, a green `catalogue_parses` test guarantees the released binary parses.
const CATALOGUE_TOML: &str = include_str!("../../data/tooling.toml");

/// Framework-plugin name markers. A package whose bare name OR whose
/// `@scope/`-stripped tail starts with one of these is a framework plugin and
/// must NOT be listed in the catalogue (its config-parsing plugin credits it
/// only when it actually appears in the config, avoiding a false negative). The
/// tail check catches scoped community forms like
/// `@ianvs/prettier-plugin-sort-imports`. Enforced by
/// `catalogue_rejects_framework_plugin_exact_entries`.
#[cfg(test)]
const FRAMEWORK_PLUGIN_FAMILY_PREFIXES: &[&str] = &[
    "vite-plugin-",
    "prettier-plugin-",
    "eslint-plugin-",
    "rollup-plugin-",
];

/// Official scoped framework-plugin namespaces, checked against the FULL name.
/// `@rollup/plugin-*` is Rollup's official plugin scope. This is deliberately
/// NOT generalized to `@scope/plugin-*`, because `@vitejs/plugin-react` and
/// peers are legitimately-kept tooling exacts.
#[cfg(test)]
const FRAMEWORK_PLUGIN_SCOPED_PREFIXES: &[&str] = &["@rollup/plugin-"];

#[derive(serde::Deserialize)]
struct ToolingCatalogue {
    #[serde(default)]
    prefix: Vec<PrefixEntry>,
    #[serde(default)]
    exact: Vec<ExactEntry>,
}

#[derive(serde::Deserialize)]
struct PrefixEntry {
    /// Match when `name.starts_with(pattern)`. Required and must be non-empty
    /// (an empty pattern would match every package, disabling unused-dep
    /// detection entirely).
    pattern: String,
    /// Optional human context; does not affect matching.
    #[expect(
        dead_code,
        reason = "documentation field, surfaced via the catalogue source"
    )]
    #[serde(default)]
    notes: Option<String>,
}

#[derive(serde::Deserialize)]
struct ExactEntry {
    /// Exact package name to credit as tooling.
    name: String,
    /// Optional grouping label; does not affect matching.
    #[expect(
        dead_code,
        reason = "documentation field, surfaced via the catalogue source"
    )]
    #[serde(default)]
    ecosystem: Option<String>,
}

/// Parsed catalogue: ordered prefix patterns + an exact-match set.
struct Catalogue {
    prefixes: Vec<String>,
    exact: FxHashSet<String>,
}

/// Parse and cache the embedded catalogue once. Panics with a clear message if
/// the embedded TOML is malformed; this is unreachable in a released binary
/// because the bytes are compile-time-embedded and gated by `catalogue_parses`.
fn catalogue() -> &'static Catalogue {
    static CATALOGUE: std::sync::OnceLock<Catalogue> = std::sync::OnceLock::new();
    CATALOGUE.get_or_init(|| {
        let parsed: ToolingCatalogue = toml::from_str(CATALOGUE_TOML).expect(
            "embedded crates/core/data/tooling.toml must parse; run \
             `cargo test -p fallow-core catalogue_parses` to see the error",
        );
        Catalogue {
            prefixes: parsed.prefix.into_iter().map(|p| p.pattern).collect(),
            exact: parsed.exact.into_iter().map(|e| e.name).collect(),
        }
    })
}

/// Check whether a package is a known tooling/dev dependency by name.
///
/// This is the single source of truth for general tooling detection.
/// Per-plugin tooling dependencies are declared via `Plugin::tooling_dependencies()`
/// and aggregated separately in `AggregatedPluginResult`.
#[must_use]
pub fn is_known_tooling_dependency(name: &str) -> bool {
    let catalogue = catalogue();
    catalogue
        .prefixes
        .iter()
        .any(|p| name.starts_with(p.as_str()))
        || catalogue.exact.contains(name)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── Prefix matching ──────────────────────────────────────────

    #[test]
    fn types_prefix_matches_scoped() {
        assert!(is_known_tooling_dependency("@types/node"));
        assert!(is_known_tooling_dependency("@types/react"));
        assert!(is_known_tooling_dependency("@types/express"));
    }

    #[test]
    fn types_prefix_does_not_match_similar_names() {
        // "type-fest" should NOT match "@types/" prefix
        assert!(!is_known_tooling_dependency("type-fest"));
        assert!(!is_known_tooling_dependency("typesafe-actions"));
    }

    #[test]
    fn storybook_not_blanket_matched() {
        // @storybook/ and storybook prefixes removed — handled by StorybookPlugin config parsing
        assert!(!is_known_tooling_dependency("@storybook/react"));
        assert!(!is_known_tooling_dependency("@storybook/addon-essentials"));
        assert!(!is_known_tooling_dependency("storybook"));
    }

    #[test]
    fn testing_library_prefix_matches() {
        assert!(is_known_tooling_dependency("@testing-library/react"));
        assert!(is_known_tooling_dependency("@testing-library/jest-dom"));
    }

    #[test]
    fn babel_not_blanket_matched() {
        // @babel/ and babel- prefixes removed — handled by BabelPlugin config parsing
        assert!(!is_known_tooling_dependency("@babel/core"));
        assert!(!is_known_tooling_dependency("@babel/preset-env"));
        assert!(!is_known_tooling_dependency("babel-loader"));
        assert!(!is_known_tooling_dependency("babel-jest"));
    }

    #[test]
    fn vitest_prefix_matches() {
        assert!(is_known_tooling_dependency("@vitest/coverage-v8"));
        assert!(is_known_tooling_dependency("@vitest/ui"));
    }

    #[test]
    fn eslint_not_blanket_matched() {
        // eslint and @typescript-eslint prefixes removed — handled by EslintPlugin config parsing
        assert!(!is_known_tooling_dependency("eslint"));
        assert!(!is_known_tooling_dependency("eslint-plugin-react"));
        assert!(!is_known_tooling_dependency("eslint-config-next"));
        assert!(!is_known_tooling_dependency("@typescript-eslint/parser"));
    }

    #[test]
    fn biomejs_prefix_matches() {
        assert!(is_known_tooling_dependency("@biomejs/biome"));
    }

    // ── Exact matching ───────────────────────────────────────────

    #[test]
    fn exact_typescript_matches() {
        assert!(is_known_tooling_dependency("typescript"));
    }

    #[test]
    fn exact_prettier_matches() {
        assert!(is_known_tooling_dependency("prettier"));
    }

    #[test]
    fn exact_vitest_matches() {
        assert!(is_known_tooling_dependency("vitest"));
    }

    #[test]
    fn exact_jest_matches() {
        assert!(is_known_tooling_dependency("jest"));
    }

    #[test]
    fn exact_vite_matches() {
        assert!(is_known_tooling_dependency("vite"));
    }

    #[test]
    fn exact_esbuild_matches() {
        assert!(is_known_tooling_dependency("esbuild"));
    }

    #[test]
    fn exact_tsup_matches() {
        assert!(is_known_tooling_dependency("tsup"));
    }

    #[test]
    fn exact_turbo_matches() {
        assert!(is_known_tooling_dependency("turbo"));
    }

    // ── Non-tooling dependencies ─────────────────────────────────

    #[test]
    fn common_runtime_deps_not_tooling() {
        assert!(!is_known_tooling_dependency("react"));
        assert!(!is_known_tooling_dependency("react-dom"));
        assert!(!is_known_tooling_dependency("express"));
        assert!(!is_known_tooling_dependency("lodash"));
        assert!(!is_known_tooling_dependency("next"));
        assert!(!is_known_tooling_dependency("vue"));
        assert!(!is_known_tooling_dependency("axios"));
    }

    #[test]
    fn empty_string_not_tooling() {
        assert!(!is_known_tooling_dependency(""));
    }

    #[test]
    fn near_miss_not_tooling() {
        // These look similar to tooling but should NOT match
        assert!(!is_known_tooling_dependency("type-fest"));
        assert!(!is_known_tooling_dependency("typestyle"));
        assert!(!is_known_tooling_dependency("prettier-bytes")); // not the exact "prettier"
        // "prettier" is an [[exact]] entry in tooling.toml, not a [[prefix]],
        // so "prettier-bytes" does not match.
    }

    #[test]
    fn sass_variants_are_tooling() {
        assert!(is_known_tooling_dependency("sass"));
        assert!(is_known_tooling_dependency("sass-embedded"));
    }

    #[test]
    fn framework_plugin_packages_no_longer_exact_matched() {
        // Issue #462: these 5 framework-plugin packages were removed from the
        // catalogue. They are credited only when they actually appear in the
        // parsed Vite / Prettier config (via the plugins' config parsers), so a
        // declared-but-unused plugin now correctly surfaces as unused instead of
        // being silently treated as used by an exact-name shadow match.
        assert!(!is_known_tooling_dependency("vite-plugin-svgr"));
        assert!(!is_known_tooling_dependency("vite-plugin-eslint"));
        assert!(!is_known_tooling_dependency("prettier-plugin-tailwindcss"));
        assert!(!is_known_tooling_dependency(
            "prettier-plugin-organize-imports"
        ));
        assert!(!is_known_tooling_dependency(
            "@ianvs/prettier-plugin-sort-imports"
        ));
    }

    // ── Additional prefix matching ────────────────────────────────

    #[test]
    fn electron_forge_prefix_matches() {
        assert!(is_known_tooling_dependency("@electron-forge/cli"));
        assert!(is_known_tooling_dependency(
            "@electron-forge/maker-squirrel"
        ));
    }

    #[test]
    fn electron_prefix_matches() {
        assert!(is_known_tooling_dependency("@electron/rebuild"));
        assert!(is_known_tooling_dependency("@electron/notarize"));
    }

    #[test]
    fn formatjs_prefix_matches() {
        assert!(is_known_tooling_dependency("@formatjs/cli"));
        assert!(is_known_tooling_dependency("@formatjs/intl"));
    }

    #[test]
    fn rollup_not_blanket_matched() {
        // @rollup/ prefix removed — handled by RollupPlugin config parsing
        assert!(!is_known_tooling_dependency("@rollup/plugin-commonjs"));
        assert!(!is_known_tooling_dependency("@rollup/plugin-node-resolve"));
        assert!(!is_known_tooling_dependency("@rollup/plugin-typescript"));
    }

    #[test]
    fn semantic_release_prefix_matches() {
        assert!(is_known_tooling_dependency("@semantic-release/github"));
        assert!(is_known_tooling_dependency("@semantic-release/npm"));
        assert!(is_known_tooling_dependency("semantic-release"));
    }

    #[test]
    fn release_it_prefix_matches() {
        assert!(is_known_tooling_dependency(
            "@release-it/conventional-changelog"
        ));
    }

    #[test]
    fn lerna_lite_prefix_matches() {
        assert!(is_known_tooling_dependency("@lerna-lite/cli"));
        assert!(is_known_tooling_dependency("@lerna-lite/publish"));
    }

    #[test]
    fn changesets_prefix_matches() {
        assert!(is_known_tooling_dependency("@changesets/cli"));
        assert!(is_known_tooling_dependency("@changesets/changelog-github"));
    }

    #[test]
    fn graphql_codegen_prefix_matches() {
        assert!(is_known_tooling_dependency("@graphql-codegen/cli"));
        assert!(is_known_tooling_dependency(
            "@graphql-codegen/typescript-operations"
        ));
    }

    #[test]
    fn secretlint_prefix_matches() {
        assert!(is_known_tooling_dependency("secretlint"));
        assert!(is_known_tooling_dependency(
            "@secretlint/secretlint-rule-preset-recommend"
        ));
    }

    #[test]
    fn oxlint_prefix_matches() {
        assert!(is_known_tooling_dependency("oxlint"));
    }

    #[test]
    fn react_native_community_prefix_matches() {
        assert!(is_known_tooling_dependency("@react-native-community/cli"));
        assert!(is_known_tooling_dependency(
            "@react-native-community/cli-platform-android"
        ));
    }

    #[test]
    fn react_native_prefix_matches() {
        assert!(is_known_tooling_dependency("@react-native/metro-config"));
        assert!(is_known_tooling_dependency(
            "@react-native/typescript-config"
        ));
    }

    #[test]
    fn jest_prefix_matches() {
        assert!(is_known_tooling_dependency("@jest/globals"));
        assert!(is_known_tooling_dependency("@jest/types"));
    }

    #[test]
    fn playwright_prefix_matches() {
        assert!(is_known_tooling_dependency("@playwright/test"));
        assert!(is_known_tooling_dependency("playwright"));
    }

    #[test]
    fn tapjs_prefix_matches() {
        assert!(is_known_tooling_dependency("@tapjs/test"));
        assert!(is_known_tooling_dependency("@tapjs/snapshot"));
    }

    // ── Additional exact matching ─────────────────────────────────

    #[test]
    fn exact_tap_matches() {
        assert!(is_known_tooling_dependency("tap"));
    }

    #[test]
    fn exact_rolldown_matches() {
        assert!(is_known_tooling_dependency("rolldown"));
        assert!(is_known_tooling_dependency("rolldown-vite"));
    }

    #[test]
    fn exact_electron_matches() {
        assert!(is_known_tooling_dependency("electron"));
        assert!(is_known_tooling_dependency("electron-builder"));
        assert!(is_known_tooling_dependency("electron-vite"));
    }

    #[test]
    fn exact_sharp_matches() {
        assert!(is_known_tooling_dependency("sharp"));
    }

    #[test]
    fn exact_puppeteer_matches() {
        assert!(is_known_tooling_dependency("puppeteer"));
    }

    #[test]
    fn exact_madge_matches() {
        assert!(is_known_tooling_dependency("madge"));
    }

    #[test]
    fn exact_patch_package_matches() {
        assert!(is_known_tooling_dependency("patch-package"));
    }

    #[test]
    fn exact_nx_matches() {
        assert!(is_known_tooling_dependency("nx"));
    }

    #[test]
    fn exact_vue_tsc_matches() {
        assert!(is_known_tooling_dependency("vue-tsc"));
    }

    #[test]
    fn exact_tsconfig_packages_match() {
        assert!(is_known_tooling_dependency("@tsconfig/node20"));
        assert!(is_known_tooling_dependency("@tsconfig/react-native"));
        assert!(is_known_tooling_dependency("@vue/tsconfig"));
    }

    #[test]
    fn exact_vitejs_plugins_match() {
        assert!(is_known_tooling_dependency("@vitejs/plugin-vue"));
        assert!(is_known_tooling_dependency("@vitejs/plugin-react"));
        assert!(is_known_tooling_dependency("@vitejs/plugin-react-swc"));
        assert!(is_known_tooling_dependency("@vitejs/plugin-legacy"));
    }

    #[test]
    fn exact_oxc_transform_matches() {
        assert!(is_known_tooling_dependency("oxc-transform"));
    }

    #[test]
    fn exact_typescript_native_preview_matches() {
        assert!(is_known_tooling_dependency("@typescript/native-preview"));
    }

    #[test]
    fn exact_tw_animate_css_matches() {
        assert!(is_known_tooling_dependency("tw-animate-css"));
    }

    #[test]
    fn exact_manypkg_cli_matches() {
        assert!(is_known_tooling_dependency("@manypkg/cli"));
    }

    #[test]
    fn exact_swc_variants_match() {
        assert!(is_known_tooling_dependency("@swc/core"));
        assert!(is_known_tooling_dependency("@swc/jest"));
    }

    // ── Negative tests for near-misses ────────────────────────────

    #[test]
    fn runtime_deps_with_similar_names_not_tooling() {
        // These are NOT tooling — they don't match any prefix or exact entry
        assert!(!is_known_tooling_dependency("react-scripts"));
        assert!(!is_known_tooling_dependency("express-validator"));
        assert!(!is_known_tooling_dependency("sass-loader")); // "sass" is exact, not prefix
    }

    #[test]
    fn postcss_not_blanket_matched() {
        // postcss, autoprefixer, tailwindcss, @tailwindcss prefixes removed —
        // handled by PostCssPlugin and TailwindPlugin config parsing
        assert!(!is_known_tooling_dependency("postcss-modules"));
        assert!(!is_known_tooling_dependency("postcss-import"));
        assert!(!is_known_tooling_dependency("autoprefixer"));
        assert!(!is_known_tooling_dependency("tailwindcss"));
        assert!(!is_known_tooling_dependency("@tailwindcss/typography"));
    }

    #[test]
    fn catalogue_is_deterministic() {
        // The OnceLock-cached catalogue returns identical results across calls.
        assert_eq!(
            is_known_tooling_dependency("typescript"),
            is_known_tooling_dependency("typescript")
        );
        assert!(is_known_tooling_dependency("typescript"));
    }

    // ── Catalogue parse + schema guards (issue #462) ──────────────

    #[test]
    fn catalogue_parses() {
        // The embedded TOML must parse and carry the canonical entries. Because
        // the bytes are include_str!-embedded at compile time, this CI test
        // passing == the released binary parses (no startup panic reachable).
        let cat = catalogue();
        assert!(!cat.prefixes.is_empty(), "catalogue must have prefixes");
        assert!(!cat.exact.is_empty(), "catalogue must have exact entries");
        assert!(cat.exact.contains("typescript"));
        assert!(cat.prefixes.iter().any(|p| p == "@types/"));
    }

    #[test]
    fn catalogue_has_no_empty_or_whitespace_prefixes() {
        // An empty / whitespace prefix would make `name.starts_with(p)` match
        // EVERY package, silently disabling unused-dependency detection.
        for prefix in &catalogue().prefixes {
            assert!(
                !prefix.trim().is_empty(),
                "catalogue prefix must be non-empty / non-whitespace; got {prefix:?}"
            );
        }
    }

    #[test]
    fn catalogue_has_no_duplicate_entries() {
        // Re-parse from source so we can detect duplicates the FxHashSet would
        // otherwise silently collapse.
        let parsed: ToolingCatalogue = toml::from_str(CATALOGUE_TOML).unwrap();

        let mut seen_exact = FxHashSet::default();
        for entry in &parsed.exact {
            assert!(
                seen_exact.insert(entry.name.as_str()),
                "duplicate exact catalogue entry: {:?}",
                entry.name
            );
        }

        let mut seen_prefix = FxHashSet::default();
        for entry in &parsed.prefix {
            assert!(
                seen_prefix.insert(entry.pattern.as_str()),
                "duplicate prefix catalogue entry: {:?}",
                entry.pattern
            );
        }
    }

    #[test]
    fn catalogue_rejects_framework_plugin_exact_entries() {
        // Framework-plugin packages (vite-plugin-*, prettier-plugin-*,
        // eslint-plugin-*, @rollup/plugin-*) must NOT be exact catalogue
        // entries: their config-parsing plugin credits them only when present
        // in the config, so listing them here re-introduces the false negative
        // issue #462 removed. Keep them out (use the plugin's config parser).
        let parsed: ToolingCatalogue = toml::from_str(CATALOGUE_TOML).unwrap();
        for entry in &parsed.exact {
            // Bare name, plus the segment after a leading `@scope/`, so a scoped
            // community plugin like `@ianvs/prettier-plugin-sort-imports` is
            // caught by its `prettier-plugin-` tail.
            let tail = entry
                .name
                .strip_prefix('@')
                .and_then(|rest| rest.split_once('/'))
                .map(|(_scope, tail)| tail);
            for bad in FRAMEWORK_PLUGIN_FAMILY_PREFIXES {
                assert!(
                    !entry.name.starts_with(bad) && !tail.is_some_and(|t| t.starts_with(bad)),
                    "exact catalogue entry {:?} is a framework plugin ({bad}); \
                     credit it in the relevant plugin's config parser instead of the catalogue",
                    entry.name,
                );
            }
            for bad in FRAMEWORK_PLUGIN_SCOPED_PREFIXES {
                assert!(
                    !entry.name.starts_with(bad),
                    "exact catalogue entry {:?} is a framework plugin ({bad}); \
                     credit it in the relevant plugin's config parser instead of the catalogue",
                    entry.name,
                );
            }
        }
    }
}