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
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
use std::collections::{HashMap, HashSet};

use crate::parser::collect_dotted_accesses_from_expression;
use crate::qt_types::QtTypeDb;
use crate::types::{FileItem, Function, Property, PropertyType, PropertyValue, QmlChild};

// ── typy błędów ───────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorKind {
    /// `property int height` w Rectangle – height już istnieje w bazie Qt
    PropertyRedefinition { name: String, base_type: String },
    /// `property int foo: false` – zadeklarowany typ != typ przypisywanej wartości
    PropertyTypeMismatch {
        name: String,
        declared: String,
        assigned: String,
    },
    /// `property bool b: otherIntProp` – zadeklarowany typ != typ wskazanej property
    PropertyRefTypeMismatch {
        name: String,
        declared: String,
        ref_name: String,
        ref_type: String,
    },
    /// `Layout.notExist: true` – property nie istnieje w typie
    UnknownPropertyAssignment { name: String },
    /// `Layout.fillWidth: 21` – typ wartości nie zgadza się z oczekiwanym
    AssignmentTypeMismatch {
        name: String,
        expected: String,
        assigned: String,
    },
    /// `zzzz = x + y` wewnątrz funkcji – nazwa niezdefiniowana nigdzie w zasięgu
    UndefinedName { name: String, function: String },
    /// `function onStatussChanged()` – sygnał o tej nazwie nie istnieje
    UnknownSignalHandler { handler: String },
    /// `element.nonExisting = true` – property nie istnieje na elemencie
    UnknownMemberAccess { object: String, member: String },
    /// `element.boolProp = 0` – typ przypisanej wartości niezgodny z typem property
    MemberAssignmentTypeMismatch {
        object: String,
        member: String,
        expected: String,
        assigned: String,
    },
    /// `property bool foo: undeclared.bar` – nazwa w wyrażeniu property nie jest w zasięgu
    UndefinedPropertyAccess { prop: String, name: String },
    /// `Sub3 { }` – typ nie jest zdefiniowany nigdzie w projekcie ani w Qt
    UnknownType { type_name: String },
    /// `diskManager.nonExisting` — member not found in C++ object
    UnknownCppMember { object: String, member: String },
}

#[derive(Debug, Clone)]
pub struct AnalysisError {
    pub kind: ErrorKind,
    /// Opcjonalny kontekst (np. nazwa elementu dziecka)
    pub context: Option<String>,
    /// Source line number (1-based), if known.
    pub line: Option<usize>,
    /// Full element path within the file (populated in --complex mode).
    /// E.g. ["Row", "cancelButton"] means the error is inside the cancelButton
    /// child of a Row which is a direct child of the file root.
    pub element_path: Vec<String>,
}

impl AnalysisError {
    fn new(kind: ErrorKind) -> Self {
        Self {
            kind,
            context: None,
            line: None,
            element_path: Vec::new(),
        }
    }
    fn with_context(mut self, ctx: impl Into<String>) -> Self {
        self.context = Some(ctx.into());
        self
    }
    fn with_line(mut self, line: usize) -> Self {
        if line > 0 {
            self.line = Some(line);
        }
        self
    }
}

impl std::fmt::Display for AnalysisError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let prefix = match &self.context {
            Some(c) => format!("[{c}] "),
            None => String::new(),
        };
        match &self.kind {
            ErrorKind::PropertyRedefinition { name, base_type } => write!(
                f,
                "{prefix}Property `{name}` redefines existing property from `{base_type}`"
            ),
            ErrorKind::PropertyTypeMismatch {
                name,
                declared,
                assigned,
            } => write!(
                f,
                "{prefix}Property `{name}` declared as `{declared}` but assigned `{assigned}` literal"
            ),
            ErrorKind::PropertyRefTypeMismatch {
                name,
                declared,
                ref_name,
                ref_type,
            } => write!(
                f,
                "{prefix}Property `{name}` declared as `{declared}` but assigned property `{ref_name}` of type `{ref_type}`"
            ),
            ErrorKind::UnknownPropertyAssignment { name } => {
                write!(f, "{prefix}Assignment to unknown property `{name}`")
            }
            ErrorKind::AssignmentTypeMismatch {
                name,
                expected,
                assigned,
            } => write!(f, "{prefix}Property `{name}` expects `{expected}` but got `{assigned}`"),
            ErrorKind::UndefinedName { name, function } => {
                write!(f, "{prefix}Undefined name `{name}` used in function `{function}`")
            }
            ErrorKind::UnknownSignalHandler { handler } => write!(
                f,
                "{prefix}Signal handler `{handler}` has no corresponding signal or property"
            ),
            ErrorKind::UnknownMemberAccess { object, member } => {
                write!(f, "{prefix}Assignment to unknown property `{member}` on `{object}`")
            }
            ErrorKind::MemberAssignmentTypeMismatch {
                object,
                member,
                expected,
                assigned,
            } => write!(
                f,
                "{prefix}`{object}.{member}` expects `{expected}` but got `{assigned}`"
            ),
            ErrorKind::UndefinedPropertyAccess { prop, name } => {
                write!(f, "{prefix}Undefined name `{name}` used in property `{prop}`")
            }
            ErrorKind::UnknownType { type_name } => write!(f, "{prefix}Unknown type `{type_name}`"),
            ErrorKind::UnknownCppMember { object, member } => {
                write!(f, "{prefix}Unknown member `{member}` on C++ object `{object}`")
            }
        }
    }
}

// ── publiczne API ─────────────────────────────────────────────────────────

/// Kontekst przekazywany do checkera — informacje o projekcie spoza parsowanego pliku.
#[derive(Default)]
pub struct CheckContext {
    /// Typy plików QML które zostały sparsowane (nie są opaque).
    pub known_types: std::collections::HashSet<String>,
    /// Ręcznie zadeklarowane dzieci: parent_type → list of child_types.
    pub extra_children: std::collections::HashMap<String, Vec<String>>,
    /// Nazwy C++ dostępne globalnie w QML (singletony + obiekty kontekstu + globals).
    pub cpp_globals: std::collections::HashSet<String>,
    /// C++ object member sets: name → None (opaque, all access OK) | Some(members).
    /// Only objects listed here have their member access validated.
    pub cpp_object_members: std::collections::HashMap<String, Option<std::collections::HashSet<String>>>,
    /// Dla każdego znaneego typu QML: (nazwy property, nazwy sygnałów).
    /// Używane do dodawania własnych property/sygnałów do zasięgu danego dziecka.
    pub file_members: std::collections::HashMap<String, (Vec<String>, Vec<String>)>,
    /// Dla każdego znaneego typu QML: typ bazowy (base_type z pliku .qml).
    /// Używane do pobierania właściwości Qt dla znanych typów (np. Sub2 → Switch → checked).
    pub file_base_types: std::collections::HashMap<String, String>,
    /// Dla każdego typu QML: nazwy property i sygnałów dostępnych z zasięgu rodzica (parent scope).
    /// Zapobiega fałszywym błędom dla referencji do property zdefiniowanych w pliku nadrzędnym.
    pub parent_scopes: std::collections::HashMap<String, std::collections::HashSet<String>>,
    /// When true, errors carry a full within-file element path (for --complex output mode).
    pub complex: bool,
}

impl CheckContext {
    pub fn empty() -> Self {
        Self::default()
    }
}

pub fn check_file(file: &FileItem, db: &QtTypeDb, ctx: &CheckContext) -> Vec<AnalysisError> {
    let mut errors = Vec::new();
    let checker = Checker { db, ctx };

    // Zbuduj zestaw nazw zadeklarowanych na poziomie pliku (property + sygnały + id)
    let file_scope = checker.build_file_scope(file);

    checker.check_root(file, &file_scope, &mut errors);
    errors
}

// ── wewnętrzna logika ─────────────────────────────────────────────────────

struct Checker<'a> {
    db: &'a QtTypeDb,
    ctx: &'a CheckContext,
}

/// Info o dziecku potrzebne do sprawdzania member access w funkcjach.
struct ChildInfo {
    type_name: String,
    properties: Vec<Property>,
}

fn build_child_id_map(children: &[QmlChild]) -> HashMap<String, ChildInfo> {
    let mut map = HashMap::new();
    for child in children {
        if let Some(id) = &child.id {
            map.insert(
                id.clone(),
                ChildInfo {
                    type_name: child.type_name.clone(),
                    properties: child.properties.clone(),
                },
            );
        }
        // Rekurencyjnie — id zagnieżdżonych dzieci też są widoczne
        map.extend(build_child_id_map(&child.children));
    }
    map
}

impl Checker<'_> {
    // ── rozwiązywanie łańcucha typów bazowych ────────────────────────────

    /// Zwraca najbliższy typ Qt w łańcuchu typów bazowych.
    /// Np. TextButton → GenericButton → RoundButton (Qt type).
    /// Zapobiega nieskończonej pętli przez max 32 kroki.
    fn resolve_qt_type(&self, type_name: &str) -> String {
        let mut current = type_name.to_string();
        for _ in 0..32 {
            if self.db.has_type(&current) {
                return current;
            }
            match self.ctx.file_base_types.get(&current) {
                Some(base) => current = base.clone(),
                None => return current,
            }
        }
        current
    }

    // ── inherited file members ────────────────────────────────────────────

    /// Collects all (prop, sig) names declared in a type's QML base chain.
    /// E.g. Sub4 → SwitchWrapper → returns props from both files.
    fn all_file_member_names(&self, type_name: &str) -> 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)) = self.ctx.file_members.get(&current) {
                names.extend(props.iter().cloned());
                names.extend(sigs.iter().cloned());
            }
            match self.ctx.file_base_types.get(&current) {
                Some(b) if self.ctx.file_members.contains_key(b.as_str()) => current = b.clone(),
                _ => break,
            }
        }
        names
    }

    // ── budowanie zasięgu ─────────────────────────────────────────────────

    /// Wszystkie nazwy widoczne na poziomie root FileItem.
    fn build_file_scope(&self, file: &FileItem) -> HashSet<String> {
        let mut scope = HashSet::new();
        // Wbudowane JS globals
        for g in JS_GLOBALS {
            scope.insert(g.to_string());
        }
        // QML delegate globals (model, modelData, index)
        for g in QML_DELEGATE_GLOBALS {
            scope.insert(g.to_string());
        }
        // Aliasy importów: `import "..." as Alias` → Alias jest dostępne globalnie
        // Also: `import TypeName 1.0` → TypeName is a C++ registered type/enum module
        for import_str in &file.imports {
            if let Some(as_pos) = import_str.find(" as ") {
                let alias = import_str[as_pos + 4..].trim();
                if !alias.is_empty()
                    && alias.chars().next().is_some_and(|c| c.is_alphabetic() || c == '_')
                    && alias.chars().all(|c| c.is_alphanumeric() || c == '_')
                {
                    scope.insert(alias.to_string());
                }
            } else {
                // `import TypeName 1.0` — module name itself becomes accessible as a namespace
                let parts: Vec<&str> = import_str.split_whitespace().collect();
                if parts.len() >= 3
                    && parts[0] == "import"
                    && !parts[1].starts_with('"')
                    && parts[1].chars().next().is_some_and(|c| c.is_uppercase())
                {
                    scope.insert(parts[1].to_string());
                }
            }
        }
        // id root elementu
        if let Some(id) = &file.id {
            scope.insert(id.clone());
        }
        // property root
        for p in &file.properties {
            scope.insert(p.name.clone());
        }
        // sygnały root
        for s in &file.signals {
            scope.insert(s.name.clone());
        }
        // funkcje root
        for f in &file.functions {
            scope.insert(f.name.clone());
        }
        // id dzieci
        self.collect_child_ids(&file.children, &mut scope);
        // property z bazy Qt (przez pełen łańcuch typów bazowych)
        let resolved_base = self.resolve_qt_type(&file.base_type);
        for (name, _) in self.db.all_properties(&resolved_base) {
            scope.insert(name);
        }
        // Properties/signals from user-defined QML base type chain
        // e.g. Global extends WindowBase → windowBusy from WindowBase is in scope
        {
            let mut current = file.base_type.clone();
            let mut seen = HashSet::new();
            while seen.insert(current.clone()) {
                if let Some((props, sigs)) = self.ctx.file_members.get(&current) {
                    for p in props {
                        scope.insert(p.clone());
                    }
                    for s in sigs {
                        scope.insert(s.clone());
                    }
                }
                match self.ctx.file_base_types.get(&current) {
                    Some(b) if self.ctx.file_members.contains_key(b.as_str()) => current = b.clone(),
                    _ => break,
                }
            }
        }
        // Property dostępne z zasięgu rodzica (pliku nadrzędnego który zawiera ten typ).
        // Np. Sub4.qml może odwoływać się do property zdefiniowanych w Global.qml.
        if let Some(parent_scope) = self.ctx.parent_scopes.get(&file.name) {
            scope.extend(parent_scope.iter().cloned());
        }
        // Syntetyczne id dzieci z konfiguracji (new_child)
        // The config supports two forms of keys:
        //  - ParentType = ["ChildType1", ...]         -> applies file-wide (creates synthetic child ids from child types)
        //  - ParentType.childId = ["ChildType1", ...] -> attaches child types to a specific child id inside the file
        if let Some(child_types) = self.ctx.extra_children.get(&file.name) {
            for child_type in child_types {
                scope.insert(type_name_to_id(child_type));
                // Also expose that child type's properties and signals
                if let Some((props, sigs)) = self.ctx.file_members.get(child_type) {
                    for prop in props {
                        scope.insert(prop.clone());
                    }
                    for sig in sigs {
                        scope.insert(sig.clone());
                    }
                }
            }
        }
        // per-instance entries: keys like "FileName.childId"
        let prefix = format!("{}.", file.name);
        for (key, child_types) in &self.ctx.extra_children {
            if key.starts_with(&prefix) {
                let child_id = key[prefix.len()..].to_string();
                // expose the child id itself and its members
                scope.insert(child_id.clone());
                for child_type in child_types {
                    if let Some((props, sigs)) = self.ctx.file_members.get(child_type) {
                        for prop in props {
                            scope.insert(prop.clone());
                        }
                        for sig in sigs {
                            scope.insert(sig.clone());
                        }
                    }
                }
            }
        }
        // C++ globale (singletony + obiekty kontekstu)
        for name in &self.ctx.cpp_globals {
            scope.insert(name.clone());
        }
        scope
    }

    fn collect_child_ids(&self, children: &[QmlChild], scope: &mut HashSet<String>) {
        for child in children {
            if let Some(id) = &child.id {
                scope.insert(id.clone());
            }
            self.collect_child_ids(&child.children, scope);
        }
    }

    // ── sprawdzanie root ──────────────────────────────────────────────────

    fn check_root(&self, file: &FileItem, file_scope: &HashSet<String>, errors: &mut Vec<AnalysisError>) {
        let effective_base = self.resolve_qt_type(&file.base_type);
        let qt_props = self.db.all_properties(&effective_base);
        let qt_signals = self.db.all_signals(&effective_base);

        // Budujemy mapę property pliku (name → PropertyType) do sprawdzania referencji
        let file_prop_types: HashMap<String, &PropertyType> =
            file.properties.iter().map(|p| (p.name.clone(), &p.prop_type)).collect();

        // Mapa id dzieci → info o dziecku (do sprawdzania member access w funkcjach)
        let mut child_id_map = build_child_id_map(&file.children);
        // Syntetyczne dzieci z konfiguracji (new_child)
        if let Some(child_types) = self.ctx.extra_children.get(&file.name) {
            for child_type in child_types {
                let id = type_name_to_id(child_type);
                child_id_map.entry(id).or_insert_with(|| ChildInfo {
                    type_name: child_type.clone(),
                    properties: vec![],
                });
            }
        }
        // per-instance extra_children entries (ParentType.childId)
        let prefix = format!("{}.", file.name);
        for (key, child_types) in &self.ctx.extra_children {
            if key.starts_with(&prefix) {
                let child_id = key[prefix.len()..].to_string();
                for child_type in child_types {
                    child_id_map.entry(child_id.clone()).or_insert_with(|| ChildInfo {
                        type_name: child_type.clone(),
                        properties: vec![],
                    });
                }
            }
        }

        // 1. Sprawdź deklaracje property
        for prop in &file.properties {
            Self::check_property_decl(prop, &qt_props, &file_prop_types, file_scope, self.db, errors, None);
        }

        // 2. Sprawdź funkcje / handlery sygnałów
        for func in &file.functions {
            self.check_function(
                func,
                file_scope,
                &qt_props,
                &qt_signals,
                &file.signals,
                &child_id_map,
                errors,
                None,
            );
        }

        // 3. Sprawdź root-level inline assignments (e.g. `width: expr`, `invalidProp: val`).
        let type_member_names = self.all_file_member_names(&file.name);
        for (name, value_expr, line) in &file.assignments {
            let prop_known = qt_props.contains_key(name.as_str())
                || file.properties.iter().any(|p| p.name == *name)
                || type_member_names.contains(name.as_str());
            if !prop_known {
                errors.push(
                    AnalysisError::new(ErrorKind::UnknownPropertyAssignment { name: name.clone() }).with_line(*line),
                );
            }
            // Check names in the value expression against scope.
            // Skip standalone object literals `{…}` and inline type instantiations `TypeName {…}`.
            if !is_inline_type_instantiation(value_expr) {
                let mut seen_names: HashSet<String> = HashSet::new();
                let mut seen_cpp: HashSet<(String, String)> = HashSet::new();
                for (base_name, member) in collect_dotted_accesses_from_expression(value_expr) {
                    if let Some(accessed) = &member {
                        let key = (base_name.clone(), accessed.clone());
                        if seen_cpp.insert(key)
                            && let Some(members_opt) = self.ctx.cpp_object_members.get(&base_name)
                            && let Some(members) = members_opt
                            && !members.contains(accessed.as_str())
                        {
                            errors.push(
                                AnalysisError::new(ErrorKind::UnknownCppMember {
                                    object: base_name.clone(),
                                    member: accessed.clone(),
                                })
                                .with_line(*line),
                            );
                        }
                    }
                    if !seen_names.insert(base_name.clone()) {
                        continue;
                    }
                    if self.db.has_type(&base_name) {
                        continue;
                    }
                    if !file_scope.contains(base_name.as_str()) && !is_js_global(&base_name) {
                        errors.push(
                            AnalysisError::new(ErrorKind::UndefinedPropertyAccess {
                                prop: name.clone(),
                                name: base_name.clone(),
                            })
                            .with_line(*line),
                        );
                    }
                }
            }
        }

        // 4. Sprawdź dzieci
        for child in &file.children {
            self.check_child(child, file_scope, errors, &[], &child_id_map);
        }
    }

    // ── sprawdzanie property ──────────────────────────────────────────────

    fn check_property_decl(
        prop: &Property,
        qt_props: &HashMap<String, String>,
        file_prop_types: &HashMap<String, &PropertyType>,
        scope: &HashSet<String>,
        db: &QtTypeDb,
        errors: &mut Vec<AnalysisError>,
        context: Option<&str>,
    ) {
        // 1. Redefinicja property z klasy bazowej
        if qt_props.contains_key(&prop.name) {
            let base = "base type".to_string();
            let mut e = AnalysisError::new(ErrorKind::PropertyRedefinition {
                name: prop.name.clone(),
                base_type: base,
            })
            .with_line(prop.line);
            if let Some(c) = context {
                e = e.with_context(c);
            }
            errors.push(e);
        }

        // 2. Niezgodność typu literału
        if !matches!(prop.prop_type, PropertyType::Var)
            && let Some(mismatch) = Self::literal_type_mismatch(&prop.prop_type, &prop.value)
        {
            let mut e = AnalysisError::new(ErrorKind::PropertyTypeMismatch {
                name: prop.name.clone(),
                declared: prop_type_name(&prop.prop_type),
                assigned: mismatch,
            })
            .with_line(prop.line);
            if let Some(c) = context {
                e = e.with_context(c);
            }
            errors.push(e);
        }

        // 3. Niezgodność przez referencję do innej property (TooComplex = skip)
        if matches!(prop.value, PropertyValue::TooComplex) {
            // wyrażenie — nie sprawdzamy
        } else if prop.value == PropertyValue::Unset {
            // brak wartości — ok
        } else {
            // sprawdź accessed_properties – jeśli jest dokładnie jedna i wartość to TooComplex -> nie
            // (obsługiwane wyżej)
        }

        // Jeśli wartość to identyfikator innej property (prosty ref, np. `property bool b: other`)
        // – sprawdź zgodność typów. Dla złożonych wyrażeń (np. `!other`) pomijamy.
        if matches!(prop.value, PropertyValue::TooComplex)
            && prop.is_simple_ref
            && !matches!(prop.prop_type, PropertyType::Var)
        {
            let ref_name = &prop.accessed_properties[0];
            if let Some(ref_type) = file_prop_types.get(ref_name)
                && !types_compatible(&prop.prop_type, ref_type)
            {
                let mut e = AnalysisError::new(ErrorKind::PropertyRefTypeMismatch {
                    name: prop.name.clone(),
                    declared: prop_type_name(&prop.prop_type),
                    ref_name: ref_name.clone(),
                    ref_type: prop_type_name(ref_type),
                })
                .with_line(prop.line);
                if let Some(c) = context {
                    e = e.with_context(c);
                }
                errors.push(e);
            }
        }

        // Sprawdź czy wszystkie nazwy w wyrażeniu property są w zasięgu
        if matches!(prop.value, PropertyValue::TooComplex) {
            let mut seen = HashSet::new();
            for name in &prop.accessed_properties {
                if !seen.insert(name.as_str()) {
                    continue; // zdeduplikuj
                }
                if db.has_type(name) {
                    continue; // Qt type used as enum namespace (e.g. `Popup.CloseOnEscape`)
                }
                if !scope.contains(name.as_str()) {
                    let mut e = AnalysisError::new(ErrorKind::UndefinedPropertyAccess {
                        prop: prop.name.clone(),
                        name: name.clone(),
                    })
                    .with_line(prop.line);
                    if let Some(c) = context {
                        e = e.with_context(c);
                    }
                    errors.push(e);
                }
            }
        }
    }

    /// Zwraca Some("bool"/"int"/…) jeśli literał nie pasuje do deklarowanego typu.
    fn literal_type_mismatch(declared: &PropertyType, value: &PropertyValue) -> Option<String> {
        // early-return for non-literals / null
        match value {
            PropertyValue::Unset | PropertyValue::TooComplex | PropertyValue::Null => return None,
            _ => {}
        }

        match (declared, value) {
            // assigned a bool where a non-bool numeric/string was expected
            (
                PropertyType::Int | PropertyType::String | PropertyType::Double | PropertyType::Real,
                PropertyValue::Bool(_),
            ) => Some("bool".into()),
            // assigned an int where a bool or string was expected
            (PropertyType::Bool | PropertyType::String, PropertyValue::Int(_)) => Some("int".into()),
            // assigned a double where a bool or int was expected
            (PropertyType::Bool | PropertyType::Int, PropertyValue::Double(_)) => Some("double".into()),
            // assigned a string literal where a numeric or bool type was expected
            (
                PropertyType::Int | PropertyType::Bool | PropertyType::Double | PropertyType::Real,
                PropertyValue::String(_),
            ) => Some("string".into()),
            // assigned a double literal where string was expected
            (PropertyType::String, PropertyValue::Double(_)) => Some("double".into()),
            _ => None,
        }
    }

    // ── sprawdzanie funkcji ───────────────────────────────────────────────

    fn check_function(
        &self,
        func: &Function,
        file_scope: &HashSet<String>,
        qt_props: &HashMap<String, String>,
        qt_signals: &HashMap<String, Vec<String>>,
        declared_signals: &[crate::types::Signal],
        child_id_map: &HashMap<String, ChildInfo>,
        errors: &mut Vec<AnalysisError>,
        context: Option<&str>,
    ) {
        // Sprawdź czy handler sygnału ma odpowiadający sygnał/property
        if func.is_signal_handler {
            let signal_name = handler_to_signal(&func.name);
            let prop_exists = qt_props.contains_key(&signal_name) || file_scope.contains(&signal_name);
            let signal_exists = qt_signals.contains_key(&format!("{signal_name}Changed"))
                || qt_signals.contains_key(&signal_name)
                || declared_signals.iter().any(|s| s.name == signal_name)
                // handler onXxxChanged → property xxx istnieje?
                || {
                    let maybe_prop = strip_changed_suffix(&signal_name);
                    maybe_prop.is_some_and(|p| qt_props.contains_key(p) || file_scope.contains(p))
                };

            if !prop_exists && !signal_exists {
                let mut e = AnalysisError::new(ErrorKind::UnknownSignalHandler {
                    handler: func.name.clone(),
                })
                .with_line(func.line);
                if let Some(c) = context {
                    e = e.with_context(c);
                }
                errors.push(e);
            }
        }

        // Zbuduj lokalny zasięg: parametry + wszystkie nazwy użyte w ciele funkcji
        // (parser nie rozróżnia deklaracji od użycia – traktujemy wszystkie nazwy
        //  obecne w ciele jako potencjalnie lokalne, Plan: nie sprawdzamy zakresu zmiennych)
        let mut local: HashSet<String> = func.parameters.iter().cloned().collect();
        for used in &func.used_names {
            if !file_scope.contains(&used.name) && !is_js_global(&used.name) {
                local.insert(used.name.clone());
            }
        }

        let _all_scope: HashSet<&str> = file_scope
            .iter()
            .map(String::as_str)
            .chain(func.parameters.iter().map(String::as_str))
            .chain(local.iter().map(String::as_str))
            .chain(JS_GLOBALS.iter().copied())
            .collect();

        // Nazwy które nie istnieją nigdzie = błąd.
        // Ponieważ powyżej dodaliśmy wszystkie used_names do local,
        // jedynymi undefined są te, które nie zostały zgłoszone przez żadne
        // znane źródło. Strategia: zgłaszamy tylko te, które nie mają żadnego
        // „partnera" – tzn. nie mają accessed_item (bo samo `zzzz` bez żadnego
        // przypisania lokalnego jest błędem).
        //
        // Uproszczenie z Planu: sprawdzamy czy nazwa istnieje w którymkolwiek zasięgu.
        // Skoro local zawiera wszystkie used_names, wszystko będzie w all_scope.
        // Musimy zatem inaczej: nie dodawać do local nazw które NIE są w file_scope.
        // Zamiast tego: tylko nazwy których nie ma w file_scope ORAZ nie są wyłącznie
        // po lewej stronie = (przypisanie), co parser też zbiera.
        //
        // Rzeczywiste podejście: collect_function_body_names zbiera WSZYSTKIE identyfikatory
        // z ciała. Nie wiemy które są deklaracjami. Plan mówi "nie sprawdzamy zakresu zmiennych".
        // Dlatego: sprawdzamy tylko te nazwy, które są w `used_names` BEZ accessed_item
        // ORAZ nie ma ich w file_scope. Jeśli nazwa ma accessed_item (foo.bar) – ignorujemy,
        // bo używana jako obiekt (var/local). Bez accessed_item i bez file_scope → undefined.

        // Zbuduj TYLKO file_scope + parameters + declared_locals + JS_GLOBALS (bez local!)
        let strict_scope: HashSet<&str> = file_scope
            .iter()
            .map(String::as_str)
            .chain(func.parameters.iter().map(String::as_str))
            .chain(func.declared_locals.iter().map(String::as_str))
            .chain(JS_GLOBALS.iter().copied())
            .collect();

        let mut seen_bases: HashSet<&str> = HashSet::new();
        let mut seen_cpp_members: HashSet<(String, String)> = HashSet::new();
        for used in &func.used_names {
            let name = used.name.as_str();
            // Use the per-name line if available, otherwise fall back to the function start line.
            let err_line = if used.line > 0 { used.line } else { func.line };

            if let Some(accessed) = &used.accessed_item {
                // Check C++ object member access if the object has known members.
                let key = (name.to_string(), accessed.clone());
                if seen_cpp_members.insert(key)
                    && let Some(members_opt) = self.ctx.cpp_object_members.get(name)
                    && let Some(members) = members_opt
                    && !members.contains(accessed.as_str())
                {
                    let mut e = AnalysisError::new(ErrorKind::UnknownCppMember {
                        object: name.to_string(),
                        member: accessed.clone(),
                    })
                    .with_line(err_line);
                    if let Some(c) = context {
                        e = e.with_context(c);
                    }
                    errors.push(e);
                }
                // Also check that the base name itself is in scope
                // (e.g. `nnnonono_existent.state` → `nnnonono_existent` must be defined).
                if !seen_bases.contains(name) {
                    seen_bases.insert(name);
                    if !strict_scope.contains(name) && !self.db.has_type(name) {
                        let mut e = AnalysisError::new(ErrorKind::UndefinedName {
                            name: name.to_string(),
                            function: func.name.clone(),
                        })
                        .with_line(err_line);
                        if let Some(c) = context {
                            e = e.with_context(c);
                        }
                        errors.push(e);
                    }
                }
                continue;
            }

            if seen_bases.contains(name) {
                continue;
            }
            seen_bases.insert(name);

            if !strict_scope.contains(name) && !self.db.has_type(name) {
                let mut e = AnalysisError::new(ErrorKind::UndefinedName {
                    name: name.to_string(),
                    function: func.name.clone(),
                })
                .with_line(err_line);
                if let Some(c) = context {
                    e = e.with_context(c);
                }
                errors.push(e);
            }
        }

        // Sprawdź member assignments: obj.prop = value
        for assignment in &func.member_assignments {
            // C++ objects: skip child-element checks (member access was already
            // validated in the used_names loop above via accessed_item).
            if self.ctx.cpp_object_members.contains_key(&assignment.object) {
                continue;
            }

            let Some(child_info) = child_id_map.get(&assignment.object) else {
                // Nie znamy tego obiektu (może być JS local) — pomijamy
                continue;
            };

            // Connections objects are treated as opaque — their API is dynamic.
            if child_info.type_name == "Connections" {
                continue;
            }

            // Jeśli typ dziecka jest całkowicie nieznany (nie w Qt DB, nie ma zadeklarowanych
            // properties) — traktujemy jako opaque i zezwalamy na wszystkie operacje.
            if !self.db.has_type(&child_info.type_name) && child_info.properties.is_empty() {
                continue;
            }

            let child_qt_props = self.db.all_properties(&child_info.type_name);
            let qt_type_str = child_qt_props.get(&assignment.member);
            let decl_prop = child_info.properties.iter().find(|p| p.name == assignment.member);

            if qt_type_str.is_none() && decl_prop.is_none() {
                let mut e = AnalysisError::new(ErrorKind::UnknownMemberAccess {
                    object: assignment.object.clone(),
                    member: assignment.member.clone(),
                })
                .with_line(func.line);
                if let Some(c) = context {
                    e = e.with_context(c);
                }
                errors.push(e);
                continue;
            }

            // Sprawdź zgodność typu
            if matches!(assignment.value, PropertyValue::TooComplex | PropertyValue::Unset) {
                continue;
            }
            let expected_type = if let Some(decl) = decl_prop {
                decl.prop_type.clone()
            } else if let Some(qt_str) = qt_type_str {
                PropertyType::from_token(qt_str)
            } else {
                continue;
            };

            if matches!(expected_type, PropertyType::Var) {
                continue;
            }

            if let Some(mismatch) = Self::literal_type_mismatch(&expected_type, &assignment.value) {
                let mut e = AnalysisError::new(ErrorKind::MemberAssignmentTypeMismatch {
                    object: assignment.object.clone(),
                    member: assignment.member.clone(),
                    expected: prop_type_name(&expected_type),
                    assigned: mismatch,
                })
                .with_line(func.line);
                if let Some(c) = context {
                    e = e.with_context(c);
                }
                errors.push(e);
            }
        }
    }

    // ── sprawdzanie dzieci ────────────────────────────────────────────────

    fn check_child(
        &self,
        child: &QmlChild,
        parent_scope: &HashSet<String>,
        errors: &mut Vec<AnalysisError>,
        elem_path: &[String],
        file_id_map: &HashMap<String, ChildInfo>,
    ) {
        let ctx = child.id.as_deref().unwrap_or(&child.type_name).to_string();

        // Build the element path for this child (used in --complex mode).
        let mut my_elem_path = elem_path.to_vec();
        my_elem_path.push(ctx.clone());

        // Sprawdź czy typ jest znany — albo Qt, albo sparsowany plik QML.
        // Sprawdzamy tylko gdy known_types nie jest puste (tzn. mamy pełen kontekst projektu).
        // Puste known_types oznacza tryb izolowany (testy jednostkowe) — pomijamy sprawdzanie.
        if !self.ctx.known_types.is_empty()
            && !self.db.has_type(&child.type_name)
            && !self.ctx.known_types.contains(&child.type_name)
        {
            errors.push(
                AnalysisError::new(ErrorKind::UnknownType {
                    type_name: child.type_name.clone(),
                })
                .with_line(child.line),
            );
            return;
        }

        // Dla znanych typów QML (pliki .qml) używamy ich typu bazowego do wyszukiwania Qt props.
        // Np. Sub2 extends Switch → qt_props = Switch's properties (includes `checked`).
        // Musimy iść przez cały łańcuch typów bazowych aż do Qt type (TextButton → GenericButton → RoundButton).
        let effective_type = self.resolve_qt_type(&child.type_name);
        let qt_props = self.db.all_properties(&effective_type);
        let qt_signals = self.db.all_signals(&effective_type);

        // Zbuduj zasięg dziecka = zasięg rodzica + własne property + własne id
        let mut child_scope = parent_scope.clone();
        if let Some(id) = &child.id {
            child_scope.insert(id.clone());
        }
        for p in &child.properties {
            child_scope.insert(p.name.clone());
        }
        for name in qt_props.keys() {
            child_scope.insert(name.clone());
        }
        // Add properties/signals from QML base type chain (e.g. Sub4 → SwitchWrapper → switchWrapperColor).
        for name in self.all_file_member_names(&child.type_name) {
            child_scope.insert(name);
        }

        let child_prop_types: HashMap<String, &PropertyType> = child
            .properties
            .iter()
            .map(|p| (p.name.clone(), &p.prop_type))
            .collect();

        // Track errors generated at this level so we can attach element_path in --complex mode.
        let level_start = errors.len();

        for prop in &child.properties {
            Self::check_property_decl(
                prop,
                &qt_props,
                &child_prop_types,
                &child_scope,
                self.db,
                errors,
                Some(&ctx),
            );
        }

        for func in &child.functions {
            self.check_function(
                func,
                &child_scope,
                &qt_props,
                &qt_signals,
                &[],
                file_id_map, // file-wide id map: QML ids are file-scoped
                errors,
                Some(&ctx),
            );
        }

        // Check inline property assignments (e.g. `non_existtttttttend: 2`) against known props,
        // and check names used in the value expression against scope.
        let type_member_names = self.all_file_member_names(&child.type_name);
        for (name, value_expr, line) in &child.assignments {
            let prop_known = qt_props.contains_key(name.as_str())
                || child.properties.iter().any(|p| p.name == *name)
                || type_member_names.contains(name.as_str());
            if !prop_known {
                errors.push(
                    AnalysisError::new(ErrorKind::UnknownPropertyAssignment { name: name.clone() })
                        .with_line(*line)
                        .with_context(&ctx),
                );
            }
            // Check names used in the value expression against scope.
            // Skip standalone object literals `{ … }` and inline type instantiations `TypeName { … }`.
            if !is_inline_type_instantiation(value_expr) {
                let mut seen_names: HashSet<String> = HashSet::new();
                let mut seen_cpp: HashSet<(String, String)> = HashSet::new();
                for (base_name, member) in collect_dotted_accesses_from_expression(value_expr) {
                    // Validate C++ member access (base.member)
                    if let Some(accessed) = &member {
                        let key = (base_name.clone(), accessed.clone());
                        if seen_cpp.insert(key)
                            && let Some(members_opt) = self.ctx.cpp_object_members.get(&base_name)
                            && let Some(members) = members_opt
                            && !members.contains(accessed.as_str())
                        {
                            errors.push(
                                AnalysisError::new(ErrorKind::UnknownCppMember {
                                    object: base_name.clone(),
                                    member: accessed.clone(),
                                })
                                .with_line(*line)
                                .with_context(&ctx),
                            );
                        }
                    }

                    if !seen_names.insert(base_name.clone()) {
                        continue;
                    }
                    // Skip Qt types used as enum namespaces (e.g. Text.AlignLeft)
                    if self.db.has_type(&base_name) {
                        continue;
                    }
                    if !child_scope.contains(base_name.as_str()) && !is_js_global(&base_name) {
                        errors.push(
                            AnalysisError::new(ErrorKind::UndefinedPropertyAccess {
                                prop: name.clone(),
                                name: base_name.clone(),
                            })
                            .with_line(*line)
                            .with_context(&ctx),
                        );
                    }
                }
            }
        }

        // In --complex mode, stamp all errors generated at this level with the element path.
        if self.ctx.complex {
            for e in &mut errors[level_start..] {
                e.element_path = my_elem_path.clone();
            }
        }

        for grandchild in &child.children {
            self.check_child(grandchild, &child_scope, errors, &my_elem_path, file_id_map);
        }
    }
}

// ── pomocnicze ────────────────────────────────────────────────────────────

// `onWidthChanged` → `widthChanged` lub `width`
fn handler_to_signal(handler: &str) -> String {
    let body = &handler[2..]; // strip `on`
    let mut chars = body.chars();
    match chars.next() {
        Some(c) => c.to_lowercase().to_string() + chars.as_str(),
        None => String::new(),
    }
}

/// `widthChanged` → Some("width")
fn strip_changed_suffix(s: &str) -> Option<&str> {
    s.strip_suffix("Changed")
}

/// `NewExaminationScreen` → `newExaminationScreen`
fn type_name_to_id(type_name: &str) -> String {
    let mut chars = type_name.chars();
    match chars.next() {
        Some(c) => c.to_lowercase().to_string() + chars.as_str(),
        None => String::new(),
    }
}

fn prop_type_name(t: &PropertyType) -> String {
    match t {
        PropertyType::Int => "int".into(),
        PropertyType::Bool => "bool".into(),
        PropertyType::String => "string".into(),
        PropertyType::Var => "var".into(),
        PropertyType::Double => "double".into(),
        PropertyType::Real => "real".into(),
        PropertyType::Url => "url".into(),
        PropertyType::Color => "color".into(),
        PropertyType::List => "list".into(),
        PropertyType::Custom(s) => s.clone(),
    }
}

fn types_compatible(a: &PropertyType, b: &PropertyType) -> bool {
    if matches!(a, PropertyType::Var) || matches!(b, PropertyType::Var) {
        return true;
    }
    // real i double są kompatybilne
    let numeric = |t: &PropertyType| matches!(t, PropertyType::Double | PropertyType::Real);
    if numeric(a) && numeric(b) {
        return true;
    }
    a == b
}

fn is_js_global(name: &str) -> bool {
    JS_GLOBALS.contains(&name)
}

/// Returns `true` when `expr` is an inline QML type instantiation like `Rotation { … }` or
/// `Scale { … }` — an uppercase-starting identifier immediately followed by `{`.
/// Properties inside are not free variable references, so the caller should skip name-checking.
fn is_inline_type_instantiation(expr: &str) -> bool {
    let t = expr.trim_start();
    if t.starts_with('{') {
        return true; // pure JS object literal — already handled by callers, kept for convenience
    }
    let mut chars = t.chars();
    let Some(first) = chars.next() else { return false };
    if !first.is_uppercase() {
        return false;
    }
    let rest = chars.as_str();
    let name_end = rest
        .find(|c: char| !c.is_alphanumeric() && c != '_')
        .unwrap_or(rest.len());
    rest[name_end..].trim_start().starts_with('{')
}

/// QML-specyficzne zmienne dostępne w kontekście delegatów modeli.
const QML_DELEGATE_GLOBALS: &[&str] = &[
    "model",     // obiekt modelu w delegacie (model.field)
    "modelData", // dane elementu w prostym delegacie
    "index",     // indeks elementu w delegacie
];

const JS_GLOBALS: &[&str] = &[
    "console",
    "Math",
    "JSON",
    "parseInt",
    "parseFloat",
    "qsTr",
    "Qt",
    "undefined",
    "null",
    "true",
    "false",
    "NaN",
    "Infinity",
    "String",
    "Number",
    "Boolean",
    "Array",
    "Object",
    "Date",
    "RegExp",
    "Error",
    "Promise",
    "Symbol",
    "Map",
    "Set",
    "WeakMap",
    "WeakSet",
    "x",
    "y",
    "z", // często lokalne w lambdach
];