|
27 | 27 | perf/lifetime axis that WpfAnalyzers / PropertyChangedAnalyzers do **not** cover — |
28 | 28 | we deliberately do not re-implement their correctness rules: |
29 | 29 |
|
30 | | - XAML100 ResourceShouldBeHoisted (cat 9) Freezable keyed in N local scopes |
| 30 | + XAML100 ResourceShouldBeHoisted (cat 9) Freezable/Style/template dup'd per-scope |
31 | 31 | XAML101 DuplicateStatelessConverterResource (cat 9) per-instance resource churn |
32 | 32 | XAML102 DynamicResourceLikelyStatic (cat 9) WPF-only, deferred-lookup cost |
33 | 33 | XAML103 SuspiciousSharedFalse (cat 9) WPF-only, x:Shared opt-out |
|
41 | 41 | XAML112 TemplateBindingOpportunity (cat 9) cheaper compiled binding available |
42 | 42 | XAML113 InlineFreezableDuplication (cat 9) identical inline brush/geometry |
43 | 43 |
|
44 | | -XAML100 covers the recurring-Freezable case (the same keyed brush/geometry/transform |
45 | | -declared in several control-local scopes); the Style/template variant (deep |
46 | | -structural equivalence) and XAML105 MergedDictionaryKeyShadowing across *external* |
47 | | -dictionaries (cross-file resolution) remain a later slice — documented so nothing on |
48 | | -the wishlist quietly falls through. Phase 2 (Roslyn-linked XAML2xx) and Phase 3 |
49 | | -(runtime correlation) live elsewhere per the design note. |
| 44 | +XAML100 covers the recurring heavy-resource case across control-local scopes — both |
| 45 | +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. |
50 | 50 |
|
51 | 51 | WPF-only rules (XAML102/103/106) are skipped on Avalonia ``.axaml`` because the |
52 | 52 | ``DynamicResource`` / ``x:Shared`` / ``Freezable`` semantics differ or do not exist |
|
95 | 95 | # the document root and the window/app/page-level dictionaries (XAML100). |
96 | 96 | TOP_LEVEL_SCOPES = {"root", "Window", "UserControl", "Application", "Page", |
97 | 97 | "NavigationWindow", "ResourceDictionary"} |
| 98 | +# Heavy keyed resources worth hoisting when duplicated across control-local scopes |
| 99 | +# (XAML100): Freezables (a shallow sig would do) plus Styles/templates (which need the |
| 100 | +# DEEP structural signature, since their direct-child types alone — all `Setter` — say |
| 101 | +# nothing about the values). |
| 102 | +HOISTABLE_RESOURCE_TYPES = FREEZABLE_TYPES | { |
| 103 | + "Style", "ControlTemplate", "DataTemplate", "HierarchicalDataTemplate", |
| 104 | + "ItemsPanelTemplate", |
| 105 | +} |
98 | 106 |
|
99 | 107 | # Binding markers for XAML108 (per-keystroke source updates). |
100 | 108 | _TWOWAY_RE = re.compile(r"Mode\s*=\s*TwoWay", re.IGNORECASE) |
@@ -396,24 +404,38 @@ def _scope_owner(rd: Node) -> str: |
396 | 404 | return "root" |
397 | 405 |
|
398 | 406 |
|
| 407 | +def _deep_resource_sig(node: Node) -> tuple[Any, ...]: |
| 408 | + """A *recursive* structural signature: the element's tag (sans namespace prefix, |
| 409 | + keeping the property-element dotted form), its non-``x:Key`` attributes, and the |
| 410 | + signatures of all its children in document order. Two resources share this only |
| 411 | + when their entire subtree is structurally identical — so a `Style`/`ControlTemplate` |
| 412 | + is matched on its actual setters/triggers/template, not just "has Setter children". |
| 413 | + Child order is significant (a conservative choice: only literal duplicates flag).""" |
| 414 | + head = node.tag.split(":", 1)[-1] |
| 415 | + attrs = tuple(sorted((k.split(":", 1)[-1], v) for k, v in node.attrib.items() |
| 416 | + if k.split(":", 1)[-1] != "Key")) |
| 417 | + kids = tuple(_deep_resource_sig(c) for c in node.children) |
| 418 | + return (head, attrs, kids) |
| 419 | + |
| 420 | + |
399 | 421 | def _rule_resource_hoist(root: Node, avalonia: bool, |
400 | 422 | min_copies: int = 2) -> list[XamlFinding]: |
401 | | - """XAML100 — a heavy Freezable resource (brush/geometry/transform/image) declared |
402 | | - with ``x:Key`` in several **control-local** ``<X.Resources>`` scopes with the same |
403 | | - structure: each copy is a separate object multiplying working set across siblings, |
404 | | - when one shared resource at window/app scope would do (the "52x52 Brush collapse" |
405 | | - of the design note). Restricted to Freezables, where the shallow structural |
406 | | - signature is reliable; Styles/templates (deep structural equivalence) are a later |
407 | | - refinement. Resources already at a shared (root/window/app) scope are the hoist |
408 | | - *target*, not a finding.""" |
| 423 | + """XAML100 — a heavy keyed resource (Freezable brush/geometry/transform/image, or a |
| 424 | + ``Style``/template) declared in several **control-local** ``<X.Resources>`` scopes |
| 425 | + with the same *deep* structure: each copy is a separate object multiplying working |
| 426 | + set across siblings, when one shared resource at window/app scope would do (the |
| 427 | + "52x52 Brush collapse" of the design note). Equivalence is the full-subtree |
| 428 | + ``_deep_resource_sig``, so a `Style`/template only collapses with a structurally |
| 429 | + identical twin (same setters/values), never merely a same-shaped one. Resources |
| 430 | + already at a shared (root/window/app) scope are the hoist *target*, not a finding.""" |
409 | 431 | by_sig: dict[tuple[Any, ...], list[Node]] = {} |
410 | 432 | for rd in _resource_dictionaries(root): |
411 | 433 | if _scope_owner(rd) in TOP_LEVEL_SCOPES: |
412 | 434 | continue # already a shared scope — the place to hoist *to* |
413 | 435 | for _key, c in _keyed_resources(rd): |
414 | | - if c.type_name() not in FREEZABLE_TYPES: |
| 436 | + if c.type_name() not in HOISTABLE_RESOURCE_TYPES: |
415 | 437 | continue |
416 | | - sig = _inline_freezable_sig(c) # type + non-key attrs + child types |
| 438 | + sig = _deep_resource_sig(c) # full subtree: tag + non-key attrs + children |
417 | 439 | if not sig[1] and not sig[2]: |
418 | 440 | continue # empty/defaulted element — nothing meaningful to share |
419 | 441 | by_sig.setdefault(sig, []).append(c) |
@@ -1037,6 +1059,24 @@ def rules(text: str) -> dict[str, XamlFinding]: |
1037 | 1059 | check("XAML100" not in rules(distinct), |
1038 | 1060 | "XAML100 false positive: structurally distinct local resources are not duplicates") |
1039 | 1061 |
|
| 1062 | + # XAML100 for Styles — the deep signature: an IDENTICAL style in two local scopes |
| 1063 | + # is a duplicate, but two styles with different Setter values are NOT. |
| 1064 | + def _two_styles(setter1: str, setter2: str) -> str: |
| 1065 | + def blk(owner: str, setter: str) -> str: |
| 1066 | + return (f' <{owner}><{owner}.Resources>\n' |
| 1067 | + ' <Style x:Key="s" TargetType="Button">\n' |
| 1068 | + f' <Setter Property="Background" Value="{setter}" />\n' |
| 1069 | + ' </Style>\n' |
| 1070 | + f' </{owner}.Resources></{owner}>\n') |
| 1071 | + return (f'<UserControl {_WPF_NS}>\n <Grid>\n' |
| 1072 | + + blk("Border", setter1) + blk("StackPanel", setter2) |
| 1073 | + + ' </Grid>\n</UserControl>\n') |
| 1074 | + check("XAML100" in rules(_two_styles("Red", "Red")), |
| 1075 | + "XAML100 must flag an identical Style duplicated across control-local scopes") |
| 1076 | + check("XAML100" not in rules(_two_styles("Red", "Blue")), |
| 1077 | + "XAML100 false positive: Styles with different Setter values are not duplicates " |
| 1078 | + "(deep structural signature)") |
| 1079 | + |
1040 | 1080 | # Implicit <X.Resources> dictionary (the common WPF syntax, no <ResourceDictionary> |
1041 | 1081 | # wrapper) must feed the keyed-resource rules — else XAML101/102/106 miss most files. |
1042 | 1082 | implicit = (f'<Window {_WPF_NS}>\n' |
|
0 commit comments