Skip to content

Commit aa1c739

Browse files
committed
Moved checking attributes to a separate method.
1 parent dac7efc commit aa1c739

File tree

1 file changed

+109
-38
lines changed

1 file changed

+109
-38
lines changed

CSharpToJavaScript/Walker.cs

Lines changed: 109 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Reflection;
1010
using System.Text;
1111

12+
1213
namespace CSharpToJavaScript
1314
{
1415
//Useful links:
@@ -803,6 +804,15 @@ public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax no
803804

804805
switch (kind)
805806
{
807+
case SyntaxKind.ObjectCreationExpression:
808+
VisitObjectCreationExpression((ObjectCreationExpressionSyntax)asNode);
809+
break;
810+
case SyntaxKind.BaseExpression:
811+
VisitBaseExpression((BaseExpressionSyntax)asNode);
812+
break;
813+
case SyntaxKind.GenericName:
814+
VisitGenericName((GenericNameSyntax)asNode);
815+
break;
806816
case SyntaxKind.IdentifierName:
807817
VisitIdentifierName((IdentifierNameSyntax)asNode);
808818
break;
@@ -1985,6 +1995,9 @@ public override void VisitArrayRankSpecifier(ArrayRankSpecifierSyntax node)
19851995

19861996
switch (kind)
19871997
{
1998+
case SyntaxKind.IdentifierName:
1999+
VisitIdentifierName((IdentifierNameSyntax)asNode);
2000+
break;
19882001
case SyntaxKind.NumericLiteralExpression:
19892002
case SyntaxKind.DivideExpression:
19902003
Visit(asNode);
@@ -2853,6 +2866,60 @@ public bool IdentifierToken(SyntaxNode node)
28532866

28542867
//Attributes of this node(type)
28552868
ImmutableArray<AttributeData> _attributeDatas = iSymbol.GetAttributes();
2869+
2870+
List<object> _attributes = new();
2871+
2872+
foreach (AttributeData attributeData in _attributeDatas)
2873+
{
2874+
if (attributeData.AttributeClass.Name == nameof(EnumValueAttribute))
2875+
{
2876+
_attributes.Add(new EnumValueAttribute(attributeData.ConstructorArguments[0].Value as string));
2877+
}
2878+
2879+
if (attributeData.AttributeClass.Name == nameof(ValueAttribute))
2880+
{
2881+
_attributes.Add(new ValueAttribute(attributeData.ConstructorArguments[0].Value as string));
2882+
}
2883+
2884+
if (attributeData.AttributeClass.Name == nameof(ToAttribute))
2885+
{
2886+
_attributes.Add(new ToAttribute(attributeData.ConstructorArguments[0].Value as string));
2887+
}
2888+
}
2889+
2890+
bool _cad = _CheckAttributeData(_attributes.ToArray());
2891+
if (_cad)
2892+
{
2893+
return true;
2894+
}
2895+
else
2896+
{
2897+
//Attributes of a parent node(type)
2898+
_attributes.Clear();
2899+
_attributeDatas = iSymbol.ContainingType.GetAttributes();
2900+
2901+
foreach (AttributeData attributeData in _attributeDatas)
2902+
{
2903+
if (attributeData.AttributeClass.Name == nameof(EnumValueAttribute))
2904+
{
2905+
_attributes.Add(new EnumValueAttribute(attributeData.ConstructorArguments[0].Value as string));
2906+
}
2907+
2908+
if (attributeData.AttributeClass.Name == nameof(ValueAttribute))
2909+
{
2910+
_attributes.Add(new ValueAttribute(attributeData.ConstructorArguments[0].Value as string));
2911+
}
2912+
2913+
if (attributeData.AttributeClass.Name == nameof(ToAttribute))
2914+
{
2915+
_attributes.Add(new ToAttribute(attributeData.ConstructorArguments[0].Value as string));
2916+
}
2917+
}
2918+
2919+
return _CheckAttributeData(_attributes.ToArray());
2920+
}
2921+
2922+
/*
28562923
foreach (AttributeData _attr in _attributeDatas)
28572924
{
28582925
if (_attr.AttributeClass.Name == nameof(EnumValueAttribute))
@@ -2913,7 +2980,7 @@ public bool IdentifierToken(SyntaxNode node)
29132980
VisitTrailingTrivia(identifier);
29142981
return true;
29152982
}
2916-
}
2983+
}*/
29172984
}
29182985

29192986
if (iSymbol.ContainingNamespace.ToString().Contains(_NameSpaceStr))
@@ -3009,26 +3076,7 @@ where e.IsKind(SyntaxKind.IdentifierToken)
30093076
{
30103077
if (type.Name == text)
30113078
{
3012-
object[] _attrs = type.GetCustomAttributes(true);
3013-
foreach (object _attr in _attrs)
3014-
{
3015-
3016-
if (_attr is ValueAttribute valueAttribute)
3017-
{
3018-
VisitLeadingTrivia(identifier);
3019-
JSSB.Append($"{valueAttribute.Value}");
3020-
VisitTrailingTrivia(identifier);
3021-
return true;
3022-
}
3023-
if (_attr is ToAttribute toAttribute)
3024-
{
3025-
VisitLeadingTrivia(identifier);
3026-
JSSB.Append($"{toAttribute.Convert(text)}");
3027-
return true;
3028-
}
3029-
}
3030-
3031-
return true;
3079+
return _CheckAttributeData(type.GetCustomAttributes(true));
30323080
}
30333081

30343082
MemberInfo[] _Members = type.GetMembers();
@@ -3038,43 +3086,66 @@ where e.IsKind(SyntaxKind.IdentifierToken)
30383086
if (b == true)
30393087
return true;
30403088
}
3089+
3090+
3091+
30413092
bool _CheckMembersInNestedClasses(MemberInfo[] _members)
30423093
{
30433094
foreach (MemberInfo _memberInfo in _members)
30443095
{
30453096
Type? _type = _memberInfo as Type;
3097+
30463098
if (_type != null && _type.IsClass)
30473099
{
30483100
return _CheckMembersInNestedClasses(_type.GetMembers());
30493101
}
3102+
30503103
if (_memberInfo.Name == text)
30513104
{
30523105
object[] _attrs = _memberInfo.GetCustomAttributes(true);
30533106

3054-
foreach (object _attr in _attrs)
3055-
{
3056-
if (_attr is ValueAttribute valueAttribute)
3057-
{
3058-
VisitLeadingTrivia(identifier);
3059-
JSSB.Append($"{valueAttribute.Value}");
3060-
VisitTrailingTrivia(identifier);
3061-
return true;
3062-
}
3063-
if (_attr is ToAttribute toAttribute)
3064-
{
3065-
VisitLeadingTrivia(identifier);
3066-
JSSB.Append($"{toAttribute.Convert(text)}");
3067-
return true;
3068-
}
3069-
}
3107+
return _CheckAttributeData(_memberInfo.GetCustomAttributes(true));
3108+
}
3109+
}
30703110

3111+
return false;
3112+
}
3113+
3114+
bool _CheckAttributeData(object[] attrs)
3115+
{
3116+
foreach (object _attr in attrs)
3117+
{
3118+
if (_attr is EnumValueAttribute enumValueAttribute)
3119+
{
3120+
VisitLeadingTrivia(identifier);
3121+
JSSB.Append($"\"{enumValueAttribute.Value}\"");
3122+
VisitTrailingTrivia(identifier);
3123+
return true;
3124+
}
3125+
if (_attr is ValueAttribute valueAttribute)
3126+
{
3127+
VisitLeadingTrivia(identifier);
3128+
JSSB.Append($"{valueAttribute.Value}");
3129+
VisitTrailingTrivia(identifier);
3130+
return true;
3131+
}
3132+
if (_attr is ToAttribute toAttribute)
3133+
{
3134+
if (toAttribute.To == ToAttribute.None)
3135+
_IgnoreTailingDot = true;
3136+
3137+
VisitLeadingTrivia(identifier);
3138+
JSSB.Append($"{toAttribute.Convert(text)}");
3139+
VisitTrailingTrivia(identifier);
30713140
return true;
30723141
}
30733142
}
30743143

30753144
return false;
30763145
}
3077-
3146+
3147+
3148+
30783149
if (CustomCSNamesToJS(node) == false)
30793150
{
30803151
if (BuiltInTypesGenerics(node, iSymbol) == false)

0 commit comments

Comments
 (0)