fallow-core 2.77.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
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
//! Jest test runner plugin.
//!
//! Detects Jest projects and marks test files as entry points.
//! Parses jest.config to extract setupFiles, testMatch, transform,
//! reporters, testEnvironment, preset, globalSetup/Teardown, watchPlugins,
//! resolver, snapshotSerializers, testRunner, and runner as referenced dependencies.
//!
//! Monorepo Jest configs that delegate test discovery to per-project
//! configs via the `projects` field are followed in two shapes:
//!
//! 1. String paths / globs (`<rootDir>/packages/*` or
//!    `<rootDir>/packages/*/jest.config.js`). Each match is resolved to a
//!    concrete config file (`jest.config.{ts,js,mjs,cjs,json}` inside a
//!    matched directory, or the matched file itself) or a per-package
//!    `package.json` carrying a top-level `"jest"` key. The resolved
//!    config is parsed recursively.
//! 2. Inline `ProjectConfig` objects
//!    (`projects: [{ preset: "ts-jest", runner: "jest-runner-eslint" }]`).
//!    Each object's package-typed fields are credited as referenced
//!    dependencies and its setup files are credited as setup entry points.

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

use rustc_hash::FxHashSet;

use super::config_parser;
use super::{Plugin, PluginResult};

/// Built-in Jest reporter names that should not be treated as dependencies.
const BUILTIN_REPORTERS: &[&str] = &["default", "verbose", "summary"];

/// Maximum depth for following `projects` chains across nested Jest configs.
/// Real monorepos rarely chain more than 2 levels (root, then per-package);
/// 4 is a generous ceiling that also bounds pathological inputs.
const MAX_PROJECTS_DEPTH: usize = 4;

/// Maximum number of child configs accepted from a single `projects` entry.
/// Real monorepos contain dozens of packages, not thousands; this ceiling
/// keeps pathological globs (`projects: ["**/*"]`) bounded.
/// Scope is per-entry: a config with N entries can still resolve up to
/// N * MAX_EXPANDED_PROJECTS children. That is fine in practice because
/// real configs use a small handful of focused globs, not many broad ones.
const MAX_EXPANDED_PROJECTS: usize = 64;

/// Hard cap on raw glob iterations (matches inspected, accepted or not).
/// `MAX_EXPANDED_PROJECTS` only bounds accepted child configs, so a `**/*`
/// across a tree of non-config files would otherwise still iterate every
/// match. This cap bounds the iteration itself.
const MAX_GLOB_ITERATIONS: usize = 1024;

/// Filenames a Jest project directory may contain, in resolution order
/// (matches Jest's own `--config` lookup precedence). `package.json` is
/// probed last because Jest uses it only when no dedicated config is found.
const JEST_CONFIG_FILENAMES: &[&str] = &[
    "jest.config.ts",
    "jest.config.js",
    "jest.config.mjs",
    "jest.config.cjs",
    "jest.config.json",
];

/// Filename whose top-level `"jest"` key holds an embedded Jest config.
const PACKAGE_JSON_FILENAME: &str = "package.json";

define_plugin!(
    struct JestPlugin => "jest",
    enablers: &["jest"],
    entry_patterns: &[
        "**/*.test.{ts,tsx,js,jsx}",
        "**/*.spec.{ts,tsx,js,jsx}",
        "**/__tests__/**/*.{ts,tsx,js,jsx}",
        "**/__mocks__/**/*.{ts,tsx,js,jsx,mjs,cjs}",
    ],
    config_patterns: &["jest.config.{ts,js,mjs,cjs}", "jest.config.json"],
    always_used: &["jest.config.{ts,js,mjs,cjs}", "jest.setup.{ts,js,tsx,jsx}"],
    tooling_dependencies: &["jest", "jest-environment-jsdom", "ts-jest", "babel-jest"],
    fixture_glob_patterns: &[
        "**/__fixtures__/**/*.{ts,tsx,js,jsx,json}",
        "**/fixtures/**/*.{ts,tsx,js,jsx,json}",
    ],
    package_json_config_key: "jest",
    resolve_config(config_path, source, root) {
        let mut result = PluginResult::default();
        let mut visited = FxHashSet::default();
        extract_jest_config(config_path, source, root, &mut result, &mut visited, 0);
        result
    },
);

/// Parse a Jest config and recurse into any `projects` entries.
///
/// The visited set is keyed by canonicalized config path so cycles
/// (`a` referencing `b` referencing `a`) terminate after one full pass
/// across each file.
fn extract_jest_config(
    config_path: &Path,
    source: &str,
    root: &Path,
    result: &mut PluginResult,
    visited: &mut FxHashSet<PathBuf>,
    depth: usize,
) {
    let key = std::fs::canonicalize(config_path).unwrap_or_else(|_| config_path.to_path_buf());
    if !visited.insert(key) {
        return;
    }

    // Resolve the on-disk file shape into a (parse_source, parse_path) pair
    // that the AST helpers can ingest. `package.json` is parsed via serde to
    // pluck out the `"jest"` key, then re-serialised as a parenthesised
    // expression so the helpers see the same shape they would for a
    // standalone `jest.config.json`.
    let filename = config_path.file_name().and_then(|n| n.to_str());
    let (parse_source, parse_path_buf) = if filename == Some(PACKAGE_JSON_FILENAME) {
        let Some(jest_str) = extract_package_json_jest_value(source) else {
            return;
        };
        (format!("({jest_str})"), config_path.with_extension("js"))
    } else if config_path.extension().is_some_and(|ext| ext == "json") {
        (format!("({source})"), config_path.with_extension("js"))
    } else {
        (source.to_string(), config_path.to_path_buf())
    };
    let parse_path: &Path = &parse_path_buf;

    // Extract import sources as referenced dependencies
    let imports = config_parser::extract_imports(&parse_source, parse_path);
    for imp in &imports {
        let dep = crate::resolve::extract_package_name(imp);
        result.referenced_dependencies.push(dep);
    }

    extract_jest_setup_files(&parse_source, parse_path, root, result);
    extract_jest_dependencies(&parse_source, parse_path, result);

    if depth >= MAX_PROJECTS_DEPTH {
        return;
    }
    extract_jest_inline_projects(&parse_source, parse_path, root, result);

    let project_entries =
        config_parser::extract_config_string_array(&parse_source, parse_path, &["projects"]);
    for entry in &project_entries {
        for child_config in expand_project_entry(entry, config_path, root) {
            let Ok(child_source) = std::fs::read_to_string(&child_config) else {
                continue;
            };
            // Each child config carries its own <rootDir>: the child's
            // own directory. Setup files are resolved against it.
            let child_root = child_config
                .parent()
                .map_or_else(|| root.to_path_buf(), Path::to_path_buf);

            // Jest's `projects` semantics scope `testMatch` / `testRegex` /
            // `replace_entry_patterns` to each child individually: a narrow
            // pattern in one project must not replace the parent's broad
            // defaults for sibling projects. Run each child into a scratch
            // result and merge only the workspace-global fields back.
            let mut child_result = PluginResult::default();
            extract_jest_config(
                &child_config,
                &child_source,
                &child_root,
                &mut child_result,
                visited,
                depth + 1,
            );
            result
                .referenced_dependencies
                .extend(child_result.referenced_dependencies);
            result.setup_files.extend(child_result.setup_files);
        }
    }
}

/// Parse `package.json` source and return its top-level `"jest"` value
/// re-serialised as a JSON string suitable for wrapping in parentheses
/// and feeding to the existing AST-based config helpers.
fn extract_package_json_jest_value(source: &str) -> Option<String> {
    let parsed: serde_json::Value = serde_json::from_str(source).ok()?;
    let jest_val = parsed.get("jest")?;
    if !jest_val.is_object() {
        // `"jest": "./jest.config.js"` (string preset path) and other
        // non-object shapes are not currently followed; only the inline
        // object form participates in dependency / setup-file extraction.
        return None;
    }
    serde_json::to_string(jest_val).ok()
}

/// Credit referenced dependencies and setup files for inline
/// `ProjectConfig` objects in a `projects: [...]` array, e.g.
/// `projects: [{ preset: "ts-jest", runner: "jest-runner-eslint" }]`.
///
/// Each field follows the same rules as the top-level extraction
/// (`extract_jest_setup_files` / `extract_jest_dependencies`): built-in
/// runners / environments are filtered, relative-path resolvers are
/// dropped, and the existing `BUILTIN_REPORTERS` list applies. `transform`
/// (an object whose values are package names) is intentionally not
/// followed when it appears inline, because the existing
/// `extract_config_shallow_strings` helper only walks the top level.
///
/// All field reads route through
/// `extract_config_array_nested_string_or_array`, which inspects
/// only object elements of the array (string elements like
/// `"<rootDir>/packages/*"` are skipped here and handled separately by
/// the string-path expansion path).
fn extract_jest_inline_projects(
    parse_source: &str,
    parse_path: &Path,
    root: &Path,
    result: &mut PluginResult,
) {
    let read = |key: &str| -> Vec<String> {
        config_parser::extract_config_array_nested_string_or_array(
            parse_source,
            parse_path,
            &["projects"],
            &[key],
        )
    };

    // preset: every value is a referenced package.
    for value in read("preset") {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(&value));
    }

    // resolver: skip relative / absolute paths, credit bare package names.
    for value in read("resolver") {
        if value.starts_with('.') || value.starts_with('/') {
            continue;
        }
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(&value));
    }

    // testRunner: filter builtin runner names.
    for value in read("testRunner") {
        if !matches!(
            value.as_str(),
            "jest-jasmine2" | "jest-circus" | "jest-circus/runner"
        ) {
            result
                .referenced_dependencies
                .push(crate::resolve::extract_package_name(&value));
        }
    }

    // runner: filter the default `jest-runner` name.
    for value in read("runner") {
        if value != "jest-runner" {
            result
                .referenced_dependencies
                .push(crate::resolve::extract_package_name(&value));
        }
    }

    // testEnvironment: filter node / jsdom; otherwise just the bare value
    // is credited. Jest accepts both the bare and `jest-environment-*`
    // resolution forms; whichever the user wrote is what fallow records.
    for value in read("testEnvironment") {
        if !matches!(value.as_str(), "node" | "jsdom") {
            result
                .referenced_dependencies
                .push(crate::resolve::extract_package_name(&value));
        }
    }

    // String-array (or single-string) setup file fields, made absolute
    // against the parent <rootDir>. Inline ProjectConfigs share the parent
    // config file's filesystem location so per-project rebasing does not
    // apply.
    for key in [
        "setupFiles",
        "setupFilesAfterEnv",
        "globalSetup",
        "globalTeardown",
    ] {
        for value in read(key) {
            result
                .setup_files
                .push(root.join(value.trim_start_matches("./")));
        }
    }

    for value in read("snapshotSerializers") {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(&value));
    }
    for value in read("watchPlugins") {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(&value));
    }
    for value in read("reporters") {
        if !BUILTIN_REPORTERS.contains(&value.as_str()) {
            result
                .referenced_dependencies
                .push(crate::resolve::extract_package_name(&value));
        }
    }
}

/// Resolve a `projects:` entry to one or more concrete Jest config files.
///
/// Accepts directories (probed for any `jest.config.{ts,js,mjs,cjs,json}`,
/// then `package.json` with a `"jest"` key), direct config file paths,
/// and glob patterns of either form.
///
/// Two caps bound the work: `MAX_GLOB_ITERATIONS` limits the raw number
/// of matches inspected (so `**/*` on a deep tree of non-config files
/// still terminates promptly), and `MAX_EXPANDED_PROJECTS` limits the
/// number of accepted child configs.
fn expand_project_entry(entry: &str, config_path: &Path, root: &Path) -> Vec<PathBuf> {
    let resolved = resolve_project_pattern(entry, config_path, root);
    let pattern_str = resolved.to_string_lossy();
    let mut configs = Vec::new();

    let Ok(matches) = glob::glob(&pattern_str) else {
        return configs;
    };
    let mut iterations = 0_usize;
    for matched in matches.flatten() {
        iterations += 1;
        if iterations > MAX_GLOB_ITERATIONS || configs.len() >= MAX_EXPANDED_PROJECTS {
            break;
        }
        if matched.is_dir() {
            if let Some(found) = probe_directory_for_jest_config(&matched) {
                configs.push(found);
            }
        } else if matched.is_file() && is_recognised_config_path(&matched) {
            configs.push(matched);
        }
    }
    configs
}

/// Probe a matched directory for a recognised Jest config in resolution
/// order: dedicated `jest.config.{ts,js,mjs,cjs,json}` first, then
/// `package.json` carrying a top-level `"jest"` key.
fn probe_directory_for_jest_config(dir: &Path) -> Option<PathBuf> {
    for filename in JEST_CONFIG_FILENAMES {
        let candidate = dir.join(filename);
        if candidate.is_file() {
            return Some(candidate);
        }
    }
    let pkg = dir.join(PACKAGE_JSON_FILENAME);
    if pkg.is_file() && package_json_has_jest_key(&pkg) {
        return Some(pkg);
    }
    None
}

/// True when `package.json` exists and has a top-level `"jest"` object.
/// Reading + parsing the file is cheap relative to the cost of recursing
/// into a synthesised config that turns out to be empty.
fn package_json_has_jest_key(path: &Path) -> bool {
    let Ok(raw) = std::fs::read_to_string(path) else {
        return false;
    };
    let Ok(parsed) = serde_json::from_str::<serde_json::Value>(&raw) else {
        return false;
    };
    parsed.get("jest").is_some_and(serde_json::Value::is_object)
}

/// True when `path`'s filename is a recognised Jest config (dedicated
/// config file or `package.json` whose `"jest"` key holds an object).
fn is_recognised_config_path(path: &Path) -> bool {
    let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
        return false;
    };
    if JEST_CONFIG_FILENAMES.contains(&name) {
        return true;
    }
    name == PACKAGE_JSON_FILENAME && package_json_has_jest_key(path)
}

/// Substitute `<rootDir>` and resolve relative paths against the config file.
fn resolve_project_pattern(entry: &str, config_path: &Path, root: &Path) -> PathBuf {
    if let Some(rest) = entry.strip_prefix("<rootDir>") {
        let trimmed = rest.trim_start_matches(['/', '\\']);
        return root.join(trimmed);
    }
    let path = Path::new(entry);
    if path.is_absolute() {
        return path.to_path_buf();
    }
    // Bare `projects` entries resolve against the config file's parent.
    config_path.parent().unwrap_or(root).join(entry)
}

/// Extract setup files from Jest config (setupFiles, setupFilesAfterEnv, globalSetup, globalTeardown).
fn extract_jest_setup_files(
    parse_source: &str,
    parse_path: &Path,
    root: &Path,
    result: &mut PluginResult,
) {
    // preset → referenced dependency (e.g., "ts-jest", "react-native")
    if let Some(preset) =
        config_parser::extract_config_string(parse_source, parse_path, &["preset"])
    {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(&preset));
    }

    for key in &["setupFiles", "setupFilesAfterEnv"] {
        let files = config_parser::extract_config_string_array(parse_source, parse_path, &[key]);
        for f in &files {
            result
                .setup_files
                .push(root.join(f.trim_start_matches("./")));
        }
    }

    for key in &["globalSetup", "globalTeardown"] {
        if let Some(path) = config_parser::extract_config_string(parse_source, parse_path, &[key]) {
            result
                .setup_files
                .push(root.join(path.trim_start_matches("./")));
        }
    }

    // testMatch → entry patterns that replace defaults
    // Jest treats testMatch as a full override of its default patterns,
    // so when present the static ENTRY_PATTERNS should be dropped.
    let test_match =
        config_parser::extract_config_string_array(parse_source, parse_path, &["testMatch"]);
    if !test_match.is_empty() {
        result.replace_entry_patterns = true;
    }
    result.extend_entry_patterns(test_match);

    // testRegex → convert to best-effort glob and replace defaults
    // Jest's testRegex restricts which files are tests. Common pattern: "src/.*\\.test\\.ts$"
    // Extract a directory prefix (if any) and generate a matching glob.
    if result.entry_patterns.is_empty()
        && let Some(regex) =
            config_parser::extract_config_string(parse_source, parse_path, &["testRegex"])
        && let Some(glob) = test_regex_to_glob(&regex)
    {
        result.replace_entry_patterns = true;
        result.push_entry_pattern(glob);
    }
}

/// Best-effort conversion of a Jest `testRegex` to a glob pattern.
///
/// Handles common patterns like:
/// - `"src/.*\\.test\\.ts$"` → `"src/**/*.test.ts"`
/// - `".*\\.(test|spec)\\.tsx?$"` → stays as defaults (no fixed prefix)
fn test_regex_to_glob(regex: &str) -> Option<String> {
    // Extract a fixed directory prefix before the first regex metachar
    let meta_chars = ['.', '*', '+', '?', '(', '[', '|', '^', '$', '{', '\\'];
    let prefix_end = regex
        .find(|c: char| meta_chars.contains(&c))
        .unwrap_or(regex.len());
    let prefix = &regex[..prefix_end];

    // Must have a non-empty directory prefix to be useful (otherwise same as defaults)
    if prefix.is_empty() || !prefix.contains('/') {
        return None;
    }

    // Detect file extension from the regex suffix
    let ext = if regex.contains("tsx?") {
        "{ts,tsx}"
    } else if regex.contains("jsx?") {
        "{js,jsx}"
    } else if regex.contains("\\.ts") {
        "ts"
    } else if regex.contains("\\.js") {
        "js"
    } else {
        "{ts,tsx,js,jsx}"
    };

    // Detect test naming convention
    let name_pattern = if regex.contains("(test|spec)") || regex.contains("(spec|test)") {
        "*.{test,spec}"
    } else if regex.contains("\\.spec\\.") {
        "*.spec"
    } else {
        "*.test"
    };

    Some(format!("{prefix}**/{name_pattern}.{ext}"))
}

/// Extract referenced dependencies from Jest config (transform, reporters, environment, etc.).
fn extract_jest_dependencies(parse_source: &str, parse_path: &Path, result: &mut PluginResult) {
    // transform values → referenced dependencies
    let transform_values =
        config_parser::extract_config_shallow_strings(parse_source, parse_path, "transform");
    for val in &transform_values {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(val));
    }

    // reporters → referenced dependencies
    let reporters =
        config_parser::extract_config_shallow_strings(parse_source, parse_path, "reporters");
    for reporter in &reporters {
        if !BUILTIN_REPORTERS.contains(&reporter.as_str()) {
            result
                .referenced_dependencies
                .push(crate::resolve::extract_package_name(reporter));
        }
    }

    // testEnvironment → if not built-in, it's a referenced dependency
    if let Some(env) =
        config_parser::extract_config_string(parse_source, parse_path, &["testEnvironment"])
        && !matches!(env.as_str(), "node" | "jsdom")
    {
        result
            .referenced_dependencies
            .push(format!("jest-environment-{env}"));
        result.referenced_dependencies.push(env);
    }

    // watchPlugins → referenced dependencies
    let watch_plugins =
        config_parser::extract_config_shallow_strings(parse_source, parse_path, "watchPlugins");
    for plugin in &watch_plugins {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(plugin));
    }

    // resolver → referenced dependency (only if it's a package, not a relative path)
    if let Some(resolver) =
        config_parser::extract_config_string(parse_source, parse_path, &["resolver"])
        && !resolver.starts_with('.')
        && !resolver.starts_with('/')
    {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(&resolver));
    }

    // snapshotSerializers → referenced dependencies
    let serializers = config_parser::extract_config_string_array(
        parse_source,
        parse_path,
        &["snapshotSerializers"],
    );
    for s in &serializers {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(s));
    }

    // testRunner → referenced dependency (filter built-in runners)
    if let Some(runner) =
        config_parser::extract_config_string(parse_source, parse_path, &["testRunner"])
        && !matches!(
            runner.as_str(),
            "jest-jasmine2" | "jest-circus" | "jest-circus/runner"
        )
    {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(&runner));
    }

    // runner → referenced dependency (process runner, not test runner)
    if let Some(runner) =
        config_parser::extract_config_string(parse_source, parse_path, &["runner"])
        && runner != "jest-runner"
    {
        result
            .referenced_dependencies
            .push(crate::resolve::extract_package_name(&runner));
    }
}

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

    #[test]
    fn resolve_config_preset() {
        let source = r#"module.exports = { preset: "ts-jest" };"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string())
        );
    }

    #[test]
    fn resolve_config_global_setup_teardown() {
        let source = r#"
            module.exports = {
                globalSetup: "./test/global-setup.ts",
                globalTeardown: "./test/global-teardown.ts"
            };
        "#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            result
                .setup_files
                .contains(&std::path::PathBuf::from("/project/test/global-setup.ts"))
        );
        assert!(result.setup_files.contains(&std::path::PathBuf::from(
            "/project/test/global-teardown.ts"
        )));
    }

    #[test]
    fn resolve_config_watch_plugins() {
        let source = r#"
            module.exports = {
                watchPlugins: [
                    "jest-watch-typeahead/filename",
                    "jest-watch-typeahead/testname"
                ]
            };
        "#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        let deps = &result.referenced_dependencies;
        assert!(deps.contains(&"jest-watch-typeahead".to_string()));
    }

    #[test]
    fn resolve_config_resolver() {
        let source = r#"module.exports = { resolver: "jest-resolver-enhanced" };"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"jest-resolver-enhanced".to_string())
        );
    }

    #[test]
    fn resolve_config_resolver_relative_not_added() {
        let source = r#"module.exports = { resolver: "./custom-resolver.js" };"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            !result
                .referenced_dependencies
                .iter()
                .any(|d| d.contains("custom-resolver"))
        );
    }

    #[test]
    fn resolve_config_snapshot_serializers() {
        let source = r#"
            module.exports = {
                snapshotSerializers: ["enzyme-to-json/serializer"]
            };
        "#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"enzyme-to-json".to_string())
        );
    }

    #[test]
    fn resolve_config_test_runner_builtin() {
        let source = r#"module.exports = { testRunner: "jest-circus/runner" };"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            !result
                .referenced_dependencies
                .iter()
                .any(|d| d.contains("jest-circus"))
        );
    }

    #[test]
    fn resolve_config_custom_runner() {
        let source = r#"module.exports = { runner: "jest-runner-eslint" };"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"jest-runner-eslint".to_string())
        );
    }

    #[test]
    fn resolve_config_json() {
        let source = r#"{"preset": "ts-jest", "testEnvironment": "jsdom"}"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.json"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string())
        );
    }

    #[test]
    fn test_regex_with_directory_prefix() {
        assert_eq!(
            test_regex_to_glob(r"src/.*\.test\.ts$"),
            Some("src/**/*.test.ts".to_string())
        );
    }

    #[test]
    fn test_regex_without_directory_prefix() {
        assert_eq!(
            test_regex_to_glob(r".*\.test\.ts$"),
            None,
            "regex without directory prefix should return None (same as defaults)"
        );
    }

    #[test]
    fn test_regex_tsx_extension() {
        assert_eq!(
            test_regex_to_glob(r"src/.*\.test\.tsx?$"),
            Some("src/**/*.test.{ts,tsx}".to_string())
        );
    }

    #[test]
    fn test_regex_spec_pattern() {
        assert_eq!(
            test_regex_to_glob(r"src/.*\.spec\.ts$"),
            Some("src/**/*.spec.ts".to_string())
        );
    }

    #[test]
    fn test_regex_test_or_spec() {
        assert_eq!(
            test_regex_to_glob(r"src/.*(test|spec)\.ts$"),
            Some("src/**/*.{test,spec}.ts".to_string())
        );
    }

    #[test]
    fn resolve_config_test_regex_replaces_defaults() {
        let source =
            r#"{"testRegex": "src/.*\\.test\\.ts$", "transform": {"^.+\\.tsx?$": "ts-jest"}}"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.json"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            result.replace_entry_patterns,
            "testRegex with directory prefix should trigger replacement"
        );
        assert_eq!(result.entry_patterns, vec!["src/**/*.test.ts"]);
    }

    #[test]
    fn resolve_config_json_transform_object_values() {
        let source = r#"{"transform": {"^.+\\.tsx?$": "ts-jest"}}"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.json"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string()),
            "should extract transform values from object"
        );
    }

    /// `<rootDir>/packages/*` should expand to every package directory and
    /// pick up each child's `jest.config.*` so the children's deps are credited.
    #[test]
    fn resolve_config_projects_directory_glob() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let pkg_a = root.join("packages/a");
        let pkg_b = root.join("packages/b");
        fs::create_dir_all(&pkg_a).unwrap();
        fs::create_dir_all(&pkg_b).unwrap();
        fs::write(
            pkg_a.join("jest.config.js"),
            r#"module.exports = { preset: "ts-jest" };"#,
        )
        .unwrap();
        fs::write(
            pkg_b.join("jest.config.js"),
            r#"module.exports = { preset: "babel-jest" };"#,
        )
        .unwrap();

        let parent_source = r#"module.exports = { projects: ["<rootDir>/packages/*"] };"#;
        let parent_path = root.join("jest.config.js");
        fs::write(&parent_path, parent_source).unwrap();

        let plugin = JestPlugin;
        let result = plugin.resolve_config(&parent_path, parent_source, root);

        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string()),
            "child a's preset must be credited, got: {:?}",
            result.referenced_dependencies,
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"babel-jest".to_string()),
            "child b's preset must be credited, got: {:?}",
            result.referenced_dependencies,
        );
    }

    /// `<rootDir>/packages/*/jest.config.ts` (file-shaped glob) should still
    /// expand and recurse into every match.
    #[test]
    fn resolve_config_projects_file_glob() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let pkg_a = root.join("packages/a");
        fs::create_dir_all(&pkg_a).unwrap();
        fs::write(
            pkg_a.join("jest.config.ts"),
            r#"export default { reporters: ["jest-junit"] };"#,
        )
        .unwrap();

        let parent_source =
            r#"module.exports = { projects: ["<rootDir>/packages/*/jest.config.ts"] };"#;
        let parent_path = root.join("jest.config.js");
        fs::write(&parent_path, parent_source).unwrap();

        let plugin = JestPlugin;
        let result = plugin.resolve_config(&parent_path, parent_source, root);

        assert!(
            result
                .referenced_dependencies
                .contains(&"jest-junit".to_string()),
            "child reporter must be credited, got: {:?}",
            result.referenced_dependencies,
        );
    }

    /// A directory glob that matches a package without a jest config must
    /// not crash and must not affect parent-config extraction.
    #[test]
    fn resolve_config_projects_missing_child_config_ignored() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let pkg = root.join("packages/empty");
        fs::create_dir_all(&pkg).unwrap();

        let parent_source =
            r#"module.exports = { projects: ["<rootDir>/packages/*"], preset: "ts-jest" };"#;
        let parent_path = root.join("jest.config.js");
        fs::write(&parent_path, parent_source).unwrap();

        let plugin = JestPlugin;
        let result = plugin.resolve_config(&parent_path, parent_source, root);

        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string()),
            "parent's own preset must still be credited",
        );
    }

    /// Two configs that reference each other must terminate after both are
    /// parsed once. Both presets get credited; the cycle does not loop.
    #[test]
    fn resolve_config_projects_cycle_guard() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let a_dir = root.join("a");
        let b_dir = root.join("b");
        fs::create_dir_all(&a_dir).unwrap();
        fs::create_dir_all(&b_dir).unwrap();
        let a_path = a_dir.join("jest.config.js");
        let b_path = b_dir.join("jest.config.js");

        fs::write(
            &a_path,
            format!(
                r#"module.exports = {{ projects: ["{}"], preset: "ts-jest" }};"#,
                b_path.to_string_lossy().replace('\\', "/"),
            ),
        )
        .unwrap();
        fs::write(
            &b_path,
            format!(
                r#"module.exports = {{ projects: ["{}"], preset: "babel-jest" }};"#,
                a_path.to_string_lossy().replace('\\', "/"),
            ),
        )
        .unwrap();

        let plugin = JestPlugin;
        let source_a = std::fs::read_to_string(&a_path).unwrap();
        let result = plugin.resolve_config(&a_path, &source_a, root);

        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string()),
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"babel-jest".to_string()),
        );
    }

    /// A child config's `testMatch` / `replace_entry_patterns` must NOT bleed
    /// into the parent's result. Jest scopes those flags per-project; if they
    /// propagated, a narrow `testMatch` in one package would silently drop
    /// every other package's tests from the parent's entry-point list.
    #[test]
    fn resolve_config_projects_child_entry_patterns_do_not_leak_to_parent() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let pkg = root.join("packages/a");
        fs::create_dir_all(&pkg).unwrap();
        fs::write(
            pkg.join("jest.config.js"),
            r#"module.exports = { testMatch: ["src/**/only-here.test.ts"], preset: "ts-jest" };"#,
        )
        .unwrap();

        let parent_source = r#"module.exports = { projects: ["<rootDir>/packages/*"] };"#;
        let parent_path = root.join("jest.config.js");
        fs::write(&parent_path, parent_source).unwrap();

        let plugin = JestPlugin;
        let result = plugin.resolve_config(&parent_path, parent_source, root);

        assert!(
            !result.replace_entry_patterns,
            "child's testMatch must NOT toggle replace_entry_patterns on the parent",
        );
        assert!(
            !result
                .entry_patterns
                .iter()
                .any(|p| p.pattern == "src/**/only-here.test.ts"),
            "child's testMatch entry pattern must NOT appear in the parent's entry list, got: {:?}",
            result.entry_patterns,
        );
        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string()),
            "child's preset must still be credited via the merge",
        );
    }

    /// Metadata-only inline `ProjectConfig` fields (`displayName`,
    /// `rootDir`, etc.) must not pollute referenced deps. String entries
    /// pointing at a non-existent root resolve to no child configs and
    /// must not crash.
    #[test]
    fn resolve_config_projects_metadata_only_inline_object_no_deps() {
        let source =
            r#"module.exports = { projects: [{ displayName: "foo" }, "<rootDir>/packages/a"] };"#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/tmp/fallow-jest-projects-nonexistent-zzz"),
        );
        assert!(
            result.referenced_dependencies.is_empty(),
            "metadata-only field must not be credited as a dep, got: {:?}",
            result.referenced_dependencies,
        );
    }

    /// Inline `ProjectConfig` objects must credit their package-typed
    /// fields (preset, runner, testRunner, testEnvironment, etc.) and
    /// setup files just like a standalone child config would.
    #[test]
    fn resolve_config_projects_inline_object_credits_dependencies() {
        let source = r#"
            module.exports = {
                projects: [
                    { preset: "ts-jest", runner: "jest-runner-eslint" },
                    {
                        displayName: "browser",
                        testEnvironment: "jest-environment-puppeteer",
                        snapshotSerializers: ["enzyme-to-json/serializer"],
                        reporters: ["default", "jest-junit"]
                    },
                    {
                        setupFiles: ["./setup-browser.ts"],
                        globalSetup: "./global-setup.ts"
                    }
                ]
            };
        "#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );

        let deps = &result.referenced_dependencies;
        assert!(
            deps.contains(&"ts-jest".to_string()),
            "preset must be credited, got: {deps:?}",
        );
        assert!(
            deps.contains(&"jest-runner-eslint".to_string()),
            "custom runner must be credited, got: {deps:?}",
        );
        assert!(
            deps.contains(&"jest-environment-puppeteer".to_string()),
            "testEnvironment must surface as a dep, got: {deps:?}",
        );
        assert!(
            deps.contains(&"enzyme-to-json".to_string()),
            "snapshotSerializer package must be credited, got: {deps:?}",
        );
        assert!(
            deps.contains(&"jest-junit".to_string()),
            "non-builtin reporter must be credited, got: {deps:?}",
        );
        assert!(
            !deps.iter().any(|d| d == "default"),
            "builtin reporter `default` must not be credited as a dep, got: {deps:?}",
        );
        assert!(
            result
                .setup_files
                .contains(&std::path::PathBuf::from("/project/setup-browser.ts")),
            "inline setupFiles must be added to result.setup_files, got: {:?}",
            result.setup_files,
        );
        assert!(
            result
                .setup_files
                .contains(&std::path::PathBuf::from("/project/global-setup.ts")),
            "inline globalSetup must be added to result.setup_files, got: {:?}",
            result.setup_files,
        );
    }

    /// Built-in test runners and environments must NOT be credited as
    /// referenced deps even when they appear inside an inline ProjectConfig.
    #[test]
    fn resolve_config_projects_inline_object_filters_builtins() {
        let source = r#"
            module.exports = {
                projects: [
                    { testRunner: "jest-circus", testEnvironment: "node", runner: "jest-runner" }
                ]
            };
        "#;
        let plugin = JestPlugin;
        let result = plugin.resolve_config(
            std::path::Path::new("jest.config.js"),
            source,
            std::path::Path::new("/project"),
        );
        assert!(
            !result
                .referenced_dependencies
                .iter()
                .any(|d| d.contains("jest-circus") || d.contains("jest-runner") || d == "node"),
            "built-in testRunner / runner / testEnvironment must not be credited, got: {:?}",
            result.referenced_dependencies,
        );
    }

    /// A monorepo where each package keeps its Jest config under
    /// `package.json#jest` (instead of a dedicated `jest.config.*` file)
    /// must still have its child deps credited when the parent uses a
    /// directory-shaped `projects` glob.
    #[test]
    fn resolve_config_projects_directory_glob_picks_up_package_json_jest_key() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let pkg = root.join("packages/a");
        fs::create_dir_all(&pkg).unwrap();
        fs::write(
            pkg.join("package.json"),
            r#"{ "name": "a", "jest": { "preset": "ts-jest" } }"#,
        )
        .unwrap();

        let parent_source = r#"module.exports = { projects: ["<rootDir>/packages/*"] };"#;
        let parent_path = root.join("jest.config.js");
        fs::write(&parent_path, parent_source).unwrap();

        let plugin = JestPlugin;
        let result = plugin.resolve_config(&parent_path, parent_source, root);

        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string()),
            "preset from package.json#jest must be credited, got: {:?}",
            result.referenced_dependencies,
        );
    }

    /// A directory entry whose `package.json` has no `jest` key must be
    /// treated as having no child config (silently skipped, not an error).
    #[test]
    fn resolve_config_projects_package_json_without_jest_key_skipped() {
        use std::fs;
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let pkg = root.join("packages/a");
        fs::create_dir_all(&pkg).unwrap();
        fs::write(pkg.join("package.json"), r#"{ "name": "a" }"#).unwrap();

        let parent_source =
            r#"module.exports = { projects: ["<rootDir>/packages/*"], preset: "ts-jest" };"#;
        let parent_path = root.join("jest.config.js");
        fs::write(&parent_path, parent_source).unwrap();

        let plugin = JestPlugin;
        let result = plugin.resolve_config(&parent_path, parent_source, root);

        assert!(
            result
                .referenced_dependencies
                .contains(&"ts-jest".to_string()),
        );
        assert_eq!(
            result.referenced_dependencies.len(),
            1,
            "no extra deps should be credited from a package.json with no jest key, got: {:?}",
            result.referenced_dependencies,
        );
    }
}