|
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,44 @@ 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 | + if len(occ) < 2: |
| 434 | + continue # the normal case: a key defined once |
| 435 | + where = ", ".join(f"{w} (line {ln})" for w, ln in occ) |
| 436 | + out.append(XamlFinding( |
| 437 | + "XAML105", occ[0][1], |
| 438 | + f"resource key '{key}' is defined in {len(occ)} merged/primary scopes " |
| 439 | + f"[{where}]; the effective value depends on merge order (last merged " |
| 440 | + "wins, primary beats merged) [resource: merged dictionary]")) |
| 441 | + return out |
| 442 | + |
| 443 | + |
404 | 444 | def _scope_owner(rd: Node) -> str: |
405 | 445 | """The element type that owns a resource dictionary — the nearest ``<X.Resources>`` |
406 | 446 | ancestor (``Grid``, ``Border``, ``DataTemplate`` …), or ``root`` for the document |
@@ -725,6 +765,7 @@ def _rule_layout_transform(root: Node, avalonia: bool) -> list[XamlFinding]: |
725 | 765 |
|
726 | 766 | RULES: list[Callable[[Node, bool], list[XamlFinding]]] = [ |
727 | 767 | _rule_resource_hoist, |
| 768 | + _rule_merged_dict_shadowing, |
728 | 769 | _rule_virtualization, |
729 | 770 | _rule_template_complexity, |
730 | 771 | _rule_per_keystroke_binding, |
@@ -941,6 +982,39 @@ def rules(text: str) -> dict[str, XamlFinding]: |
941 | 982 | check(r.get("XAML104") and r["XAML104"].line == 5, |
942 | 983 | "XAML104 must point at the duplicate include (line 5)") |
943 | 984 |
|
| 985 | + # XAML105 — the same key in two inline merged dictionaries (order-dependent). |
| 986 | + shadow = (f'<ResourceDictionary {_WPF_NS}>\n' |
| 987 | + ' <ResourceDictionary.MergedDictionaries>\n' |
| 988 | + ' <ResourceDictionary>\n' |
| 989 | + ' <SolidColorBrush x:Key="accent" Color="Red" />\n' |
| 990 | + ' </ResourceDictionary>\n' |
| 991 | + ' <ResourceDictionary>\n' |
| 992 | + ' <SolidColorBrush x:Key="accent" Color="Blue" />\n' |
| 993 | + ' </ResourceDictionary>\n' |
| 994 | + ' </ResourceDictionary.MergedDictionaries>\n' |
| 995 | + '</ResourceDictionary>\n') |
| 996 | + r = rules(shadow) |
| 997 | + check("XAML105" in r, "XAML105 must flag a key defined in two inline merged dictionaries") |
| 998 | + check(r.get("XAML105") and "accent" in r["XAML105"].message, "XAML105 must name the key") |
| 999 | + # primary key + a merged-dictionary key collision is also shadowing. |
| 1000 | + primary = (f'<ResourceDictionary {_WPF_NS}>\n' |
| 1001 | + ' <SolidColorBrush x:Key="accent" Color="Green" />\n' |
| 1002 | + ' <ResourceDictionary.MergedDictionaries>\n' |
| 1003 | + ' <ResourceDictionary>\n' |
| 1004 | + ' <SolidColorBrush x:Key="accent" Color="Blue" />\n' |
| 1005 | + ' </ResourceDictionary>\n' |
| 1006 | + ' </ResourceDictionary.MergedDictionaries>\n' |
| 1007 | + '</ResourceDictionary>\n') |
| 1008 | + check("XAML105" in rules(primary), |
| 1009 | + "XAML105 must flag a primary key shadowed by a merged dictionary") |
| 1010 | + # distinct keys across merged dicts -> no collision, no finding. |
| 1011 | + nodup = shadow.replace('x:Key="accent" Color="Blue"', 'x:Key="other" Color="Blue"') |
| 1012 | + check("XAML105" not in rules(nodup), |
| 1013 | + "XAML105 false positive: distinct keys across merged dictionaries do not shadow") |
| 1014 | + # external Source dictionaries are not resolved -> not compared (deferred). |
| 1015 | + check("XAML105" not in rules(dup), |
| 1016 | + "XAML105 must not compare keys of external Source= dictionaries (cross-file deferred)") |
| 1017 | + |
944 | 1018 | # XAML101 — duplicate stateless converter across dictionaries. |
945 | 1019 | conv = (f'<ResourceDictionary {_WPF_NS} xmlns:c="clr-namespace:App.Converters">\n' |
946 | 1020 | ' <c:BoolToVisibilityConverter x:Key="b2v" />\n' |
|
0 commit comments