Skip to content

Commit 709dfde

Browse files
Rewrite loop for readability
Clean up loop checking child content names in `ComponentChildContentDiagnosticPass` to be a bit more readable.
1 parent dfee7ce commit 709dfde

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Components/ComponentChildContentDiagnosticPass.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@ public override void VisitComponentChildContent(ComponentChildContentIntermediat
5050
if (node.IsParameterized)
5151
{
5252
var ancestors = Ancestors;
53-
5453
var parentComponent = (ComponentIntermediateNode)ancestors[0];
55-
ancestors = ancestors[1..];
5654

57-
while (!ancestors.IsEmpty)
55+
// Skip the immediate parent component as we've already validated against it.
56+
// Loop to ancestors.Length - 1 because we're always checking pairs.
57+
58+
for (var i = 1; i < ancestors.Length - 1; i++)
5859
{
59-
if (ancestors is
60-
[
61-
ComponentChildContentIntermediateNode { IsParameterized: true } ancestor,
62-
ComponentIntermediateNode ancestorParentComponent,
63-
..
64-
]
65-
&& ancestor.ParameterName == node.ParameterName)
60+
if (ancestors[i] is ComponentChildContentIntermediateNode { IsParameterized: true } ancestor &&
61+
ancestor.ParameterName == node.ParameterName &&
62+
ancestors[i + 1] is ComponentIntermediateNode ancestorParentComponent)
6663
{
6764
// Duplicate name. We report an error because this will almost certainly also lead to an error
6865
// from the C# compiler that's way less clear.
@@ -73,8 +70,6 @@ public override void VisitComponentChildContent(ComponentChildContentIntermediat
7370
childContent2: ancestor,
7471
component2: ancestorParentComponent));
7572
}
76-
77-
ancestors = ancestors[1..];
7873
}
7974
}
8075

0 commit comments

Comments
 (0)