@@ -97,48 +97,51 @@ private static string GetIfExistsString(PSObject psobj, string memberName)
9797 internal static ScriptFileMarker FromDiagnosticRecord ( PSObject psObject )
9898 {
9999 Validate . IsNotNull ( "psObject" , psObject ) ;
100- // return new ScriptFileMarker
101- // {
102- // Message = GetIfExistsString(psObject, "Message"),
103- // //Level = GetMarkerLevelFromDiagnosticSeverity(GetIfExistsString(psObject, "Severity")),
104- // Level = GetMarkerLevelFromDiagnosticSeverity("Warning"),
105- // ScriptRegion = ScriptRegion.Create((IScriptExtent)psObject.Members["Extent"].Value)
100+ MarkerCorrection correction = null ;
106101
107- // Validate.IsNotNull("diagnosticRecord", diagnosticRecord);
102+ // make sure psobject is of type DiagnosticRecord
103+ if ( ! psObject . TypeNames . Contains (
104+ "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" ,
105+ StringComparer . OrdinalIgnoreCase ) )
106+ {
107+ throw new ArgumentException ( "Input PSObject must of DiagnosticRecord type." ) ;
108+ }
108109
109- MarkerCorrection correction = null ;
110+ // casting psobject to dynamic allows us to access
111+ // the diagnostic record's properties directly i.e. <instance>.<propertyName>
112+ // without having to go through PSObject's Members property.
113+ var diagnosticRecord = psObject as dynamic ;
114+ string ruleName = diagnosticRecord . RuleName as string ;
110115
111- if ( psObject . SuggestedCorrections != null )
116+ if ( diagnosticRecord . SuggestedCorrections != null )
112117 {
113- IEnumerable < ScriptRegion > editRegions =
114- psObject
115- . SuggestedCorrections
116- . Select (
117- c => new ScriptRegion
118- {
119- File = psObject . Extent . File ,
120- Text = c . Text ,
121- StartLineNumber = c . StartLineNumber ,
122- StartColumnNumber = c . StartColumnNumber ,
123- EndLineNumber = c . EndLineNumber ,
124- EndColumnNumber = c . EndColumnNumber
125- } ) ;
118+ var suggestedCorrections = diagnosticRecord . SuggestedCorrections as dynamic ;
119+ List < ScriptRegion > editRegions = new List < ScriptRegion > ( ) ;
120+ foreach ( var suggestedCorrection in suggestedCorrections )
121+ {
122+ editRegions . Add ( new ScriptRegion
123+ {
124+ File = diagnosticRecord . ScriptPath ,
125+ Text = suggestedCorrection . Text ,
126+ StartLineNumber = suggestedCorrection . StartLineNumber ,
127+ StartColumnNumber = suggestedCorrection . StartColumnNumber ,
128+ EndLineNumber = suggestedCorrection . EndLineNumber ,
129+ EndColumnNumber = suggestedCorrection . EndColumnNumber
130+ } ) ;
131+ }
126132
127133 correction = new MarkerCorrection
128134 {
129- Name =
130- psObject . SuggestedCorrections . Select ( e => e . Description ) . FirstOrDefault ( )
131- ?? psObject . Message ,
132-
135+ Name = diagnosticRecord . Message ,
133136 Edits = editRegions . ToArray ( )
134137 } ;
135138 }
136139
137140 return new ScriptFileMarker
138141 {
139- Message = psObject . Message ,
140- Level = GetMarkerLevelFromDiagnosticSeverity ( psObject . Severity ) ,
141- ScriptRegion = ScriptRegion . Create ( psObject . Extent ) ,
142+ Message = diagnosticRecord . Message as string ,
143+ Level = GetMarkerLevelFromDiagnosticSeverity ( ( diagnosticRecord . Severity as Enum ) . ToString ( ) ) ,
144+ ScriptRegion = ScriptRegion . Create ( diagnosticRecord . Extent as IScriptExtent ) ,
142145 Correction = correction
143146 } ;
144147 }
0 commit comments