2
2
using System . Linq ;
3
3
using System . Management . Automation . Language ;
4
4
using System . Collections . Generic ;
5
+ using System . Text . RegularExpressions ;
5
6
6
7
namespace Microsoft . Windows . Powershell . ScriptAnalyzer . Generic
7
8
{
@@ -46,6 +47,24 @@ public string RuleSuppressionID
46
47
set ;
47
48
}
48
49
50
+ /// <summary>
51
+ /// Scope of the rule suppression
52
+ /// </summary>
53
+ public string Scope
54
+ {
55
+ get ;
56
+ set ;
57
+ }
58
+
59
+ /// <summary>
60
+ /// Target of the rule suppression
61
+ /// </summary>
62
+ public string Target
63
+ {
64
+ get ;
65
+ set ;
66
+ }
67
+
49
68
/// <summary>
50
69
/// Returns error occurred in trying to parse the attribute
51
70
/// </summary>
@@ -55,6 +74,18 @@ public string Error
55
74
set ;
56
75
}
57
76
77
+ private static HashSet < string > scopeSet ;
78
+
79
+ /// <summary>
80
+ /// Initialize the scopeSet
81
+ /// </summary>
82
+ static RuleSuppression ( )
83
+ {
84
+ scopeSet = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
85
+ scopeSet . Add ( "function" ) ;
86
+ scopeSet . Add ( "class" ) ;
87
+ }
88
+
58
89
/// <summary>
59
90
/// Returns rule suppression from an attribute ast that has the type suppressmessageattribute
60
91
/// </summary>
@@ -85,6 +116,14 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
85
116
{
86
117
switch ( count )
87
118
{
119
+ case 4 :
120
+ Target = ( positionalArguments [ 3 ] as StringConstantExpressionAst ) . Value ;
121
+ goto case 3 ;
122
+
123
+ case 3 :
124
+ Scope = ( positionalArguments [ 2 ] as StringConstantExpressionAst ) . Value ;
125
+ goto case 2 ;
126
+
88
127
case 2 :
89
128
RuleSuppressionID = ( positionalArguments [ 1 ] as StringConstantExpressionAst ) . Value ;
90
129
goto case 1 ;
@@ -134,17 +173,62 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
134
173
RuleSuppressionID = ( name . Argument as StringConstantExpressionAst ) . Value ;
135
174
goto default ;
136
175
176
+ case "scope" :
177
+ if ( ! String . IsNullOrWhiteSpace ( Scope ) )
178
+ {
179
+ Error = String . Format ( Strings . NamedAndPositionalArgumentsConflictError , name ) ;
180
+ }
181
+
182
+ Scope = ( name . Argument as StringConstantExpressionAst ) . Value ;
183
+
184
+ if ( ! scopeSet . Contains ( Scope ) )
185
+ {
186
+ Error = Strings . WrongScopeArgumentSuppressionAttributeError ;
187
+ }
188
+
189
+ goto default ;
190
+
191
+ case "target" :
192
+ if ( ! String . IsNullOrWhiteSpace ( Target ) )
193
+ {
194
+ Error = String . Format ( Strings . NamedAndPositionalArgumentsConflictError , name ) ;
195
+ }
196
+
197
+ Target = ( name . Argument as StringConstantExpressionAst ) . Value ;
198
+ goto default ;
199
+
137
200
default :
138
201
break ;
139
202
}
140
203
}
141
204
}
205
+
206
+ // Must have scope and target together
207
+ if ( String . IsNullOrWhiteSpace ( Scope ) && ! String . IsNullOrWhiteSpace ( Target ) )
208
+ {
209
+ Error = Strings . TargetWithoutScopeSuppressionAttributeError ;
210
+ }
142
211
}
143
212
144
213
StartOffset = start ;
145
214
EndOffset = end ;
146
215
}
147
216
217
+ /// <summary>
218
+ /// Constructs rule expression from rule name, id, start and end
219
+ /// </summary>
220
+ /// <param name="ruleName"></param>
221
+ /// <param name="ruleSuppressionID"></param>
222
+ /// <param name="start"></param>
223
+ /// <param name="end"></param>
224
+ public RuleSuppression ( string ruleName , string ruleSuppressionID , int start , int end )
225
+ {
226
+ RuleName = ruleName ;
227
+ RuleSuppressionID = ruleSuppressionID ;
228
+ StartOffset = start ;
229
+ EndOffset = end ;
230
+ }
231
+
148
232
/// <summary>
149
233
/// Given a list of attribute asts, return a list of rule suppression
150
234
/// with startoffset at start and endoffset at end
@@ -153,11 +237,11 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
153
237
/// <param name="start"></param>
154
238
/// <param name="end"></param>
155
239
/// <returns></returns>
156
- public static List < RuleSuppression > GetSuppressions ( IEnumerable < AttributeAst > attrAsts , int start , int end )
240
+ public static List < RuleSuppression > GetSuppressions ( IEnumerable < AttributeAst > attrAsts , int start , int end , Ast scopeAst )
157
241
{
158
242
List < RuleSuppression > result = new List < RuleSuppression > ( ) ;
159
243
160
- if ( attrAsts == null )
244
+ if ( attrAsts == null || scopeAst == null )
161
245
{
162
246
return result ;
163
247
}
@@ -169,8 +253,44 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
169
253
{
170
254
RuleSuppression ruleSupp = new RuleSuppression ( attributeAst , start , end ) ;
171
255
172
- if ( string . IsNullOrWhiteSpace ( ruleSupp . Error ) )
256
+ // If there is no error and scope is not null
257
+ if ( String . IsNullOrWhiteSpace ( ruleSupp . Error ) && ! String . IsNullOrWhiteSpace ( ruleSupp . Scope ) )
258
+ {
259
+ if ( String . IsNullOrWhiteSpace ( ruleSupp . Target ) )
260
+ {
261
+ ruleSupp . Target = "*" ;
262
+ }
263
+
264
+ // regex for wild card *
265
+ Regex reg = new Regex ( String . Format ( "^{0}$" , Regex . Escape ( ruleSupp . Target ) . Replace ( @"\*" , ".*" ) ) ) ;
266
+ IEnumerable < Ast > targetAsts = null ;
267
+
268
+ switch ( ruleSupp . Scope . ToLower ( ) )
269
+ {
270
+ case "function" :
271
+ targetAsts = scopeAst . FindAll ( item => item is FunctionDefinitionAst && reg . IsMatch ( ( item as FunctionDefinitionAst ) . Name ) , true ) ;
272
+ goto default ;
273
+
274
+ case "class" :
275
+ targetAsts = scopeAst . FindAll ( item => item is TypeDefinitionAst && reg . IsMatch ( ( item as TypeDefinitionAst ) . Name ) , true ) ;
276
+ goto default ;
277
+
278
+ default :
279
+ break ;
280
+ }
281
+
282
+ if ( targetAsts != null )
283
+ {
284
+ foreach ( Ast targetAst in targetAsts )
285
+ {
286
+ result . Add ( new RuleSuppression ( ruleSupp . RuleName , ruleSupp . RuleSuppressionID , targetAst . Extent . StartOffset , targetAst . Extent . EndOffset ) ) ;
287
+ }
288
+ }
289
+
290
+ }
291
+ else
173
292
{
293
+ // this may add rule suppression that contains erro but we will check for this in the engine to throw out error
174
294
result . Add ( ruleSupp ) ;
175
295
}
176
296
}
0 commit comments