Skip to content

Commit 2576a20

Browse files
committed
Small changes to static and "this".
1 parent 76def38 commit 2576a20

File tree

2 files changed

+157
-54
lines changed

2 files changed

+157
-54
lines changed

CSharpToJavaScript/CSTOJS.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
473473
.AddReferences(references.ToArray())
474474
.AddSyntaxTrees(trueST);
475475

476+
476477
_Walker = new(this, compilation.GetSemanticModel(trueST));
477478

478479
_Walker.JSSB.Append(Options.AddSBInFront);

CSharpToJavaScript/Walker.cs

Lines changed: 156 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using CSharpToJavaScript.APIs.JS;
2-
using CSharpToJavaScript.Utils;
1+
using CSharpToJavaScript.Utils;
32
using Microsoft.CodeAnalysis;
43
using Microsoft.CodeAnalysis.CSharp;
54
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -28,7 +27,6 @@ internal class Walker : CSharpSyntaxWalker
2827
private SyntaxNode? _BaseConstructorInitializerNode = null;
2928

3029
private string _NameSpaceStr = string.Empty;
31-
private bool _UsedThis = false;
3230

3331
private bool _PropertyStatic = false;
3432
private bool _ConstKeyword = false;
@@ -284,6 +282,9 @@ public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax no
284282
case SyntaxKind.StaticKeyword:
285283
{
286284
VisitToken(asToken);
285+
286+
JSSB.AppendLine();
287+
287288
for (int j = 0; j < nodesAndTokens.Count; j++)
288289
{
289290
asNode = nodesAndTokens[j].AsNode();
@@ -523,9 +524,7 @@ public override void VisitExpressionStatement(ExpressionStatementSyntax node)
523524
case SyntaxKind.DivideAssignmentExpression:
524525
case SyntaxKind.AwaitExpression:
525526
{
526-
_UsedThis = false;
527527
Visit(asNode);
528-
_UsedThis = false;
529528
break;
530529
}
531530
default:
@@ -580,25 +579,28 @@ public override void VisitArgument(ArgumentSyntax node)
580579
case SyntaxKind.TrueLiteralExpression:
581580
case SyntaxKind.StringLiteralExpression:
582581
case SyntaxKind.InterpolatedStringExpression:
583-
case SyntaxKind.ObjectCreationExpression:
584-
case SyntaxKind.ImplicitObjectCreationExpression:
585-
{
586582
Visit(asNode);
587583
break;
588-
}
584+
case SyntaxKind.ObjectCreationExpression:
585+
VisitObjectCreationExpression(asNode as ObjectCreationExpressionSyntax);
586+
break;
587+
case SyntaxKind.ImplicitObjectCreationExpression:
588+
VisitImplicitObjectCreationExpression(asNode as ImplicitObjectCreationExpressionSyntax);
589+
break;
589590
case SyntaxKind.AnonymousObjectCreationExpression:
590591
{
591592
VisitAnonymousObjectCreationExpression(asNode as AnonymousObjectCreationExpressionSyntax);
592593
break;
593594
}
594-
595-
case SyntaxKind.ElementAccessExpression:
596-
case SyntaxKind.IdentifierName:
597595
case SyntaxKind.SimpleMemberAccessExpression:
596+
case SyntaxKind.ElementAccessExpression:
598597
{
599-
_UsedThis = false;
600598
Visit(asNode);
601-
_UsedThis = false;
599+
break;
600+
}
601+
case SyntaxKind.IdentifierName:
602+
{
603+
VisitIdentifierName(asNode as IdentifierNameSyntax);
602604
break;
603605
}
604606
default:
@@ -816,19 +818,24 @@ public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
816818
{
817819
SyntaxKind kind = asNode.Kind();
818820

819-
if (kind == SyntaxKind.AccessorList)
821+
if (kind == SyntaxKind.AccessorList)
820822
{
821-
string _getSetStr = asNode.ToString().Trim().Replace(" ","");
823+
string _getSetStr = asNode.ToString().Trim().Replace(" ", "");
822824

823825
if (_getSetStr == "{get;set;}" || _getSetStr == "{get;}")
824826
{
825827
IEnumerable<SyntaxNodeOrToken> key = from n in nodesAndTokens
826-
where n.IsNode
827-
where n.AsNode().IsKind(SyntaxKind.PredefinedType)
828-
select n;
828+
where n.IsNode
829+
where n.AsNode().IsKind(SyntaxKind.PredefinedType)
830+
select n;
829831

830832
FieldDeclarationSyntax field = null;
831833

834+
string _indentifier = "_" + nodesAndTokens[i - 1].AsToken().ToString() + "_";
835+
836+
if (!_PropertyStatic)
837+
_indentifier = "#" + _indentifier;
838+
832839
if (!key.Any())
833840
{
834841
key = from n in nodesAndTokens
@@ -839,17 +846,18 @@ where n.AsNode().IsKind(SyntaxKind.IdentifierName)
839846
if (!key.Any())
840847
{
841848
key = from n in nodesAndTokens
842-
where n.IsNode
843-
where n.AsNode().IsKind(SyntaxKind.GenericName)
844-
select n;
849+
where n.IsNode
850+
where n.AsNode().IsKind(SyntaxKind.GenericName)
851+
select n;
845852
}
846853

854+
847855
field = SyntaxFactory.FieldDeclaration(
848856
SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(key.First().ToString()))
849857
.WithVariables(
850858
SyntaxFactory.SingletonSeparatedList(
851859
SyntaxFactory.VariableDeclarator(
852-
SyntaxFactory.Identifier("#_" + nodesAndTokens[i - 1].AsToken().ToString() + "_")))))
860+
SyntaxFactory.Identifier(_indentifier)))))
853861
.WithModifiers(
854862
SyntaxFactory.TokenList(new[]
855863
{
@@ -858,7 +866,7 @@ where n.AsNode().IsKind(SyntaxKind.GenericName)
858866
.WithLeadingTrivia(node.GetLeadingTrivia())
859867
.WithTrailingTrivia(node.GetTrailingTrivia());
860868
}
861-
else
869+
else
862870
{
863871
key = key.First().ChildNodesAndTokens();
864872
field = SyntaxFactory.FieldDeclaration(
@@ -867,7 +875,7 @@ where n.AsNode().IsKind(SyntaxKind.GenericName)
867875
.WithVariables(
868876
SyntaxFactory.SingletonSeparatedList(
869877
SyntaxFactory.VariableDeclarator(
870-
SyntaxFactory.Identifier("#_" + nodesAndTokens[i - 1].AsToken().ToString() + "_")))))
878+
SyntaxFactory.Identifier(_indentifier)))))
871879
.WithModifiers(
872880
SyntaxFactory.TokenList(new[]
873881
{
@@ -882,6 +890,15 @@ where n.AsNode().IsKind(SyntaxKind.GenericName)
882890
}
883891
}
884892
}
893+
else
894+
{
895+
SyntaxToken asToken = nodesAndTokens[i].AsToken();
896+
SyntaxKind kind = asToken.Kind();
897+
898+
if (kind == SyntaxKind.StaticKeyword)
899+
_PropertyStatic = true;
900+
}
901+
885902
}
886903

887904
for (int i = 0; i < nodesAndTokens.Count; i++)
@@ -925,7 +942,6 @@ where n.AsNode().IsKind(SyntaxKind.GenericName)
925942
VisitLeadingTrivia(asToken);
926943
break;
927944
case SyntaxKind.StaticKeyword:
928-
_PropertyStatic = true;
929945
VisitToken(asToken);
930946
break;
931947
default:
@@ -1727,7 +1743,7 @@ where e.IsKind(SyntaxKind.IdentifierToken)
17271743

17281744
if (iSymbol != null && iSymbol.Kind != SymbolKind.ErrorType)
17291745
{
1730-
if (iSymbol.ContainingNamespace.ToString().Contains(nameof(APIs.JS)))
1746+
if (iSymbol.ContainingNamespace.ToString().Contains(nameof(CSharpToJavaScript)))
17311747
{
17321748
string _text = string.Empty;
17331749
SyntaxToken _identifier = new();
@@ -1798,6 +1814,96 @@ where e.IsKind(SyntaxKind.IdentifierToken)
17981814
}
17991815
}
18001816

1817+
public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
1818+
{
1819+
ChildSyntaxList nodesAndTokens = node.ChildNodesAndTokens();
1820+
1821+
for (int i = 0; i < nodesAndTokens.Count; i++)
1822+
{
1823+
SyntaxNode? asNode = nodesAndTokens[i].AsNode();
1824+
1825+
if (asNode != null)
1826+
{
1827+
SyntaxKind kind = asNode.Kind();
1828+
1829+
switch (kind)
1830+
{
1831+
case SyntaxKind.ArgumentList:
1832+
Visit(asNode);
1833+
break;
1834+
case SyntaxKind.IdentifierName:
1835+
{
1836+
VisitIdentifierName(asNode as IdentifierNameSyntax);
1837+
break;
1838+
}
1839+
case SyntaxKind.GenericName:
1840+
VisitGenericName(asNode as GenericNameSyntax);
1841+
break;
1842+
default:
1843+
_CSTOJS.Log($"asNode : {kind}");
1844+
break;
1845+
}
1846+
}
1847+
else
1848+
{
1849+
SyntaxToken asToken = nodesAndTokens[i].AsToken();
1850+
SyntaxKind kind = asToken.Kind();
1851+
1852+
switch (kind)
1853+
{
1854+
case SyntaxKind.NewKeyword:
1855+
{
1856+
VisitToken(asToken);
1857+
break;
1858+
}
1859+
default:
1860+
_CSTOJS.Log($"asToken : {kind}");
1861+
break;
1862+
}
1863+
}
1864+
}
1865+
}
1866+
1867+
public override void VisitThisExpression(ThisExpressionSyntax node)
1868+
{
1869+
ChildSyntaxList nodesAndTokens = node.ChildNodesAndTokens();
1870+
1871+
for (int i = 0; i < nodesAndTokens.Count; i++)
1872+
{
1873+
SyntaxNode? asNode = nodesAndTokens[i].AsNode();
1874+
1875+
if (asNode != null)
1876+
{
1877+
SyntaxKind kind = asNode.Kind();
1878+
1879+
switch (kind)
1880+
{
1881+
1882+
default:
1883+
_CSTOJS.Log($"asNode : {kind}");
1884+
break;
1885+
}
1886+
}
1887+
else
1888+
{
1889+
SyntaxToken asToken = nodesAndTokens[i].AsToken();
1890+
SyntaxKind kind = asToken.Kind();
1891+
1892+
switch (kind)
1893+
{
1894+
case SyntaxKind.ThisKeyword:
1895+
{
1896+
VisitToken(asToken);
1897+
break;
1898+
}
1899+
default:
1900+
_CSTOJS.Log($"asToken : {kind}");
1901+
break;
1902+
}
1903+
}
1904+
}
1905+
}
1906+
18011907
public override void VisitBaseExpression(BaseExpressionSyntax node)
18021908
{
18031909
ChildSyntaxList nodesAndTokens = node.ChildNodesAndTokens();
@@ -2079,16 +2185,10 @@ public bool IdentifierToken(SyntaxNode node)
20792185
{
20802186
return false;
20812187
}
2082-
2083-
2084-
DataFlowAnalysis _dfa = _Model.AnalyzeDataFlow(node);
2085-
2086-
if(_dfa.Succeeded == false)
2087-
_dfa = _Model.AnalyzeDataFlow(node.Parent);
2088-
2089-
if (_dfa.Succeeded == true)
2188+
/*
2189+
if (iSymbol.DeclaringSyntaxReferences.Length >= 0)
20902190
{
2091-
if (_dfa.WrittenOutside[0].Name == "this" || iSymbol.IsStatic)
2191+
if (_UsedThis == false)
20922192
{
20932193
_UsedThis = true;
20942194
@@ -2102,9 +2202,7 @@ public bool IdentifierToken(SyntaxNode node)
21022202
return true;
21032203
}
21042204
}
2105-
2106-
2107-
/*
2205+
21082206
IEnumerable<SyntaxNode?> _all = from e in node.Parent.ChildNodes()
21092207
where e.IsKind(SyntaxKind.IdentifierName)
21102208
select e;
@@ -2129,7 +2227,7 @@ where e.IsKind(SyntaxKind.GenericName)
21292227
//TODO! TEST THIS!
21302228
//DO NOT REMOVE BREAKPOINT TEST THIS!
21312229
//return false;
2132-
}
2230+
}*/
21332231

21342232
ClassDeclarationSyntax _class = (ClassDeclarationSyntax)node.Ancestors().First(n => n.Kind() == SyntaxKind.ClassDeclaration);
21352233
var a = _class.Members;
@@ -2138,12 +2236,6 @@ where e.IsKind(SyntaxKind.GenericName)
21382236
SyntaxToken _sT = default;
21392237
if (item is MethodDeclarationSyntax m)
21402238
{
2141-
/*
2142-
IEnumerable<SyntaxToken> d3 = from e in m.ChildTokens()
2143-
where e.IsKind(SyntaxKind.IdentifierToken)
2144-
select e;
2145-
_sT = d3.First();
2146-
21472239
_sT = m.Identifier;
21482240
}
21492241

@@ -2167,21 +2259,31 @@ where e.IsKind(SyntaxKind.IdentifierToken)
21672259

21682260
if (_sT.ToString() == node.ToString())
21692261
{
2170-
if (_UsedThis == false)
2262+
if (node.Parent.Parent != null &&
2263+
!node.Parent.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
21712264
{
2172-
_UsedThis = true;
2265+
DataFlowAnalysis _dfa = _Model.AnalyzeDataFlow(node);
2266+
2267+
if (_dfa.Succeeded == false)
2268+
_dfa = _Model.AnalyzeDataFlow(node.Parent);
21732269

2174-
VisitLeadingTrivia(identifier);
2270+
if (_dfa.Succeeded == true)
2271+
{
2272+
if (_dfa.WrittenOutside[0].Name == "this" || iSymbol.IsStatic)
2273+
{
2274+
VisitLeadingTrivia(identifier);
21752275

2176-
JSSB.Append($"this.");
2177-
VisitToken(identifier.WithoutTrivia());
2276+
JSSB.Append($"this.");
2277+
VisitToken(identifier.WithoutTrivia());
21782278

2179-
VisitTrailingTrivia(identifier);
2279+
VisitTrailingTrivia(identifier);
21802280

2181-
return true;
2281+
return true;
2282+
}
2283+
}
21822284
}
21832285
}
2184-
}*/
2286+
}
21852287

21862288
return false;
21872289
}

0 commit comments

Comments
 (0)