|
32 | 32 | XAML102 DynamicResourceLikelyStatic (cat 9) WPF-only, deferred-lookup cost |
33 | 33 | XAML103 SuspiciousSharedFalse (cat 9) WPF-only, x:Shared opt-out |
34 | 34 | XAML104 DuplicateMergedDictionaryInclude (cat 9) wasted load + order ambiguity |
| 35 | + XAML105 MergedDictionaryKeyShadowing (cat 9) in-file key collision, order-dependent |
35 | 36 | XAML106 FreezableResourceShouldFreeze (cat 9) WPF-only, change-notify overhead |
36 | 37 | XAML107 VirtualizationExplicitlyDisabled (cat 8) virtualization accidentally killed |
37 | 38 | XAML108 PerKeystrokeBindingWithoutDelay (cat 6) per-keystroke source flooding |
|
43 | 44 |
|
44 | 45 | XAML100 covers the recurring heavy-resource case across control-local scopes — both |
45 | 46 | Freezables and Styles/templates (the latter on a full-subtree signature). XAML105 |
46 | | -MergedDictionaryKeyShadowing across *external* dictionaries (cross-file resolution) |
47 | | -remains a later slice — documented so nothing on the wishlist quietly falls through. |
48 | | -Phase 2 (Roslyn-linked XAML2xx) and Phase 3 (runtime correlation) live elsewhere per |
49 | | -the design note. |
| 47 | +covers the *in-file* merged-dictionary key collision (inline dictionaries + the |
| 48 | +primary); the same shadowing across **external** ``Source=`` dictionaries needs |
| 49 | +cross-file resolution and remains a later slice — documented so nothing on the |
| 50 | +wishlist quietly falls through. Phase 2 (Roslyn-linked XAML2xx) and Phase 3 (runtime |
| 51 | +correlation) live elsewhere per the design note. |
50 | 52 |
|
51 | 53 | WPF-only rules (XAML102/103/106) are skipped on Avalonia ``.axaml`` because the |
52 | 54 | ``DynamicResource`` / ``x:Shared`` / ``Freezable`` semantics differ or do not exist |
@@ -401,6 +403,52 @@ def _keyed_resources(rd: Node): |
401 | 403 | yield key, c |
402 | 404 |
|
403 | 405 |
|
| 406 | +def _rule_merged_dict_shadowing(root: Node, avalonia: bool) -> list[XamlFinding]: |
| 407 | + """XAML105 — the same ``x:Key`` defined in more than one **inline** merged |
| 408 | + dictionary (or in the primary dictionary AND an inline merged one): the effective |
| 409 | + value then depends on merge order (WPF: last merged wins, and a primary key beats |
| 410 | + merged ones), a silent order-dependence that breaks when includes are reordered. |
| 411 | +
|
| 412 | + In-file only: dictionaries pulled in by ``Source="..."`` reference external files |
| 413 | + this single-file pass can't resolve, so their keys aren't compared (the cross-file |
| 414 | + variant is a documented later slice). FP-safe — only literal same-key collisions |
| 415 | + flag, and a lone key (the normal case) never does.""" |
| 416 | + out: list[XamlFinding] = [] |
| 417 | + for md in root.walk(): |
| 418 | + if md.local() != "MergedDictionaries" or not md.is_property_element(): |
| 419 | + continue |
| 420 | + host = md.parent # the owning ResourceDictionary / <X.Resources> |
| 421 | + # key -> [(where, line)] across the primary dict and each inline merged dict |
| 422 | + keymap: dict[str, list[tuple[str, int]]] = {} |
| 423 | + if host is not None: |
| 424 | + for key, c in _keyed_resources(host): |
| 425 | + keymap.setdefault(key, []).append(("primary", c.line)) |
| 426 | + inline_dicts = [c for c in md.children |
| 427 | + if c.type_name() == "ResourceDictionary" and c.attr("Source") is None] |
| 428 | + for i, mdict in enumerate(inline_dicts, start=1): |
| 429 | + for key, c in _keyed_resources(mdict): |
| 430 | + keymap.setdefault(key, []).append((f"merged #{i}", c.line)) |
| 431 | + |
| 432 | + for key, occ in keymap.items(): |
| 433 | + # Shadowing requires the key in 2+ DISTINCT scopes (primary vs merged, or |
| 434 | + # two different merged dicts). Two entries in the SAME scope are a |
| 435 | + # duplicate-key error (runtime-invalid) — a different problem, not XAML105. |
| 436 | + # Collapse to one line per scope (first occurrence) so the count and the |
| 437 | + # "where" list reflect distinct scopes, not repeated same-scope entries. |
| 438 | + by_scope: dict[str, int] = {} |
| 439 | + for w, ln in occ: |
| 440 | + by_scope.setdefault(w, ln) |
| 441 | + if len(by_scope) < 2: |
| 442 | + continue |
| 443 | + where = ", ".join(f"{w} (line {ln})" for w, ln in by_scope.items()) |
| 444 | + out.append(XamlFinding( |
| 445 | + "XAML105", next(iter(by_scope.values())), |
| 446 | + f"resource key '{key}' is defined in {len(by_scope)} merged/primary scopes " |
| 447 | + f"[{where}]; the effective value depends on merge order (last merged " |
| 448 | + "wins, primary beats merged) [resource: merged dictionary]")) |
| 449 | + return out |
| 450 | + |
| 451 | + |
404 | 452 | def _scope_owner(rd: Node) -> str: |
405 | 453 | """The element type that owns a resource dictionary — the nearest ``<X.Resources>`` |
406 | 454 | ancestor (``Grid``, ``Border``, ``DataTemplate`` …), or ``root`` for the document |
@@ -725,6 +773,7 @@ def _rule_layout_transform(root: Node, avalonia: bool) -> list[XamlFinding]: |
725 | 773 |
|
726 | 774 | RULES: list[Callable[[Node, bool], list[XamlFinding]]] = [ |
727 | 775 | _rule_resource_hoist, |
| 776 | + _rule_merged_dict_shadowing, |
728 | 777 | _rule_virtualization, |
729 | 778 | _rule_template_complexity, |
730 | 779 | _rule_per_keystroke_binding, |
@@ -941,6 +990,70 @@ def rules(text: str) -> dict[str, XamlFinding]: |
941 | 990 | check(r.get("XAML104") and r["XAML104"].line == 5, |
942 | 991 | "XAML104 must point at the duplicate include (line 5)") |
943 | 992 |
|
| 993 | + # XAML105 — the same key in two inline merged dictionaries (order-dependent). |
| 994 | + shadow = (f'<ResourceDictionary {_WPF_NS}>\n' |
| 995 | + ' <ResourceDictionary.MergedDictionaries>\n' |
| 996 | + ' <ResourceDictionary>\n' |
| 997 | + ' <SolidColorBrush x:Key="accent" Color="Red" />\n' |
| 998 | + ' </ResourceDictionary>\n' |
| 999 | + ' <ResourceDictionary>\n' |
| 1000 | + ' <SolidColorBrush x:Key="accent" Color="Blue" />\n' |
| 1001 | + ' </ResourceDictionary>\n' |
| 1002 | + ' </ResourceDictionary.MergedDictionaries>\n' |
| 1003 | + '</ResourceDictionary>\n') |
| 1004 | + r = rules(shadow) |
| 1005 | + check("XAML105" in r, "XAML105 must flag a key defined in two inline merged dictionaries") |
| 1006 | + check(r.get("XAML105") and "accent" in r["XAML105"].message, "XAML105 must name the key") |
| 1007 | + # primary key + a merged-dictionary key collision is also shadowing. |
| 1008 | + primary = (f'<ResourceDictionary {_WPF_NS}>\n' |
| 1009 | + ' <SolidColorBrush x:Key="accent" Color="Green" />\n' |
| 1010 | + ' <ResourceDictionary.MergedDictionaries>\n' |
| 1011 | + ' <ResourceDictionary>\n' |
| 1012 | + ' <SolidColorBrush x:Key="accent" Color="Blue" />\n' |
| 1013 | + ' </ResourceDictionary>\n' |
| 1014 | + ' </ResourceDictionary.MergedDictionaries>\n' |
| 1015 | + '</ResourceDictionary>\n') |
| 1016 | + check("XAML105" in rules(primary), |
| 1017 | + "XAML105 must flag a primary key shadowed by a merged dictionary") |
| 1018 | + # distinct keys across merged dicts -> no collision, no finding. |
| 1019 | + nodup = shadow.replace('x:Key="accent" Color="Blue"', 'x:Key="other" Color="Blue"') |
| 1020 | + check("XAML105" not in rules(nodup), |
| 1021 | + "XAML105 false positive: distinct keys across merged dictionaries do not shadow") |
| 1022 | + # external Source dictionaries are not resolved -> not compared (deferred). |
| 1023 | + check("XAML105" not in rules(dup), |
| 1024 | + "XAML105 must not compare keys of external Source= dictionaries (cross-file deferred)") |
| 1025 | + # two primary entries with the SAME key are a duplicate-key error in ONE scope, not |
| 1026 | + # order-dependent merge shadowing -> XAML105 must not fire (needs 2+ distinct scopes). |
| 1027 | + samescope = (f'<ResourceDictionary {_WPF_NS}>\n' |
| 1028 | + ' <SolidColorBrush x:Key="accent" Color="Green" />\n' |
| 1029 | + ' <SolidColorBrush x:Key="accent" Color="Teal" />\n' |
| 1030 | + ' <ResourceDictionary.MergedDictionaries>\n' |
| 1031 | + ' <ResourceDictionary>\n' |
| 1032 | + ' <SolidColorBrush x:Key="other" Color="Blue" />\n' |
| 1033 | + ' </ResourceDictionary>\n' |
| 1034 | + ' </ResourceDictionary.MergedDictionaries>\n' |
| 1035 | + '</ResourceDictionary>\n') |
| 1036 | + check("XAML105" not in rules(samescope), |
| 1037 | + "XAML105 false positive: same-scope duplicate keys are not merge-order shadowing") |
| 1038 | + # a key duplicated in primary AND present in a merged dict still shadows across the |
| 1039 | + # two distinct scopes, but the same-scope dup must collapse: report 2 scopes (not 3), |
| 1040 | + # with 'primary' listed once. |
| 1041 | + dupprimary = (f'<ResourceDictionary {_WPF_NS}>\n' |
| 1042 | + ' <SolidColorBrush x:Key="accent" Color="Green" />\n' |
| 1043 | + ' <SolidColorBrush x:Key="accent" Color="Teal" />\n' |
| 1044 | + ' <ResourceDictionary.MergedDictionaries>\n' |
| 1045 | + ' <ResourceDictionary>\n' |
| 1046 | + ' <SolidColorBrush x:Key="accent" Color="Blue" />\n' |
| 1047 | + ' </ResourceDictionary>\n' |
| 1048 | + ' </ResourceDictionary.MergedDictionaries>\n' |
| 1049 | + '</ResourceDictionary>\n') |
| 1050 | + dp = rules(dupprimary) |
| 1051 | + check("XAML105" in dp, "XAML105 must flag a primary key also defined in a merged dict") |
| 1052 | + check(dp.get("XAML105") and "2 merged/primary scopes" in dp["XAML105"].message, |
| 1053 | + "XAML105 must collapse same-scope duplicates: 2 distinct scopes, not 3") |
| 1054 | + check(dp.get("XAML105") and dp["XAML105"].message.count("primary (line") == 1, |
| 1055 | + "XAML105 must list each distinct scope once (no repeated 'primary')") |
| 1056 | + |
944 | 1057 | # XAML101 — duplicate stateless converter across dictionaries. |
945 | 1058 | conv = (f'<ResourceDictionary {_WPF_NS} xmlns:c="clr-namespace:App.Converters">\n' |
946 | 1059 | ' <c:BoolToVisibilityConverter x:Key="b2v" />\n' |
|
0 commit comments