@@ -251,7 +251,7 @@ public List<ExternalRule> GetExternalRule(string[] moduleNames)
251
251
string desc = posh . AddScript ( script ) . Invoke ( ) [ 0 ] . ImmediateBaseObject . ToString ( )
252
252
. Replace ( "\r \n " , " " ) . Trim ( ) ;
253
253
254
- rules . Add ( new ExternalRule ( funcInfo . Name , funcInfo . Name , desc , param . ParameterType . FullName ,
254
+ rules . Add ( new ExternalRule ( funcInfo . Name , funcInfo . Name , desc , param . ParameterType . Name ,
255
255
funcInfo . ModuleName , funcInfo . Module . Path ) ) ;
256
256
}
257
257
}
@@ -289,12 +289,12 @@ public IEnumerable<DiagnosticRecord> GetExternalRecord(Ast ast, Token[] token, E
289
289
290
290
// Groups rules by AstType and Tokens.
291
291
Dictionary < string , List < ExternalRule > > astRuleGroups = rules
292
- . Where < ExternalRule > ( item => item . GetParameter ( ) . EndsWith ( "ast" , true , CultureInfo . CurrentCulture ) )
292
+ . Where < ExternalRule > ( item => item . GetParameter ( ) . EndsWith ( "ast" , StringComparison . OrdinalIgnoreCase ) )
293
293
. GroupBy < ExternalRule , string > ( item => item . GetParameter ( ) )
294
294
. ToDictionary ( item => item . Key , item => item . ToList ( ) ) ;
295
295
296
296
Dictionary < string , List < ExternalRule > > tokenRuleGroups = rules
297
- . Where < ExternalRule > ( item => item . GetParameter ( ) . EndsWith ( "token" , true , CultureInfo . CurrentCulture ) )
297
+ . Where < ExternalRule > ( item => item . GetParameter ( ) . EndsWith ( "token" , StringComparison . OrdinalIgnoreCase ) )
298
298
. GroupBy < ExternalRule , string > ( item => item . GetParameter ( ) )
299
299
. ToDictionary ( item => item . Key , item => item . ToList ( ) ) ;
300
300
@@ -337,7 +337,7 @@ public IEnumerable<DiagnosticRecord> GetExternalRecord(Ast ast, Token[] token, E
337
337
{
338
338
// Find all AstTypes that appeared in rule groups.
339
339
IEnumerable < Ast > childAsts = ast . FindAll ( new Func < Ast , bool > ( ( testAst ) =>
340
- ( testAst . GetType ( ) . Name . ToLower ( CultureInfo . CurrentCulture ) == astRuleGroup . Key . ToLower ( CultureInfo . CurrentCulture ) ) ) , false ) ;
340
+ ( String . Equals ( testAst . GetType ( ) . Name , astRuleGroup . Key , StringComparison . OrdinalIgnoreCase ) ) ) , false ) ;
341
341
342
342
foreach ( Ast childAst in childAsts )
343
343
{
@@ -365,52 +365,63 @@ public IEnumerable<DiagnosticRecord> GetExternalRecord(Ast ast, Token[] token, E
365
365
}
366
366
367
367
#endregion
368
-
369
368
#region Collects the results from commands.
370
-
371
- for ( int i = 0 ; i < powerShellCommands . Count ; i ++ )
369
+ List < DiagnosticRecord > diagnostics = new List < DiagnosticRecord > ( ) ;
370
+ try
372
371
{
373
- // EndInvoke will wait for each command to finish, so we will be getting the commands
374
- // in the same order that they have been invoked withy BeginInvoke.
375
- PSDataCollection < PSObject > psobjects = powerShellCommands [ i ] . EndInvoke ( powerShellCommandResults [ i ] ) ;
376
-
377
- foreach ( var psobject in psobjects )
372
+ for ( int i = 0 ; i < powerShellCommands . Count ; i ++ )
378
373
{
379
- DiagnosticSeverity severity ;
380
- IScriptExtent extent ;
381
- string message = string . Empty ;
382
- string ruleName = string . Empty ;
374
+ // EndInvoke will wait for each command to finish, so we will be getting the commands
375
+ // in the same order that they have been invoked withy BeginInvoke.
376
+ PSDataCollection < PSObject > psobjects = powerShellCommands [ i ] . EndInvoke ( powerShellCommandResults [ i ] ) ;
383
377
384
- if ( psobject != null && psobject . ImmediateBaseObject != null )
378
+ foreach ( var psobject in psobjects )
385
379
{
386
- // Because error stream is merged to output stream,
387
- // we need to handle the error records.
388
- if ( psobject . ImmediateBaseObject is ErrorRecord )
389
- {
390
- ErrorRecord record = ( ErrorRecord ) psobject . ImmediateBaseObject ;
391
- command . WriteError ( record ) ;
392
- continue ;
393
- }
380
+ DiagnosticSeverity severity ;
381
+ IScriptExtent extent ;
382
+ string message = string . Empty ;
383
+ string ruleName = string . Empty ;
394
384
395
- // DiagnosticRecord may not be correctly returned from external rule.
396
- try
385
+ if ( psobject != null && psobject . ImmediateBaseObject != null )
397
386
{
398
- Enum . TryParse < DiagnosticSeverity > ( psobject . Properties [ "Severity" ] . Value . ToString ( ) . ToUpper ( ) , out severity ) ;
399
- message = psobject . Properties [ "Message" ] . Value . ToString ( ) ;
400
- extent = ( IScriptExtent ) psobject . Properties [ "Extent" ] . Value ;
401
- ruleName = psobject . Properties [ "RuleName" ] . Value . ToString ( ) ;
387
+ // Because error stream is merged to output stream,
388
+ // we need to handle the error records.
389
+ if ( psobject . ImmediateBaseObject is ErrorRecord )
390
+ {
391
+ ErrorRecord record = ( ErrorRecord ) psobject . ImmediateBaseObject ;
392
+ command . WriteError ( record ) ;
393
+ continue ;
394
+ }
395
+
396
+ // DiagnosticRecord may not be correctly returned from external rule.
397
+ try
398
+ {
399
+ Enum . TryParse < DiagnosticSeverity > ( psobject . Properties [ "Severity" ] . Value . ToString ( ) . ToUpper ( ) , out severity ) ;
400
+ message = psobject . Properties [ "Message" ] . Value . ToString ( ) ;
401
+ extent = ( IScriptExtent ) psobject . Properties [ "Extent" ] . Value ;
402
+ ruleName = psobject . Properties [ "RuleName" ] . Value . ToString ( ) ;
403
+ }
404
+ catch ( Exception ex )
405
+ {
406
+ command . WriteError ( new ErrorRecord ( ex , ex . HResult . ToString ( "X" ) , ErrorCategory . NotSpecified , this ) ) ;
407
+ continue ;
408
+ }
409
+
410
+ if ( ! string . IsNullOrEmpty ( message ) )
411
+ {
412
+ diagnostics . Add ( new DiagnosticRecord ( message , extent , ruleName , severity , null ) ) ;
413
+ }
402
414
}
403
- catch ( Exception ex )
404
- {
405
- command . WriteError ( new ErrorRecord ( ex , ex . HResult . ToString ( "X" ) , ErrorCategory . NotSpecified , this ) ) ;
406
- continue ;
407
- }
408
-
409
- if ( ! string . IsNullOrEmpty ( message ) ) yield return new DiagnosticRecord ( message , extent , ruleName , severity , null ) ;
410
415
}
411
416
}
412
417
}
418
+ //Catch exception where customized defined rules have exceptins when doing invoke
419
+ catch ( Exception ex )
420
+ {
421
+ command . WriteError ( new ErrorRecord ( ex , ex . HResult . ToString ( "X" ) , ErrorCategory . NotSpecified , this ) ) ;
422
+ }
413
423
424
+ return diagnostics ;
414
425
#endregion
415
426
}
416
427
}
0 commit comments