@@ -1320,6 +1320,64 @@ private IEnumerable<DiagnosticRecord> AnalyzeFile(string filePath)
1320
1320
return this . AnalyzeSyntaxTree ( scriptAst , scriptTokens , filePath ) ;
1321
1321
}
1322
1322
1323
+ private bool IsSeverityAllowed ( IEnumerable < uint > allowedSeverities , IRule rule )
1324
+ {
1325
+ return severity == null
1326
+ || ( allowedSeverities != null
1327
+ && rule != null
1328
+ && HasGetSeverity ( rule )
1329
+ && allowedSeverities . Contains ( ( uint ) rule . GetSeverity ( ) ) ) ;
1330
+ }
1331
+
1332
+ IEnumerable < uint > GetAllowedSeveritiesInInt ( )
1333
+ {
1334
+ return severity != null
1335
+ ? severity . Select ( item => ( uint ) Enum . Parse ( typeof ( DiagnosticSeverity ) , item , true ) )
1336
+ : null ;
1337
+ }
1338
+
1339
+ bool HasMethod < T > ( T obj , string methodName )
1340
+ {
1341
+ var type = obj . GetType ( ) ;
1342
+ return type . GetMethod ( methodName ) != null ;
1343
+ }
1344
+
1345
+ bool HasGetSeverity < T > ( T obj )
1346
+ {
1347
+ return HasMethod < T > ( obj , "GetSeverity" ) ;
1348
+ }
1349
+
1350
+ bool IsRuleAllowed ( IRule rule )
1351
+ {
1352
+ IEnumerable < uint > allowedSeverities = GetAllowedSeveritiesInInt ( ) ;
1353
+ bool includeRegexMatch = false ;
1354
+ bool excludeRegexMatch = false ;
1355
+ foreach ( Regex include in includeRegexList )
1356
+ {
1357
+ if ( include . IsMatch ( rule . GetName ( ) ) )
1358
+ {
1359
+ includeRegexMatch = true ;
1360
+ break ;
1361
+ }
1362
+ }
1363
+
1364
+ foreach ( Regex exclude in excludeRegexList )
1365
+ {
1366
+ if ( exclude . IsMatch ( rule . GetName ( ) ) )
1367
+ {
1368
+ excludeRegexMatch = true ;
1369
+ break ;
1370
+ }
1371
+ }
1372
+
1373
+ bool helpRule = String . Equals ( rule . GetName ( ) , "PSUseUTF8EncodingForHelpFile" , StringComparison . OrdinalIgnoreCase ) ;
1374
+ bool includeSeverity = IsSeverityAllowed ( allowedSeverities , rule ) ;
1375
+
1376
+ return ( includeRule == null || includeRegexMatch )
1377
+ && ( excludeRule == null || ! excludeRegexMatch )
1378
+ && IsSeverityAllowed ( allowedSeverities , rule ) ;
1379
+ }
1380
+
1323
1381
/// <summary>
1324
1382
/// Analyzes the syntax tree of a script file that has already been parsed.
1325
1383
/// </summary>
@@ -1373,42 +1431,20 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1373
1431
1374
1432
Helper . Instance . Tokens = scriptTokens ;
1375
1433
}
1376
-
1434
+
1377
1435
#region Run ScriptRules
1378
1436
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
1379
1437
string fileName = filePathIsNullOrWhiteSpace ? String . Empty : System . IO . Path . GetFileName ( filePath ) ;
1380
-
1381
1438
if ( this . ScriptRules != null )
1382
1439
{
1383
- var tasks = this . ScriptRules . Select ( scriptRule => Task . Factory . StartNew ( ( ) =>
1384
- {
1385
- bool includeRegexMatch = false ;
1386
- bool excludeRegexMatch = false ;
1440
+ var allowedRules = this . ScriptRules . Where ( IsRuleAllowed ) ;
1387
1441
1388
- foreach ( Regex include in includeRegexList )
1389
- {
1390
- if ( include . IsMatch ( scriptRule . GetName ( ) ) )
1391
- {
1392
- includeRegexMatch = true ;
1393
- break ;
1394
- }
1395
- }
1396
-
1397
- foreach ( Regex exclude in excludeRegexList )
1398
- {
1399
- if ( exclude . IsMatch ( scriptRule . GetName ( ) ) )
1400
- {
1401
- excludeRegexMatch = true ;
1402
- break ;
1403
- }
1404
- }
1405
-
1406
- bool helpRule = String . Equals ( scriptRule . GetName ( ) , "PSUseUTF8EncodingForHelpFile" , StringComparison . OrdinalIgnoreCase ) ;
1407
-
1408
- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
1442
+ if ( allowedRules . Any ( ) )
1443
+ {
1444
+ var tasks = allowedRules . Select ( scriptRule => Task . Factory . StartNew ( ( ) =>
1409
1445
{
1446
+ bool helpRule = String . Equals ( scriptRule . GetName ( ) , "PSUseUTF8EncodingForHelpFile" , StringComparison . OrdinalIgnoreCase ) ;
1410
1447
List < object > result = new List < object > ( ) ;
1411
-
1412
1448
result . Add ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , scriptRule . GetName ( ) ) ) ;
1413
1449
1414
1450
// Ensure that any unhandled errors from Rules are converted to non-terminating errors
@@ -1442,26 +1478,26 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1442
1478
}
1443
1479
1444
1480
verboseOrErrors . Add ( result ) ;
1445
- }
1446
- } ) ) ;
1481
+ } ) ) ;
1447
1482
1448
- Task . Factory . ContinueWhenAll ( tasks . ToArray ( ) , t => verboseOrErrors . CompleteAdding ( ) ) ;
1483
+ Task . Factory . ContinueWhenAll ( tasks . ToArray ( ) , t => verboseOrErrors . CompleteAdding ( ) ) ;
1449
1484
1450
- while ( ! verboseOrErrors . IsCompleted )
1451
- {
1452
- List < object > data = null ;
1453
- try
1485
+ while ( ! verboseOrErrors . IsCompleted )
1454
1486
{
1455
- data = verboseOrErrors . Take ( ) ;
1456
- }
1457
- catch ( InvalidOperationException ) { }
1487
+ List < object > data = null ;
1488
+ try
1489
+ {
1490
+ data = verboseOrErrors . Take ( ) ;
1491
+ }
1492
+ catch ( InvalidOperationException ) { }
1458
1493
1459
- if ( data != null )
1460
- {
1461
- this . outputWriter . WriteVerbose ( data [ 0 ] as string ) ;
1462
- if ( data . Count == 2 )
1494
+ if ( data != null )
1463
1495
{
1464
- this . outputWriter . WriteError ( data [ 1 ] as ErrorRecord ) ;
1496
+ this . outputWriter . WriteVerbose ( data [ 0 ] as string ) ;
1497
+ if ( data . Count == 2 )
1498
+ {
1499
+ this . outputWriter . WriteError ( data [ 1 ] as ErrorRecord ) ;
1500
+ }
1465
1501
}
1466
1502
}
1467
1503
}
@@ -1475,25 +1511,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1475
1511
{
1476
1512
foreach ( ITokenRule tokenRule in this . TokenRules )
1477
1513
{
1478
- bool includeRegexMatch = false ;
1479
- bool excludeRegexMatch = false ;
1480
- foreach ( Regex include in includeRegexList )
1481
- {
1482
- if ( include . IsMatch ( tokenRule . GetName ( ) ) )
1483
- {
1484
- includeRegexMatch = true ;
1485
- break ;
1486
- }
1487
- }
1488
- foreach ( Regex exclude in excludeRegexList )
1489
- {
1490
- if ( exclude . IsMatch ( tokenRule . GetName ( ) ) )
1491
- {
1492
- excludeRegexMatch = true ;
1493
- break ;
1494
- }
1495
- }
1496
- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
1514
+ if ( IsRuleAllowed ( tokenRule ) )
1497
1515
{
1498
1516
this . outputWriter . WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , tokenRule . GetName ( ) ) ) ;
1499
1517
@@ -1592,24 +1610,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1592
1610
// Run all DSC Rules
1593
1611
foreach ( IDSCResourceRule dscResourceRule in this . DSCResourceRules )
1594
1612
{
1595
- bool includeRegexMatch = false ;
1596
- bool excludeRegexMatch = false ;
1597
- foreach ( Regex include in includeRegexList )
1598
- {
1599
- if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
1600
- {
1601
- includeRegexMatch = true ;
1602
- break ;
1603
- }
1604
- }
1605
- foreach ( Regex exclude in excludeRegexList )
1606
- {
1607
- if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
1608
- {
1609
- excludeRegexMatch = true ;
1610
- }
1611
- }
1612
- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
1613
+ if ( IsRuleAllowed ( dscResourceRule ) )
1613
1614
{
1614
1615
this . outputWriter . WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
1615
1616
@@ -1646,8 +1647,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1646
1647
1647
1648
foreach ( ExternalRule exRule in this . ExternalRules )
1648
1649
{
1649
- if ( ( includeRule == null || includeRule . Contains ( exRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) &&
1650
- ( excludeRule == null || ! excludeRule . Contains ( exRule . GetName ( ) , StringComparer . OrdinalIgnoreCase ) ) )
1650
+ if ( IsRuleAllowed ( exRule ) )
1651
1651
{
1652
1652
string ruleName = string . Format ( CultureInfo . CurrentCulture , "{0}\\ {1}" , exRule . GetSourceName ( ) , exRule . GetName ( ) ) ;
1653
1653
this . outputWriter . WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , ruleName ) ) ;
@@ -1676,15 +1676,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeSyntaxTree(
1676
1676
// Need to reverse the concurrentbag to ensure that results are sorted in the increasing order of line numbers
1677
1677
IEnumerable < DiagnosticRecord > diagnosticsList = diagnostics . Reverse ( ) ;
1678
1678
1679
- if ( severity != null )
1680
- {
1681
- var diagSeverity = severity . Select ( item => Enum . Parse ( typeof ( DiagnosticSeverity ) , item , true ) ) ;
1682
- if ( diagSeverity . Count ( ) != 0 )
1683
- {
1684
- diagnosticsList = diagnostics . Where ( item => diagSeverity . Contains ( item . Severity ) ) ;
1685
- }
1686
- }
1687
-
1688
1679
return this . suppressedOnly ?
1689
1680
suppressed . OfType < DiagnosticRecord > ( ) :
1690
1681
diagnosticsList ;
0 commit comments