Skip to content

Commit 32c9df1

Browse files
committed
Improved this expression detection.
Fixed globalstatement formatting.
1 parent a972284 commit 32c9df1

File tree

3 files changed

+104
-28
lines changed

3 files changed

+104
-28
lines changed

CSharpToJavaScript/CSTOJS.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ private static MetadataReference[] GetReferences(CSTOJSOptions options)
177177

178178
if (File.Exists(Path.Combine(objectAssemblyPath, "System.Console.dll")))
179179
assemblyMetadata.Add(MetadataReference.CreateFromFile(Path.Combine(objectAssemblyPath, "System.Console.dll")));
180-
181180
}
182181
if (binPath != null)
183182
{

CSharpToJavaScript/Utils/Unsupported.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace CSharpToJavaScript.Utils;
1+
using CSharpToJavaScript.APIs.JS.Ecma;
2+
3+
namespace CSharpToJavaScript.Utils;
24

35
public class Unsupported
46
{
@@ -8,5 +10,12 @@ public class Unsupported
810
public static implicit operator Unsupported(double value) { return new Unsupported { Value = value }; }
911
public static implicit operator Unsupported(float value) { return new Unsupported { Value = value }; }
1012
public static implicit operator Unsupported(int value) { return new Unsupported { Value = value }; }
13+
public static implicit operator Unsupported(Object value) { return new Unsupported { Value = value }; }
14+
15+
public static implicit operator string(Unsupported value) { return new Unsupported { Value = value }; }
16+
public static implicit operator double(Unsupported value) { return new Unsupported { Value = value }; }
17+
public static implicit operator float(Unsupported value) { return new Unsupported { Value = value }; }
18+
public static implicit operator int(Unsupported value) { return new Unsupported { Value = value }; }
19+
public static implicit operator Object(Unsupported value) { return new Unsupported { Value = value }; }
1120
}
1221

CSharpToJavaScript/Walker.cs

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ internal class Walker : CSharpSyntaxWalker
5757
private bool _IgnoreAsParenthesis = false;
5858
private bool _IgnoreTailingDot = false;
5959
private bool _GlobalStatement = false;
60-
60+
private bool _ThisIsUsed = false;
61+
6162
private int _EnumMembers = 0;
6263

6364
private string[] _AttributeDatasForInvocation = new string[2];
@@ -1057,8 +1058,11 @@ public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax no
10571058
VisitIdentifierName((IdentifierNameSyntax)asNode);
10581059
break;
10591060
case SyntaxKind.ThisExpression:
1060-
VisitThisExpression((ThisExpressionSyntax)asNode);
1061-
break;
1061+
{
1062+
_ThisIsUsed = true;
1063+
VisitThisExpression((ThisExpressionSyntax)asNode);
1064+
break;
1065+
}
10621066
case SyntaxKind.ParenthesizedExpression:
10631067
VisitParenthesizedExpression((ParenthesizedExpressionSyntax)asNode);
10641068
break;
@@ -1127,6 +1131,8 @@ public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax no
11271131
}
11281132
}
11291133
}
1134+
1135+
_ThisIsUsed = false;
11301136
}
11311137
public override void VisitAnonymousObjectCreationExpression(AnonymousObjectCreationExpressionSyntax node)
11321138
{
@@ -3532,7 +3538,7 @@ private bool IsRightAttribute<T>(T identifier, SyntaxToken asToken) where T : Si
35323538
return true;
35333539
}
35343540

3535-
if (_attributeData[j].AttributeClass.Name == nameof(EnumValueAttribute))
3541+
if (_attributeData[j].AttributeClass!.Name == nameof(EnumValueAttribute))
35363542
{
35373543
SyntaxTriviaList _syntaxTrivias = identifier.GetLeadingTrivia();
35383544
for (int _i = 0; _i < _syntaxTrivias.Count; _i++)
@@ -3551,7 +3557,7 @@ private bool IsRightAttribute<T>(T identifier, SyntaxToken asToken) where T : Si
35513557
return true;
35523558
}
35533559

3554-
if (_attributeData[j].AttributeClass.Name == nameof(ValueAttribute))
3560+
if (_attributeData[j].AttributeClass!.Name == nameof(ValueAttribute))
35553561
{
35563562
SyntaxTriviaList _syntaxTrivias = identifier.GetLeadingTrivia();
35573563
for (int _i = 0; _i < _syntaxTrivias.Count; _i++)
@@ -3570,9 +3576,9 @@ private bool IsRightAttribute<T>(T identifier, SyntaxToken asToken) where T : Si
35703576
return true;
35713577
}
35723578

3573-
if (_attributeData[j].AttributeClass.Name == nameof(ToAttribute))
3579+
if (_attributeData[j].AttributeClass!.Name == nameof(ToAttribute))
35743580
{
3575-
ToAttribute _toAttr = new ToAttribute(_attributeData[j].ConstructorArguments[0].Value.ToString());
3581+
ToAttribute _toAttr = new(_attributeData[j].ConstructorArguments[0].Value.ToString());
35763582

35773583
if (_toAttr.To == ToAttribute.None)
35783584
_IgnoreTailingDot = true;
@@ -6102,9 +6108,9 @@ public bool IdentifierToken(SyntaxNode node)
61026108
else
61036109
iSymbol = symbolInfo?.Symbol;
61046110

6105-
if (iSymbol != null &&
6106-
iSymbol.Kind != SymbolKind.ErrorType &&
6107-
iSymbol.Kind != SymbolKind.DynamicType &&
6111+
if (iSymbol != null &&
6112+
iSymbol.Kind != SymbolKind.ErrorType &&
6113+
iSymbol.Kind != SymbolKind.DynamicType &&
61086114
iSymbol.Kind != SymbolKind.Namespace)
61096115
{
61106116
string? _containingNamespace = iSymbol.ContainingNamespace.ToString();
@@ -6114,14 +6120,76 @@ public bool IdentifierToken(SyntaxNode node)
61146120
_containingNamespace = string.Empty;
61156121
}
61166122

6123+
if (_GlobalStatement)
6124+
{
6125+
if (iSymbol.Kind == SymbolKind.Parameter ||
6126+
iSymbol.Kind == SymbolKind.Local)
6127+
{
6128+
return false;
6129+
}
6130+
if (iSymbol.Kind == SymbolKind.Method &&
6131+
((IMethodSymbol)iSymbol).MethodKind == MethodKind.LocalFunction)
6132+
return false;
6133+
}
6134+
61176135
if (_containingNamespace.Contains(_NameSpaceStr) && !_GlobalStatement)
61186136
{
61196137
if (iSymbol.Kind == SymbolKind.Parameter ||
61206138
iSymbol.Kind == SymbolKind.Local)
61216139
{
61226140
return false;
61236141
}
6142+
if (!_ThisIsUsed)
6143+
{
6144+
if (iSymbol.Kind == SymbolKind.Method &&
6145+
((IMethodSymbol)iSymbol).MethodKind == MethodKind.Ordinary)
6146+
{
6147+
if (node.Parent is MemberAccessExpressionSyntax member)
6148+
{
6149+
ISymbol? _iSymbolParent = _Model.GetSymbolInfo(member.Expression).Symbol;
6150+
if (_iSymbolParent != null && (_iSymbolParent.Kind == SymbolKind.Local || _iSymbolParent.Kind == SymbolKind.Method))
6151+
return false;
6152+
}
6153+
if (((IMethodSymbol)iSymbol).ReceiverType.ToString().EndsWith(_CurrentClassStr))
6154+
{
6155+
VisitLeadingTrivia(identifier);
6156+
6157+
JSSB.Append($"this.");
6158+
VisitToken(identifier.WithoutTrivia());
6159+
6160+
VisitTrailingTrivia(identifier);
61246161

6162+
return true;
6163+
}
6164+
return false;
6165+
}
6166+
6167+
if (iSymbol.Kind == SymbolKind.Property ||
6168+
iSymbol.Kind == SymbolKind.Field)
6169+
{
6170+
if (node.Parent is MemberAccessExpressionSyntax member)
6171+
{
6172+
ISymbol? _iSymbolParent = _Model.GetSymbolInfo(member.Expression).Symbol;
6173+
if (_iSymbolParent != null && (_iSymbolParent.Kind == SymbolKind.Local || _iSymbolParent.Kind == SymbolKind.Method))
6174+
return false;
6175+
}
6176+
string? _type = iSymbol.ContainingType.ToString();
6177+
if (_type != null &&
6178+
_type.EndsWith(_CurrentClassStr))
6179+
{
6180+
VisitLeadingTrivia(identifier);
6181+
6182+
JSSB.Append($"this.");
6183+
VisitToken(identifier.WithoutTrivia());
6184+
6185+
VisitTrailingTrivia(identifier);
6186+
6187+
return true;
6188+
}
6189+
return false;
6190+
}
6191+
}
6192+
/*
61256193
ClassDeclarationSyntax? _class = node.Ancestors().FirstOrDefault(n => n.IsKind(SyntaxKind.ClassDeclaration)) as ClassDeclarationSyntax;
61266194
61276195
if (_class == null)
@@ -6134,7 +6202,7 @@ public bool IdentifierToken(SyntaxNode node)
61346202
//This is for struct.
61356203
//maybe later convert struct to class?
61366204
//
6137-
if (_class == default(ClassDeclarationSyntax))
6205+
if (_class == default(ClassDeclarationSyntax))
61386206
{
61396207
Log.WarningLine("_class is default");
61406208
return false;
@@ -6177,7 +6245,7 @@ where e.IsKind(SyntaxKind.IdentifierToken)
61776245
if (node.Parent is MemberAccessExpressionSyntax member)
61786246
{
61796247
ISymbol? _iSymbolParent = _Model.GetSymbolInfo(member.Expression).Symbol;
6180-
if (_iSymbolParent != null && _iSymbolParent.Kind == SymbolKind.Local)
6248+
if (_iSymbolParent != null && (_iSymbolParent.Kind == SymbolKind.Local || _iSymbolParent.Kind == SymbolKind.Method))
61816249
return false;
61826250
}
61836251
@@ -6199,11 +6267,11 @@ where e.IsKind(SyntaxKind.IdentifierToken)
61996267
}
62006268
}
62016269
}
6202-
6270+
*/
62036271
return false;
62046272
}
62056273
}
6206-
6274+
62076275
if (iSymbol != null && iSymbol.ContainingType != null && iSymbol.ContainingType.IsAnonymousType == true)
62086276
{
62096277
if (CustomCSNamesToJS(node) == false)
@@ -6278,28 +6346,28 @@ private bool BuiltInTypesGenerics(SyntaxNode nodeL, ISymbol? symbol)
62786346
return false;
62796347
}
62806348

6281-
if (nodeL is IdentifierNameSyntax _identifierName)
6282-
{
6283-
VisitLeadingTrivia(_identifierName.Identifier);
6284-
}
6285-
else if (nodeL is GenericNameSyntax _genericName)
6286-
{
6287-
VisitLeadingTrivia(_genericName.Identifier);
6288-
}
6289-
62906349
ISymbol typeSymbol = symbol;
62916350

6292-
if(typeSymbol.Kind != SymbolKind.NamedType)
6351+
if (typeSymbol.Kind != SymbolKind.NamedType)
62936352
{
62946353
typeSymbol = symbol.ContainingSymbol;
62956354

6296-
if(typeSymbol.Kind != SymbolKind.NamedType)
6355+
if (typeSymbol.Kind != SymbolKind.NamedType)
62976356
{
62986357
Log.WarningLine($"node: \"{node}\", typeSymbol is \"{typeSymbol.Kind}\". USE \"CustomCSNamesToJS\"!");
62996358
return false;
63006359
}
63016360
}
6302-
6361+
6362+
if (nodeL is IdentifierNameSyntax _identifierName)
6363+
{
6364+
VisitLeadingTrivia(_identifierName.Identifier);
6365+
}
6366+
else if (nodeL is GenericNameSyntax _genericName)
6367+
{
6368+
VisitLeadingTrivia(_genericName.Identifier);
6369+
}
6370+
63036371
string typeName = typeSymbol.Name;
63046372

63056373
string? jsStr = _NETAPI.ReturnJSString(typeName);

0 commit comments

Comments
 (0)