@@ -26,6 +26,15 @@ public class RuleSuppression
2626 {
2727 private string _ruleName ;
2828
29+ /// <summary>
30+ /// The start offset of the rule suppression attribute (not where it starts to apply)
31+ /// </summary>
32+ public int StartAttributeLine
33+ {
34+ get ;
35+ set ;
36+ }
37+
2938 /// <summary>
3039 /// The start offset of the rule suppression
3140 /// </summary>
@@ -57,7 +66,9 @@ public string RuleName
5766 set
5867 {
5968 _ruleName = value ;
60- if ( ( ScriptAnalyzer . Instance . ScriptRules != null
69+
70+ if ( ! String . IsNullOrWhiteSpace ( _ruleName )
71+ && ( ScriptAnalyzer . Instance . ScriptRules != null
6172 && ScriptAnalyzer . Instance . ScriptRules . Count ( item => String . Equals ( item . GetName ( ) , _ruleName , StringComparison . OrdinalIgnoreCase ) ) == 0 )
6273 && ( ScriptAnalyzer . Instance . TokenRules != null
6374 && ScriptAnalyzer . Instance . TokenRules . Count ( item => String . Equals ( item . GetName ( ) , _ruleName , StringComparison . OrdinalIgnoreCase ) ) == 0 )
@@ -131,6 +142,7 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
131142
132143 if ( attrAst != null )
133144 {
145+ StartAttributeLine = attrAst . Extent . StartLineNumber ;
134146 var positionalArguments = attrAst . PositionalArguments ;
135147 var namedArguments = attrAst . NamedArguments ;
136148
@@ -236,6 +248,18 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
236248 }
237249 }
238250
251+ if ( ! String . IsNullOrWhiteSpace ( Error ) )
252+ {
253+ // May be cases where the rulename is null because we didn't look at the rulename after
254+ // we found out there is an error
255+ RuleName = String . Empty ;
256+ }
257+ else if ( String . IsNullOrWhiteSpace ( RuleName ) )
258+ {
259+ RuleName = String . Empty ;
260+ Error = Strings . NullRuleNameError ;
261+ }
262+
239263 // Must have scope and target together
240264 if ( String . IsNullOrWhiteSpace ( Scope ) && ! String . IsNullOrWhiteSpace ( Target ) )
241265 {
@@ -248,24 +272,26 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
248272
249273 if ( ! String . IsNullOrWhiteSpace ( Error ) )
250274 {
251- Error = String . Format ( CultureInfo . CurrentCulture , Strings . RuleSuppressionErrorFormat , attrAst . Extent . StartLineNumber ,
275+ Error = String . Format ( CultureInfo . CurrentCulture , Strings . RuleSuppressionErrorFormat , StartAttributeLine ,
252276 System . IO . Path . GetFileName ( attrAst . Extent . File ) , Error ) ;
253277 }
254278 }
255279
256280 /// <summary>
257- /// Constructs rule expression from rule name, id, start and end
281+ /// Constructs rule expression from rule name, id, start, end and startAttributeLine
258282 /// </summary>
259283 /// <param name="ruleName"></param>
260284 /// <param name="ruleSuppressionID"></param>
261285 /// <param name="start"></param>
262286 /// <param name="end"></param>
263- public RuleSuppression ( string ruleName , string ruleSuppressionID , int start , int end )
287+ /// <param name="startAttributeLine"></param>
288+ public RuleSuppression ( string ruleName , string ruleSuppressionID , int start , int end , int startAttributeLine )
264289 {
265290 RuleName = ruleName ;
266291 RuleSuppressionID = ruleSuppressionID ;
267292 StartOffset = start ;
268293 EndOffset = end ;
294+ StartAttributeLine = startAttributeLine ;
269295 }
270296
271297 /// <summary>
@@ -320,16 +346,24 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
320346
321347 if ( targetAsts != null )
322348 {
349+ if ( targetAsts . Count ( ) == 0 )
350+ {
351+ ruleSupp . Error = String . Format ( CultureInfo . CurrentCulture , Strings . RuleSuppressionErrorFormat , ruleSupp . StartAttributeLine ,
352+ System . IO . Path . GetFileName ( scopeAst . Extent . File ) , String . Format ( Strings . TargetCannotBeFoundError , ruleSupp . Target , ruleSupp . Scope ) ) ;
353+ result . Add ( ruleSupp ) ;
354+ continue ;
355+ }
356+
323357 foreach ( Ast targetAst in targetAsts )
324358 {
325- result . Add ( new RuleSuppression ( ruleSupp . RuleName , ruleSupp . RuleSuppressionID , targetAst . Extent . StartOffset , targetAst . Extent . EndOffset ) ) ;
359+ result . Add ( new RuleSuppression ( ruleSupp . RuleName , ruleSupp . RuleSuppressionID , targetAst . Extent . StartOffset , targetAst . Extent . EndOffset , attributeAst . Extent . StartLineNumber ) ) ;
326360 }
327361 }
328362
329363 }
330364 else
331365 {
332- // this may add rule suppression that contains erro but we will check for this in the engine to throw out error
366+ // this may add rule suppression that contains error but we will check for this in the engine to throw out error
333367 result . Add ( ruleSupp ) ;
334368 }
335369 }
0 commit comments