-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWithSemanticWalker.cs
More file actions
717 lines (621 loc) · 20.9 KB
/
WithSemanticWalker.cs
File metadata and controls
717 lines (621 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
using CSharpToJavaScript.Utils;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Collections.Immutable;
namespace CSharpToJavaScript;
internal class WithSemanticWalker : CSharpSyntaxWalker
{
private readonly NETAPI _NETAPI = new();
private readonly SemanticModel _Model;
private readonly CSTOJSOptions _Options;
private readonly Dictionary<string, List<string>> _ExportedClasses;
private ITypeSymbol? _CurrentClassSymbol = null;
private bool _NoneWithTrailingDotRemoved = false;
public Dictionary<SyntaxNode, SyntaxNode> ReplaceNodes = new();
public Dictionary<string, List<string>> ImportClasses = new();
public WithSemanticWalker(SemanticModel semanticModel, CSTOJSOptions options, Dictionary<string, List<string>> exportedClasses)
{
_Model = semanticModel;
_Options = options;
_ExportedClasses = exportedClasses;
}
#if DEBUG
public override void Visit(SyntaxNode? node)
{
if (node != null)
Log.InfoLine($"kind: {node.Kind()} \n\t{node.ToString()}");
base.Visit(node);
}
#endif
public override void VisitUsingDirective(UsingDirectiveSyntax node)
{
return;
}
public override void VisitExternAliasDirective(ExternAliasDirectiveSyntax node)
{
return;
}
public override void VisitClassDeclaration(ClassDeclarationSyntax node)
{
_CurrentClassSymbol = _Model.GetDeclaredSymbol(node);
base.VisitClassDeclaration(node);
}
public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
{
base.VisitMemberAccessExpression(node);
if (_NoneWithTrailingDotRemoved)
{
SyntaxNode _node = node.Name;
if (ReplaceNodes.ContainsKey(_node))
_node = ReplaceNodes[_node];
ReplaceNodes.Add(node, _node);
_NoneWithTrailingDotRemoved = false;
}
if (node.Expression is IdentifierNameSyntax syntax)
{
if (TryReplaceIdentifierWithThis(syntax))
return;
}
}
public override void VisitEqualsValueClause(EqualsValueClauseSyntax node)
{
base.VisitEqualsValueClause(node);
if (node.Value is IdentifierNameSyntax syntax)
{
if (TryReplaceIdentifierWithThis(syntax))
return;
}
}
public override void VisitConditionalExpression(ConditionalExpressionSyntax node)
{
if (node.WhenTrue is IdentifierNameSyntax syntax)
{
TryReplaceIdentifierWithThis(syntax);
}
if (node.WhenFalse is IdentifierNameSyntax syntax1)
{
TryReplaceIdentifierWithThis(syntax1);
}
base.VisitConditionalExpression(node);
}
public override void VisitSwitchStatement(SwitchStatementSyntax node)
{
base.VisitSwitchStatement(node);
if (node.Expression is IdentifierNameSyntax syntax)
{
if (TryReplaceIdentifierWithThis(syntax))
return;
}
}
public override void VisitPostfixUnaryExpression(PostfixUnaryExpressionSyntax node)
{
if (node.Operand is IdentifierNameSyntax syntax)
{
TryReplaceIdentifierWithThis(syntax);
}
base.VisitPostfixUnaryExpression(node);
}
public override void VisitElementAccessExpression(ElementAccessExpressionSyntax node)
{
base.VisitElementAccessExpression(node);
if (node.Expression is IdentifierNameSyntax syntax)
{
if (TryReplaceIdentifierWithThis(syntax))
return;
}
}
public override void VisitInterpolation(InterpolationSyntax node)
{
if (node.Expression is IdentifierNameSyntax syntax)
{
if (TryReplaceIdentifierWithThis(syntax))
return;
}
base.VisitInterpolation(node);
}
public override void VisitArgument(ArgumentSyntax node)
{
if (node.Expression is IdentifierNameSyntax syntax)
{
if (TryReplaceIdentifierWithThis(syntax))
return;
}
base.VisitArgument(node);
}
public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
//Return if the expression is nameof.
//With an early return, we won't process built-in arguments(if used) in nameof.
if (node.Expression is IdentifierNameSyntax identifier)
{
if (identifier.Identifier.Text == "nameof")
{
return;
}
}
base.VisitInvocationExpression(node);
if (node.Expression is IdentifierNameSyntax ||
node.Expression is GenericNameSyntax)
{
ISymbol? symbol = TryGetISymbol(node.Expression);
if (symbol != null)
{
if (TryReplaceIdentifierWithThis((SimpleNameSyntax)node.Expression))
return;
ImmutableArray<AttributeData> _attributeData = symbol.GetAttributes();
for (int i = 0; i < _attributeData.Length; i++)
{
if (_attributeData[i].AttributeClass != null)
{
if (_attributeData[i].AttributeClass!.Name == nameof(BinaryAttribute))
{
IdentifierNameSyntax _identifier = SyntaxFactory.IdentifierName(_attributeData[i].ConstructorArguments[0].Value.ToString())
.WithAdditionalAnnotations(BinaryAttribute.Annotation)
.WithLeadingTrivia(node.Expression.GetLeadingTrivia())
.WithTrailingTrivia(node.Expression.GetTrailingTrivia());
if (ReplaceNodes.ContainsKey(node.Expression))
ReplaceNodes[node.Expression] = _identifier;
else
ReplaceNodes.Add(node.Expression, _identifier);
return;
}
if (_attributeData[i].AttributeClass!.Name == nameof(UnaryAttribute))
{
IdentifierNameSyntax _identifier = SyntaxFactory.IdentifierName(_attributeData[i].ConstructorArguments[0].Value.ToString())
.WithAdditionalAnnotations(UnaryAttribute.Annotation)
.WithLeadingTrivia(node.Expression.GetLeadingTrivia())
.WithTrailingTrivia(node.Expression.GetTrailingTrivia());
if (ReplaceNodes.ContainsKey(node.Expression))
ReplaceNodes[node.Expression] = _identifier;
else
ReplaceNodes.Add(node.Expression, _identifier);
return;
}
if (_attributeData[i].AttributeClass!.Name == nameof(GenericUnaryAttribute))
{
TypeSyntax _arg = ((GenericNameSyntax)node.Expression).TypeArgumentList.Arguments[0];
//Check if the generic is contained in ReplaceNodes.
//If yes, then use the replaced node.
//The same applies to GenericBinaryAttribute.
if (ReplaceNodes.ContainsKey(_arg))
_arg = (TypeSyntax)ReplaceNodes[_arg];
//Replace GenericName with IdentifierName from an attribute.
IdentifierNameSyntax _identifier = SyntaxFactory.IdentifierName(SyntaxFactory.Identifier(_attributeData[i].ConstructorArguments[0].Value.ToString()))
.WithAdditionalAnnotations(UnaryAttribute.Annotation)
.WithLeadingTrivia(node.Expression.GetLeadingTrivia())
.WithTrailingTrivia(node.Expression.GetTrailingTrivia());
if (ReplaceNodes.ContainsKey(node.Expression))
ReplaceNodes[node.Expression] = _identifier;
else
ReplaceNodes.Add(node.Expression, _identifier);
ReplaceNodes.Add(node.ArgumentList, node.ArgumentList
.AddArguments(SyntaxFactory.Argument(SyntaxFactory.IdentifierName(_arg.ToString()))));
return;
}
if (_attributeData[i].AttributeClass!.Name == nameof(GenericBinaryAttribute))
{
TypeSyntax _arg = ((GenericNameSyntax)node.Expression).TypeArgumentList.Arguments[0];
if (ReplaceNodes.ContainsKey(_arg))
_arg = (TypeSyntax)ReplaceNodes[_arg];
IdentifierNameSyntax _identifier = SyntaxFactory.IdentifierName(_attributeData[i].ConstructorArguments[0].Value.ToString())
.WithAdditionalAnnotations(BinaryAttribute.Annotation)
.WithLeadingTrivia(node.Expression.GetLeadingTrivia())
.WithTrailingTrivia(node.Expression.GetTrailingTrivia());
if (ReplaceNodes.ContainsKey(node.Expression))
ReplaceNodes[node.Expression] = _identifier;
else
ReplaceNodes.Add(node.Expression, _identifier);
ReplaceNodes.Add(node.ArgumentList, node.ArgumentList
.AddArguments(SyntaxFactory.Argument(SyntaxFactory.IdentifierName(_arg.ToString())))
.NormalizeWhitespace());
return;
}
}
}
}
}
if (node.Expression is MemberAccessExpressionSyntax member)
{
ISymbol? symbol = TryGetISymbol(member);
if (symbol != null)
{
ImmutableArray<AttributeData> _attributeData = symbol.GetAttributes();
for (int i = 0; i < _attributeData.Length; i++)
{
if (_attributeData[i].AttributeClass != null)
{
if (_attributeData[i].AttributeClass!.Name == nameof(GenericAsArgumentAttribute))
{
TypeSyntax _arg = ((GenericNameSyntax)member.Name).TypeArgumentList.Arguments[0];
if (ReplaceNodes.ContainsKey(_arg))
_arg = (TypeSyntax)ReplaceNodes[_arg];
ReplaceNodes.Add(node.ArgumentList, node.ArgumentList
.AddArguments(SyntaxFactory.Argument(SyntaxFactory.IdentifierName(_arg.ToString())))
.NormalizeWhitespace());
return;
}
}
}
}
}
}
public override void VisitBinaryExpression(BinaryExpressionSyntax node)
{
if (node.Left is IdentifierNameSyntax syntax)
{
TryReplaceIdentifierWithThis(syntax);
}
if (node.Right is IdentifierNameSyntax syntax1)
{
TryReplaceIdentifierWithThis(syntax1);
}
base.VisitBinaryExpression(node);
}
public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
{
if (node.Left is IdentifierNameSyntax syntax)
{
TryReplaceIdentifierWithThis(syntax);
}
if (node.Right is IdentifierNameSyntax syntax1)
{
TryReplaceIdentifierWithThis(syntax1);
}
base.VisitAssignmentExpression(node);
}
public override void VisitReturnStatement(ReturnStatementSyntax node)
{
if (node.Expression is IdentifierNameSyntax syntax)
{
TryReplaceIdentifierWithThis(syntax);
}
base.VisitReturnStatement(node);
}
public override void VisitPrefixUnaryExpression(PrefixUnaryExpressionSyntax node)
{
if (node.Operand is IdentifierNameSyntax syntax)
{
TryReplaceIdentifierWithThis(syntax);
}
base.VisitPrefixUnaryExpression(node);
}
public override void VisitIfStatement(IfStatementSyntax node)
{
if (node.Condition is IdentifierNameSyntax syntax)
{
TryReplaceIdentifierWithThis(syntax);
}
base.VisitIfStatement(node);
}
public override void VisitCastExpression(CastExpressionSyntax node)
{
if (node.Expression is IdentifierNameSyntax syntax)
{
TryReplaceIdentifierWithThis(syntax);
}
base.VisitCastExpression(node);
}
public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
{
base.VisitObjectCreationExpression(node);
ISymbol? symbol = TryGetISymbol(node.Type);
if (symbol != null)
{
ImmutableArray<AttributeData> _attributeData = symbol.GetAttributes();
for (int i = 0; i < _attributeData.Length; i++)
{
if (_attributeData[i].AttributeClass != null)
{
if (_attributeData[i].AttributeClass!.Name == nameof(ToObjectAttribute))
{
ReplaceNodes.Add(node.Type, node.Type.WithAdditionalAnnotations(ToObjectAttribute.Annotation));
break;
}
}
}
}
}
public override void VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node)
{
base.VisitImplicitObjectCreationExpression(node);
ISymbol? symbol = TryGetISymbol(node);
if (symbol != null)
{
ArgumentListSyntax _args = node.ArgumentList;
InitializerExpressionSyntax? _initializer = node.Initializer;
if (ReplaceNodes.ContainsKey(_args))
_args = (ArgumentListSyntax)ReplaceNodes[_args];
if (_initializer != null && ReplaceNodes.ContainsKey(_initializer))
_initializer = (InitializerExpressionSyntax)ReplaceNodes[_initializer];
SimpleNameSyntax _type = SyntaxFactory.IdentifierName(symbol.ContainingType.Name).WithLeadingTrivia(SyntaxFactory.Whitespace(" "));
//Check if type needs to be replaced
VisitSimpleName(_type, symbol);
//if yes, use replaced node!
if (ReplaceNodes.ContainsKey(_type))
_type = (SimpleNameSyntax)ReplaceNodes[_type];
ReplaceNodes.Add(node, SyntaxFactory.ObjectCreationExpression(node.NewKeyword,
_type,
_args,
_initializer));
}
}
public override void VisitIdentifierName(IdentifierNameSyntax node)
{
ISymbol? symbol = TryGetISymbol(node);
if (_Options.EnableModules >= 2)
{
if (symbol != null)
AddImportModule(symbol);
}
VisitSimpleName(node, symbol);
}
public override void VisitGenericName(GenericNameSyntax node)
{
base.VisitGenericName(node);
VisitSimpleName(node, TryGetISymbol(node));
}
public override void VisitPredefinedType(PredefinedTypeSyntax node)
{
ISymbol? symbol = TryGetISymbol(node);
if (symbol != null)
{
if (BuiltInTypesGenerics(node, symbol, out string str))
{
ReplaceNodes.Add(node, SyntaxFactory.IdentifierName(str).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()));
return;
}
}
}
private void AddImportModule(ISymbol symbol)
{
if (symbol.Kind == SymbolKind.NamedType &&
symbol.DeclaredAccessibility == Accessibility.Public)
{
//Return it if it is not in the source files.
if (symbol.Locations[0].IsInMetadata)
return;
if (symbol.Locations[0].SourceTree != null)
{
string _filename = symbol.Locations[0].SourceTree!.FilePath;
if (_filename != _CurrentClassSymbol.Locations[0].SourceTree.FilePath)
{
//If the symbol location is empty,
//return, the import requires a file path/filename.
//Warning printed in "ExportClassesWalker.VisitClassDeclaration"
if (_filename == string.Empty)
return;
if (_ExportedClasses.TryGetValue(_filename, out List<string>? _classes))
{
if (_classes.Contains(symbol.Name))
{
if (ImportClasses.ContainsKey(_filename))
{
if (!ImportClasses[_filename].Contains(symbol.Name))
ImportClasses[_filename].Add(symbol.Name);
}
else
ImportClasses.Add(_filename, new() { symbol.Name });
}
}
}
}
}
}
private bool TryReplaceIdentifierWithThis(SimpleNameSyntax identifier)
{
ISymbol? symbol = TryGetISymbol(identifier);
if (symbol != null && !symbol.IsStatic)
{
if (((symbol.Kind == SymbolKind.Method &&
((IMethodSymbol)symbol).MethodKind == MethodKind.Ordinary)) ||
symbol.Kind == SymbolKind.Property ||
symbol.Kind == SymbolKind.Field)
{
ITypeSymbol? _base = _CurrentClassSymbol;
while (_base != null)
{
ImmutableArray<ISymbol> _members = _base.GetMembers();
for (int i = 0; i < _members.Length; i++)
{
if (_members[i].Name == identifier.Identifier.Text)
{
//Check if an identifier exists before creating a member.
SimpleNameSyntax _temp = identifier;
if (ReplaceNodes.ContainsKey(identifier))
_temp = (SimpleNameSyntax)ReplaceNodes[identifier];
MemberAccessExpressionSyntax _member = SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.ThisExpression(), _temp.WithoutTrivia())
.WithLeadingTrivia(_temp.GetLeadingTrivia())
.WithTrailingTrivia(_temp.GetTrailingTrivia());
if (ReplaceNodes.ContainsKey(identifier))
ReplaceNodes[identifier] = _member;
else
ReplaceNodes.Add(identifier, _member);
return true;
}
}
_base = _base.BaseType;
}
}
}
return false;
}
private void VisitSimpleName(SimpleNameSyntax identifier, ISymbol? symbol)
{
ISymbol? oldSymbol = symbol;
if (symbol != null)
{
CheckParentAttributes:
ImmutableArray<AttributeData> _attributeData = symbol.GetAttributes();
for (int i = 0; i < _attributeData.Length; i++)
{
if (_attributeData[i].AttributeClass != null)
{
if (_attributeData[i].AttributeClass!.Name == nameof(ValueAttribute))
{
string _v = _attributeData[i].ConstructorArguments[0].Value.ToString();
ReplaceNodes.Add(identifier, SyntaxFactory.IdentifierName(_v).WithLeadingTrivia(identifier.Identifier.LeadingTrivia).WithTrailingTrivia(identifier.Identifier.TrailingTrivia));
return;
}
if (_attributeData[i].AttributeClass!.Name == nameof(ToAttribute))
{
ToAttribute _toAttr = new(_attributeData[i].ConstructorArguments[0].Value.ToString());
if (_toAttr.To == ToAttribute.NoneWithLeadingDotRemoved)
{
SyntaxNode? _mae = identifier.Parent;
if (_mae == null)
{
Log.ErrorLine($"Parent of {identifier} is null");
return;
}
if (_mae is not MemberAccessExpressionSyntax)
{
Log.ErrorLine($"Parent of {identifier} is {_mae.GetType()}. Should be MemberAccessExpressionSyntax");
return;
}
//Check if node is in ReplaceNodes.
//If yes, then use replaced node.
SyntaxNode _node = ((MemberAccessExpressionSyntax)_mae).Expression;
if (ReplaceNodes.ContainsKey(_node))
_node = ReplaceNodes[_node];
ReplaceNodes.Add(identifier.Parent, _node);
return;
}
if (_toAttr.To == ToAttribute.NoneWithTrailingDotRemoved)
{
SyntaxNode? _mae = identifier.Parent;
if (_mae == null)
{
Log.ErrorLine($"Parent of {identifier} is null");
return;
}
if (_mae is not MemberAccessExpressionSyntax)
{
Log.ErrorLine($"Parent of {identifier} is {_mae.GetType()}. Should be MemberAccessExpressionSyntax");
return;
}
//Delay adding to ReplaceNodes until the full MemberAccessExpressionSyntax is processed.
_NoneWithTrailingDotRemoved = true;
return;
}
ReplaceNodes.Add(identifier, SyntaxFactory.IdentifierName(_toAttr.Convert(identifier.Identifier.Text)).WithLeadingTrivia(identifier.Identifier.LeadingTrivia).WithTrailingTrivia(identifier.Identifier.TrailingTrivia));
return;
}
}
}
if (symbol.ContainingType != null)
{
symbol = symbol.ContainingType;
goto CheckParentAttributes;
}
}
symbol = oldSymbol;
if (_Options.CustomCSNamesToJS.TryGetValue(identifier.Identifier.Text, out string? _value))
{
ReplaceNodes.Add(identifier, SyntaxFactory.IdentifierName(_value));
return;
}
if (BuiltInTypesGenerics(identifier, symbol, out string str))
{
ReplaceNodes.Add(identifier, SyntaxFactory.IdentifierName(str).WithLeadingTrivia(identifier.Identifier.LeadingTrivia).WithTrailingTrivia(identifier.GetTrailingTrivia()));
return;
}
}
private bool BuiltInTypesGenerics(TypeSyntax node, ISymbol? symbol, out string str)
{
str = string.Empty;
//We need a symbol, if it is null, return.
if (symbol == null)
{
//Log.WarningLine($"node: \"{node}\", symbol is null. USE \"CustomCSNamesToJS\"!");
return false;
}
//If symbol locations are zero, return. Treat as symbol null.
//Also, if symbol in a source return, it is not a built-in type.
if (symbol.Locations.Length == 0 || symbol.Locations[0].IsInSource)
return false;
ISymbol typeSymbol = symbol;
if (typeSymbol.Kind != SymbolKind.NamedType)
{
typeSymbol = symbol.ContainingSymbol;
if (typeSymbol == null)
{
Log.WarningLine($"node: \"{node}\", symbol: \"{symbol}\", typeSymbol is null. USE \"CustomCSNamesToJS\"!");
return false;
}
if (typeSymbol.Kind != SymbolKind.NamedType)
{
Log.WarningLine($"node: \"{node}\", symbol: \"{symbol}\", symbol kind: \"{symbol.Kind}\", typeSymbol kind: \"{typeSymbol.Kind}\". USE \"CustomCSNamesToJS\"!");
return false;
}
}
string typeName = typeSymbol.Name;
string? jsStr = _NETAPI.ReturnJSString(typeName);
if (jsStr == null)
{
Log.WarningLine($"typeName: \"{typeName}\", typeSymbol: \"{typeSymbol}\" Is not supported! USE \"CustomCSNamesToJS\"");
return false;
}
else
{
//return early if the type name is the same as a symbol.
if (typeName == symbol.Name)
{
str = jsStr;
return true;
}
jsStr = _NETAPI.ReturnJSString(typeName, symbol.Name);
if (jsStr == null)
{
Log.WarningLine($"node: \"{node}\", typeName: \"{typeName}\", typeSymbol: \"{typeSymbol}\", symbol: \"{symbol}\", symbolName: \"{symbol.Name}\", Is not supported! USE \"CustomCSNamesToJS\"");
return false;
}
else
{
str = jsStr;
return true;
}
}
}
private ISymbol? TryGetISymbol(SyntaxNode node)
{
ISymbol? symbol = null;
SymbolInfo? symbolInfo = _Model.GetSymbolInfo(node);
if (symbolInfo == null)
return symbol;
if (symbolInfo?.CandidateSymbols.Length >= 1)
symbol = symbolInfo?.CandidateSymbols[0];
else
symbol = symbolInfo?.Symbol;
return symbol;
}
}
internal class ExportClassesWalker : CSharpSyntaxWalker
{
private readonly SemanticModel _Model;
private readonly Dictionary<string, List<string>> _ExportClasses;
public ExportClassesWalker(SemanticModel semanticModel, ref Dictionary<string, List<string>> exportClasses)
{
_Model = semanticModel;
_ExportClasses = exportClasses;
}
public override void VisitClassDeclaration(ClassDeclarationSyntax node)
{
INamedTypeSymbol? classSymbol = _Model.GetDeclaredSymbol(node);
if (classSymbol != null)
{
if (classSymbol.DeclaredAccessibility == Accessibility.Public)
{
if (node.SyntaxTree.FilePath == string.Empty)
Log.WarningLine("'FileName' is empty. Modules won't be imported! (Only exported)");
if (_ExportClasses.ContainsKey(node.SyntaxTree.FilePath))
_ExportClasses[node.SyntaxTree.FilePath].Add(classSymbol.Name);
else
_ExportClasses.Add(node.SyntaxTree.FilePath, new() { classSymbol.Name });
}
}
}
}