@@ -107,6 +107,14 @@ public SourceType GetSourceType()
107
107
return SourceType . Builtin ;
108
108
}
109
109
110
+ /// <summary>
111
+ /// GetSourceName: Retrieves the module/assembly name the rule is from.
112
+ /// </summary>
113
+ public string GetSourceName ( )
114
+ {
115
+ return string . Format ( CultureInfo . CurrentCulture , Strings . SourceName ) ;
116
+ }
117
+
110
118
/// <summary>
111
119
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
112
120
/// </summary>
@@ -116,12 +124,9 @@ public RuleSeverity GetSeverity()
116
124
return RuleSeverity . Warning ;
117
125
}
118
126
119
- /// <summary>
120
- /// GetSourceName: Retrieves the module/assembly name the rule is from.
121
- /// </summary>
122
- public string GetSourceName ( )
127
+ private DiagnosticSeverity GetDianosticSeverity ( )
123
128
{
124
- return string . Format ( CultureInfo . CurrentCulture , Strings . SourceName ) ;
129
+ return DiagnosticSeverity . Warning ;
125
130
}
126
131
127
132
/// <summary>
@@ -164,9 +169,9 @@ private DiagnosticRecord GetViolation(Vertex v)
164
169
CultureInfo . CurrentCulture ,
165
170
Strings . ShouldProcessErrorHasAttribute ,
166
171
fast . Name ) ,
167
- ast . Extent ,
172
+ Helper . Instance . GetShouldProcessAttributeAst ( fast . Body . ParamBlock . Attributes ) . Extent ,
168
173
GetName ( ) ,
169
- DiagnosticSeverity . Warning ,
174
+ GetDianosticSeverity ( ) ,
170
175
ast . Extent . File ) ;
171
176
}
172
177
}
@@ -187,16 +192,55 @@ private DiagnosticRecord GetViolation(Vertex v)
187
192
CultureInfo . CurrentCulture ,
188
193
Strings . ShouldProcessErrorHasCmdlet ,
189
194
fast . Name ) ,
190
- v . Ast . Extent ,
195
+ GetShouldProcessCallExtent ( fast ) ,
191
196
GetName ( ) ,
192
- DiagnosticSeverity . Warning ,
197
+ GetDianosticSeverity ( ) ,
193
198
fileName ) ;
194
199
}
195
200
}
196
201
197
202
return null ;
198
203
}
199
204
205
+ /// <summary>
206
+ /// Gets the extent of ShouldProcess call
207
+ /// </summary>
208
+ private static IScriptExtent GetShouldProcessCallExtent ( FunctionDefinitionAst functionDefinitionAst )
209
+ {
210
+ var invokeMemberExpressionAstFound = functionDefinitionAst . Find ( IsShouldProcessCall , true ) ;
211
+ if ( invokeMemberExpressionAstFound == null )
212
+ {
213
+ return functionDefinitionAst . Extent ;
214
+ }
215
+
216
+ return ( invokeMemberExpressionAstFound as InvokeMemberExpressionAst ) . Member . Extent ;
217
+ }
218
+
219
+ /// <summary>
220
+ /// Returns true if ast if of the form $PSCmdlet.PSShouldProcess()
221
+ /// </summary>
222
+ private static bool IsShouldProcessCall ( Ast ast )
223
+ {
224
+ var invokeMemberExpressionAst = ast as InvokeMemberExpressionAst ;
225
+ if ( invokeMemberExpressionAst == null )
226
+ {
227
+ return false ;
228
+ }
229
+
230
+ var memberExprAst = invokeMemberExpressionAst . Member as StringConstantExpressionAst ;
231
+ if ( memberExprAst == null )
232
+ {
233
+ return false ;
234
+ }
235
+
236
+ if ( "ShouldProcess" . Equals ( memberExprAst . Value , StringComparison . OrdinalIgnoreCase ) )
237
+ {
238
+ return true ;
239
+ }
240
+
241
+ return false ;
242
+ }
243
+
200
244
private bool callsShouldProcessDirectly ( Vertex vertex )
201
245
{
202
246
return funcDigraph . GetNeighbors ( vertex ) . Contains ( shouldProcessVertex ) ;
0 commit comments