Skip to content

Commit 330f600

Browse files
committed
Reworked how to use SyntaxAnnotations.
Now it is a single kind but with different data. Also, better debug formatting.
1 parent 62236eb commit 330f600

5 files changed

Lines changed: 82 additions & 20 deletions

File tree

CSharpToJavaScript/CSTOJS.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,25 @@ public static FileData[] Translate(FileData[] files, MetadataReference[]? refere
119119
if (files[i].OptionsForFile.Debug)
120120
{
121121
files[i].DebugStrings[0] = _root.ToFullString();
122-
files[i].DebugStrings[0] += "\n-\n";
123-
files[i].DebugStrings[0] += "\n-\n";
124-
files[i].DebugStrings[0] += "\n-\n";
122+
files[i].DebugStrings[0] += "\n--- --- ---\n";
123+
files[i].DebugStrings[0] += "ReplaceNodes:";
124+
files[i].DebugStrings[0] += "\n--- --- ---\n";
125125
foreach (KeyValuePair<SyntaxNode, SyntaxNode> _nodes in _withSemanticWalker.ReplaceNodes)
126126
{
127-
files[i].DebugStrings[0] += $"\tNode({_nodes.Key.GetLocation().GetLineSpan()}): \n";
127+
files[i].DebugStrings[0] += $"\tNode ({_nodes.Key.GetLocation().GetLineSpan()}): \n|";
128128
files[i].DebugStrings[0] += _nodes.Key.ToString();
129-
files[i].DebugStrings[0] += "\n";
130-
files[i].DebugStrings[0] += "\tReplaced node: \n";
129+
files[i].DebugStrings[0] += "|\n";
130+
files[i].DebugStrings[0] += $"\tReplaced node (data annotation: {_nodes.Value.GetDataAnnotation()}): \n|";
131131
files[i].DebugStrings[0] += _nodes.Value.ToString();
132-
files[i].DebugStrings[0] += "\n\n";
132+
files[i].DebugStrings[0] += "|\n";
133133
}
134134
}
135+
135136
_root = _withoutSemanticRewriter.Visit(_root);
136137

137138
if (files[i].OptionsForFile.Debug)
138139
files[i].DebugStrings[1] = _root.ToFullString();
139-
140+
140141
if (files[i].OptionsForFile.NormalizeWhitespace)
141142
_root = _root.NormalizeWhitespace();
142143

CSharpToJavaScript/StringBuilderWalker.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public override void VisitFileScopedNamespaceDeclaration(FileScopedNamespaceDecl
280280

281281
public override void VisitInvocationExpression(InvocationExpressionSyntax node)
282282
{
283-
if (node.Expression.HasAnnotation(BinaryAttribute.Annotation))
283+
if (node.Expression.HasDataAnnotation(BinaryAttribute.Annotation.Data!))
284284
{
285285
VisitArgument(node.ArgumentList.Arguments[0]);
286286

@@ -294,7 +294,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
294294
VisitArgument(node.ArgumentList.Arguments[1]);
295295
return;
296296
}
297-
if (node.Expression.HasAnnotation(UnaryAttribute.Annotation))
297+
if (node.Expression.HasDataAnnotation(UnaryAttribute.Annotation.Data!))
298298
{
299299
JSSB.Append(node.Expression.ToString());
300300

@@ -308,7 +308,7 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
308308
}
309309
public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
310310
{
311-
if (!node.Identifier.HasAnnotation(WithoutSemanticRewriter.StaticConstructor))
311+
if (!node.Identifier.HasDataAnnotation(WithoutSemanticRewriter.StaticConstructor.Data!))
312312
base.VisitConstructorDeclaration(node);
313313
else
314314
{

CSharpToJavaScript/Utils/Attributes.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ namespace CSharpToJavaScript.Utils;
99
[AttributeUsage(AttributeTargets.All)]
1010
public class IgnoreAttribute : Attribute
1111
{
12-
public static SyntaxAnnotation Annotation { get; } = new(nameof(IgnoreAttribute));
12+
public static SyntaxAnnotation Annotation { get; } = new(Extensions.AnnotationKind, nameof(IgnoreAttribute));
1313
}
1414

1515
/// <summary>
1616
/// Add a custom import to the file. Useful for basic interop for calling JS code. Can be combined with IgnoreAttribute/ValueAttribute.
1717
/// </summary>
18+
/// <remarks>
19+
/// <blockquote class="IMPORTANT"><h5>IMPORTANT</h5>
20+
/// <para>
21+
/// Only <see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#named_import"><em>named imports</em></see> supported with this attribute.
22+
/// </para>
23+
/// </blockquote>
24+
/// </remarks>
1825
[AttributeUsage(AttributeTargets.All)]
1926
public class ImportAttribute : Attribute
2027
{
@@ -92,7 +99,7 @@ public string Convert(string str)
9299
[AttributeUsage(AttributeTargets.All)]
93100
public class BinaryAttribute : Attribute
94101
{
95-
public static SyntaxAnnotation Annotation { get; } = new(nameof(BinaryAttribute));
102+
public static SyntaxAnnotation Annotation { get; } = new(Extensions.AnnotationKind, nameof(BinaryAttribute));
96103
public string Value { get; init; }
97104
public BinaryAttribute(string value)
98105
{
@@ -117,7 +124,7 @@ public GenericBinaryAttribute(string value)
117124
[AttributeUsage(AttributeTargets.All)]
118125
public class UnaryAttribute : Attribute
119126
{
120-
public static SyntaxAnnotation Annotation { get; } = new(nameof(UnaryAttribute));
127+
public static SyntaxAnnotation Annotation { get; } = new(Extensions.AnnotationKind, nameof(UnaryAttribute));
121128
public string Value { get; init; }
122129
public UnaryAttribute(string value)
123130
{
@@ -143,7 +150,7 @@ public GenericUnaryAttribute(string value)
143150
[AttributeUsage(AttributeTargets.All)]
144151
public class ToObjectAttribute : Attribute
145152
{
146-
public static SyntaxAnnotation Annotation { get; } = new(nameof(ToObjectAttribute));
153+
public static SyntaxAnnotation Annotation { get; } = new(Extensions.AnnotationKind, nameof(ToObjectAttribute));
147154
}
148155

149156
/// <summary>

CSharpToJavaScript/Utils/Extensions.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,63 @@
11
using System;
2+
using System.Collections.Generic;
3+
4+
using Microsoft.CodeAnalysis;
25

36
namespace CSharpToJavaScript.Utils;
47

58
internal static class Extensions
69
{
10+
//Todo? Transfer to a separate file?
11+
public const string AnnotationKind = "Global";
12+
13+
public static bool HasDataAnnotation(this SyntaxNode node, string data)
14+
{
15+
if(!node.HasAnnotations(AnnotationKind))
16+
return false;
17+
18+
IEnumerable<SyntaxAnnotation> annotations = node.GetAnnotations(Extensions.AnnotationKind);
19+
20+
foreach(SyntaxAnnotation ann in annotations)
21+
{
22+
if(ann.Data == data)
23+
return true;
24+
}
25+
26+
return false;
27+
}
28+
public static bool HasDataAnnotation(this SyntaxToken token, string data)
29+
{
30+
if(!token.HasAnnotations(AnnotationKind))
31+
return false;
32+
33+
IEnumerable<SyntaxAnnotation> annotations = token.GetAnnotations(Extensions.AnnotationKind);
34+
35+
foreach(SyntaxAnnotation ann in annotations)
36+
{
37+
if(ann.Data == data)
38+
return true;
39+
}
40+
41+
return false;
42+
}
43+
44+
public static string? GetDataAnnotation(this SyntaxNode node)
45+
{
46+
if(!node.HasAnnotations(AnnotationKind))
47+
return null;
48+
49+
IEnumerable<SyntaxAnnotation> annotations = node.GetAnnotations(Extensions.AnnotationKind);
50+
51+
foreach(SyntaxAnnotation ann in annotations)
52+
{
53+
return ann.Data;
54+
}
55+
56+
return null;
57+
}
58+
59+
60+
761
//https://stackoverflow.com/a/21755933
862
public static string? FirstCharToLowerCase(this string? str)
963
{

CSharpToJavaScript/WithoutSemanticRewriter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace CSharpToJavaScript;
1010
internal class WithoutSemanticRewriter : CSharpSyntaxRewriter
1111
{
1212
private readonly CSTOJSOptions _Options;
13-
public static SyntaxAnnotation StaticConstructor { get; set; } = new("StaticConstructor");
13+
public static SyntaxAnnotation StaticConstructor { get; set; } = new(Extensions.AnnotationKind, "StaticConstructor");
1414

1515
public WithoutSemanticRewriter(CSTOJSOptions options)
1616
{
@@ -32,7 +32,7 @@ public WithoutSemanticRewriter(CSTOJSOptions options)
3232
public override SyntaxNode? VisitClassDeclaration(ClassDeclarationSyntax node)
3333
{
3434
//Ignore class translation if the IgnoreAttribute is used.
35-
if(node.HasAnnotation(IgnoreAttribute.Annotation))
35+
if(node.HasDataAnnotation(IgnoreAttribute.Annotation.Data!))
3636
return null;
3737

3838
node = (ClassDeclarationSyntax)base.VisitClassDeclaration(node)!;
@@ -254,7 +254,7 @@ public WithoutSemanticRewriter(CSTOJSOptions options)
254254
public override SyntaxNode? VisitFieldDeclaration(FieldDeclarationSyntax node)
255255
{
256256
//Ignore field translation if the IgnoreAttribute is used.
257-
if(node.Declaration.HasAnnotation(IgnoreAttribute.Annotation))
257+
if(node.Declaration.HasDataAnnotation(IgnoreAttribute.Annotation.Data!))
258258
return null;
259259

260260
node = (FieldDeclarationSyntax)base.VisitFieldDeclaration(node)!;
@@ -284,7 +284,7 @@ public WithoutSemanticRewriter(CSTOJSOptions options)
284284
public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
285285
{
286286
//Ignore method translation if the IgnoreAttribute is used.
287-
if(node.ReturnType.HasAnnotation(IgnoreAttribute.Annotation))
287+
if(node.ReturnType.HasDataAnnotation(IgnoreAttribute.Annotation.Data!))
288288
return null;
289289

290290
node = (MethodDeclarationSyntax)base.VisitMethodDeclaration(node)!;
@@ -367,7 +367,7 @@ public WithoutSemanticRewriter(CSTOJSOptions options)
367367
{
368368
node = (ObjectCreationExpressionSyntax)base.VisitObjectCreationExpression(node)!;
369369

370-
if (node.Type.HasAnnotation(ToObjectAttribute.Annotation))
370+
if (node.Type.HasDataAnnotation(ToObjectAttribute.Annotation.Data))
371371
{
372372
SyntaxTriviaList trivias = node.Type.GetTrailingTrivia();
373373

0 commit comments

Comments
 (0)