Skip to content

Commit b5d7fca

Browse files
committed
Found with dotnet-trace that GetDiagnostics takes half of the time.
Added the "DisableDiagnostics" option and removed the "DisableCompilationErrors" option. Default is false.
1 parent cb55652 commit b5d7fca

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

CSharpToJavaScript/CSTOJS.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,22 @@ public static FileData[] Translate(FileData[] files, MetadataReference[]? refere
6262

6363
SemanticModel _model = compilation.GetSemanticModel(trees[i]);
6464

65-
ImmutableArray<Diagnostic> diagnostics = _model.GetDiagnostics();
66-
for (int j = 0; j < diagnostics.Length; j++)
65+
if (files[i].OptionsForFile.DisableDiagnostics == false)
6766
{
68-
if (files[i].OptionsForFile.Debug)
69-
Log.WarningLine(diagnostics[j].ToString());
70-
71-
//Print an error if compilation fails.
72-
if (diagnostics[j].Severity == DiagnosticSeverity.Error)
67+
ImmutableArray<Diagnostic> diagnostics = _model.GetDiagnostics();
68+
for (int j = 0; j < diagnostics.Length; j++)
7369
{
74-
if (files[i].OptionsForFile.DisableCompilationErrors == false)
70+
if (diagnostics[j].Severity == DiagnosticSeverity.Hidden && files[i].OptionsForFile.Debug == true)
71+
Log.WriteLine(diagnostics[j].ToString());
72+
else if (diagnostics[j].Severity == DiagnosticSeverity.Info && files[i].OptionsForFile.Debug == true)
73+
Log.InfoLine(diagnostics[j].ToString());
74+
else if (diagnostics[j].Severity == DiagnosticSeverity.Warning)
75+
Log.WarningLine(diagnostics[i].ToString());
76+
else if (diagnostics[j].Severity == DiagnosticSeverity.Error)
7577
Log.ErrorLine(diagnostics[i].ToString());
7678
}
7779
}
78-
80+
7981
SyntaxNode _root = trees[i].GetRoot();
8082

8183
WithSemanticRewriter _withSemanticRewriter = new(_model, files[i].OptionsForFile);

CSharpToJavaScript/CSTOJSOptions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ public class CSTOJSOptions
2222
/// Default: <c>false</c>
2323
/// </value>
2424
public bool Debug { get; set; } = false;
25+
2526
/// <summary>
26-
/// Useful for the tests.
27+
/// Getting diagnostics is expensive.
28+
/// Enable if you want faster translation.
2729
/// </summary>
2830
/// <remarks>
2931
/// <blockquote class="NOTE"><h5>NOTE</h5>
3032
/// <para>
31-
/// cstojs_options.xml: &lt;Option DisableCompilationErrors=&quot;false&quot; /&gt;
33+
/// cstojs_options.xml: &lt;Option DisableDiagnostics=&quot;false&quot; /&gt;
3234
/// </para>
3335
/// </blockquote>
36+
/// <para><see href = "https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.semanticmodel.getdiagnostics?view=roslyn-dotnet-4.13.0#remarks"><em>See remarks.</em></see></para>
3437
/// </remarks>
3538
/// <value>
3639
/// Default: <c>false</c>
3740
/// </value>
38-
public bool DisableCompilationErrors { get; set; } = false;
41+
public bool DisableDiagnostics { get; set; } = false;
3942

4043
/// <summary>
4144
/// Self-explanatory, Use <c>var</c> over <c>let</c>.
@@ -163,7 +166,7 @@ public class CSTOJSOptions
163166
/// Default: <c>new()</c>
164167
/// </value>
165168
public StringBuilder AddSBAtTheBottom { get; set; } = new();
166-
169+
167170
/// <summary>
168171
/// Creates new default options. See <see cref="CSTOJSOptions" />.
169172
/// </summary>

0 commit comments

Comments
 (0)