66using Microsoft . CodeAnalysis ;
77using System . Linq ;
88using CSharpToJavaScript . Utils ;
9+ using System . Collections . Immutable ;
910
1011namespace CSharpToJavaScript ;
1112
@@ -49,74 +50,75 @@ public static FileData[] Translate(FileData[] files, MetadataReference[]? refere
4950
5051 trees [ 0 ] = AddGlobalUsings ( trees [ 0 ] ) ;
5152
53+ CSharpCompilation compilation = CSharpCompilation
54+ . Create ( "HelloWorld" )
55+ . AddReferences ( references )
56+ . AddSyntaxTrees ( trees ) ;
57+
5258 for ( int i = 0 ; i < files . Length ; i ++ )
5359 {
5460 if ( files [ i ] . OptionsForFile . TranslateFile == false )
5561 continue ;
5662
57- if ( files [ i ] . OptionsForFile . NormalizeWhitespace )
58- trees [ i ] = trees [ i ] . GetRoot ( ) . NormalizeWhitespace ( ) . SyntaxTree ;
63+ SemanticModel _model = compilation . GetSemanticModel ( trees [ i ] ) ;
5964
60- if ( files [ i ] . OptionsForFile . KeepBraceOnTheSameLine )
65+ ImmutableArray < Diagnostic > diagnostics = _model . GetDiagnostics ( ) ;
66+ for ( int j = 0 ; j < diagnostics . Length ; j ++ )
6167 {
62- //Mostly deleted whitespaces, still TODO?
63- SyntaxToken [ ] allBraces = trees [ i ] . GetRoot ( ) . DescendantTokens ( ) . Where ( ( e ) => e . IsKind ( SyntaxKind . OpenBraceToken ) ) . ToArray ( ) ;
64-
65- List < SyntaxTrivia > allTriviaToDelete = new ( ) ;
68+ if ( files [ i ] . OptionsForFile . Debug )
69+ Log . WarningLine ( diagnostics [ j ] . ToString ( ) ) ;
6670
67- for ( int j = 0 ; j < allBraces . Length ; j ++ )
71+ //Print an error if compilation fails.
72+ if ( diagnostics [ j ] . Severity == DiagnosticSeverity . Error )
6873 {
69- if ( allBraces [ j ] . HasLeadingTrivia )
70- {
71- SyntaxTriviaList _lt = allBraces [ j ] . LeadingTrivia ;
72- for ( int y = 0 ; y < _lt . Count ; y ++ )
73- {
74- allTriviaToDelete . Add ( _lt [ y ] ) ;
75- }
76- }
74+ if ( files [ i ] . OptionsForFile . DisableCompilationErrors == false )
75+ Log . ErrorLine ( diagnostics [ i ] . ToString ( ) ) ;
7776 }
78- //Is this the right way to delete trivia?
79- trees [ i ] = trees [ i ] . GetRoot ( ) . ReplaceTrivia ( allTriviaToDelete , ( o , r ) => SyntaxFactory . ElasticMarker ) . SyntaxTree ;
77+ }
8078
81- allBraces = trees [ i ] . GetRoot ( ) . DescendantTokens ( ) . Where ( ( e ) => e . IsKind ( SyntaxKind . OpenBraceToken ) ) . ToArray ( ) ;
79+ SyntaxNode _root = trees [ i ] . GetRoot ( ) ;
8280
83- List < SyntaxTrivia > allTriviaToReplace = new ( ) ;
81+ WithSemanticRewriter _withSemanticRewriter = new ( _model , files [ i ] . OptionsForFile ) ;
82+ WithoutSemanticRewriter _withoutSemanticRewriter = new ( files [ i ] . OptionsForFile ) ;
83+
84+ StringBuilderWalker _stringBuilderWalker = new ( ) ;
8485
85- for ( int j = 0 ; j < allBraces . Length ; j ++ )
86- {
87- SyntaxToken _token = allBraces [ j ] . GetPreviousToken ( ) ;
88- if ( _token . HasTrailingTrivia )
89- {
90- SyntaxTrivia _trivia = _token . TrailingTrivia . Where ( ( e ) => e . IsKind ( SyntaxKind . EndOfLineTrivia ) ) . FirstOrDefault ( ) ;
91- if ( ! _trivia . IsKind ( SyntaxKind . None ) )
92- {
93- allTriviaToReplace . Add ( _trivia ) ;
94- }
95- }
96- }
97- trees [ i ] = trees [ i ] . GetRoot ( ) . ReplaceTrivia ( allTriviaToReplace , ( o , r ) => SyntaxFactory . Space ) . SyntaxTree ;
98- }
99- }
86+ SyntaxNode newRoot1 = _withSemanticRewriter . Visit ( _root ) ;
87+ if ( _root != newRoot1 )
88+ _root = newRoot1 ;
10089
101- CSharpCompilation compilation = CSharpCompilation
102- . Create ( "HelloWorld" )
103- . AddReferences ( references )
104- . AddSyntaxTrees ( trees ) ;
90+ _root = _root . ReplaceNodes ( _withSemanticRewriter . ReplaceNodes . Keys , ( o , r ) =>
91+ {
92+ return _withSemanticRewriter . ReplaceNodes [ o ] ;
93+ } ) ;
10594
106- for ( int i = 0 ; i < files . Length ; i ++ )
107- {
108- if ( files [ i ] . OptionsForFile . TranslateFile == false )
109- continue ;
95+ if ( files [ i ] . OptionsForFile . Debug )
96+ files [ i ] . Debug_WithSemanticRewriter = _root . ToFullString ( ) ;
11097
111- Walker _walker = new ( files [ i ] . OptionsForFile , compilation . GetSemanticModel ( trees [ i ] ) ) ;
98+ SyntaxNode newRoot2 = _withoutSemanticRewriter . Visit ( _root ) ;
99+ if ( _root != newRoot2 )
100+ _root = newRoot2 ;
112101
113- _walker . JSSB . Append ( files [ i ] . OptionsForFile . AddSBAtTheTop ) ;
102+ if ( files [ i ] . OptionsForFile . Debug )
103+ files [ i ] . Debug_WithoutSemanticRewriter = _root . ToFullString ( ) ;
104+
105+ if ( files [ i ] . OptionsForFile . NormalizeWhitespace )
106+ _root = _root . NormalizeWhitespace ( ) ;
114107
115- _walker . Visit ( trees [ i ] . GetRoot ( ) ) ;
108+ if ( files [ i ] . OptionsForFile . KeepBraceOnTheSameLine )
109+ {
110+ KeepBraceOnTheSameLineRewriter _keepBraceOnTheSameLineRewriter = new ( ) ;
111+
112+ SyntaxNode newRoot3 = _keepBraceOnTheSameLineRewriter . Visit ( _root ) ;
113+ if ( _root != newRoot3 )
114+ _root = newRoot3 ;
115+ }
116116
117- _walker . JSSB . Append ( files [ i ] . OptionsForFile . AddSBAtTheBottom ) ;
117+ _stringBuilderWalker . JSSB . Append ( files [ i ] . OptionsForFile . AddSBAtTheTop ) ;
118+ _stringBuilderWalker . Visit ( _root ) ;
119+ _stringBuilderWalker . JSSB . Append ( files [ i ] . OptionsForFile . AddSBAtTheBottom ) ;
118120
119- files [ i ] . TranslatedStr = _walker . JSSB . ToString ( ) ;
121+ files [ i ] . TranslatedStr = _stringBuilderWalker . JSSB . ToString ( ) ;
120122 }
121123
122124 return files ;
@@ -200,7 +202,7 @@ private static MetadataReference[] GetReferences(CSTOJSOptions options)
200202 }
201203 Log . InfoLine ( $ "---") ;
202204 }
203-
205+
204206 MetadataReference [ ] references = new MetadataReference [ assemblyMetadata . Count ] ;
205207 int i = 0 ;
206208 foreach ( MetadataReference metadata in assemblyMetadata )
@@ -218,7 +220,7 @@ private static MetadataReference[] GetReferences(CSTOJSOptions options)
218220 }
219221 Log . InfoLine ( $ "+++") ;
220222 }
221-
223+
222224 return references ;
223225 }
224226
@@ -397,9 +399,20 @@ public class FileData
397399 /// CS input string.
398400 /// </summary>
399401 public string SourceStr { get ; set ; } = string . Empty ;
400-
402+
401403 /// <summary>
402404 /// JS translated string.
403405 /// </summary>
404406 public string TranslatedStr { get ; set ; } = string . Empty ;
405- }
407+
408+
409+ /// <summary>
410+ /// Debug string.
411+ /// </summary>
412+ public string Debug_WithSemanticRewriter { get ; set ; } = string . Empty ;
413+
414+ /// <summary>
415+ /// Debug string.
416+ /// </summary>
417+ public string Debug_WithoutSemanticRewriter { get ; set ; } = string . Empty ;
418+ }
0 commit comments