Skip to content

Commit 0c8f8af

Browse files
committed
Colorful output to console)
Added static keyword for methods and properties. Small fixes, formatting and "Trim"->"trim".
1 parent 7dad50a commit 0c8f8af

File tree

4 files changed

+85
-29
lines changed

4 files changed

+85
-29
lines changed

CSharpToJavaScript/APIs/JS/Generated/JS.generated.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//11.08.2023 14:29:24
1+
//13.08.2023 16:10:39
22
using static CSharpToJavaScript.APIs.JS.GlobalObject;
33
using CSharpToJavaScript.Utils;
44
using System.Collections.Generic;

CSharpToJavaScript/CSTOJS.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.CodeAnalysis;
99
using CSharpToJavaScript.Utils;
1010
using System.Linq;
11+
using System;
1112

1213
namespace CSharpToJavaScript
1314
{
@@ -37,6 +38,10 @@ public CSTOJS()
3738
}
3839

3940
Trace.Listeners.Add(new ConsoleTraceListener());
41+
42+
Assembly assembly = Assembly.GetExecutingAssembly();
43+
//https://stackoverflow.com/a/73474279
44+
SM.Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
4045
}
4146

4247
/// <summary>
@@ -57,6 +62,10 @@ public CSTOJS(CSTOJSOptions options)
5762
}
5863

5964
Trace.Listeners.Add(new ConsoleTraceListener());
65+
66+
Assembly assembly = Assembly.GetExecutingAssembly();
67+
//https://stackoverflow.com/a/73474279
68+
SM.Log($"{assembly.GetName().Name} {assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}");
6069
}
6170

6271
/// <summary>
@@ -296,11 +305,12 @@ private async Task GenerateAsync(string path, Assembly assembly, string filename
296305
Directory.CreateDirectory(_Options.OutPutPath);
297306
}
298307

299-
await File.WriteAllTextAsync(Path.Combine(_Options.OutPutPath, filename), _Walker.JSSB.ToString());
308+
string pathCombined = Path.Combine(_Options.OutPutPath, filename);
309+
310+
await File.WriteAllTextAsync(pathCombined, _Walker.JSSB.ToString());
300311

301312
SM.Log($"--- Done!");
302-
SM.Log($"--- Path: {_Options.OutPutPath}");
303-
SM.Log($"--- File: {filename}");
313+
SM.Log($"--- Path: {pathCombined}");
304314
SM.Log($"--- --- ---");
305315
}
306316
}

CSharpToJavaScript/Utils/StaticMethods.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics;
1+
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Runtime.CompilerServices;
45

@@ -8,7 +9,17 @@ internal class SM
89
{
910
internal static void Log(string message, [CallerFilePath] string? file = null, [CallerMemberName] string? member = null, [CallerLineNumber] int line = 0)
1011
{
12+
if (message.StartsWith("---"))
13+
Console.ForegroundColor = ConsoleColor.Green;
14+
15+
if (message.StartsWith("ERROR"))
16+
Console.ForegroundColor = ConsoleColor.Red;
17+
18+
if (message.StartsWith("WARNING"))
19+
Console.ForegroundColor = ConsoleColor.Yellow;
20+
1121
Trace.WriteLine($"({line}):{Path.GetFileName(file)} {member}: {message}");
22+
Console.ResetColor();
1223
}
1324
}
1425
}

CSharpToJavaScript/Walker.cs

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ internal class Walker : CSharpSyntaxWalker
2727
private string _NameSpaceStr = string.Empty;
2828
private bool _UsedThis = false;
2929

30+
private bool _PropertyStatic = false;
31+
3032
public Walker() : base(SyntaxWalkerDepth.Trivia)
3133
{
3234

@@ -304,6 +306,7 @@ public override void VisitParameter(ParameterSyntax node)
304306
Visit(asNode);
305307
break;
306308
}
309+
case SyntaxKind.NullableType:
307310
case SyntaxKind.GenericName:
308311
case SyntaxKind.IdentifierName:
309312
case SyntaxKind.PredefinedType:
@@ -657,6 +660,7 @@ public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
657660
case SyntaxKind.PublicKeyword:
658661
VisitLeadingTrivia(asToken);
659662
break;
663+
case SyntaxKind.StaticKeyword:
660664
case SyntaxKind.AsyncKeyword:
661665
case SyntaxKind.IdentifierToken:
662666
{
@@ -746,6 +750,14 @@ where n.IsNode
746750
where n.AsNode().IsKind(SyntaxKind.IdentifierName)
747751
select n;
748752

753+
if (key.Count() == 0)
754+
{
755+
key = from n in nodesAndTokens
756+
where n.IsNode
757+
where n.AsNode().IsKind(SyntaxKind.GenericName)
758+
select n;
759+
}
760+
749761
field = SyntaxFactory.FieldDeclaration(
750762
SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(key.First().ToString()))
751763
.WithVariables(
@@ -757,7 +769,8 @@ where n.AsNode().IsKind(SyntaxKind.IdentifierName)
757769
{
758770
SyntaxFactory.Token(SyntaxKind.PrivateKeyword)
759771
}))
760-
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia("\t\t"));
772+
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia("\t\t"))
773+
.WithTrailingTrivia(SyntaxFactory.ParseLeadingTrivia("\r\n"));
761774
}
762775
else
763776
{
@@ -774,7 +787,8 @@ where n.AsNode().IsKind(SyntaxKind.IdentifierName)
774787
{
775788
SyntaxFactory.Token(SyntaxKind.PrivateKeyword)
776789
}))
777-
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia("\t\t"));
790+
.WithLeadingTrivia(SyntaxFactory.ParseLeadingTrivia("\t\t"))
791+
.WithTrailingTrivia(SyntaxFactory.ParseLeadingTrivia("\r\n"));
778792
}
779793

780794
VisitFieldDeclaration(field);
@@ -798,9 +812,10 @@ where n.AsNode().IsKind(SyntaxKind.IdentifierName)
798812
case SyntaxKind.EqualsValueClause:
799813
case SyntaxKind.PredefinedType:
800814
case SyntaxKind.IdentifierName:
815+
case SyntaxKind.GenericName:
801816
break;
802817
case SyntaxKind.AccessorList:
803-
Visit(asNode);
818+
VisitAccessorList(asNode as AccessorListSyntax);
804819
break;
805820
default:
806821
SM.Log($"asNode : {kind}");
@@ -823,6 +838,10 @@ where n.AsNode().IsKind(SyntaxKind.IdentifierName)
823838
case SyntaxKind.PrivateKeyword:
824839
VisitLeadingTrivia(asToken);
825840
break;
841+
case SyntaxKind.StaticKeyword:
842+
_PropertyStatic = true;
843+
VisitToken(asToken);
844+
break;
826845
default:
827846
SM.Log($"asToken : {kind}");
828847
break;
@@ -890,14 +909,15 @@ where e.IsKind(SyntaxKind.IdentifierName)
890909
}
891910
else
892911
{
893-
JSSB.Append($"\n");
912+
//JSSB.Append($"\n");
894913

895914
SyntaxTriviaList _syntaxTrivias = asNode.Parent.Parent.GetLeadingTrivia();
896-
915+
916+
/* Todo! Why there is already "/t/t" in JSSB????
897917
for (int _i = 0; _i < _syntaxTrivias.Count; _i++)
898918
{
899919
VisitTrivia(_syntaxTrivias[_i]);
900-
}
920+
}*/
901921

902922
JSSB.Append($"get {d3.Text}() {{ return this._{d3.Text}_; }}");
903923

@@ -933,6 +953,11 @@ where e.IsKind(SyntaxKind.IdentifierToken)
933953
VisitTrivia(_syntaxTrivias[_i]);
934954
}
935955

956+
if (_PropertyStatic == true)
957+
{
958+
JSSB.Append($"static ");
959+
_PropertyStatic = false;
960+
}
936961
JSSB.Append($"set {d3.Text}(value)");
937962

938963
_syntaxTrivias = asNode.GetTrailingTrivia();
@@ -953,6 +978,11 @@ where e.IsKind(SyntaxKind.IdentifierToken)
953978
VisitTrivia(_syntaxTrivias[_i]);
954979
}
955980

981+
if (_PropertyStatic == true)
982+
{
983+
JSSB.Append($"static ");
984+
_PropertyStatic = false;
985+
}
956986
JSSB.Append($"set {d3.Text}(value) {{ this._{d3.Text}_ = value; }}");
957987

958988
_syntaxTrivias = asNode.Parent.GetTrailingTrivia();
@@ -1248,6 +1278,7 @@ public override void VisitForEachStatement(ForEachStatementSyntax node)
12481278
case SyntaxKind.Block:
12491279
VisitBlock(asNode as BlockSyntax);
12501280
break;
1281+
case SyntaxKind.GenericName:
12511282
case SyntaxKind.PredefinedType:
12521283
{
12531284
SyntaxTriviaList _syntaxTrivias = asNode.GetLeadingTrivia();
@@ -1590,25 +1621,29 @@ public override void VisitImplicitObjectCreationExpression(ImplicitObjectCreatio
15901621
symbolInfo = CSTOJS.Model.GetSymbolInfo(_aes.Left);
15911622

15921623
ClassDeclarationSyntax classD = (ClassDeclarationSyntax)node.Ancestors().First(n => n.IsKind(SyntaxKind.ClassDeclaration));
1593-
SyntaxList<MemberDeclarationSyntax> mem = classD.Members;
1624+
1625+
IEnumerable<ClassDeclarationSyntax> classesD = from n in classD.Parent.DescendantNodes()
1626+
where n.IsKind(SyntaxKind.ClassDeclaration)
1627+
select n as ClassDeclarationSyntax;
1628+
1629+
List<MemberDeclarationSyntax> mem = new();
1630+
foreach (ClassDeclarationSyntax item in classesD)
1631+
{
1632+
mem.AddRange(item.Members.ToList());
1633+
}
15941634

15951635
foreach (MemberDeclarationSyntax item in mem)
15961636
{
15971637
SyntaxToken _sT = default;
15981638
if (item is MethodDeclarationSyntax m)
15991639
{
1600-
IEnumerable<SyntaxToken> d3 = from e in m.ChildTokens()
1601-
where e.IsKind(SyntaxKind.IdentifierToken)
1602-
select e;
1603-
_sT = d3.First();
1640+
_sT = m.Identifier;
16041641
}
16051642

16061643
if (item is PropertyDeclarationSyntax p)
16071644
{
1608-
IEnumerable<SyntaxToken> d3 = from e in p.DescendantTokens()
1609-
where e.IsKind(SyntaxKind.IdentifierToken)
1610-
select e;
1611-
_sT = d3.Last();
1645+
_sT = p.Identifier;
1646+
syntaxNode = p.Type;
16121647
}
16131648

16141649
if (item is FieldDeclarationSyntax f)
@@ -1619,14 +1654,15 @@ where e.IsKind(SyntaxKind.IdentifierToken)
16191654
_sT = d3.Last();
16201655
}
16211656

1622-
if (_sT.ToString() == _aes.Left.ToString())
1657+
if (_aes.Left.ToString().Contains(_sT.Text))
16231658
{
1624-
VariableDeclarationSyntax s = item.DescendantNodes().First(e => e.IsKind(SyntaxKind.VariableDeclaration)) as VariableDeclarationSyntax;
1625-
syntaxNode = s.Type;
1659+
//Todo?
1660+
//VariableDeclarationSyntax s = item.DescendantNodes().First(e => e.IsKind(SyntaxKind.VariableDeclaration)) as VariableDeclarationSyntax;
1661+
//syntaxNode = s.Type;
16261662
symbolInfo = CSTOJS.Model.GetSymbolInfo(syntaxNode);
1663+
break;
16271664
}
16281665
}
1629-
16301666
}
16311667
else
16321668
{
@@ -1967,7 +2003,7 @@ where e.IsKind(SyntaxKind.IdentifierToken)
19672003
select e;
19682004
_sT = d3.First();*/
19692005

1970-
_sT = m.Identifier;
2006+
_sT = m.Identifier;
19712007
}
19722008

19732009
if (item is PropertyDeclarationSyntax p)
@@ -2119,15 +2155,13 @@ where e.IsKind(SyntaxKind.IdentifierToken)
21192155
{
21202156
if (_Options.Debug)
21212157
{
2122-
SM.Log("------");
2123-
SM.Log("--- Diagnostics starts ---");
2158+
SM.Log("WARNING! Diagnostics starts ---");
21242159
ImmutableArray<Diagnostic> diag = CSTOJS.Model.GetDiagnostics();
21252160
foreach (Diagnostic item in diag)
21262161
{
21272162
SM.Log(item.ToString());
21282163
}
2129-
SM.Log("--- Diagnostics ends ---");
2130-
SM.Log("------");
2164+
SM.Log("WARNING! Diagnostics ends ---");
21312165
}
21322166
SM.Log($"ERROR! !-{node}-! By reaching this means, a name did not convert to JS. CHECK FOR UPPERCASE CHARACTERS IN NAMES IN THE JS FILE!");
21332167

@@ -2295,6 +2329,7 @@ private bool BuiltInTypesGenerics(SyntaxNode nodeL, ISymbol symbol)
22952329
return true;
22962330
}
22972331
case string _str when
2332+
_str.Contains(nameof(string.Trim)) ||
22982333
_str.Contains(nameof(string.Substring)) ||
22992334
_str.Contains(nameof(string.StartsWith)) ||
23002335
_str.Contains(nameof(string.Replace)):

0 commit comments

Comments
 (0)