Repository navigation
Expand file tree
/
Copy pathPocoToTypescript.cs
More file actions
890 lines (736 loc) · 30.1 KB
/
Copy pathPocoToTypescript.cs
File metadata and controls
890 lines (736 loc) · 30.1 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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SyntaxKind = Microsoft.CodeAnalysis.CSharp.SyntaxKind;
namespace Pocoyo
{
/// <summary>
/// Generates Typescript generations from CodeAnalysis compulation
/// </summary>
public class Pocoyo
{
public bool Silent { get; set; }
public bool PreprocessMode { get; set; }
public bool DiscoverTypes { get; set; }
public string OutputFile { get; set; }
public static string DefaultNamespace { get; set; }
public static List<string> Excluded { get; set; } = new List<string>();
public static List<string> ExcludedAttributes { get; set; } = new List<string>();
public static List<string> KnownTypes { get; set; } = new List<string>();
private int _indent;
private string Indent => _indent > 0 ? " ".PadRight(_indent) : "";
private const string OpenBrace = "{";
private const string CloseBrace = "}";
internal static List<string> Namespaces { get; set; } = new List<string>();
internal static string Namespace => string.Join(".", Namespaces);
internal static Dictionary<string, string> DiscoveredTypes { get; set; } = new Dictionary<string, string>();
internal static Dictionary<string, string> ExcludedTypes { get; set; } = new Dictionary<string, string>();
internal static bool IsKnownType(string fullType)
{
var _fullType = fullType.ToLower();
return DiscoveredTypes.ContainsKey(_fullType) || KnownTypes.Contains(_fullType);
}
internal static bool IsSpecifiedKnownType(string fullType)
{
return KnownTypes.Contains(fullType);
}
internal static string MapKnownType(string fullType)
{
var _fullType = fullType.ToLower();
return DiscoveredTypes.ContainsKey(_fullType) ? DiscoveredTypes[_fullType] : null;
}
internal static bool IsExcludedType(string fullType)
{
return Excluded.Contains(fullType) || ExcludedTypes.ContainsKey(fullType);
}
internal static string MapExcludedType(string fullType)
{
if (Excluded.Contains(fullType))
return fullType;
return ExcludedTypes.ContainsKey(fullType) ? ExcludedTypes[fullType] : null;
}
/// <summary>
/// Pre-process compulation unit to output file
/// </summary>
public static bool PreProcess(string inputFile)
{
var textCode = SharedFile.ReadAllText(inputFile);
if (string.IsNullOrEmpty(textCode))
{
Log.Error($"empty c# input file: {inputFile}");
return false;
}
var tree = CSharpSyntaxTree.ParseText(textCode);
var root = (CompilationUnitSyntax)tree.GetRoot();
var info = new Pocoyo
{
PreprocessMode = true,
DiscoverTypes = true,
Silent = !Debugger.IsAttached,
};
info.Process(root.Members);
return true;
}
/// <summary>
/// Process compulation unit to output file
/// </summary>
public static bool Process(string inputFile, string outputFile, bool discoverTypes)
{
var textCode = SharedFile.ReadAllText(inputFile);
if (string.IsNullOrEmpty(textCode))
{
Log.Error($"empty c# input file: {inputFile}");
return false;
}
var tree = CSharpSyntaxTree.ParseText(textCode);
var root = (CompilationUnitSyntax)tree.GetRoot();
var info = new Pocoyo
{
DiscoverTypes = discoverTypes,
OutputFile = outputFile,
};
if (Log.VerbosMode)
{
info.AddLine($@"/*
==========================================
{inputFile}
==========================================
*/
");
}
info.Process(root.Members);
if (Log.VerbosMode)
{
info.AddLine($@"/*
==========================================
==========================================
*/
");
}
return true;
}
public static void Process(CompilationUnitSyntax compilationUnit)
{
var info = new Pocoyo();
info.Process(compilationUnit.Members);
}
public void Process(MemberDeclarationSyntax memberItem)
{
switch (memberItem)
{
case NamespaceDeclarationSyntax syntaxItem:
Process(syntaxItem);
return;
case EnumDeclarationSyntax syntaxItem:
Process(syntaxItem);
return;
case ClassDeclarationSyntax syntaxItem:
Process(syntaxItem);
return;
case InterfaceDeclarationSyntax syntaxItem:
Process(syntaxItem);
return;
case StructDeclarationSyntax syntaxItem:
Process(syntaxItem);
return;
case DelegateDeclarationSyntax syntaxItem:
Process(syntaxItem);
return;
}
Log.Warn($"Unhandled type: {memberItem.GetType().Name} {memberItem.Kind()} => {memberItem}");
}
public void Process(DelegateDeclarationSyntax members)
{
// Nothing to do for delegates
}
public void Process(IEnumerable<MemberDeclarationSyntax> members)
{
foreach (var memberItem in members)
{
Process(memberItem);
}
}
private void Process(NamespaceDeclarationSyntax syntaxItem)
{
if (syntaxItem == null || syntaxItem.Members.Count == 0)
return;
Namespaces.Add(syntaxItem.Name.ToString());
if (!string.IsNullOrEmpty(DefaultNamespace))
AddLevel($@"declare module {DefaultNamespace}");
else
AddLevel($@"declare module {syntaxItem.Name}");
Process(syntaxItem.Members);
CloseLevel();
Namespaces.RemoveAt(Namespaces.Count - 1);
}
private void Process(EnumDeclarationSyntax syntaxItem)
{
if (syntaxItem == null || syntaxItem.Members.Count == 0)
return;
if (syntaxItem.IsExluded())
{
AddExcludedType(syntaxItem);
return;
}
AddDiscoveredType(syntaxItem);
if (PreprocessMode)
return;
AddLevel($@"export {syntaxItem.EnumKeyword} {syntaxItem.Identifier}");
for (var idx = 0; idx < syntaxItem.Members.Count - 1; idx++)
{
var memberItem = syntaxItem.Members[idx];
var value = memberItem.EqualsValue?.Value != null ? " = " + memberItem.EqualsValue.Value : "";
AddLine($"{memberItem.Identifier}{value},");
}
// Add final item no trailing comma
{
var idx = syntaxItem.Members.Count - 1;
var memberItem = syntaxItem.Members[idx];
var value = memberItem.EqualsValue?.Value != null ? " = " + memberItem.EqualsValue.Value : "";
AddLine($"{memberItem.Identifier}{value}");
}
CloseLevel();
}
private string ToExtends(BaseListSyntax syntaxItem)
{
if (syntaxItem == null)
return "";
var baseTypes = new List<string>();
foreach (var syntaxItemType in syntaxItem.Types)
{
switch (syntaxItemType.Type)
{
case IdentifierNameSyntax typeItem:
if (typeItem.IsKnownType())
baseTypes.Add(syntaxItemType.Type.ToTypescript());
else if (!typeItem.IsExcluded())
Log.Warn($"Uknown base type: {syntaxItemType.Type} for {syntaxItemType}");
break;
case GenericNameSyntax typeItem:
{
var baseType = typeItem.ToTypescript();
if (baseType != "any")
baseTypes.Add(syntaxItemType.Type.ToTypescript());
else if (!typeItem.IsExcluded())
Log.Warn($"Uknown base type: {syntaxItemType.Type}");
}
break;
default:
Log.Warn($"Uknown base type: {syntaxItemType.Type} for {syntaxItemType}");
break;
}
}
// Dont add extends any
if (baseTypes.Count > 0)
{
var extension = string.Join(",", baseTypes);
if (extension != "any")
return " extends " + string.Join(",", baseTypes);
}
return "";
}
private void Process(TypeDeclarationSyntax syntaxItem)
{
if (syntaxItem == null || syntaxItem.Members.Count == 0)
return;
if (syntaxItem.IsExluded())
{
AddExcludedType(syntaxItem);
return;
}
if (syntaxItem.PropertyCount() == 0)
return;
AddDiscoveredType(syntaxItem);
if (!PreprocessMode)
AddLevel($@"export interface {syntaxItem.Identifier}{syntaxItem.TypeParameterList}{ToExtends(syntaxItem.BaseList)}");
var items = new List<string>();
foreach (var memberItem in syntaxItem.Members)
{
switch (memberItem)
{
case PropertyDeclarationSyntax declItem:
{
var name = (declItem.Identifier.Value as string).ToLower();
var found = items.Contains(name);
if (!found)
{
items.Add(declItem.Identifier.Value as string);
Process(declItem);
}
}
break;
case ClassDeclarationSyntax declItem:
{
var name = (declItem.Identifier.Value as string).ToLower();
var found = items.Contains(name);
if (!found)
{
items.Add(declItem.Identifier.Value as string);
Process(declItem);
}
}
break;
case FieldDeclarationSyntax _:
case MethodDeclarationSyntax _:
case ConstructorDeclarationSyntax _:
// skip methods etc
break;
default:
Log.Warn($"unhandled type {memberItem.Kind()} {memberItem}");
break;
}
}
CloseLevel();
}
private void Process(PropertyDeclarationSyntax syntaxItem)
{
if (PreprocessMode)
return;
if (syntaxItem == null || syntaxItem.IsPrivate() || syntaxItem.IsInternal() || syntaxItem.IsExluded())
return;
var publicOnly = syntaxItem.Parent.Kind() == SyntaxKind.ClassDeclaration;
if (!publicOnly || syntaxItem.IsPublic())
{
var nullable = syntaxItem.Type.IsNullable() ? "?" : "";
AddLine($"{syntaxItem.Identifier.ToTypescript()}{nullable} : {syntaxItem.Type.ToTypescript()};");
}
}
private bool AddDiscoveredType(TypeDeclarationSyntax syntaxItem)
{
if (!DiscoverTypes || string.IsNullOrEmpty(Namespace))
return false;
var fullType = $"{Namespace}.{syntaxItem.Identifier.Text}{syntaxItem.TypeParameterList}";
return AddDiscoveredType(syntaxItem, fullType);
}
private bool AddDiscoveredType(BaseTypeDeclarationSyntax syntaxItem)
{
if (!DiscoverTypes || string.IsNullOrEmpty(Namespace))
return false;
var fullType = $"{Namespace}.{syntaxItem.Identifier.Text}{syntaxItem.Identifier}";
return AddDiscoveredType(syntaxItem, fullType);
}
private bool AddDiscoveredType(BaseTypeDeclarationSyntax syntaxItem, string fullType)
{
if (!DiscoverTypes || string.IsNullOrEmpty(fullType) || string.IsNullOrEmpty(Namespace))
return false;
//if (fullType.Contains("BaseDataObject"))
// Log.Info("found");
// Add Identifier
var identifierFullType = $"{Namespace}.{syntaxItem.Identifier.Text}";
var _fullType = fullType.ToLower();
var _identifierFullType = identifierFullType.ToLower();
var _identifier = syntaxItem.Identifier.Text.ToLower();
if (fullType != identifierFullType)
{
if (!DiscoveredTypes.ContainsKey(_identifierFullType))
{
DiscoveredTypes[_identifierFullType] = identifierFullType;
}
}
// Add Unqualified Identifier
if (_fullType != _identifier)
{
if (!DiscoveredTypes.ContainsKey(_identifier))
{
DiscoveredTypes[_identifier] = syntaxItem.Identifier.Text;
}
}
if (DiscoveredTypes.ContainsKey(_fullType))
{
Log.Warn($"Discovered Type already found: {fullType} {syntaxItem}");
return false;
}
// Use default namespace if specified
if (!string.IsNullOrEmpty(DefaultNamespace))
DiscoveredTypes[_fullType] = fullType.Replace(Namespace, DefaultNamespace);
else
DiscoveredTypes[_fullType] = fullType;
return true;
}
private bool AddExcludedType(TypeDeclarationSyntax syntaxItem)
{
if (!DiscoverTypes || string.IsNullOrEmpty(Namespace))
return false;
var fullType = $"{Namespace}.{syntaxItem.Identifier.Text}{syntaxItem.TypeParameterList}";
return AddExcludedType(syntaxItem, fullType);
}
private bool AddExcludedType(BaseTypeDeclarationSyntax syntaxItem)
{
if (!DiscoverTypes || string.IsNullOrEmpty(Namespace))
return false;
var fullType = $"{Namespace}.{syntaxItem.Identifier.Text}{syntaxItem.Identifier}";
return AddExcludedType(syntaxItem, fullType);
}
private bool AddExcludedType(BaseTypeDeclarationSyntax syntaxItem, string fullType)
{
if (!DiscoverTypes || string.IsNullOrEmpty(fullType) || string.IsNullOrEmpty(Namespace))
return false;
//if (fullType.Contains("BaseDataObject"))
// Log.Info("found");
// Add Identifier
var identifierFullType = $"{Namespace}.{syntaxItem.Identifier.Text}";
if (fullType != identifierFullType)
{
if (!ExcludedTypes.ContainsKey(identifierFullType))
{
ExcludedTypes[identifierFullType] = identifierFullType;
}
}
// Add Unqualified Identifier
if (fullType != syntaxItem.Identifier.Text)
{
if (!ExcludedTypes.ContainsKey(syntaxItem.Identifier.Text))
{
ExcludedTypes[syntaxItem.Identifier.Text] = syntaxItem.Identifier.Text;
}
}
if (ExcludedTypes.ContainsKey(fullType))
{
Log.Warn($"Excluded Type already found: {fullType} {syntaxItem}");
return false;
}
// Use default namespace if specified
if (!string.IsNullOrEmpty(DefaultNamespace))
ExcludedTypes[fullType] = fullType.Replace(Namespace, DefaultNamespace);
else
ExcludedTypes[fullType] = fullType;
return true;
}
private void IncreaseLevel()
{
if (Silent || PreprocessMode)
return;
_indent += 3;
if (_indent <= 0)
throw new Exception($"Invalid indent {_indent}");
}
private void DecreaseLevel()
{
if (Silent || PreprocessMode)
return;
_indent -= 3;
if (_indent < 0)
throw new Exception($"Invalid indent {_indent}");
}
private void AddLevel(string line)
{
if (Silent || PreprocessMode)
return;
var output = $"{Indent}{line} {OpenBrace}";
Log.Verbose(output);
if (!string.IsNullOrEmpty(OutputFile))
SharedFile.AppendLine(OutputFile, output);
IncreaseLevel();
}
private void AddLine(string line)
{
if (Silent || PreprocessMode)
return;
var output = $"{Indent}{line}";
Log.Verbose(output);
if (!string.IsNullOrEmpty(OutputFile))
SharedFile.AppendLine(OutputFile, output);
}
private void CloseLevel(string line = "")
{
if (Silent || PreprocessMode)
return;
DecreaseLevel();
var output = $@"{Indent}{line}{CloseBrace}
";
Log.Verbose(output);
if (!string.IsNullOrEmpty(OutputFile))
SharedFile.AppendLine(OutputFile, output);
}
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Extension methods for CodeAnaylsis SyntaxItems
/// </summary>
public static class CodeAnalysisExtensions
{
public static string ToTypescript(this PredefinedTypeSyntax syntaxItem)
{
switch (syntaxItem.Keyword.Kind())
{
case SyntaxKind.StringKeyword:
case SyntaxKind.CharKeyword:
return "string";
case SyntaxKind.BoolKeyword:
return "boolean";
case SyntaxKind.ByteKeyword:
case SyntaxKind.SByteKeyword:
case SyntaxKind.ShortKeyword:
case SyntaxKind.UShortKeyword:
case SyntaxKind.IntKeyword:
case SyntaxKind.UIntKeyword:
case SyntaxKind.LongKeyword:
case SyntaxKind.ULongKeyword:
case SyntaxKind.DoubleKeyword:
case SyntaxKind.FloatKeyword:
case SyntaxKind.DecimalKeyword:
return "number";
case SyntaxKind.ObjectKeyword:
return "any";
default:
Log.Warn($"Unknown type: {syntaxItem}");
return "any";
}
}
public static string ToTypescript(this IdentifierNameSyntax syntaxItem)
{
// Don't warn for common generic <T> argument
if (syntaxItem.Identifier.Text == "T")
return "T";
if (syntaxItem.Identifier.Text == "dynamic")
return "any";
if (string.Equals(syntaxItem.Identifier.Text, "datetime", StringComparison.OrdinalIgnoreCase))
return "string";
if (string.Equals(syntaxItem.Identifier.Text, "timespan", StringComparison.OrdinalIgnoreCase))
return "string";
if (syntaxItem.IsSpecifiedKnownType())
return "any";
if (syntaxItem.IsKnownType())
return syntaxItem.Identifier.Text;
Log.Warn($"Uknown identifier {syntaxItem}");
return "any";
}
public static string ToTypescript(this QualifiedNameSyntax syntaxItem)
{
// check for Generic List
// TODO: Other known types?
if (syntaxItem.ToFullType().StartsWith("System.Collections.Generic.List<"))
return syntaxItem.Right.ToTypescript();
var fullType = Pocoyo.MapKnownType(syntaxItem.ToFullType());
if (!string.IsNullOrEmpty(fullType))
return fullType;
// Can't get full type so return any
return "any";
}
public static string ToTypescript(this TypeSyntax type)
{
switch (type)
{
case PredefinedTypeSyntax syntaxItem:
return syntaxItem.ToTypescript();
case NullableTypeSyntax syntaxItem:
return syntaxItem.ElementType.ToTypescript();
case GenericNameSyntax syntaxItem:
return syntaxItem.ToTypescript();
case IdentifierNameSyntax syntaxItem:
return syntaxItem.ToTypescript();
case ArrayTypeSyntax syntaxItem:
if (syntaxItem.RankSpecifiers.IsMultiRankArray())
{
Log.Info($"WARNING: To many ranks: {syntaxItem} returning any");
return "any";
}
return syntaxItem.ElementType.ToTypescript() + syntaxItem.RankSpecifiers.ToTypescript();
case QualifiedNameSyntax syntaxItem:
return syntaxItem.ToTypescript();
}
Log.Warn($"unknown type: {type}");
return type.ToString();
}
/// <summary>
/// TODO: Figure out how to map multip rank c# to typescript.
/// For example:
/// c# array[,] or array[][] typescript: ???
/// </summary>
public static string ToTypescript(this SyntaxList<ArrayRankSpecifierSyntax> syntaxList)
{
if (syntaxList.Count != 1 || syntaxList[0].Rank != 1)
{
Log.Info($"WARNING: To many ranks: {syntaxList} returning any");
return "any";
}
return "[]";
}
public static string ToTypescript(this TypeArgumentListSyntax syntaxItem, bool dictionary = false)
{
if (dictionary && syntaxItem.Arguments.Count == 2)
{
var key = syntaxItem.Arguments[0].ToTypescript();
var value = syntaxItem.Arguments[1].ToTypescript();
if (string.Equals(key, "string", StringComparison.OrdinalIgnoreCase))
{
return $"Map<string, {value}>";
}
}
if (syntaxItem.Arguments.Count != 1)
{
Log.Warn($"to many generic type args: {string.Join(",", syntaxItem.Arguments)} from {syntaxItem}");
return "any";
}
var arg = syntaxItem.Arguments.First();
return arg.ToTypescript();
}
public static string ToTypescript(this GenericNameSyntax syntaxItem)
{
if (syntaxItem.Identifier.Text == "Dictionary")
{
return syntaxItem.TypeArgumentList.ToTypescript(true);
}
if (syntaxItem.Identifier.Text == "List")
{
return syntaxItem.TypeArgumentList.ToTypescript() + "[]";
}
if (syntaxItem.Identifier.Text == "Expression")
{
return "any";
}
if (syntaxItem.IsSpecifiedKnownType())
{
return "any";
}
if (syntaxItem.IsKnownType())
{
return $"{syntaxItem.Identifier.Text}<{syntaxItem.TypeArgumentList.ToTypescript()}>";
}
if (syntaxItem.IsExcluded())
{
return "any";
}
Log.Warn($"unknown Generic: {syntaxItem} mapped to any");
return "any";
}
public static string ToTypescript(this SyntaxToken syntaxItem)
{
return syntaxItem.ToString().ToCamelCaseString();
}
public static string ToCamelCaseString(this string value)
{
if (string.IsNullOrEmpty(value))
return null;
return char.ToLower(value[0]) + value.Substring(1);
}
public static bool IsNullable(this TypeSyntax syntaxItem)
{
return syntaxItem is NullableTypeSyntax;
}
public static bool IsPublic(this PropertyDeclarationSyntax syntaxItem)
{
return syntaxItem.Modifiers.Any(modifier => modifier.Kind() == SyntaxKind.PublicKeyword);
}
public static bool IsPrivate(this PropertyDeclarationSyntax syntaxItem)
{
return syntaxItem.Modifiers.Any(modifier => modifier.Kind() == SyntaxKind.PrivateKeyword);
}
public static bool IsInternal(this PropertyDeclarationSyntax syntaxItem)
{
return syntaxItem.Modifiers.Any(modifier => modifier.Kind() == SyntaxKind.InternalKeyword);
}
public static int PropertyCount(this TypeDeclarationSyntax syntaxItem)
{
if (syntaxItem == null || syntaxItem.Members.Count == 0)
return 0;
var publicOnly = syntaxItem.Keyword.Kind() == SyntaxKind.ClassKeyword;
var count = 0;
foreach (var memberItem in syntaxItem.Members)
{
switch (memberItem)
{
case PropertyDeclarationSyntax propItem:
if (!propItem.IsPrivate() && !propItem.IsInternal() && !propItem.IsExluded())
{
if (!publicOnly || propItem.IsPublic())
count++;
}
break;
}
}
return count;
}
public static string ToFullType(this QualifiedNameSyntax syntaxItem)
{
return $"{syntaxItem.Left}.{syntaxItem.Right}";
}
public static bool IsKnownType(this BaseTypeDeclarationSyntax syntaxItem)
{
return Pocoyo.IsKnownType(syntaxItem.Identifier.Text);
}
public static bool IsKnownType(this IdentifierNameSyntax syntaxItem)
{
return Pocoyo.IsKnownType(syntaxItem.Identifier.Text);
}
public static bool IsKnownType(this GenericNameSyntax syntaxItem)
{
return Pocoyo.IsKnownType(syntaxItem.Identifier.Text);
}
public static bool IsSpecifiedKnownType(this BaseTypeDeclarationSyntax syntaxItem)
{
return Pocoyo.IsSpecifiedKnownType(syntaxItem.Identifier.Text);
}
public static bool IsSpecifiedKnownType(this IdentifierNameSyntax syntaxItem)
{
return Pocoyo.IsSpecifiedKnownType(syntaxItem.Identifier.Text);
}
public static bool IsSpecifiedKnownType(this GenericNameSyntax syntaxItem)
{
return Pocoyo.IsSpecifiedKnownType(syntaxItem.Identifier.Text);
}
public static bool IsExcluded(this BaseTypeDeclarationSyntax syntaxItem)
{
return Pocoyo.IsExcludedType(syntaxItem.Identifier.Text);
}
public static bool IsExcluded(this IdentifierNameSyntax syntaxItem)
{
return Pocoyo.IsExcludedType(syntaxItem.Identifier.Text);
}
public static bool IsExcluded(this GenericNameSyntax syntaxItem)
{
return Pocoyo.IsExcludedType(syntaxItem.Identifier.Text);
}
public static bool IsExluded(this BaseTypeDeclarationSyntax syntaxItem)
{
if (syntaxItem == null)
return true;
if (Pocoyo.IsExcludedType(syntaxItem.Identifier.Text))
return true;
return syntaxItem.AttributeLists.IsExluded();
}
public static bool IsExluded(this PropertyDeclarationSyntax syntaxItem)
{
if (syntaxItem == null)
return true;
return syntaxItem.AttributeLists.IsExluded();
}
public static bool IsExluded(this SyntaxList<AttributeListSyntax> attributeListSyntax)
{
if (Pocoyo.ExcludedAttributes.Count == 0)
return false;
foreach (var attributeList in attributeListSyntax)
{
foreach (var attribute in attributeList.Attributes)
{
switch (attribute.Name)
{
case IdentifierNameSyntax attribItem:
{
var found = Pocoyo.ExcludedAttributes.FirstOrDefault(item => string.Equals(item, attribItem.Identifier.Text));
if (found != null)
return true;
}
break;
}
}
}
return false;
}
public static bool IsMultiRankArray(this SyntaxList<ArrayRankSpecifierSyntax> syntaxList)
{
return syntaxList.Count != 1 || syntaxList[0].Rank != 1;
}
}
}