1010using Microsoft . CodeAnalysis . Text ;
1111using System . Text ;
1212using System ;
13+ using CSharpToJavaScript . Utils ;
1314
1415namespace CSharpToJavaScript
1516{
1617 /// <summary>
1718 /// Main type for CSharpToJavaScript.
1819 /// </summary>
1920 public class CSTOJS : ILog
20- {
21- private CSTOJSOptions _Options = new ( ) ;
22- private Stopwatch _Stopwatch = new ( ) ;
21+ {
22+ private readonly CSTOJSOptions _Options = new ( ) ;
23+ private readonly Stopwatch _Stopwatch = new ( ) ;
24+ private readonly ILog ? _Log = null ;
25+
2326 private Walker ? _Walker = null ;
24- private readonly ILog ? _Log = null ;
2527
2628 /// <summary>
2729 /// New instance of <see cref="CSTOJS"/> with default options, see <see cref="CSTOJSOptions"/>.
2830 /// </summary>
2931 public CSTOJS ( )
3032 {
31- _Log = this ;
33+ _Log = this ;
3234
3335 Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
34- //https://stackoverflow.com/a/73474279
35- _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
36+ //https://stackoverflow.com/a/73474279
37+ _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
3638 }
3739
3840 /// <summary>
@@ -41,16 +43,16 @@ public CSTOJS()
4143 /// <param name="options">Options of <see cref="CSTOJS"/>, see <see cref="CSTOJSOptions"/>.</param>
4244 public CSTOJS ( CSTOJSOptions options )
4345 {
44- _Options = options ;
46+ _Options = options ;
4547
46- _Log = ILog . GetILog ( this , _Options ) ;
48+ _Log = ILog . GetILog ( this , _Options ) ;
4749
48- if ( _Options . DisableConsoleOutput == false )
50+ if ( _Options . DisableConsoleOutput == false )
4951 {
5052 Assembly assembly = Assembly . GetExecutingAssembly ( ) ;
51- //https://stackoverflow.com/a/73474279
52- _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
53- }
53+ //https://stackoverflow.com/a/73474279
54+ _Log . SuccessLine ( $ "{ assembly . GetName ( ) . Name } { assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( ) ? . InformationalVersion } ") ;
55+ }
5456 }
5557
5658 /// <summary>
@@ -61,7 +63,8 @@ public CSTOJS(CSTOJSOptions options)
6163 /// <returns>empty Task</returns>
6264 public async Task GenerateOneAsync ( string path , string ? filename = null )
6365 {
64- Assembly assembly = Assembly . GetEntryAssembly ( ) ;
66+
67+ Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
6568 List < FileInfo > files = new ( ) ;
6669
6770 if ( File . Exists ( path ) )
@@ -100,7 +103,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
100103 else
101104 pathCombined = Path . Combine ( _Options . OutPutPath , file . Name . Replace ( ".cs" , ".js" ) ) ;
102105
103- await File . WriteAllTextAsync ( pathCombined , _Walker . JSSB . ToString ( ) ) ;
106+ await File . WriteAllTextAsync ( pathCombined , _Walker ? . JSSB . ToString ( ) ) ;
104107
105108 _Log . SuccessLine ( $ "--- Done!") ;
106109 _Log . SuccessLine ( $ "--- Path: { pathCombined } ") ;
@@ -115,7 +118,7 @@ public async Task GenerateOneAsync(string path, string? filename = null)
115118 /// <returns>List of StringBuilder</returns>
116119 public List < StringBuilder > GenerateOne ( string path )
117120 {
118- Assembly assembly = Assembly . GetEntryAssembly ( ) ;
121+ Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
119122 List < FileInfo > files = new ( ) ;
120123 List < StringBuilder > jsStringBuilders = new ( ) ;
121124
@@ -161,10 +164,9 @@ public List<StringBuilder> GenerateOne(string path)
161164 /// <exception cref="ArgumentNullException"></exception>
162165 public StringBuilder GenerateOneFromString ( string csstring , List < MetadataReference > ? references = null )
163166 {
164- if ( csstring == null )
165- throw new ArgumentNullException ( nameof ( csstring ) ) ;
167+ ArgumentNullException . ThrowIfNull ( csstring ) ;
166168
167- Assembly assembly = Assembly . GetEntryAssembly ( ) ;
169+ Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
168170
169171 SyntaxTree ? _tree = CSharpSyntaxTree . ParseText ( csstring ) ;
170172
@@ -189,10 +191,9 @@ public StringBuilder GenerateOneFromString(string csstring, List<MetadataReferen
189191 /// <exception cref="ArgumentNullException"></exception>
190192 public async Task GenerateOneFromStringAsync ( string csstring , string ? filename = "main.js" , List < MetadataReference > ? references = null )
191193 {
192- if ( csstring == null )
193- throw new ArgumentNullException ( nameof ( csstring ) ) ;
194+ ArgumentNullException . ThrowIfNull ( csstring ) ;
194195
195- Assembly assembly = Assembly . GetEntryAssembly ( ) ;
196+ Assembly ? assembly = Assembly . GetEntryAssembly ( ) ;
196197
197198 SyntaxTree ? _tree = CSharpSyntaxTree . ParseText ( csstring ) ;
198199
@@ -217,21 +218,21 @@ public async Task GenerateOneFromStringAsync(string csstring, string? filename =
217218 }
218219
219220
220- private void Generate ( SyntaxTree ? tree , Assembly assembly , List < MetadataReference > ? refs = null )
221+ private void Generate ( SyntaxTree ? tree , Assembly ? assembly , List < MetadataReference > ? refs = null )
221222 {
222- if ( _Options . Debug )
223- {
224- _Stopwatch . Restart ( ) ;
223+ if ( _Options . Debug )
224+ {
225+ _Stopwatch . Restart ( ) ;
225226 _Log . WriteLine ( "Start stopwatch" ) ;
226- }
227+ }
227228
228229 CompilationUnitSyntax root = tree . GetCompilationUnitRoot ( ) ;
229230
230231
231- string assemblyPath = Path . GetDirectoryName ( assembly . Location ) ;
232+ string ? assemblyPath = Path . GetDirectoryName ( assembly ? . Location ) ;
232233 List < MetadataReference > references = new ( ) { } ;
233234
234- string rtPath = Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ;
235+ string ? rtPath = Path . GetDirectoryName ( typeof ( object ) . Assembly . Location ) ;
235236
236237 if ( refs == null )
237238 {
@@ -485,12 +486,12 @@ private void Generate(SyntaxTree? tree, Assembly assembly, List<MetadataReferenc
485486
486487 _Walker . JSSB . Append ( _Options . AddSBInEnd ) ;
487488
488- if ( _Options . Debug )
489- {
490- _Stopwatch . Stop ( ) ;
489+ if ( _Options . Debug )
490+ {
491+ _Stopwatch . Stop ( ) ;
491492 _Log . WriteLine ( $ "Stop stopwatch: { _Stopwatch . Elapsed } ") ;
492- }
493- }
493+ }
494+ }
494495
495496 }
496497}
0 commit comments