Skip to content

Commit a925b19

Browse files
committed
Remove unnecessary loop, use list pattern instead
1 parent 9297335 commit a925b19

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Input/RelayCommandGenerator.Execute.cs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -969,48 +969,51 @@ static void GatherForwardedAttributes(
969969
in ImmutableArrayBuilder<AttributeInfo> fieldAttributesInfo,
970970
in ImmutableArrayBuilder<AttributeInfo> propertyAttributesInfo)
971971
{
972-
foreach (SyntaxReference syntaxReference in methodSymbol.DeclaringSyntaxReferences)
972+
// Get the single syntax reference for the input method symbol (there should be only one)
973+
if (methodSymbol.DeclaringSyntaxReferences is not [SyntaxReference syntaxReference])
973974
{
974-
// Try to get the target method declaration syntax node
975-
if (syntaxReference.GetSyntax(token) is not MethodDeclarationSyntax methodDeclaration)
975+
return;
976+
}
977+
978+
// Try to get the target method declaration syntax node
979+
if (syntaxReference.GetSyntax(token) is not MethodDeclarationSyntax methodDeclaration)
980+
{
981+
return;
982+
}
983+
984+
// Gather explicit forwarded attributes info
985+
foreach (AttributeListSyntax attributeList in methodDeclaration.AttributeLists)
986+
{
987+
// Same as in the [ObservableProperty] generator, except we're also looking for fields here
988+
if (attributeList.Target?.Identifier is not SyntaxToken(SyntaxKind.PropertyKeyword or SyntaxKind.FieldKeyword))
976989
{
977990
continue;
978991
}
979992

980-
// Gather explicit forwarded attributes info
981-
foreach (AttributeListSyntax attributeList in methodDeclaration.AttributeLists)
993+
foreach (AttributeSyntax attribute in attributeList.Attributes)
982994
{
983-
// Same as in the [ObservableProperty] generator, except we're also looking for fields here
984-
if (attributeList.Target?.Identifier is not SyntaxToken(SyntaxKind.PropertyKeyword or SyntaxKind.FieldKeyword))
995+
// Get the symbol info for the attribute (once again just like in the [ObservableProperty] generator)
996+
if (!semanticModel.GetSymbolInfo(attribute, token).TryGetAttributeTypeSymbol(out INamedTypeSymbol? attributeTypeSymbol))
985997
{
998+
diagnostics.Add(
999+
InvalidFieldOrPropertyTargetedAttributeOnRelayCommandMethod,
1000+
attribute,
1001+
methodSymbol,
1002+
attribute.Name);
1003+
9861004
continue;
9871005
}
9881006

989-
foreach (AttributeSyntax attribute in attributeList.Attributes)
1007+
AttributeInfo attributeInfo = AttributeInfo.From(attributeTypeSymbol, semanticModel, attribute.ArgumentList?.Arguments ?? Enumerable.Empty<AttributeArgumentSyntax>(), token);
1008+
1009+
// Add the new attribute info to the right builder
1010+
if (attributeList.Target?.Identifier is SyntaxToken(SyntaxKind.FieldKeyword))
1011+
{
1012+
fieldAttributesInfo.Add(attributeInfo);
1013+
}
1014+
else
9901015
{
991-
// Get the symbol info for the attribute (once again just like in the [ObservableProperty] generator)
992-
if (!semanticModel.GetSymbolInfo(attribute, token).TryGetAttributeTypeSymbol(out INamedTypeSymbol? attributeTypeSymbol))
993-
{
994-
diagnostics.Add(
995-
InvalidFieldOrPropertyTargetedAttributeOnRelayCommandMethod,
996-
attribute,
997-
methodSymbol,
998-
attribute.Name);
999-
1000-
continue;
1001-
}
1002-
1003-
AttributeInfo attributeInfo = AttributeInfo.From(attributeTypeSymbol, semanticModel, attribute.ArgumentList?.Arguments ?? Enumerable.Empty<AttributeArgumentSyntax>(), token);
1004-
1005-
// Add the new attribute info to the right builder
1006-
if (attributeList.Target?.Identifier is SyntaxToken(SyntaxKind.FieldKeyword))
1007-
{
1008-
fieldAttributesInfo.Add(attributeInfo);
1009-
}
1010-
else
1011-
{
1012-
propertyAttributesInfo.Add(attributeInfo);
1013-
}
1016+
propertyAttributesInfo.Add(attributeInfo);
10141017
}
10151018
}
10161019
}

0 commit comments

Comments
 (0)