Skip to content

Commit 801db04

Browse files
committed
Changed "GenerateOneFromStringAsync" to "GenerateOneFromString".
Deleted creation of debug.txt when Debug is enabled. Console output is enough. Small fixes.
1 parent 3bd8437 commit 801db04

File tree

5 files changed

+35
-40
lines changed

5 files changed

+35
-40
lines changed

CSharpToJavaScript/CSTOJS.cs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ namespace CSharpToJavaScript
1919
/// </summary>
2020
public class CSTOJS
2121
{
22-
public SemanticModel Model { get; set; } = null;
23-
2422
public CSTOJSOptions Options { get; set; } = new();
2523

26-
private Walker _Walker = new();
24+
private Walker? _Walker = null;
2725

2826
/// <summary>
2927
/// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
@@ -49,15 +47,6 @@ public CSTOJS(CSTOJSOptions options)
4947

5048
if (Options.DisableConsoleOutput == false)
5149
{
52-
if (Options.Debug)
53-
{
54-
if (File.Exists(Directory.GetCurrentDirectory() + "/debug.txt"))
55-
File.Delete(Directory.GetCurrentDirectory() + "/debug.txt");
56-
57-
Trace.Listeners.Add(new TextWriterTraceListener("debug.txt"));
58-
Trace.AutoFlush = true;
59-
}
60-
6150
ConsoleTraceListener consoleTraceListener = new();
6251
if (Trace.Listeners.Contains(consoleTraceListener) == false)
6352
Trace.Listeners.Add(consoleTraceListener);
@@ -102,7 +91,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
10291
_tree = CSharpSyntaxTree.ParseText(SourceText.From(stream), path: file.FullName);
10392
}
10493

105-
await GenerateAsync(_tree, assembly);
94+
Generate(_tree, assembly);
10695

10796
if (!Directory.Exists(Options.OutPutPath))
10897
{
@@ -123,14 +112,15 @@ public async Task GenerateOneAsync(string path, string? filename = null)
123112
Log($"--- --- ---");
124113
}
125114
}
115+
126116
/// <summary>
127117
/// Method for generating from string.
128118
/// </summary>
129119
/// <param name="csstring">CSharp string.</param>
130120
/// <param name="references">Needed if you don't have access to files. Because Assembly.location is null in Blazor WebAssembly.</param>
131121
/// <returns>JS <see cref="StringBuilder"/></returns>
132122
/// <exception cref="ArgumentNullException"></exception>
133-
public async Task<StringBuilder> GenerateOneFromStringAsync(string csstring, List<MetadataReference>? references = null)
123+
public StringBuilder GenerateOneFromString(string csstring, List<MetadataReference>? references = null)
134124
{
135125
if (csstring == null)
136126
throw new ArgumentNullException(nameof(csstring));
@@ -140,20 +130,18 @@ public async Task<StringBuilder> GenerateOneFromStringAsync(string csstring, Lis
140130
SyntaxTree? _tree = CSharpSyntaxTree.ParseText(csstring.Normalize());
141131

142132
if(references != null)
143-
await GenerateAsync(_tree, assembly, references);
133+
Generate(_tree, assembly, references);
144134
else
145-
await GenerateAsync(_tree, assembly);
135+
Generate(_tree, assembly);
146136

147137
Log($"--- Done!");
148138
Log($"--- --- ---");
149139

150140
return _Walker.JSSB;
151141
}
152142

153-
private async Task GenerateAsync(SyntaxTree? tree, Assembly assembly, List<MetadataReference>? refs = null)
143+
private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReference>? refs = null)
154144
{
155-
_Walker = new(this);
156-
157145
CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
158146

159147

@@ -344,7 +332,7 @@ private async Task GenerateAsync(SyntaxTree? tree, Assembly assembly, List<Metad
344332
//Should I make "NormalizeWhitespace" option??? TODO!
345333
//.NormalizeWhitespace().AddUsings(oldUsing);
346334

347-
if (rtPath != null || rtPath != string.Empty)
335+
if (rtPath != null && rtPath != string.Empty)
348336
{
349337
if (File.Exists(Path.Combine(rtPath, "System.dll")))
350338
references.Add(MetadataReference.CreateFromFile(Path.Combine(rtPath, "System.dll")));
@@ -379,8 +367,8 @@ private async Task GenerateAsync(SyntaxTree? tree, Assembly assembly, List<Metad
379367
if (Options.Debug)
380368
{
381369
Log($"+++");
382-
Log($"assemblyPath: {assemblyPath}");
383-
Log($"rtPath: {rtPath}");
370+
Log($"Path assembly: {assemblyPath}");
371+
Log($"Path rt: {rtPath}");
384372
Log($"List of references:");
385373
foreach (MetadataReference reference in references)
386374
{
@@ -395,7 +383,7 @@ private async Task GenerateAsync(SyntaxTree? tree, Assembly assembly, List<Metad
395383
.AddReferences(references.ToArray())
396384
.AddSyntaxTrees(trueST);
397385

398-
Model = compilation.GetSemanticModel(trueST);
386+
_Walker = new(this, compilation.GetSemanticModel(trueST));
399387

400388
_Walker.JSSB.Append(Options.AddSBInFront);
401389

CSharpToJavaScript/CSTOJSOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace CSharpToJavaScript
1111
public class CSTOJSOptions
1212
{
1313
/// <summary>
14-
/// Debug. When set to true prints additional info to console, cs lines to js file and creates file Debug.txt
14+
/// Debug. When set to true prints additional info to console, cs lines to js file.
1515
/// </summary>
1616
/// <value>
1717
/// Default: <c>false</c>

CSharpToJavaScript/CSharpToJavaScript.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
<Nullable>enable</Nullable>
99
<GenerateDocumentationFile>True</GenerateDocumentationFile>
1010
<Authors>TiLied</Authors>
11-
<Description>Brute forcing conversion(translation) from C# to Javascript.</Description>
11+
<Description>Brute forcing conversion(generating) from C# to Javascript.</Description>
1212
<PackageProjectUrl>https://tilied.github.io/CSTOJS_Pages/</PackageProjectUrl>
1313
<PackageReadmeFile>README.md</PackageReadmeFile>
1414
<RepositoryUrl>https://github.com/TiLied/CSharpToJavaScript/</RepositoryUrl>
1515
<PackageTags>csharp, c#, conversion, javascript, js, generating</PackageTags>
1616
<PackageReleaseNotes></PackageReleaseNotes>
1717
<Title>CSharp to JavaScript</Title>
1818
<PackageLicenseFile>LICENSE</PackageLicenseFile>
19+
<PackageIcon>CSTOJS.png</PackageIcon>
1920
</PropertyGroup>
2021

2122
<ItemGroup>
@@ -33,4 +34,11 @@
3334
</None>
3435
</ItemGroup>
3536

37+
<ItemGroup>
38+
<None Update="Utils\CSTOJS.png">
39+
<Pack>True</Pack>
40+
<PackagePath>\</PackagePath>
41+
</None>
42+
</ItemGroup>
43+
3644
</Project>

CSharpToJavaScript/Utils/CSTOJS.png

16.6 KB
Loading

CSharpToJavaScript/Walker.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ internal class Walker : CSharpSyntaxWalker
1919
{
2020
public StringBuilder JSSB { get; set; } = new();
2121

22+
2223
private readonly CSTOJS? _CSTOJS = null;
24+
private readonly SemanticModel? _Model = null;
2325

2426
private SyntaxNode? _SNOriginal = null;
2527
private SyntaxNode? _BaseConstructorInitializerNode = null;
@@ -29,13 +31,10 @@ internal class Walker : CSharpSyntaxWalker
2931

3032
private bool _PropertyStatic = false;
3133

32-
public Walker() : base(SyntaxWalkerDepth.Trivia)
33-
{
34-
35-
}
36-
public Walker(CSTOJS cstojs) : base(SyntaxWalkerDepth.Trivia)
34+
public Walker(CSTOJS cstojs, SemanticModel? model) : base(SyntaxWalkerDepth.Trivia)
3735
{
3836
_CSTOJS = cstojs;
37+
_Model = model;
3938
}
4039

4140
public override void VisitTrivia(SyntaxTrivia trivia)
@@ -1629,7 +1628,7 @@ public override void VisitImplicitObjectCreationExpression(ImplicitObjectCreatio
16291628
if (_vds == null)
16301629
{
16311630
AssignmentExpressionSyntax _aes = node.Ancestors().FirstOrDefault(e => e.IsKind(SyntaxKind.SimpleAssignmentExpression)) as AssignmentExpressionSyntax;
1632-
symbolInfo = _CSTOJS.Model.GetSymbolInfo(_aes.Left);
1631+
symbolInfo = _Model.GetSymbolInfo(_aes.Left);
16331632

16341633
ClassDeclarationSyntax classD = (ClassDeclarationSyntax)node.Ancestors().First(n => n.IsKind(SyntaxKind.ClassDeclaration));
16351634

@@ -1670,14 +1669,14 @@ where e.IsKind(SyntaxKind.IdentifierToken)
16701669
//Todo?
16711670
//VariableDeclarationSyntax s = item.DescendantNodes().First(e => e.IsKind(SyntaxKind.VariableDeclaration)) as VariableDeclarationSyntax;
16721671
//syntaxNode = s.Type;
1673-
symbolInfo = _CSTOJS.Model.GetSymbolInfo(syntaxNode);
1672+
symbolInfo = _Model.GetSymbolInfo(syntaxNode);
16741673
break;
16751674
}
16761675
}
16771676
}
16781677
else
16791678
{
1680-
symbolInfo = _CSTOJS.Model.GetSymbolInfo(_vds.Type);
1679+
symbolInfo = _Model.GetSymbolInfo(_vds.Type);
16811680
syntaxNode = _vds.Type;
16821681
}
16831682

@@ -1951,7 +1950,7 @@ public bool IdentifierToken(SyntaxNode node)
19511950
{
19521951
if (_syntaxToken.Text == text)
19531952
{
1954-
symbolInfo = _CSTOJS.Model.GetSymbolInfo(_item.Parent as IdentifierNameSyntax);
1953+
symbolInfo = _Model.GetSymbolInfo(_item.Parent as IdentifierNameSyntax);
19551954
break;
19561955
}
19571956
}
@@ -1962,11 +1961,11 @@ public bool IdentifierToken(SyntaxNode node)
19621961
{
19631962
try
19641963
{
1965-
symbolInfo = _CSTOJS.Model.GetSymbolInfo(node);
1964+
symbolInfo = _Model.GetSymbolInfo(node);
19661965

19671966
if (_CSTOJS.Options.Debug)
19681967
{
1969-
var a = _CSTOJS.Model.GetDiagnostics();
1968+
var a = _Model.GetDiagnostics();
19701969
foreach (Diagnostic item in a)
19711970
{
19721971
_CSTOJS.Log(item.ToString());
@@ -1976,7 +1975,7 @@ public bool IdentifierToken(SyntaxNode node)
19761975
catch (Exception e)
19771976
{
19781977
symbolInfo = null;
1979-
var a = _CSTOJS.Model.GetDeclarationDiagnostics();
1978+
var a = _Model.GetDeclarationDiagnostics();
19801979
foreach (Diagnostic item in a)
19811980
{
19821981
_CSTOJS.Log(item.ToString());
@@ -2047,7 +2046,7 @@ where e.IsKind(SyntaxKind.GenericName)
20472046
select e;
20482047
}
20492048

2050-
SymbolInfo _symbolInfo = _CSTOJS.Model.GetSymbolInfo(_all.First());
2049+
SymbolInfo _symbolInfo = _Model.GetSymbolInfo(_all.First());
20512050
ISymbol? _iSymbol = null;
20522051

20532052
if (_symbolInfo.CandidateSymbols.Length >= 1)
@@ -2227,14 +2226,14 @@ where e.IsKind(SyntaxKind.IdentifierToken)
22272226
if (_CSTOJS.Options.Debug)
22282227
{
22292228
_CSTOJS.Log("WARNING! Diagnostics starts ---");
2230-
ImmutableArray<Diagnostic> diag = _CSTOJS.Model.GetDiagnostics();
2229+
ImmutableArray<Diagnostic> diag = _Model.GetDiagnostics();
22312230
foreach (Diagnostic item in diag)
22322231
{
22332232
_CSTOJS.Log(item.ToString());
22342233
}
22352234
_CSTOJS.Log("WARNING! Diagnostics ends ---");
22362235
}
2237-
_CSTOJS.Log($"WARNING! !-{node}-! By reaching this means, a name did not convert to JS. CHECK FOR UPPERCASE CHARACTERS IN NAMES IN THE JS FILE!");
2236+
//_CSTOJS.Log($"WARNING! !-{node}-! By reaching this means, a name did not convert to JS. CHECK FOR UPPERCASE CHARACTERS IN NAMES IN THE JS FILE!");
22382237

22392238
//base.VisitIdentifierName(node);
22402239
return false;

0 commit comments

Comments
 (0)