-
Notifications
You must be signed in to change notification settings - Fork 237
Add new ParseError level to ScriptFileMarkerLevel and only have it send parse errors #888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
b8f56fa
4937f56
a81d4ae
0e05c92
a57fbd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ public class AnalysisService : IDisposable | |
/// <summary> | ||
/// An empty script marker result to return when no script markers can be returned. | ||
/// </summary> | ||
private static readonly ScriptFileMarker[] s_emptyScriptMarkerResult = new ScriptFileMarker[0]; | ||
private static readonly List<ScriptFileMarker> s_emptyScriptMarkerResult = new List<ScriptFileMarker>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is supposed to be a readonly empty value, it can't be a Otherwise, if we need to return a mutable collection, we should either:
If we return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point. I've switched to just some new lists. |
||
|
||
private static readonly string[] s_emptyGetRuleResult = new string[0]; | ||
|
||
|
@@ -266,7 +266,7 @@ public static Hashtable GetPSSASettingsHashtable(IDictionary<string, Hashtable> | |
/// </summary> | ||
/// <param name="file">The ScriptFile which will be analyzed for semantic markers.</param> | ||
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns> | ||
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file) | ||
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync(ScriptFile file) | ||
{ | ||
return await GetSemanticMarkersAsync<string>(file, ActiveRules, SettingsPath); | ||
} | ||
|
@@ -277,7 +277,7 @@ public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file) | |
/// <param name="file">The ScriptFile to be analyzed.</param> | ||
/// <param name="settings">ScriptAnalyzer settings</param> | ||
/// <returns></returns> | ||
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file, Hashtable settings) | ||
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync(ScriptFile file, Hashtable settings) | ||
{ | ||
return await GetSemanticMarkersAsync<Hashtable>(file, null, settings); | ||
} | ||
|
@@ -288,7 +288,7 @@ public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync(ScriptFile file, H | |
/// <param name="scriptContent">The script content to be analyzed.</param> | ||
/// <param name="settings">ScriptAnalyzer settings</param> | ||
/// <returns></returns> | ||
public async Task<ScriptFileMarker[]> GetSemanticMarkersAsync( | ||
public async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync( | ||
string scriptContent, | ||
Hashtable settings) | ||
{ | ||
|
@@ -379,7 +379,7 @@ public async Task<string> FormatAsync( | |
|
||
#region Private Methods | ||
|
||
private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>( | ||
private async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync<TSettings>( | ||
ScriptFile file, | ||
string[] rules, | ||
TSettings settings) where TSettings : class | ||
|
@@ -398,7 +398,7 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>( | |
} | ||
} | ||
|
||
private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>( | ||
private async Task<List<ScriptFileMarker>> GetSemanticMarkersAsync<TSettings>( | ||
string scriptContent, | ||
string[] rules, | ||
TSettings settings) where TSettings : class | ||
|
@@ -407,7 +407,7 @@ private async Task<ScriptFileMarker[]> GetSemanticMarkersAsync<TSettings>( | |
&& (rules != null || settings != null)) | ||
{ | ||
var scriptFileMarkers = await GetDiagnosticRecordsAsync(scriptContent, rules, settings); | ||
return scriptFileMarkers.Select(ScriptFileMarker.FromDiagnosticRecord).ToArray(); | ||
return scriptFileMarkers.Select(ScriptFileMarker.FromDiagnosticRecord).ToList(); | ||
} | ||
else | ||
{ | ||
|
@@ -514,7 +514,9 @@ private async Task<PSObject[]> GetDiagnosticRecordsAsync<TSettings>( | |
new Dictionary<string, object> | ||
{ | ||
{ "ScriptDefinition", scriptContent }, | ||
{ settingParameter, settingArgument } | ||
{ settingParameter, settingArgument }, | ||
// We ignore ParseErrors from PSSA because we already send them when we parse the file. | ||
{ "Severity", new [] { ScriptFileMarkerLevel.Error, ScriptFileMarkerLevel.Information, ScriptFileMarkerLevel.Warning }} | ||
}); | ||
|
||
diagnosticRecords = result?.Output; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,20 +33,22 @@ public class MarkerCorrection | |
/// </summary> | ||
public enum ScriptFileMarkerLevel | ||
{ | ||
/// <summary> | ||
/// The marker represents an informational message. | ||
/// </summary> | ||
Information = 0, | ||
|
||
/// <summary> | ||
/// The marker represents a warning message. | ||
/// </summary> | ||
Warning, | ||
|
||
/// <summary> | ||
/// The marker represents an error message. | ||
/// </summary> | ||
Error | ||
/// <summary> | ||
/// Information: This warning is trivial, but may be useful. They are recommended by PowerShell best practice. | ||
/// </summary> | ||
Information = 0, | ||
/// <summary> | ||
/// WARNING: This warning may cause a problem or does not follow PowerShell's recommended guidelines. | ||
/// </summary> | ||
Warning = 1, | ||
/// <summary> | ||
/// ERROR: This warning is likely to cause a problem or does not follow PowerShell's required guidelines. | ||
/// </summary> | ||
Error = 2, | ||
/// <summary> | ||
/// ERROR: This diagnostic is caused by an actual parsing error, and is generated only by the engine. | ||
/// </summary> | ||
ParseError = 3 | ||
}; | ||
|
||
/// <summary> | ||
|
@@ -62,7 +64,7 @@ public class ScriptFileMarker | |
/// Gets or sets the marker's message string. | ||
/// </summary> | ||
public string Message { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the ruleName associated with this marker. | ||
/// </summary> | ||
|
@@ -162,36 +164,27 @@ internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject) | |
}; | ||
} | ||
|
||
string severity = diagnosticRecord.Severity.ToString(); | ||
if(!Enum.TryParse(severity, out ScriptFileMarkerLevel level)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit, space between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm glad I wasn't the only one that felt like this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch - fixed. |
||
{ | ||
throw new ArgumentException( | ||
string.Format( | ||
"The provided DiagnosticSeverity value '{0}' is unknown.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit: Using string interpolation ( |
||
severity), | ||
"diagnosticSeverity"); | ||
} | ||
|
||
return new ScriptFileMarker | ||
{ | ||
Message = $"{diagnosticRecord.Message as string}", | ||
RuleName = $"{diagnosticRecord.RuleName as string}", | ||
Level = GetMarkerLevelFromDiagnosticSeverity((diagnosticRecord.Severity as Enum).ToString()), | ||
Level = level, | ||
ScriptRegion = ScriptRegion.Create(diagnosticRecord.Extent as IScriptExtent), | ||
Correction = correction, | ||
Source = "PSScriptAnalyzer" | ||
}; | ||
} | ||
|
||
private static ScriptFileMarkerLevel GetMarkerLevelFromDiagnosticSeverity( | ||
string diagnosticSeverity) | ||
{ | ||
switch (diagnosticSeverity) | ||
{ | ||
case "Information": | ||
return ScriptFileMarkerLevel.Information; | ||
case "Warning": | ||
return ScriptFileMarkerLevel.Warning; | ||
case "Error": | ||
return ScriptFileMarkerLevel.Error; | ||
default: | ||
throw new ArgumentException( | ||
string.Format( | ||
"The provided DiagnosticSeverity value '{0}' is unknown.", | ||
diagnosticSeverity), | ||
"diagnosticSeverity"); | ||
} | ||
} | ||
#endregion | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to modify the SyntaxMarkers collection in ScriptFile by adding semantic markers? Maybe we should have two collections in ScriptFile (SyntaxMarkers and SemanticMarkers) or perhaps rename the property to DiagnosticMarkers (or something like that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with the rename since the syntax markers and semantic markers are both treated the same when they get sent to vscode.