Skip to content

Commit e1f50db

Browse files
committed
Fix #10. Change receivers logic.
1 parent 2dd8ce7 commit e1f50db

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

src/UnityUxmlGenerator/Captures/UxmlFactoryCapture.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace UnityUxmlGenerator.Captures;
44

55
internal sealed class UxmlFactoryCapture : BaseCapture
66
{
7-
public UxmlFactoryCapture(ClassDeclarationSyntax @class, AttributeSyntax attribute) : base(@class)
7+
public UxmlFactoryCapture((ClassDeclarationSyntax Class, AttributeSyntax Attribute) data) : base(data.Class)
88
{
9-
Attribute = attribute;
9+
Attribute = data.Attribute;
1010
}
1111

1212
public override string ClassTag => "UxmlFactory";

src/UnityUxmlGenerator/Extensions/SyntaxNodeExtensions.cs

+23-11
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,38 @@ namespace UnityUxmlGenerator.Extensions;
55

66
internal static class SyntaxNodeExtensions
77
{
8-
public static bool IsAttributeWithName(this SyntaxNode syntaxNode, string attributeName,
9-
out AttributeSyntax? attribute)
8+
public static bool IsMemberHasAttribute<TMember>(this SyntaxNode syntaxNode, string attributeName,
9+
out (TMember Member, AttributeSyntax Attribute) result) where TMember : MemberDeclarationSyntax
1010
{
11-
if (syntaxNode is not AttributeSyntax attributeSyntax)
11+
if (syntaxNode is not TMember memberSyntax)
1212
{
13-
attribute = default;
13+
result = default;
1414
return false;
1515
}
1616

17-
attribute = attributeSyntax;
17+
result.Member = memberSyntax;
1818

19-
switch (attributeSyntax.Name)
19+
for (var i = 0; i < memberSyntax.AttributeLists.Count; i++)
2020
{
21-
case IdentifierNameSyntax identifierNameSyntax
22-
when identifierNameSyntax.Identifier.Text.Contains(attributeName):
23-
case QualifiedNameSyntax qualifiedNameSyntax
24-
when qualifiedNameSyntax.Right.Identifier.Text.Contains(attributeName):
25-
return true;
21+
var attributeList = memberSyntax.AttributeLists[i];
22+
for (var j = 0; j < attributeList.Attributes.Count; j++)
23+
{
24+
var attributeSyntax = attributeList.Attributes[j];
25+
switch (attributeSyntax.Name)
26+
{
27+
case IdentifierNameSyntax identifierNameSyntax
28+
when identifierNameSyntax.Identifier.Text.Contains(attributeName):
29+
case QualifiedNameSyntax qualifiedNameSyntax
30+
when qualifiedNameSyntax.Right.Identifier.Text.Contains(attributeName):
31+
{
32+
result.Attribute = attributeSyntax;
33+
return true;
34+
}
35+
}
36+
}
2637
}
2738

39+
result = default;
2840
return false;
2941
}
3042

src/UnityUxmlGenerator/SyntaxReceivers/UxmlFactoryReceiver.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@ internal sealed class UxmlFactoryReceiver : BaseReceiver
1515

1616
public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
1717
{
18-
if (syntaxNode.IsAttributeWithName(AttributeName, out var attribute) == false)
18+
if (syntaxNode.IsMemberHasAttribute<ClassDeclarationSyntax>(AttributeName,
19+
out (ClassDeclarationSyntax Class, AttributeSyntax Attribute) result) == false)
1920
{
2021
return;
2122
}
2223

23-
var @class = attribute!.GetParent<ClassDeclarationSyntax>();
24-
25-
if (@class.InheritsFromAnyType())
24+
if (result.Class.InheritsFromAnyType())
2625
{
27-
_captures.Add(new UxmlFactoryCapture(@class!, attribute!));
26+
_captures.Add(new UxmlFactoryCapture(result));
2827
}
29-
else if (@class is not null)
28+
else if (result.Class is not null)
3029
{
31-
RegisterDiagnostic(ClassHasNoBaseClassError, @class.GetLocation(), @class.Identifier.Text);
30+
RegisterDiagnostic(ClassHasNoBaseClassError, result.Class.GetLocation(), result.Class.Identifier.Text);
3231
}
3332
}
3433
}

src/UnityUxmlGenerator/SyntaxReceivers/UxmlTraitsReceiver.cs

+5-8
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@ internal sealed class UxmlTraitsReceiver : BaseReceiver
1616

1717
public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
1818
{
19-
if (syntaxNode.IsAttributeWithName(AttributeName, out var attribute) == false)
19+
if (syntaxNode.IsMemberHasAttribute<PropertyDeclarationSyntax>(AttributeName,
20+
out (PropertyDeclarationSyntax Property, AttributeSyntax Attribute) result) == false)
2021
{
2122
return;
2223
}
2324

24-
var property = attribute!.GetParent<PropertyDeclarationSyntax>();
25-
if (property is null)
26-
{
27-
return;
28-
}
25+
var (property, attribute) = result;
2926

30-
if (attribute!.ArgumentList is not null && attribute.ArgumentList.Arguments.Any())
27+
if (attribute.ArgumentList is not null && attribute.ArgumentList.Arguments.Any())
3128
{
3229
if (HasSameType(property, attribute.ArgumentList.Arguments.First()) == false)
3330
{
@@ -58,7 +55,7 @@ public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
5855
_captures.Add(uxmlTraits.ClassName, uxmlTraits);
5956
}
6057

61-
uxmlTraits.Properties.Add((property, attribute));
58+
uxmlTraits.Properties.Add(result);
6259
}
6360

6461
private static bool HasSameType(BasePropertyDeclarationSyntax property, AttributeArgumentSyntax attributeArgument)

0 commit comments

Comments
 (0)