qml_static_analyzer 0.2.0

A static analyzer for QML files
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
use std::collections::{HashMap, HashSet, VecDeque};
use std::path::{Path, PathBuf};
use std::process;

use qml_static_analyzer::checker::CheckContext;
use qml_static_analyzer::qt_types::QtTypeDb;
use qml_static_analyzer::types::FileItem;

// ── entry point ───────────────────────────────────────────────────────────────

fn main() {
    let args: Vec<String> = std::env::args().collect();
    let subcmd = args.get(1).map(String::as_str);

    match subcmd {
        Some("check") => cmd_check(&args[2..]),
        Some("generate-qt-types") => cmd_generate_qt_types(&args[2..]),
        Some("list-builtins") => cmd_list_builtins(),
        Some(other) => {
            eprintln!("error: unknown subcommand '{other}'");
            print_usage();
            process::exit(1);
        }
        None => {
            print_usage();
            process::exit(1);
        }
    }
}

fn print_usage() {
    eprintln!("usage:");
    eprintln!();
    eprintln!("  qml_static_analyzer check --path <dir> [options]");
    eprintln!("    --config <file>               config file");
    eprintln!("    --builtin-qt-version <ver>    use compiled-in Qt types (e.g. 6.3.2)");
    eprintln!("    --qt-types-json <file>        use qt_types JSON from disk");
    eprintln!("    --complex                     show full element hierarchy in error paths");
    eprintln!("    --no-warn-useless-ignore      suppress warnings for useless // qml-ignore comments");
    eprintln!();
    eprintln!("  qml_static_analyzer generate-qt-types --qt-path <qt_gcc64_dir> [options]");
    eprintln!("    --output <file>               output file (default: qt_types_VER.json)");
    eprintln!("    --qt-version <ver>            override version string in output");
    eprintln!();
    eprintln!("  qml_static_analyzer list-builtins");
    eprintln!("    List Qt versions compiled into this binary.");
}

// ── subcommand: list-builtins ─────────────────────────────────────────────────

fn cmd_list_builtins() {
    let versions = qml_static_analyzer::qt_types::builtin_versions();
    if versions.is_empty() {
        println!("No Qt types compiled in.");
        println!("Re-build with INCLUDED_QT_TYPES=path/to/qt_types_X.Y.Z.json");
    } else {
        println!("Built-in Qt versions:");
        for v in versions {
            println!("  {v}");
        }
    }
}

// ── subcommand: generate-qt-types ────────────────────────────────────────────

struct GenerateOpts {
    qt_path: PathBuf,
    output: Option<PathBuf>,
    qt_version: Option<String>,
}

fn parse_generate_args(args: &[String]) -> Result<GenerateOpts, String> {
    let mut qt_path: Option<PathBuf> = None;
    let mut output: Option<PathBuf> = None;
    let mut qt_version: Option<String> = None;
    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--qt-path" => {
                i += 1;
                qt_path = Some(PathBuf::from(args.get(i).ok_or("missing value for --qt-path")?));
            }
            "--output" | "-o" => {
                i += 1;
                output = Some(PathBuf::from(args.get(i).ok_or("missing value for --output")?));
            }
            "--qt-version" => {
                i += 1;
                qt_version = Some(args.get(i).ok_or("missing value for --qt-version")?.clone());
            }
            other => return Err(format!("unknown argument: {other}")),
        }
        i += 1;
    }
    Ok(GenerateOpts {
        qt_path: qt_path.ok_or("--qt-path is required")?,
        output,
        qt_version,
    })
}

fn cmd_generate_qt_types(args: &[String]) {
    let opts = parse_generate_args(args).unwrap_or_else(|e| {
        eprintln!("error: {e}");
        eprintln!("usage: qml_static_analyzer generate-qt-types --qt-path <qt_gcc64_dir> [--output <file>] [--qt-version <ver>]");
        process::exit(1);
    });

    let version = opts
        .qt_version
        .clone()
        .or_else(|| detect_qt_version_from_path(&opts.qt_path))
        .unwrap_or_else(|| {
            eprintln!(
                "error: cannot detect Qt version from path. \
                 Add --qt-version X.Y.Z explicitly."
            );
            process::exit(1);
        });

    let json = qml_static_analyzer::qt_types_gen::generate_qt_types_json(&opts.qt_path).unwrap_or_else(|e| {
        eprintln!("error: {e}");
        process::exit(1);
    });

    // Inject _version field into the JSON object
    let json_with_version = inject_version_field(&json, &version).unwrap_or_else(|e| {
        eprintln!("error: failed to inject version into JSON: {e}");
        process::exit(1);
    });

    let out_path = opts
        .output
        .unwrap_or_else(|| PathBuf::from(format!("qt_types_{version}.json")));

    std::fs::write(&out_path, &json_with_version).unwrap_or_else(|e| {
        eprintln!("error: cannot write {out_path:?}: {e}");
        process::exit(1);
    });

    println!("Generated: {}", out_path.display());
    println!("Qt version: {version}");
    println!(
        "To embed in binary: INCLUDED_QT_TYPES={} cargo build",
        out_path.display()
    );
}

/// Try to extract a version string like `6.8.3` from a Qt install path
/// (e.g. `/home/user/Qt/6.8.3/gcc_64`).
fn detect_qt_version_from_path(path: &Path) -> Option<String> {
    for component in path.components() {
        if let std::path::Component::Normal(name) = component
            && let Some(s) = name.to_str()
        {
            let parts: Vec<&str> = s.split('.').collect();
            if parts.len() >= 2
                && parts
                    .iter()
                    .all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_digit()))
            {
                return Some(s.to_string());
            }
        }
    }
    None
}

/// Parse the JSON, add `"_version": version`, re-serialize.
fn inject_version_field(json: &str, version: &str) -> Result<String, String> {
    let mut obj: serde_json::Map<String, serde_json::Value> = serde_json::from_str(json).map_err(|e| e.to_string())?;
    obj.insert("_version".to_string(), serde_json::Value::String(version.to_string()));
    serde_json::to_string_pretty(&obj).map_err(|e| e.to_string())
}

// ── subcommand: check ─────────────────────────────────────────────────────────

struct CheckOpts {
    path: PathBuf,
    config_file: Option<PathBuf>,
    builtin_qt_version: Option<String>,
    qt_types_json: Option<PathBuf>,
    complex: bool,
    warn_useless_ignore: bool,
}

fn parse_check_args(args: &[String]) -> Result<CheckOpts, String> {
    let mut path: Option<PathBuf> = None;
    let mut config_file: Option<PathBuf> = None;
    let mut builtin_qt_version: Option<String> = None;
    let mut qt_types_json: Option<PathBuf> = None;
    let mut complex = false;
    let mut warn_useless_ignore = true;
    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--path" => {
                i += 1;
                path = Some(PathBuf::from(args.get(i).ok_or("missing value for --path")?));
            }
            "--config" => {
                i += 1;
                config_file = Some(PathBuf::from(args.get(i).ok_or("missing value for --config")?));
            }
            "--builtin-qt-version" => {
                i += 1;
                builtin_qt_version = Some(args.get(i).ok_or("missing value for --builtin-qt-version")?.clone());
            }
            "--qt-types-json" | "--qt-types" => {
                i += 1;
                qt_types_json = Some(PathBuf::from(args.get(i).ok_or("missing value for --qt-types-json")?));
            }
            "--complex" => {
                complex = true;
            }
            "--no-warn-useless-ignore" => {
                warn_useless_ignore = false;
            }
            other => return Err(format!("unknown argument: {other}")),
        }
        i += 1;
    }
    if builtin_qt_version.is_some() && qt_types_json.is_some() {
        return Err("--builtin-qt-version and --qt-types-json are mutually exclusive".to_string());
    }
    Ok(CheckOpts {
        path: path.ok_or("--path is required")?,
        config_file,
        builtin_qt_version,
        qt_types_json,
        complex,
        warn_useless_ignore,
    })
}

fn cmd_check(args: &[String]) {
    let opts = parse_check_args(args).unwrap_or_else(|e| {
        eprintln!("error: {e}");
        eprintln!(
            "usage: qml_static_analyzer check --path <dir> \
             [--config <file>] [--builtin-qt-version X.Y.Z] [--qt-types-json <file>]"
        );
        process::exit(1);
    });

    if !opts.path.exists() {
        eprintln!("error: --path {:?} does not exist", opts.path);
        process::exit(1);
    }

    if let Some(ref p) = opts.config_file {
        if !p.exists() {
            eprintln!("error: --config {:?} does not exist", p);
            process::exit(1);
        }
    }

    let config = opts
        .config_file
        .as_ref()
        .and_then(|p| std::fs::read_to_string(p).ok())
        .map(|s| qml_static_analyzer::config::parse_config(&s))
        .unwrap_or_default();

    // Resolve the directory that contains the config file — headers are relative to it.
    let config_dir: std::path::PathBuf = opts
        .config_file
        .as_ref()
        .and_then(|p| p.parent())
        .map_or_else(|| std::path::PathBuf::from("."), std::path::Path::to_path_buf);

    // Build cpp_object_members: name → None (opaque) | Some(known members).
    let mut cpp_object_members: HashMap<String, Option<HashSet<String>>> = HashMap::new();
    for (name, header_path) in &config.cpp_objects {
        let members = if header_path.is_empty() {
            None
        } else {
            let full_path = config_dir.join(header_path);
            match std::fs::read_to_string(&full_path) {
                Ok(src) => Some(qml_static_analyzer::cpp_header::parse_cpp_header(&src)),
                Err(e) => {
                    eprintln!("warning: cannot read header {full_path:?}: {e}");
                    None
                }
            }
        };
        cpp_object_members.insert(name.clone(), members);
    }

    // All C++ object names are available in QML scope.
    let mut cpp_globals: HashSet<String> = cpp_object_members.keys().cloned().collect();
    // [globals] names are always-valid identifiers.
    cpp_globals.extend(config.globals.names.iter().cloned());

    let db = load_qt_db(&opts);

    let all_qml_files = collect_qml_files(&opts.path);

    // Warn about ignore paths that don't match any file — most likely a wrong path relative to --path.
    for ignore_path in &config.ignore.paths {
        let matched = all_qml_files.iter().any(|p| {
            let relative = p.strip_prefix(&opts.path).unwrap_or(p);
            relative.starts_with(Path::new(ignore_path.as_str()))
        });
        if !matched {
            eprintln!(
                "warning: [ignore] path `{ignore_path}` does not match any .qml file \
                 (paths must be relative to --path)"
            );
        }
    }

    let qml_files: Vec<PathBuf> = all_qml_files
        .into_iter()
        .filter(|p| !is_ignored(p, &opts.path, &config.ignore.paths))
        .collect();

    if qml_files.is_empty() {
        eprintln!("no .qml files found in {:?}", opts.path);
        process::exit(1);
    }

    let mut parsed: Vec<(PathBuf, FileItem)> = Vec::new();
    let mut suppressed_lines: HashMap<PathBuf, HashSet<usize>> = HashMap::new();
    for file_path in &qml_files {
        let source = std::fs::read_to_string(file_path).unwrap_or_else(|e| {
            eprintln!("cannot read {file_path:?}: {e}");
            process::exit(1);
        });
        let suppressed: HashSet<usize> = source
            .lines()
            .enumerate()
            .filter(|(_, l)| l.contains("// qml-ignore"))
            .map(|(i, _)| i + 1)
            .collect();
        if !suppressed.is_empty() {
            suppressed_lines.insert(file_path.clone(), suppressed);
        }
        let name = file_path.file_stem().and_then(|s| s.to_str()).unwrap_or("Unknown");
        let file_item = qml_static_analyzer::parser::parse_file(name, &source).unwrap_or_else(|e| {
            eprintln!("parse error in {file_path:?}: {e}");
            process::exit(1);
        });
        parsed.push((file_path.clone(), file_item));
    }

    let known_types: HashSet<String> = parsed.iter().map(|(_, f)| f.name.clone()).collect();
    let file_members: HashMap<String, (Vec<String>, Vec<String>)> = parsed
        .iter()
        .map(|(_, f)| {
            let props: Vec<String> = f.properties.iter().map(|p| p.name.clone()).collect();
            let sigs: Vec<String> = f.signals.iter().map(|s| s.name.clone()).collect();
            (f.name.clone(), (props, sigs))
        })
        .collect();
    let file_base_types: HashMap<String, String> = parsed
        .iter()
        .map(|(_, f)| (f.name.clone(), f.base_type.clone()))
        .collect();

    let mut parent_scopes: HashMap<String, HashSet<String>> = HashMap::new();
    for (_, parent_file) in &parsed {
        let mut parent_names = collect_inherited_file_members(&parent_file.name, &file_members, &file_base_types);
        parent_names.extend(collect_all_file_ids(parent_file));
        let mut all_children = collect_child_type_names(&parent_file.children);
        if let Some(extra) = config.new_child.get(&parent_file.name) {
            all_children.extend(extra.iter().cloned());
        }
        for child_type in all_children {
            if known_types.contains(&child_type) {
                parent_scopes
                    .entry(child_type)
                    .or_default()
                    .extend(parent_names.iter().cloned());
            }
        }
    }
    // For "ParentFile.loaderId" = ["ChildType"] config entries: the ChildType is loaded
    // *inside* the loader element, so it inherits the loader's own properties/signals.
    // Example: "MainContent.mainPlain" = ["NewExaminationScreen"] where mainPlain is a
    // MainScreenLoader that declares `requestLeft` → add those members to child's parent scope.
    for (key, child_types) in &config.new_child {
        if let Some(dot_pos) = key.find('.') {
            let parent_name = &key[..dot_pos];
            let loader_id = &key[dot_pos + 1..];
            if let Some((_, parent_file)) = parsed.iter().find(|(_, f)| f.name == parent_name)
                && let Some(loader_type_name) = find_child_type_by_id(&parent_file.children, loader_id)
            {
                let loader_members = collect_inherited_file_members(&loader_type_name, &file_members, &file_base_types);
                if !loader_members.is_empty() {
                    for child_type in child_types {
                        if known_types.contains(child_type) {
                            parent_scopes
                                .entry(child_type.clone())
                                .or_default()
                                .extend(loader_members.iter().cloned());
                        }
                    }
                }
            }
        }
    }

    loop {
        let mut changed = false;
        let snapshot = parent_scopes.clone();
        for (_, parent_file) in &parsed {
            let mut additional = snapshot.get(&parent_file.name).cloned().unwrap_or_default();
            additional.extend(collect_inherited_file_members(
                &parent_file.name,
                &file_members,
                &file_base_types,
            ));
            additional.extend(collect_all_file_ids(parent_file));
            if additional.is_empty() {
                continue;
            }
            let mut all_children = collect_child_type_names(&parent_file.children);
            if let Some(extra) = config.new_child.get(&parent_file.name) {
                all_children.extend(extra.iter().cloned());
            }
            for child_type in all_children {
                if known_types.contains(&child_type) {
                    let entry = parent_scopes.entry(child_type).or_default();
                    let before = entry.len();
                    entry.extend(additional.iter().cloned());
                    if entry.len() > before {
                        changed = true;
                    }
                }
            }
            // Also propagate through base-type relationships:
            // if EnterPinFlyingDialog gets scope from main, its base type FlyingDialog
            // should also receive that scope (since FlyingDialog's methods run in the
            // same context as the subclass that instantiated it).
            let base = &parent_file.base_type;
            if file_members.contains_key(base) {
                let entry = parent_scopes.entry(base.clone()).or_default();
                let before = entry.len();
                entry.extend(additional.iter().cloned());
                if entry.len() > before {
                    changed = true;
                }
            }
        }
        if !changed {
            break;
        }
    }

    let check_ctx = CheckContext {
        known_types,
        extra_children: config.new_child.clone(),
        cpp_globals,
        cpp_object_members,
        file_members,
        file_base_types: file_base_types.clone(),
        parent_scopes,
        complex: opts.complex,
    };

    let root_types: Vec<String> = parsed
        .iter()
        .filter(|(_, f)| {
            let qt_type = resolve_qt_base(&f.base_type, &db, &file_base_types);
            is_root_qt_type(&qt_type)
        })
        .map(|(_, f)| f.name.clone())
        .collect();

    let mut usage_paths: HashMap<String, Vec<String>> = HashMap::new();
    for root_type in &root_types {
        for (type_name, path) in build_usage_paths(&parsed, root_type, &config.new_child) {
            usage_paths.entry(type_name).or_insert(path);
        }
    }

    let mut total_errors = 0usize;
    let mut unreachable: Vec<&PathBuf> = Vec::new();

    for (file_path, file_item) in &parsed {
        if !usage_paths.contains_key(&file_item.name) {
            unreachable.push(file_path);
            continue;
        }

        let errors = qml_static_analyzer::checker::check_file(file_item, &db, &check_ctx);

        let usage_path = usage_paths.get(&file_item.name);

        let suppressed = suppressed_lines.get(file_path);
        let mut used_suppress_lines: HashSet<usize> = HashSet::new();
        for err in &errors {
            if let (Some(line), Some(sup)) = (err.line, suppressed)
                && sup.contains(&line)
            {
                used_suppress_lines.insert(line);
                continue;
            }
            let line_str = err.line.map(|l| format!(":{l}")).unwrap_or_default();
            let label = if opts.complex && !err.element_path.is_empty() {
                // Combine file-level usage path with within-file element path.
                let combined: Vec<&str> = usage_path
                    .map(|p| p.iter().map(String::as_str).collect::<Vec<_>>())
                    .unwrap_or_default()
                    .into_iter()
                    .chain(err.element_path.iter().map(String::as_str))
                    .collect();
                Some(format!(" [{}]", combined.join(" -> ")))
            } else {
                usage_path
                    .filter(|p| p.len() > 1)
                    .map(|p| format!(" [{}]", p.join(" -> ")))
            };
            if let Some(ref l) = label {
                println!("{}{line_str}: {err}{l}", file_path.display());
            } else {
                println!("{}{line_str}: {err}", file_path.display());
            }
        }
        total_errors += errors
            .iter()
            .filter(|e| {
                if let (Some(line), Some(sup)) = (e.line, suppressed) {
                    !sup.contains(&line)
                } else {
                    true
                }
            })
            .count();

        // Warn about `// qml-ignore` comments that didn't suppress any error.
        if opts.warn_useless_ignore
            && let Some(sup) = suppressed
        {
            let mut useless: Vec<usize> = sup.difference(&used_suppress_lines).copied().collect();
            useless.sort_unstable();
            for line in useless {
                println!(
                    "{}:{line}: Useless `// qml-ignore` — no error on this line",
                    file_path.display()
                );
                total_errors += 1;
            }
        }
    }

    if !unreachable.is_empty() {
        println!("\nNot checked (not reachable from any root):");
        for path in &unreachable {
            println!("  {}", path.display());
        }
    }

    println!("\nFound {total_errors} errors, {} files not checked", unreachable.len());

    if total_errors > 0 {
        process::exit(1);
    }
}

// ── Qt DB loading ─────────────────────────────────────────────────────────────

fn load_qt_db(opts: &CheckOpts) -> QtTypeDb {
    use qml_static_analyzer::qt_types;

    if let Some(ref path) = opts.qt_types_json {
        qt_types::load_from_json_file(path).unwrap_or_else(|e| {
            eprintln!("error: {e}");
            process::exit(1);
        })
    } else if let Some(ref version) = opts.builtin_qt_version {
        qt_types::load_builtin_db(version).unwrap_or_else(|e| {
            eprintln!("error: {e}");
            process::exit(1);
        })
    } else {
        if qt_types::builtin_versions().is_empty() {
            eprintln!(
                "error: no Qt types available. \
                 Use --builtin-qt-version or --qt-types-json, \
                 or re-build with INCLUDED_QT_TYPES=path/to/qt_types_X.Y.Z.json."
            );
            process::exit(1);
        }
        qt_types::load_default_builtin_db()
    }
}

// ── root discovery ────────────────────────────────────────────────────────────

const ROOT_QT_TYPES: &[&str] = &["Window", "ApplicationWindow", "Dialog"];

fn is_root_qt_type(qt_type: &str) -> bool {
    ROOT_QT_TYPES.contains(&qt_type)
}

/// Find the type name of a child element with the given id anywhere in the element tree.
fn find_child_type_by_id(children: &[qml_static_analyzer::types::QmlChild], id: &str) -> Option<String> {
    for child in children {
        if child.id.as_deref() == Some(id) {
            return Some(child.type_name.clone());
        }
        if let Some(found) = find_child_type_by_id(&child.children, id) {
            return Some(found);
        }
    }
    None
}

fn collect_all_file_ids(file: &FileItem) -> HashSet<String> {
    let mut ids = HashSet::new();
    if let Some(id) = &file.id {
        ids.insert(id.clone());
    }
    fn recurse(children: &[qml_static_analyzer::types::QmlChild], ids: &mut HashSet<String>) {
        for child in children {
            if let Some(id) = &child.id {
                ids.insert(id.clone());
            }
            recurse(&child.children, ids);
        }
    }
    recurse(&file.children, &mut ids);
    ids
}

fn collect_inherited_file_members(
    type_name: &str,
    file_members: &HashMap<String, (Vec<String>, Vec<String>)>,
    file_base_types: &HashMap<String, String>,
) -> HashSet<String> {
    let mut names = HashSet::new();
    let mut current = type_name.to_string();
    let mut seen = HashSet::new();
    while seen.insert(current.clone()) {
        if let Some((props, sigs)) = file_members.get(&current) {
            names.extend(props.iter().cloned());
            names.extend(sigs.iter().cloned());
        }
        match file_base_types.get(&current) {
            Some(base) if file_members.contains_key(base.as_str()) => current = base.clone(),
            _ => break,
        }
    }
    names
}

fn resolve_qt_base(type_name: &str, db: &QtTypeDb, file_base_types: &HashMap<String, String>) -> String {
    let mut current = type_name.to_string();
    for _ in 0..32 {
        if db.has_type(&current) {
            return current;
        }
        match file_base_types.get(&current) {
            Some(base) => current = base.clone(),
            None => return current,
        }
    }
    current
}

// ── helpers ──────────────────────────────────────────────────────────────────

fn is_ignored(file_path: &Path, base_dir: &Path, ignored_paths: &[String]) -> bool {
    let relative = file_path.strip_prefix(base_dir).unwrap_or(file_path);
    ignored_paths
        .iter()
        .any(|ignore| relative.starts_with(Path::new(ignore.as_str())))
}

fn build_usage_paths(
    parsed: &[(PathBuf, FileItem)],
    root_type: &str,
    extra_children: &HashMap<String, Vec<String>>,
) -> HashMap<String, Vec<String>> {
    let type_to_idx: HashMap<&str, usize> = parsed
        .iter()
        .enumerate()
        .map(|(i, (_, f))| (f.name.as_str(), i))
        .collect();

    let mut paths: HashMap<String, Vec<String>> = HashMap::new();
    if root_type.is_empty() || !type_to_idx.contains_key(root_type) {
        return paths;
    }

    paths.insert(root_type.to_string(), vec![root_type.to_string()]);
    let mut queue = VecDeque::new();
    queue.push_back(root_type.to_string());
    let mut visited: HashSet<String> = HashSet::new();
    visited.insert(root_type.to_string());

    while let Some(current) = queue.pop_front() {
        let current_path = paths[&current].clone();

        let mut child_types = Vec::new();
        if let Some(&idx) = type_to_idx.get(current.as_str()) {
            child_types.extend(collect_child_type_names(&parsed[idx].1.children));
            let base = &parsed[idx].1.base_type;
            if type_to_idx.contains_key(base.as_str()) {
                child_types.push(base.clone());
            }
        }
        if let Some(extra) = extra_children.get(&current) {
            child_types.extend(extra.iter().cloned());
        }

        for child_type in child_types {
            if type_to_idx.contains_key(child_type.as_str()) && !visited.contains(&child_type) {
                visited.insert(child_type.clone());
                let mut child_path = current_path.clone();
                child_path.push(child_type.clone());
                paths.insert(child_type.clone(), child_path);
                queue.push_back(child_type);
            }
        }
    }

    paths
}

fn collect_child_type_names(children: &[qml_static_analyzer::types::QmlChild]) -> Vec<String> {
    let mut result = Vec::new();
    for child in children {
        result.push(child.type_name.clone());
        result.extend(collect_child_type_names(&child.children));
    }
    result
}

fn collect_qml_files(dir: &PathBuf) -> Vec<PathBuf> {
    let mut files = Vec::new();
    let Ok(entries) = std::fs::read_dir(dir) else {
        return files;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            files.extend(collect_qml_files(&path));
        } else if path.extension().and_then(|e| e.to_str()) == Some("qml") {
            files.push(path);
        }
    }
    files.sort();
    files
}