55using System . Management . Automation ;
66using System . Management . Automation . Language ;
77using System . Management . Automation . Runspaces ;
8+ using System . Globalization ;
89using Microsoft . Windows . Powershell . ScriptAnalyzer . Generic ;
910
1011namespace Microsoft . Windows . Powershell . ScriptAnalyzer
@@ -740,6 +741,7 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
740741 int ruleSuppressionIndex = 0 ;
741742 DiagnosticRecord record = diagnostics . First ( ) ;
742743 RuleSuppression ruleSuppression = ruleSuppressions . First ( ) ;
744+ int suppressionCount = 0 ;
743745
744746 while ( recordIndex < diagnostics . Count )
745747 {
@@ -753,6 +755,7 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
753755 }
754756
755757 ruleSuppression = ruleSuppressions [ ruleSuppressionIndex ] ;
758+ suppressionCount = 0 ;
756759
757760 continue ;
758761 }
@@ -767,12 +770,21 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
767770 {
768771 ruleSuppressionIndex += 1 ;
769772
773+ // If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
774+ if ( ! String . IsNullOrWhiteSpace ( ruleSuppression . RuleSuppressionID ) && suppressionCount == 0 )
775+ {
776+ ruleSuppression . Error = String . Format ( CultureInfo . CurrentCulture , Strings . RuleSuppressionErrorFormat , ruleSuppression . StartOffset ,
777+ System . IO . Path . GetFileName ( record . Extent . File ) , String . Format ( Strings . RuleSuppressionIDError , ruleSuppression . RuleSuppressionID ) ) ;
778+ Helper . Instance . MyCmdlet . WriteError ( new ErrorRecord ( new ArgumentException ( ruleSuppression . Error ) , ruleSuppression . Error , ErrorCategory . InvalidArgument , ruleSuppression ) ) ;
779+ }
780+
770781 if ( ruleSuppressionIndex == ruleSuppressions . Count )
771782 {
772783 break ;
773784 }
774785
775786 ruleSuppression = ruleSuppressions [ ruleSuppressionIndex ] ;
787+ suppressionCount = 0 ;
776788
777789 continue ;
778790 }
@@ -785,15 +797,25 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
785797 && ! string . Equals ( ruleSuppression . RuleSuppressionID , record . RuleSuppressionID , StringComparison . OrdinalIgnoreCase ) )
786798 {
787799 results . Add ( record ) ;
800+ suppressionCount -= 1 ;
788801 }
789802 // otherwise, we ignore the record, move on to the next.
790803 }
791804
792805 // important assumption: this point is reached only if we want to move to the next record
793806 recordIndex += 1 ;
807+ suppressionCount += 1 ;
794808
795809 if ( recordIndex == diagnostics . Count )
796810 {
811+ // If we cannot found any error but the rulesuppression has a rulesuppressionid then it must be used wrongly
812+ if ( ! String . IsNullOrWhiteSpace ( ruleSuppression . RuleSuppressionID ) && suppressionCount == 0 )
813+ {
814+ ruleSuppression . Error = String . Format ( CultureInfo . CurrentCulture , Strings . RuleSuppressionErrorFormat , ruleSuppression . StartOffset ,
815+ System . IO . Path . GetFileName ( record . Extent . File ) , String . Format ( Strings . RuleSuppressionIDError , ruleSuppression . RuleSuppressionID ) ) ;
816+ Helper . Instance . MyCmdlet . WriteError ( new ErrorRecord ( new ArgumentException ( ruleSuppression . Error ) , ruleSuppression . Error , ErrorCategory . InvalidArgument , ruleSuppression ) ) ;
817+ }
818+
797819 break ;
798820 }
799821
0 commit comments