@@ -129,8 +129,8 @@ public SwitchParameter SuppressedOnly
129
129
#region Private Members
130
130
131
131
Dictionary < string , List < string > > validationResults = new Dictionary < string , List < string > > ( ) ;
132
- private ScriptBlockAst ast = null ;
133
- private IEnumerable < IRule > rules = null ;
132
+ private ScriptBlockAst ast = null ;
133
+ private IEnumerable < IRule > rules = null ;
134
134
135
135
#endregion
136
136
@@ -163,9 +163,9 @@ protected override void BeginProcessing()
163
163
}
164
164
else
165
165
{
166
- validationResults . Add ( "InvalidPaths" , new List < string > ( ) ) ;
166
+ validationResults . Add ( "InvalidPaths" , new List < string > ( ) ) ;
167
167
validationResults . Add ( "ValidModPaths" , new List < string > ( ) ) ;
168
- validationResults . Add ( "ValidDllPaths" , new List < string > ( ) ) ;
168
+ validationResults . Add ( "ValidDllPaths" , new List < string > ( ) ) ;
169
169
}
170
170
171
171
#endregion
@@ -174,7 +174,7 @@ protected override void BeginProcessing()
174
174
175
175
try
176
176
{
177
- if ( validationResults [ "ValidDllPaths" ] . Count == 0 &&
177
+ if ( validationResults [ "ValidDllPaths" ] . Count == 0 &&
178
178
validationResults [ "ValidModPaths" ] . Count == 0 )
179
179
{
180
180
ScriptAnalyzer . Instance . Initialize ( ) ;
@@ -185,7 +185,7 @@ protected override void BeginProcessing()
185
185
}
186
186
}
187
187
catch ( Exception ex )
188
- {
188
+ {
189
189
ThrowTerminatingError ( new ErrorRecord ( ex , ex . HResult . ToString ( "X" , CultureInfo . CurrentCulture ) ,
190
190
ErrorCategory . NotSpecified , this ) ) ;
191
191
}
@@ -228,8 +228,8 @@ private void ProcessPath(string path)
228
228
229
229
if ( path == null )
230
230
{
231
- ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
232
- string . Format ( CultureInfo . CurrentCulture , Strings . FileNotFound , path ) ,
231
+ ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
232
+ string . Format ( CultureInfo . CurrentCulture , Strings . FileNotFound , path ) ,
233
233
ErrorCategory . InvalidArgument , this ) ) ;
234
234
}
235
235
@@ -315,11 +315,11 @@ private void AnalyzeFile(string filePath)
315
315
else
316
316
{
317
317
ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
318
- string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
318
+ string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
319
319
ErrorCategory . InvalidArgument , filePath ) ) ;
320
320
}
321
321
322
- if ( errors != null && errors . Length > 0 )
322
+ if ( errors != null && errors . Length > 0 )
323
323
{
324
324
foreach ( ParseError error in errors )
325
325
{
@@ -362,7 +362,7 @@ private void AnalyzeFile(string filePath)
362
362
#region Run ScriptRules
363
363
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
364
364
string fileName = System . IO . Path . GetFileName ( filePath ) ;
365
-
365
+
366
366
if ( ScriptAnalyzer . Instance . ScriptRules != null )
367
367
{
368
368
foreach ( IScriptRule scriptRule in ScriptAnalyzer . Instance . ScriptRules )
@@ -433,7 +433,7 @@ private void AnalyzeFile(string filePath)
433
433
break ;
434
434
}
435
435
}
436
- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
436
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
437
437
{
438
438
WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , tokenRule . GetName ( ) ) ) ;
439
439
@@ -448,7 +448,7 @@ private void AnalyzeFile(string filePath)
448
448
catch ( Exception tokenRuleException )
449
449
{
450
450
WriteError ( new ErrorRecord ( tokenRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , fileName ) ) ;
451
- }
451
+ }
452
452
}
453
453
}
454
454
}
@@ -458,46 +458,50 @@ private void AnalyzeFile(string filePath)
458
458
#region DSC Resource Rules
459
459
if ( ScriptAnalyzer . Instance . DSCResourceRules != null )
460
460
{
461
- // Run DSC Class rule
462
- foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
461
+ // Invoke AnalyzeDSCClass only if the ast is a class based resource
462
+ if ( Helper . Instance . IsDscResourceClassBased ( ast ) )
463
463
{
464
- bool includeRegexMatch = false ;
465
- bool excludeRegexMatch = false ;
466
-
467
- foreach ( Regex include in includeRegexList )
464
+ // Run DSC Class rule
465
+ foreach ( IDSCResourceRule dscResourceRule in ScriptAnalyzer . Instance . DSCResourceRules )
468
466
{
469
- if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
467
+ bool includeRegexMatch = false ;
468
+ bool excludeRegexMatch = false ;
469
+
470
+ foreach ( Regex include in includeRegexList )
470
471
{
471
- includeRegexMatch = true ;
472
- break ;
472
+ if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
473
+ {
474
+ includeRegexMatch = true ;
475
+ break ;
476
+ }
473
477
}
474
- }
475
478
476
- foreach ( Regex exclude in excludeRegexList )
477
- {
478
- if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
479
+ foreach ( Regex exclude in excludeRegexList )
479
480
{
480
- excludeRegexMatch = true ;
481
- break ;
481
+ if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
482
+ {
483
+ excludeRegexMatch = true ;
484
+ break ;
485
+ }
482
486
}
483
- }
484
-
485
- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || excludeRegexMatch ) )
486
- {
487
- WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
488
487
489
- // Ensure that any unhandled errors from Rules are converted to non-terminating errors
490
- // We want the Engine to continue functioning even if one or more Rules throws an exception
491
- try
488
+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || excludeRegexMatch ) )
492
489
{
493
- var records = Helper . Instance . SuppressRule ( dscResourceRule . GetName ( ) , ruleSuppressions , dscResourceRule . AnalyzeDSCClass ( ast , filePath ) . ToList ( ) ) ;
494
- diagnostics . AddRange ( records . Item2 ) ;
495
- suppressed . AddRange ( records . Item1 ) ;
490
+ WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
491
+
492
+ // Ensure that any unhandled errors from Rules are converted to non-terminating errors
493
+ // We want the Engine to continue functioning even if one or more Rules throws an exception
494
+ try
495
+ {
496
+ var records = Helper . Instance . SuppressRule ( dscResourceRule . GetName ( ) , ruleSuppressions , dscResourceRule . AnalyzeDSCClass ( ast , filePath ) . ToList ( ) ) ;
497
+ diagnostics . AddRange ( records . Item2 ) ;
498
+ suppressed . AddRange ( records . Item1 ) ;
499
+ }
500
+ catch ( Exception dscResourceRuleException )
501
+ {
502
+ WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
503
+ }
496
504
}
497
- catch ( Exception dscResourceRuleException )
498
- {
499
- WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
500
- }
501
505
}
502
506
}
503
507
@@ -543,7 +547,7 @@ private void AnalyzeFile(string filePath)
543
547
}
544
548
}
545
549
546
- }
550
+ }
547
551
}
548
552
#endregion
549
553
@@ -607,4 +611,4 @@ private void AnalyzeFile(string filePath)
607
611
608
612
#endregion
609
613
}
610
- }
614
+ }
0 commit comments