@@ -129,8 +129,8 @@ public SwitchParameter SuppressedOnly
129129 #region Private Members
130130
131131 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 ;
134134
135135 #endregion
136136
@@ -163,9 +163,9 @@ protected override void BeginProcessing()
163163 }
164164 else
165165 {
166- validationResults . Add ( "InvalidPaths" , new List < string > ( ) ) ;
166+ validationResults . Add ( "InvalidPaths" , new List < string > ( ) ) ;
167167 validationResults . Add ( "ValidModPaths" , new List < string > ( ) ) ;
168- validationResults . Add ( "ValidDllPaths" , new List < string > ( ) ) ;
168+ validationResults . Add ( "ValidDllPaths" , new List < string > ( ) ) ;
169169 }
170170
171171 #endregion
@@ -174,7 +174,7 @@ protected override void BeginProcessing()
174174
175175 try
176176 {
177- if ( validationResults [ "ValidDllPaths" ] . Count == 0 &&
177+ if ( validationResults [ "ValidDllPaths" ] . Count == 0 &&
178178 validationResults [ "ValidModPaths" ] . Count == 0 )
179179 {
180180 ScriptAnalyzer . Instance . Initialize ( ) ;
@@ -185,7 +185,7 @@ protected override void BeginProcessing()
185185 }
186186 }
187187 catch ( Exception ex )
188- {
188+ {
189189 ThrowTerminatingError ( new ErrorRecord ( ex , ex . HResult . ToString ( "X" , CultureInfo . CurrentCulture ) ,
190190 ErrorCategory . NotSpecified , this ) ) ;
191191 }
@@ -228,8 +228,8 @@ private void ProcessPath(string path)
228228
229229 if ( path == null )
230230 {
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 ) ,
233233 ErrorCategory . InvalidArgument , this ) ) ;
234234 }
235235
@@ -315,11 +315,11 @@ private void AnalyzeFile(string filePath)
315315 else
316316 {
317317 ThrowTerminatingError ( new ErrorRecord ( new FileNotFoundException ( ) ,
318- string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
318+ string . Format ( CultureInfo . CurrentCulture , Strings . InvalidPath , filePath ) ,
319319 ErrorCategory . InvalidArgument , filePath ) ) ;
320320 }
321321
322- if ( errors != null && errors . Length > 0 )
322+ if ( errors != null && errors . Length > 0 )
323323 {
324324 foreach ( ParseError error in errors )
325325 {
@@ -362,7 +362,7 @@ private void AnalyzeFile(string filePath)
362362 #region Run ScriptRules
363363 //Trim down to the leaf element of the filePath and pass it to Diagnostic Record
364364 string fileName = System . IO . Path . GetFileName ( filePath ) ;
365-
365+
366366 if ( ScriptAnalyzer . Instance . ScriptRules != null )
367367 {
368368 foreach ( IScriptRule scriptRule in ScriptAnalyzer . Instance . ScriptRules )
@@ -433,7 +433,7 @@ private void AnalyzeFile(string filePath)
433433 break ;
434434 }
435435 }
436- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
436+ if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || ! excludeRegexMatch ) )
437437 {
438438 WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , tokenRule . GetName ( ) ) ) ;
439439
@@ -448,7 +448,7 @@ private void AnalyzeFile(string filePath)
448448 catch ( Exception tokenRuleException )
449449 {
450450 WriteError ( new ErrorRecord ( tokenRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , fileName ) ) ;
451- }
451+ }
452452 }
453453 }
454454 }
@@ -458,46 +458,50 @@ private void AnalyzeFile(string filePath)
458458 #region DSC Resource Rules
459459 if ( ScriptAnalyzer . Instance . DSCResourceRules != null )
460460 {
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 ) )
463463 {
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 )
468466 {
469- if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
467+ bool includeRegexMatch = false ;
468+ bool excludeRegexMatch = false ;
469+
470+ foreach ( Regex include in includeRegexList )
470471 {
471- includeRegexMatch = true ;
472- break ;
472+ if ( include . IsMatch ( dscResourceRule . GetName ( ) ) )
473+ {
474+ includeRegexMatch = true ;
475+ break ;
476+ }
473477 }
474- }
475478
476- foreach ( Regex exclude in excludeRegexList )
477- {
478- if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
479+ foreach ( Regex exclude in excludeRegexList )
479480 {
480- excludeRegexMatch = true ;
481- break ;
481+ if ( exclude . IsMatch ( dscResourceRule . GetName ( ) ) )
482+ {
483+ excludeRegexMatch = true ;
484+ break ;
485+ }
482486 }
483- }
484-
485- if ( ( includeRule == null || includeRegexMatch ) && ( excludeRule == null || excludeRegexMatch ) )
486- {
487- WriteVerbose ( string . Format ( CultureInfo . CurrentCulture , Strings . VerboseRunningMessage , dscResourceRule . GetName ( ) ) ) ;
488487
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 ) )
492489 {
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+ }
496504 }
497- catch ( Exception dscResourceRuleException )
498- {
499- WriteError ( new ErrorRecord ( dscResourceRuleException , Strings . RuleErrorMessage , ErrorCategory . InvalidOperation , filePath ) ) ;
500- }
501505 }
502506 }
503507
@@ -543,7 +547,7 @@ private void AnalyzeFile(string filePath)
543547 }
544548 }
545549
546- }
550+ }
547551 }
548552 #endregion
549553
@@ -607,4 +611,4 @@ private void AnalyzeFile(string filePath)
607611
608612 #endregion
609613 }
610- }
614+ }
0 commit comments