Skip to content

Commit 6422933

Browse files
committed
Changed "CustomCSNamesToJS" to Dictionary.
1 parent bbe28f7 commit 6422933

File tree

2 files changed

+39
-49
lines changed

2 files changed

+39
-49
lines changed

CSharpToJavaScript/CSTOJSOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public record class CSTOJSOptions
9191
/// <summary>
9292
/// Array of custom names to convert.
9393
/// </summary>
94-
/// <remarks>Example: <c>[new Tuple&lt;string, string&gt;("Console", "console")]</c>. Will convert "Console" to "console".</remarks>
94+
/// <remarks>Example: <c>new(){["Console"] = "console"}</c>. Will convert "Console" to "console".</remarks>
9595
/// <value>
96-
/// Default: <c>[]</c>
96+
/// Default: <c>new()</c>
9797
/// </value>
98-
public Tuple<string, string>[] CustomCSNamesToJS { get; set; } = [];
98+
public Dictionary<string, string> CustomCSNamesToJS { get; set; } = new();
9999
/// <summary>
100100
/// Array of types to convert.
101101
/// </summary>

CSharpToJavaScript/Walker.cs

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,21 +2685,21 @@ where n.IsKind(SyntaxKind.ClassDeclaration)
26852685
}
26862686
}
26872687

2688-
foreach (MemberDeclarationSyntax item in mem)
2688+
for (int j = 0; j < mem.Count; j++)
26892689
{
26902690
SyntaxToken? _sT = null;
2691-
if (item is MethodDeclarationSyntax m)
2691+
if (mem[j] is MethodDeclarationSyntax m)
26922692
{
26932693
_sT = m.Identifier;
26942694
}
26952695

2696-
if (item is PropertyDeclarationSyntax p)
2696+
if (mem[j] is PropertyDeclarationSyntax p)
26972697
{
26982698
_sT = p.Identifier;
26992699
syntaxNode = p.Type;
27002700
}
27012701

2702-
if (item is FieldDeclarationSyntax f)
2702+
if (mem[j] is FieldDeclarationSyntax f)
27032703
{
27042704
IEnumerable<SyntaxToken> d3 = from e in f.DescendantTokens()
27052705
where e.IsKind(SyntaxKind.IdentifierToken)
@@ -5017,11 +5017,13 @@ public bool IdentifierToken(SyntaxNode node)
50175017
catch (Exception e)
50185018
{
50195019
symbolInfo = null;
5020+
/*
50205021
ImmutableArray<Diagnostic> diags = _Model.GetDeclarationDiagnostics();
50215022
foreach (Diagnostic item in diags)
50225023
{
50235024
Log.WarningLine(item.ToString(), _Options);
50245025
}
5026+
*/
50255027
Log.WarningLine(e.ToString(), _Options);
50265028
//throw;
50275029
}
@@ -5169,20 +5171,20 @@ public bool IdentifierToken(SyntaxNode node)
51695171

51705172
SyntaxList<MemberDeclarationSyntax> _members = _class.Members;
51715173

5172-
foreach (MemberDeclarationSyntax item in _members)
5174+
for (int i = 0; i < _members.Count; i++)
51735175
{
51745176
SyntaxToken _sT = default;
5175-
if (item is MethodDeclarationSyntax m)
5177+
if (_members[i] is MethodDeclarationSyntax m)
51765178
{
51775179
_sT = m.Identifier;
51785180
}
51795181

5180-
if (item is PropertyDeclarationSyntax p)
5182+
if (_members[i] is PropertyDeclarationSyntax p)
51815183
{
51825184
_sT = p.Identifier;
51835185
}
51845186

5185-
if (item is FieldDeclarationSyntax f)
5187+
if (_members[i] is FieldDeclarationSyntax f)
51865188
{
51875189
IEnumerable<SyntaxNode> vds = (from el in f.DescendantNodes()
51885190
where el.IsKind(SyntaxKind.VariableDeclarator)
@@ -5243,14 +5245,14 @@ where e.IsKind(SyntaxKind.IdentifierToken)
52435245
return true;
52445246
}
52455247

5246-
foreach (Type type in _Options.CustomCSTypesToJS)
5248+
for (int i = 0; i < _Options.CustomCSTypesToJS.Length; i++)
52475249
{
5248-
if (type.Name == text)
5250+
if (_Options.CustomCSTypesToJS[i].Name == text)
52495251
{
5250-
return _CheckAttributeData(type.GetCustomAttributes(true));
5252+
return _CheckAttributeData(_Options.CustomCSTypesToJS[i].GetCustomAttributes(true));
52515253
}
52525254

5253-
MemberInfo[] _Members = type.GetMembers();
5255+
MemberInfo[] _Members = _Options.CustomCSTypesToJS[i].GetMembers();
52545256

52555257
bool b = _CheckMembersInNestedClasses(_Members);
52565258

@@ -5262,20 +5264,20 @@ where e.IsKind(SyntaxKind.IdentifierToken)
52625264

52635265
bool _CheckMembersInNestedClasses(MemberInfo[] _members)
52645266
{
5265-
foreach (MemberInfo _memberInfo in _members)
5267+
for (int i = 0; i < _members.Length; i++)
52665268
{
5267-
Type? _type = _memberInfo as Type;
5269+
Type? _type = _members[i] as Type;
52685270

52695271
if (_type != null && _type.IsClass)
52705272
{
52715273
return _CheckMembersInNestedClasses(_type.GetMembers());
52725274
}
52735275

5274-
if (_memberInfo.Name == text)
5276+
if (_members[i].Name == text)
52755277
{
5276-
object[] _attrs = _memberInfo.GetCustomAttributes(true);
5278+
object[] _attrs = _members[i].GetCustomAttributes(true);
52775279

5278-
return _CheckAttributeData(_memberInfo.GetCustomAttributes(true));
5280+
return _CheckAttributeData(_members[i].GetCustomAttributes(true));
52795281
}
52805282
}
52815283

@@ -5284,23 +5286,23 @@ bool _CheckMembersInNestedClasses(MemberInfo[] _members)
52845286

52855287
bool _CheckAttributeData(object[] attrs)
52865288
{
5287-
foreach (object _attr in attrs)
5289+
for (int i = 0; i < attrs.Length; i++)
52885290
{
5289-
if (_attr is EnumValueAttribute enumValueAttribute)
5291+
if (attrs[i] is EnumValueAttribute enumValueAttribute)
52905292
{
52915293
VisitLeadingTrivia(identifier);
52925294
JSSB.Append($"\"{enumValueAttribute.Value}\"");
52935295
VisitTrailingTrivia(identifier);
52945296
return true;
52955297
}
5296-
if (_attr is ValueAttribute valueAttribute)
5298+
if (attrs[i] is ValueAttribute valueAttribute)
52975299
{
52985300
VisitLeadingTrivia(identifier);
52995301
JSSB.Append($"{valueAttribute.Value}");
53005302
VisitTrailingTrivia(identifier);
53015303
return true;
53025304
}
5303-
if (_attr is ToAttribute toAttribute)
5305+
if (attrs[i] is ToAttribute toAttribute)
53045306
{
53055307
if (toAttribute.To == ToAttribute.None)
53065308
_IgnoreTailingDot = true;
@@ -5321,16 +5323,6 @@ bool _CheckAttributeData(object[] attrs)
53215323
{
53225324
if (BuiltInTypesGenerics(node, iSymbol) == false)
53235325
{
5324-
if (_Options.Debug)
5325-
{
5326-
Log.WarningLine("WARNING! Diagnostics starts ---", _Options);
5327-
ImmutableArray<Diagnostic> diag = _Model.GetDiagnostics();
5328-
foreach (Diagnostic item in diag)
5329-
{
5330-
Log.WarningLine(item.ToString(), _Options);
5331-
}
5332-
Log.WarningLine("WARNING! Diagnostics ends ---", _Options);
5333-
}
53345326
return false;
53355327
}
53365328
}
@@ -5352,27 +5344,25 @@ bool _CheckAttributeData(object[] attrs)
53525344

53535345
private bool CustomCSNamesToJS(SyntaxNode? node)
53545346
{
5355-
foreach (Tuple<string, string> _item in _Options.CustomCSNamesToJS)
5347+
if (node is IdentifierNameSyntax _identifierName)
53565348
{
5357-
if (node is IdentifierNameSyntax _identifierName)
5349+
if (_Options.CustomCSNamesToJS.TryGetValue(_identifierName.Identifier.Text, out string? _value))
53585350
{
5359-
if (_identifierName.Identifier.Text == _item.Item1)
5360-
{
5361-
VisitLeadingTrivia(_identifierName.Identifier);
5362-
JSSB.Append(_item.Item2);
5363-
return true;
5364-
}
5351+
VisitLeadingTrivia(_identifierName.Identifier);
5352+
JSSB.Append(_value);
5353+
return true;
53655354
}
5366-
else if (node is GenericNameSyntax _genericName)
5355+
}
5356+
else if (node is GenericNameSyntax _genericName)
5357+
{
5358+
if (_Options.CustomCSNamesToJS.TryGetValue(_genericName.Identifier.Text, out string? _value))
53675359
{
5368-
if (_genericName.Identifier.Text == _item.Item1)
5369-
{
5370-
VisitLeadingTrivia(_genericName.Identifier);
5371-
JSSB.Append(_item.Item2);
5372-
return true;
5373-
}
5360+
VisitLeadingTrivia(_genericName.Identifier);
5361+
JSSB.Append(_value);
5362+
return true;
53745363
}
53755364
}
5365+
53765366
return false;
53775367
}
53785368

0 commit comments

Comments
 (0)