5
5
using System . Management . Automation ;
6
6
using System . Management . Automation . Language ;
7
7
using System . Management . Automation . Runspaces ;
8
+ using System . Globalization ;
8
9
using Microsoft . Windows . Powershell . ScriptAnalyzer . Generic ;
9
10
10
11
namespace Microsoft . Windows . Powershell . ScriptAnalyzer
@@ -740,6 +741,7 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
740
741
int ruleSuppressionIndex = 0 ;
741
742
DiagnosticRecord record = diagnostics . First ( ) ;
742
743
RuleSuppression ruleSuppression = ruleSuppressions . First ( ) ;
744
+ int suppressionCount = 0 ;
743
745
744
746
while ( recordIndex < diagnostics . Count )
745
747
{
@@ -753,6 +755,7 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
753
755
}
754
756
755
757
ruleSuppression = ruleSuppressions [ ruleSuppressionIndex ] ;
758
+ suppressionCount = 0 ;
756
759
757
760
continue ;
758
761
}
@@ -767,12 +770,21 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
767
770
{
768
771
ruleSuppressionIndex += 1 ;
769
772
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
+
770
781
if ( ruleSuppressionIndex == ruleSuppressions . Count )
771
782
{
772
783
break ;
773
784
}
774
785
775
786
ruleSuppression = ruleSuppressions [ ruleSuppressionIndex ] ;
787
+ suppressionCount = 0 ;
776
788
777
789
continue ;
778
790
}
@@ -785,15 +797,25 @@ public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, L
785
797
&& ! string . Equals ( ruleSuppression . RuleSuppressionID , record . RuleSuppressionID , StringComparison . OrdinalIgnoreCase ) )
786
798
{
787
799
results . Add ( record ) ;
800
+ suppressionCount -= 1 ;
788
801
}
789
802
// otherwise, we ignore the record, move on to the next.
790
803
}
791
804
792
805
// important assumption: this point is reached only if we want to move to the next record
793
806
recordIndex += 1 ;
807
+ suppressionCount += 1 ;
794
808
795
809
if ( recordIndex == diagnostics . Count )
796
810
{
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
+
797
819
break ;
798
820
}
799
821
0 commit comments