Skip to content

Commit c02b1eb

Browse files
committed
xaml: collapse same-scope duplicates in the XAML105 message
The distinct-scope gate was correct, but the emitted finding still formatted raw occurrences: a key appearing twice in the primary dict AND once in a merged dict reported "3 merged/primary scopes" and listed "primary" twice. Collapse occurrences to the first line per scope before building the message, so the count and the "where" list reflect distinct scopes only. Added a selftest (dup primary + merged → "2 scopes", "primary" listed once). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015zoG4YNCUf7RA5r8GAXMmb
1 parent 044dc0f commit c02b1eb

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

‎audit/static/tools/xaml_check.py‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,17 @@ def _rule_merged_dict_shadowing(root: Node, avalonia: bool) -> list[XamlFinding]
433433
# Shadowing requires the key in 2+ DISTINCT scopes (primary vs merged, or
434434
# two different merged dicts). Two entries in the SAME scope are a
435435
# duplicate-key error (runtime-invalid) — a different problem, not XAML105.
436-
if len({w for w, _ln in occ}) < 2:
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:
437442
continue
438-
where = ", ".join(f"{w} (line {ln})" for w, ln in occ)
443+
where = ", ".join(f"{w} (line {ln})" for w, ln in by_scope.items())
439444
out.append(XamlFinding(
440-
"XAML105", occ[0][1],
441-
f"resource key '{key}' is defined in {len(occ)} merged/primary scopes "
445+
"XAML105", next(iter(by_scope.values())),
446+
f"resource key '{key}' is defined in {len(by_scope)} merged/primary scopes "
442447
f"[{where}]; the effective value depends on merge order (last merged "
443448
"wins, primary beats merged) [resource: merged dictionary]"))
444449
return out
@@ -1030,6 +1035,24 @@ def rules(text: str) -> dict[str, XamlFinding]:
10301035
'</ResourceDictionary>\n')
10311036
check("XAML105" not in rules(samescope),
10321037
"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')")
10331056

10341057
# XAML101 — duplicate stateless converter across dictionaries.
10351058
conv = (f'<ResourceDictionary {_WPF_NS} xmlns:c="clr-namespace:App.Converters">\n'

0 commit comments

Comments
 (0)