fallow-core 3.32.0

Internal detector backend for fallow-engine and fallow-api
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
//! Detection of unused package manager catalog entries and unresolved references
//! in workspace `package.json` files.
//!
//! pnpm 9+ supports two catalog forms in `pnpm-workspace.yaml`:
//! - the top-level `catalog:` map ("default" catalog)
//! - the top-level `catalogs:` map of named catalogs
//!
//! Bun supports matching catalog forms in root `package.json`, either under
//! `workspaces.catalog` / `workspaces.catalogs` or top-level `catalog` /
//! `catalogs`.
//!
//! Workspace packages reference catalog versions from their `dependencies` /
//! `devDependencies` / `peerDependencies` / `optionalDependencies` via the
//! `catalog:` protocol (`"react": "catalog:"`, `"old-react": "catalog:react17"`).
//! pnpm also resolves the protocol in override values: the `overrides` section
//! of `pnpm-workspace.yaml`, and `pnpm.overrides` and `resolutions` in the
//! root `package.json` up to pnpm 10. When these `package.json` sources are
//! not empty, pnpm 10 and earlier ignore the `pnpm-workspace.yaml` overrides.
//! pnpm before 10.5.1 does not read the `pnpm-workspace.yaml` overrides at
//! all. The `packageManager` field selects the sources; see
//! `package_manager::PackageJsonOverrideReads` and
//! `package_manager::pnpm_workspace_overrides_ignored`. These override
//! values are catalog consumers too, keyed by the override target package.
//!
//! Two findings are emitted:
//!
//! 1. **`unused-catalog-entries`**: a catalog entry no workspace `package.json`
//!    references via the `catalog:` protocol. Also tracks `hardcoded_consumers`:
//!    workspace packages declaring the same package with a non-`catalog:`
//!    version range. Helps users decide whether to delete the catalog entry or
//!    switch the consumers to `catalog:`.
//!
//! 2. **`unresolved-catalog-references`**: the inverse. A `package.json`
//!    references a `catalog:` or `catalog:<name>` that does not declare the
//!    consumed package. `pnpm install` errors with
//!    the package manager install; fallow surfaces this statically before any
//!    install runs. Each finding carries
//!    `available_in_catalogs`: other catalogs in the same workspace that DO
//!    declare the package, so consumers can flip the reference instead of
//!    adding a new catalog entry. Suppression: `ignoreCatalogReferences`
//!    config rules (per-package, optionally scoped by catalog name and/or
//!    consumer glob).
//!
//! The default catalog can be referenced as either `catalog:` (bare) or
//! `catalog:default`; both forms are treated as identical per the pnpm spec.

use std::path::{Path, PathBuf};

use fallow_config::{
    CompiledIgnoreCatalogReferenceRule, PackageJson, PnpmCatalogData, PnpmOverrideData,
    ResolvedConfig, WorkspaceDiagnostic, WorkspaceDiagnosticKind, WorkspaceInfo,
    parse_bun_package_json_resolutions, parse_override_key, parse_package_json_catalog_data,
    parse_pnpm_catalog_data, parse_pnpm_package_json_overrides, parse_pnpm_workspace_overrides,
    record_workspace_diagnostics,
};
use fallow_types::results::{EmptyCatalogGroup, UnresolvedCatalogReference, UnusedCatalogEntry};
use rustc_hash::FxHashSet;

use super::package_manager::{
    PackageJsonOverrideReads, declared_pnpm_major, pnpm_workspace_overrides_ignored,
};

mod suppressions;

use suppressions::suppressed_yaml_catalog_entries;

const PNPM_WORKSPACE_FILE: &str = "pnpm-workspace.yaml";
const PACKAGE_JSON_FILE: &str = "package.json";

/// Catalog analysis state: parsed catalog data and walked consumer references.
/// Built once per analysis run so both detectors share parsing cost.
pub struct PnpmCatalogState {
    data: PnpmCatalogData,
    consumers: CatalogConsumers,
    source_path: PathBuf,
    suppressed_entry_lines: FxHashSet<u32>,
}

/// Read catalog declarations and walk workspace `package.json` files to build
/// shared catalog analysis state. `pnpm-workspace.yaml` stays the preferred
/// source when present; otherwise Bun-style root `package.json` catalogs are
/// read from `workspaces.catalog` / `workspaces.catalogs`.
///
/// Returns `None` when `pnpm-workspace.yaml` does not parse. pnpm reads the
/// file with a more lenient parser, so its catalogs can still resolve; an
/// empty catalog would report every `catalog:` reference as unresolved.
pub fn gather_pnpm_catalog_state(
    config: &ResolvedConfig,
    workspaces: &[WorkspaceInfo],
) -> Option<PnpmCatalogState> {
    let yaml_path = config.root.join(PNPM_WORKSPACE_FILE);
    let (data, source_path, suppressed_entry_lines) =
        if let Ok(yaml_source) = std::fs::read_to_string(&yaml_path) {
            let data = match parse_pnpm_catalog_data(&yaml_source) {
                Ok(data) => data,
                Err(error) => {
                    report_malformed_pnpm_workspace_yaml(&config.root, &yaml_path, error);
                    return None;
                }
            };
            let suppressed = suppressed_yaml_catalog_entries(&yaml_source, &data);
            (data, PathBuf::from(PNPM_WORKSPACE_FILE), suppressed)
        } else {
            let package_json_path = config.root.join(PACKAGE_JSON_FILE);
            let package_json_source = std::fs::read_to_string(&package_json_path).ok()?;
            let data = parse_package_json_catalog_data(&package_json_source);
            if data.catalogs.is_empty() && data.empty_named_catalog_groups.is_empty() {
                return None;
            }
            (data, PathBuf::from(PACKAGE_JSON_FILE), FxHashSet::default())
        };
    let consumer_pkg_paths = collect_consumer_pkg_paths(config, workspaces);
    let mut consumers = collect_catalog_consumers(&consumer_pkg_paths, &config.root);
    if source_path == Path::new(PNPM_WORKSPACE_FILE) {
        collect_pnpm_override_consumers(&config.root, &mut consumers);
    }

    Some(PnpmCatalogState {
        data,
        consumers,
        source_path,
        suppressed_entry_lines,
    })
}

/// Record the degraded-continue diagnostic for a `pnpm-workspace.yaml` that
/// exists but fails to parse: catalog and override analysis proceeds with no
/// entries, and the cause surfaces in `workspace_diagnostics[]` JSON and as
/// one deduplicated stderr warning instead of being silently discarded.
pub fn report_malformed_pnpm_workspace_yaml(root: &Path, yaml_path: &Path, error: String) {
    record_workspace_diagnostics(
        root,
        vec![WorkspaceDiagnostic::new(
            root,
            yaml_path.to_path_buf(),
            WorkspaceDiagnosticKind::MalformedPnpmWorkspaceYaml { error },
        )],
    );
}

/// Emit one `UnusedCatalogEntry` for every catalog entry not referenced by any
/// workspace `package.json` via the `catalog:` protocol.
pub fn find_unused_catalog_entries(state: &PnpmCatalogState) -> Vec<UnusedCatalogEntry> {
    if state.data.catalogs.is_empty() {
        return Vec::new();
    }

    let mut findings = Vec::new();
    for catalog in &state.data.catalogs {
        for entry in &catalog.entries {
            let key = ConsumerKey {
                package_name: entry.package_name.as_str(),
                catalog_name: catalog.name.as_str(),
            };
            if state.suppressed_entry_lines.contains(&entry.line)
                || state.consumers.references.contains(&key.owned())
            {
                continue;
            }

            let hardcoded_consumers = state
                .consumers
                .hardcoded
                .iter()
                .filter(|(name, _)| name == &entry.package_name)
                .map(|(_, path)| path.clone())
                .collect();

            findings.push(UnusedCatalogEntry {
                entry_name: entry.package_name.clone(),
                catalog_name: catalog.name.clone(),
                path: state.source_path.clone(),
                line: entry.line,
                hardcoded_consumers,
            });
        }
    }

    findings
}

/// Emit one `EmptyCatalogGroup` for every named `catalogs.<name>:` group
/// that has no package entries. The top-level default `catalog:` map is
/// intentionally ignored.
pub fn find_empty_catalog_groups(state: &PnpmCatalogState) -> Vec<EmptyCatalogGroup> {
    state
        .data
        .empty_named_catalog_groups
        .iter()
        .map(|group| EmptyCatalogGroup {
            catalog_name: group.name.clone(),
            path: state.source_path.clone(),
            line: group.line,
        })
        .collect()
}

/// Emit one `UnresolvedCatalogReference` for every `catalog:` / `catalog:<name>`
/// reference whose target catalog does not declare the consumed package.
///
/// `available_in_catalogs` lists OTHER catalogs in the same workspace that DO
/// declare the package, sorted lexicographically. When non-empty, the suggested
/// fix is to flip the reference to one of those catalogs; when empty, the fix
/// is to add the missing entry to the named catalog or to remove the reference.
///
/// Findings matching any rule in `ignore_rules` are suppressed.
pub fn find_unresolved_catalog_references(
    state: &PnpmCatalogState,
    ignore_rules: &[CompiledIgnoreCatalogReferenceRule],
    root: &Path,
) -> Vec<UnresolvedCatalogReference> {
    let mut findings = Vec::new();

    for reference in &state.consumers.referenced_with_locations {
        if catalog_has_entry(
            &state.data,
            &reference.catalog_name,
            &reference.package_name,
        ) {
            continue;
        }

        let consumer_path_str = reference
            .consumer_path
            .strip_prefix(root)
            .unwrap_or(&reference.consumer_path)
            .to_string_lossy()
            .replace('\\', "/");
        if ignore_rules.iter().any(|rule| {
            rule.matches(
                &reference.package_name,
                &reference.catalog_name,
                &consumer_path_str,
            )
        }) {
            continue;
        }

        let available_in_catalogs = collect_available_in_catalogs(
            &state.data,
            &reference.package_name,
            &reference.catalog_name,
        );

        findings.push(UnresolvedCatalogReference {
            entry_name: reference.package_name.clone(),
            catalog_name: reference.catalog_name.clone(),
            path: reference.consumer_path.clone(),
            line: reference.line,
            available_in_catalogs,
        });
    }

    findings
}

fn catalog_has_entry(data: &PnpmCatalogData, catalog_name: &str, package_name: &str) -> bool {
    data.catalogs
        .iter()
        .filter(|catalog| catalog.name == catalog_name)
        .flat_map(|catalog| catalog.entries.iter())
        .any(|entry| entry.package_name == package_name)
}

fn collect_available_in_catalogs(
    data: &PnpmCatalogData,
    package_name: &str,
    excluded_catalog: &str,
) -> Vec<String> {
    let mut catalogs: Vec<String> = data
        .catalogs
        .iter()
        .filter(|catalog| catalog.name != excluded_catalog)
        .filter(|catalog| {
            catalog
                .entries
                .iter()
                .any(|entry| entry.package_name == package_name)
        })
        .map(|catalog| catalog.name.clone())
        .collect();
    catalogs.sort();
    catalogs.dedup();
    catalogs
}

/// Collect every `package.json` path that participates in the workspace:
/// the project root plus each declared workspace package.
fn collect_consumer_pkg_paths(
    config: &ResolvedConfig,
    workspaces: &[WorkspaceInfo],
) -> Vec<PathBuf> {
    let mut paths = Vec::with_capacity(workspaces.len() + 1);
    paths.push(config.root.join("package.json"));
    for ws in workspaces {
        paths.push(ws.root.join("package.json"));
    }
    paths
}

#[derive(Debug, Default)]
struct CatalogConsumers {
    /// `(package_name, catalog_name)` pairs referenced via `catalog:` protocol.
    /// Catalog name `"default"` covers both the bare `catalog:` and explicit
    /// `catalog:default` forms.
    references: FxHashSet<OwnedConsumerKey>,
    /// One entry per consumer-side `catalog:` reference, paired with the
    /// consumer `package.json` location for `unresolved-catalog-references`
    /// findings. Same package name appearing in `dependencies` AND
    /// `devDependencies` produces two entries.
    referenced_with_locations: Vec<ConsumerReference>,
    /// `(package_name, path)` pairs declaring the package with a hardcoded
    /// (non-`catalog:`) version range. Used to surface "this catalog entry
    /// is unreferenced, but these consumers declare a hardcoded version."
    hardcoded: Vec<(String, PathBuf)>,
}

#[derive(Debug, Clone)]
struct ConsumerReference {
    package_name: String,
    catalog_name: String,
    consumer_path: PathBuf,
    line: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct OwnedConsumerKey {
    package_name: String,
    catalog_name: String,
}

#[derive(Debug, Clone, Copy)]
struct ConsumerKey<'a> {
    package_name: &'a str,
    catalog_name: &'a str,
}

impl ConsumerKey<'_> {
    fn owned(self) -> OwnedConsumerKey {
        OwnedConsumerKey {
            package_name: self.package_name.to_string(),
            catalog_name: self.catalog_name.to_string(),
        }
    }
}

fn collect_catalog_consumers(pkg_paths: &[PathBuf], root: &Path) -> CatalogConsumers {
    let mut consumers = CatalogConsumers::default();
    for pkg_path in pkg_paths {
        collect_catalog_consumers_from_package(pkg_path, root, &mut consumers);
    }
    consumers
}

fn collect_catalog_consumers_from_package(
    pkg_path: &Path,
    root: &Path,
    consumers: &mut CatalogConsumers,
) {
    let Ok(raw_source) = std::fs::read_to_string(pkg_path) else {
        return;
    };
    let Ok(pkg) = serde_json::from_str::<PackageJson>(&raw_source) else {
        return;
    };
    let relative_path = pkg_path
        .strip_prefix(root)
        .map_or_else(|_| pkg_path.to_path_buf(), Path::to_path_buf);
    let line_map = scan_dep_lines(&raw_source);
    let ctx = CatalogConsumerContext {
        line_map: &line_map,
        absolute_path: pkg_path,
        relative_path: &relative_path,
    };

    for (section, deps) in [
        (DepSection::Dependencies, pkg.dependencies.as_ref()),
        (DepSection::DevDependencies, pkg.dev_dependencies.as_ref()),
        (DepSection::PeerDependencies, pkg.peer_dependencies.as_ref()),
        (
            DepSection::OptionalDependencies,
            pkg.optional_dependencies.as_ref(),
        ),
    ] {
        let Some(deps) = deps else {
            continue;
        };
        for (name, version) in deps {
            collect_catalog_consumer_dependency(consumers, name, version, section, &ctx);
        }
    }
}

/// Per-package location context for catalog consumer attribution.
struct CatalogConsumerContext<'a> {
    line_map: &'a DepLineMap,
    absolute_path: &'a Path,
    relative_path: &'a Path,
}

fn collect_catalog_consumer_dependency(
    consumers: &mut CatalogConsumers,
    name: &str,
    version: &str,
    section: DepSection,
    ctx: &CatalogConsumerContext<'_>,
) {
    if let Some(catalog) = parse_catalog_reference(version) {
        consumers.references.insert(OwnedConsumerKey {
            package_name: name.to_string(),
            catalog_name: catalog.to_string(),
        });
        let line = ctx.line_map.line_for(section, name).unwrap_or(1);
        consumers.referenced_with_locations.push(ConsumerReference {
            package_name: name.to_string(),
            catalog_name: catalog.to_string(),
            consumer_path: ctx.absolute_path.to_path_buf(),
            line,
        });
    } else if is_hardcoded_version(version) {
        consumers
            .hardcoded
            .push((name.to_string(), ctx.relative_path.to_path_buf()));
    }
}

/// Record `catalog:` values in pnpm overrides as catalog consumers.
///
/// pnpm resolves the `catalog:` protocol in the `overrides` section of
/// `pnpm-workspace.yaml`. pnpm 10 and earlier also read `pnpm.overrides` and
/// the top-level `resolutions` of the root `package.json`; pnpm 11 and later
/// ignore both. When these `package.json` sources are not empty, pnpm 10 and
/// earlier ignore the `pnpm-workspace.yaml` overrides. pnpm before 10.5.1
/// does not read the `pnpm-workspace.yaml` overrides at all. pnpm looks up the
/// catalog entry by the override target package, so
/// `"parent>child": "catalog:x"` consumes the `child` entry of catalog `x`. A
/// malformed `pnpm-workspace.yaml` is already reported by the caller.
fn collect_pnpm_override_consumers(root: &Path, consumers: &mut CatalogConsumers) {
    let package_json_path = root.join(PACKAGE_JSON_FILE);
    let package_json_source = std::fs::read_to_string(&package_json_path).ok();
    let yaml_ignored = package_json_source
        .as_deref()
        .and_then(|source| serde_json::from_str::<serde_json::Value>(source).ok())
        .is_some_and(|manifest| pnpm_workspace_overrides_ignored(&manifest).is_some());

    let yaml_path = root.join(PNPM_WORKSPACE_FILE);
    if !yaml_ignored
        && let Some(data) = std::fs::read_to_string(&yaml_path)
            .ok()
            .and_then(|source| parse_pnpm_workspace_overrides(&source).ok())
    {
        collect_override_entries(&data, &yaml_path, consumers);
    }

    let Some(source) = package_json_source else {
        return;
    };
    let reads = PackageJsonOverrideReads::for_pnpm_major(declared_pnpm_major(&source));
    if !reads.pnpm_overrides {
        return;
    }
    let overrides = parse_pnpm_package_json_overrides(&source);
    collect_override_entries(&overrides, &package_json_path, consumers);
    if reads.resolutions {
        let resolutions = pnpm_resolutions(&source, &overrides);
        collect_override_entries(&resolutions, &package_json_path, consumers);
    }
}

/// The top-level `resolutions` entries that pnpm 10 applies. pnpm parses
/// these keys with its own override grammar (a yarn path such as `a/b` fails
/// the install), and a key in `pnpm.overrides` replaces the same key here.
fn pnpm_resolutions(source: &str, pnpm_overrides: &PnpmOverrideData) -> PnpmOverrideData {
    let shadowed: FxHashSet<&str> = pnpm_overrides
        .entries
        .iter()
        .map(|entry| entry.raw_key.as_str())
        .collect();
    let mut data = parse_bun_package_json_resolutions(source);
    data.entries
        .retain(|entry| !shadowed.contains(entry.raw_key.as_str()));
    for entry in &mut data.entries {
        entry.parsed_key = parse_override_key(&entry.raw_key);
    }
    data
}

fn collect_override_entries(
    data: &PnpmOverrideData,
    source_path: &Path,
    consumers: &mut CatalogConsumers,
) {
    for entry in &data.entries {
        let Some(parsed_key) = entry.parsed_key.as_ref() else {
            continue;
        };
        let Some(catalog) = entry.raw_value.as_deref().and_then(parse_catalog_reference) else {
            continue;
        };
        let package_name = parsed_key.target_package.clone();
        consumers.references.insert(OwnedConsumerKey {
            package_name: package_name.clone(),
            catalog_name: catalog.to_string(),
        });
        consumers.referenced_with_locations.push(ConsumerReference {
            package_name,
            catalog_name: catalog.to_string(),
            consumer_path: source_path.to_path_buf(),
            line: entry.line,
        });
    }
}

/// Parse a `catalog:` protocol value. Returns the catalog name (`"default"`
/// for bare `catalog:` and explicit `catalog:default`, or the named catalog).
/// Returns `None` for any non-catalog version string.
fn parse_catalog_reference(value: &str) -> Option<&str> {
    let rest = value.strip_prefix("catalog:")?;
    if rest.is_empty() || rest == "default" {
        Some("default")
    } else {
        Some(rest)
    }
}

/// Identify version strings that represent a hardcoded version range, as
/// opposed to a workspace cross-reference (`workspace:*`, `workspace:^`),
/// a filesystem path (`file:..`), or a symlinked dependency (`link:..`).
/// Catalog references are handled by the caller and never reach this
/// function. Surfacing only true hardcoded ranges keeps
/// `hardcoded_consumers` actionable: the user can decide whether to switch
/// the consumer to `catalog:` rather than chase an internal workspace
/// reference.
fn is_hardcoded_version(value: &str) -> bool {
    !(value.starts_with("workspace:")
        || value.starts_with("file:")
        || value.starts_with("link:")
        || value.starts_with("portal:"))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum DepSection {
    Dependencies,
    DevDependencies,
    PeerDependencies,
    OptionalDependencies,
}

const DEP_SECTIONS: &[DepSection] = &[
    DepSection::Dependencies,
    DepSection::DevDependencies,
    DepSection::PeerDependencies,
    DepSection::OptionalDependencies,
];

impl DepSection {
    const fn json_key(self) -> &'static str {
        match self {
            Self::Dependencies => "dependencies",
            Self::DevDependencies => "devDependencies",
            Self::PeerDependencies => "peerDependencies",
            Self::OptionalDependencies => "optionalDependencies",
        }
    }
}

/// Maps `(section, package_name)` to its 1-based line number in the
/// `package.json` source. Built via a section-aware line scanner so we can
/// surface the consumer-side reference location for
/// `unresolved-catalog-references` findings.
#[derive(Debug, Default)]
struct DepLineMap {
    entries: Vec<((DepSection, String), u32)>,
}

impl DepLineMap {
    fn line_for(&self, section: DepSection, package_name: &str) -> Option<u32> {
        self.entries
            .iter()
            .find(|((sec, pkg), _)| *sec == section && pkg == package_name)
            .map(|(_, line)| *line)
    }
}

/// Walk the raw package.json source and record the 1-based line for each
/// `"name":` key under the four dep sections. Tracks brace depth so nested
/// objects under unrelated keys (e.g., `bin`, `peerDependenciesMeta`) do not
/// pollute the map.
///
/// Plain JSON: no comments, no trailing commas. A package.json with comments
/// would fail `serde_json::from_str` upstream and never reach this scanner.
fn scan_dep_lines(source: &str) -> DepLineMap {
    let mut scanner = DepLineScanner::default();
    for (idx, raw_line) in source.lines().enumerate() {
        scanner.scan_line(idx, raw_line);
    }
    scanner.finish()
}

#[derive(Default)]
struct DepLineScanner {
    entries: Vec<((DepSection, String), u32)>,
    current_section: Option<DepSection>,
    section_depth_at_open: u32,
    current_depth: u32,
}

impl DepLineScanner {
    fn scan_line(&mut self, idx: usize, raw_line: &str) {
        let line_no = u32::try_from(idx).unwrap_or(u32::MAX).saturating_add(1);
        let trimmed = raw_line.trim();
        self.enter_section_if_opened(trimmed, raw_line);

        let depth_before = self.current_depth;
        let delta = brace_delta_outside_strings(raw_line);
        self.record_dep_key_if_in_section(depth_before, trimmed, line_no);
        self.current_depth = depth_before
            .saturating_add(delta.opens)
            .saturating_sub(delta.closes);
        self.leave_section_if_closed();
    }

    fn enter_section_if_opened(&mut self, trimmed: &str, raw_line: &str) {
        if self.current_section.is_some() {
            return;
        }

        for section in DEP_SECTIONS {
            let needle = format!("\"{}\"", section.json_key());
            if trimmed.starts_with(&needle) && raw_line.contains('{') {
                self.current_section = Some(*section);
                self.section_depth_at_open = self.current_depth.saturating_add(1);
                break;
            }
        }
    }

    fn record_dep_key_if_in_section(&mut self, depth_before: u32, trimmed: &str, line_no: u32) {
        if let Some(section) = self.current_section
            && depth_before == self.section_depth_at_open
            && let Some(name) = parse_json_key(trimmed)
        {
            self.entries.push(((section, name), line_no));
        }
    }

    fn leave_section_if_closed(&mut self) {
        if self.current_section.is_some() && self.current_depth < self.section_depth_at_open {
            self.current_section = None;
        }
    }

    fn finish(self) -> DepLineMap {
        DepLineMap {
            entries: self.entries,
        }
    }
}

#[derive(Default)]
struct BraceDelta {
    opens: u32,
    closes: u32,
}

fn brace_delta_outside_strings(raw_line: &str) -> BraceDelta {
    let mut delta = BraceDelta::default();
    let mut in_string = false;
    let mut escaped = false;

    for ch in raw_line.chars() {
        if escaped {
            escaped = false;
            continue;
        }
        if ch == '\\' {
            escaped = true;
            continue;
        }
        if ch == '"' {
            in_string = !in_string;
            continue;
        }
        if in_string {
            continue;
        }
        match ch {
            '{' => delta.opens = delta.opens.saturating_add(1),
            '}' => delta.closes = delta.closes.saturating_add(1),
            _ => {}
        }
    }

    delta
}

/// Parse a JSON object key from a trimmed line of the form `"key": value,?`.
/// Returns `None` for lines that do not start with a quoted key followed by
/// `:`.
fn parse_json_key(trimmed: &str) -> Option<String> {
    let rest = trimmed.strip_prefix('"')?;
    let end = rest.find('"')?;
    let key = &rest[..end];
    let after = rest[end.saturating_add(1)..].trim_start();
    if after.starts_with(':') {
        Some(key.to_string())
    } else {
        None
    }
}

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

    #[test]
    fn malformed_pnpm_workspace_yaml_records_a_diagnostic_and_skips_catalog_analysis() {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            dir.path().join("pnpm-workspace.yaml"),
            "catalog:\n  react: ^18.2.0\n{this is\nnot: valid: yaml: at: all\n",
        )
        .expect("write pnpm-workspace.yaml");
        let config = fallow_config::FallowConfig::default().resolve(
            dir.path().to_path_buf(),
            fallow_config::OutputFormat::Human,
            1,
            true,
            true,
            None,
        );

        assert!(
            gather_pnpm_catalog_state(&config, &[]).is_none(),
            "an unparsed catalog must not report every `catalog:` reference as unresolved"
        );
        let diagnostics = fallow_config::workspace_diagnostics_for(dir.path());
        assert!(
            diagnostics.iter().any(|d| matches!(
                d.kind,
                WorkspaceDiagnosticKind::MalformedPnpmWorkspaceYaml { .. }
            )),
            "expected a malformed-pnpm-workspace-yaml diagnostic: {diagnostics:?}"
        );
    }

    #[test]
    fn parses_bare_catalog_as_default() {
        assert_eq!(parse_catalog_reference("catalog:"), Some("default"));
        assert_eq!(parse_catalog_reference("catalog:default"), Some("default"));
    }

    #[test]
    fn parses_named_catalog() {
        assert_eq!(parse_catalog_reference("catalog:react17"), Some("react17"));
    }

    #[test]
    fn non_catalog_versions_return_none() {
        assert_eq!(parse_catalog_reference("^18.2.0"), None);
        assert_eq!(parse_catalog_reference("workspace:*"), None);
        assert_eq!(parse_catalog_reference("npm:other-pkg@^1.0.0"), None);
        assert_eq!(parse_catalog_reference(""), None);
    }

    #[test]
    fn workspace_and_link_protocols_are_not_hardcoded() {
        assert!(!is_hardcoded_version("workspace:*"));
        assert!(!is_hardcoded_version("workspace:^"));
        assert!(!is_hardcoded_version("workspace:~"));
        assert!(!is_hardcoded_version("file:../other-pkg"));
        assert!(!is_hardcoded_version("link:../symlinked"));
        assert!(!is_hardcoded_version("portal:../portal"));
    }

    #[test]
    fn semver_ranges_and_npm_specs_are_hardcoded() {
        assert!(is_hardcoded_version("^1.0.0"));
        assert!(is_hardcoded_version("~2.5.0"));
        assert!(is_hardcoded_version("1.2.3"));
        assert!(is_hardcoded_version(">=1.0.0 <2.0.0"));
        assert!(is_hardcoded_version("npm:other-pkg@^1.0.0"));
        assert!(is_hardcoded_version("github:user/repo#commit"));
        assert!(is_hardcoded_version("https://example.com/pkg.tgz"));
    }

    #[test]
    fn scan_dep_lines_captures_each_section() {
        let source = r#"{
  "name": "demo",
  "dependencies": {
    "react": "catalog:react17",
    "lodash": "^4.0.0"
  },
  "devDependencies": {
    "vitest": "catalog:"
  }
}
"#;
        let map = scan_dep_lines(source);
        assert_eq!(map.line_for(DepSection::Dependencies, "react"), Some(4));
        assert_eq!(map.line_for(DepSection::Dependencies, "lodash"), Some(5));
        assert_eq!(map.line_for(DepSection::DevDependencies, "vitest"), Some(8));
        assert_eq!(map.line_for(DepSection::Dependencies, "vitest"), None);
    }

    #[test]
    fn scan_dep_lines_ignores_nested_object_keys() {
        let source = r#"{
  "peerDependencies": {
    "react": "*"
  },
  "peerDependenciesMeta": {
    "react": {
      "optional": true
    }
  }
}
"#;
        let map = scan_dep_lines(source);
        assert_eq!(map.line_for(DepSection::PeerDependencies, "react"), Some(3));
        let peer_react_hits = map
            .entries
            .iter()
            .filter(|((sec, name), _)| *sec == DepSection::PeerDependencies && name == "react")
            .count();
        assert_eq!(peer_react_hits, 1);
    }

    #[test]
    fn declared_pnpm_major_reads_the_package_manager_field() {
        let major = |field: &str| declared_pnpm_major(&format!(r#"{{"packageManager":{field}}}"#));
        assert_eq!(major(r#""pnpm@10.34.5""#), Some(10));
        assert_eq!(major(r#""pnpm@11.28.3+sha512.0123abcd""#), Some(11));
        assert_eq!(major(r#""pnpm@9""#), Some(9));
        assert_eq!(major(r#""yarn@4.5.0""#), None);
        assert_eq!(major(r#""pnpm@latest""#), None);
        assert_eq!(major("10"), None);
        assert_eq!(declared_pnpm_major("{}"), None);
        assert_eq!(declared_pnpm_major("not json"), None);
    }

    #[test]
    fn collect_available_in_catalogs_excludes_the_unresolved_one() {
        let data = parse_pnpm_catalog_data(
            r"
catalogs:
  react17:
    react: ^17.0.2
  react18:
    react: ^18.2.0
",
        )
        .expect("valid yaml");
        let available = collect_available_in_catalogs(&data, "react", "react17");
        assert_eq!(available, vec!["react18".to_string()]);
    }

    #[test]
    fn catalog_has_entry_default_form() {
        let data = parse_pnpm_catalog_data(
            r"
catalog:
  react: ^18.2.0
",
        )
        .expect("valid yaml");
        assert!(catalog_has_entry(&data, "default", "react"));
        assert!(!catalog_has_entry(&data, "default", "vue"));
        assert!(!catalog_has_entry(&data, "react17", "react"));
    }
}