@@ -41,7 +41,7 @@ public class UseShouldProcessCorrectly : IScriptRule
41
41
public UseShouldProcessCorrectly ( )
42
42
{
43
43
diagnosticRecords = new List < DiagnosticRecord > ( ) ;
44
- shouldProcessVertex = new Vertex { name = "ShouldProcess" , ast = null } ;
44
+ shouldProcessVertex = new Vertex ( "ShouldProcess" , null ) ;
45
45
}
46
46
47
47
/// <summary>
@@ -141,7 +141,7 @@ private void FindViolations()
141
141
private DiagnosticRecord GetViolation ( Vertex v )
142
142
{
143
143
bool callsShouldProcess = funcDigraph . IsConnected ( v , shouldProcessVertex ) ;
144
- FunctionDefinitionAst fast = v . ast as FunctionDefinitionAst ;
144
+ FunctionDefinitionAst fast = v . Ast as FunctionDefinitionAst ;
145
145
if ( fast == null )
146
146
{
147
147
return null ;
@@ -179,7 +179,7 @@ private DiagnosticRecord GetViolation(Vertex v)
179
179
CultureInfo . CurrentCulture ,
180
180
Strings . ShouldProcessErrorHasCmdlet ,
181
181
fast . Name ) ,
182
- v . ast . Extent ,
182
+ v . Ast . Extent ,
183
183
GetName ( ) ,
184
184
DiagnosticSeverity . Warning ,
185
185
fileName ) ;
@@ -196,15 +196,14 @@ private DiagnosticRecord GetViolation(Vertex v)
196
196
/// <returns>true if an upstream function declares SupportsShouldProcess, otherwise false</returns>
197
197
private bool UpstreamDeclaresShouldProcess ( Vertex v )
198
198
{
199
- var equalityComparer = new VertexEqualityComparer ( ) ;
200
199
foreach ( var vertex in funcDigraph . GetVertices ( ) )
201
200
{
202
- if ( equalityComparer . Equals ( vertex , v ) )
201
+ if ( v . Equals ( vertex ) )
203
202
{
204
203
continue ;
205
204
}
206
205
207
- var fast = vertex . ast as FunctionDefinitionAst ;
206
+ var fast = vertex . Ast as FunctionDefinitionAst ;
208
207
if ( fast == null )
209
208
{
210
209
continue ;
@@ -269,8 +268,26 @@ private bool DeclaresSupportsShouldProcess(FunctionDefinitionAst ast)
269
268
/// </summary>
270
269
class Vertex
271
270
{
272
- public string name ;
273
- public Ast ast ;
271
+ public string Name { get { return name ; } }
272
+ public Ast Ast { get { return ast ; } }
273
+
274
+ private string name ;
275
+ private Ast ast ;
276
+
277
+ public Vertex ( )
278
+ {
279
+ name = String . Empty ;
280
+ }
281
+
282
+ public Vertex ( string name , Ast ast )
283
+ {
284
+ if ( name == null )
285
+ {
286
+ throw new ArgumentNullException ( "name" ) ;
287
+ }
288
+ this . name = name ;
289
+ this . ast = ast ;
290
+ }
274
291
275
292
/// <summary>
276
293
/// Returns string representation of a Vertex instance
@@ -279,47 +296,35 @@ public override string ToString()
279
296
{
280
297
return name ;
281
298
}
282
- }
283
-
284
- /// <summary>
285
- /// Implements IEqualityComparer interface for Vertex class
286
- /// </summary>
287
- class VertexEqualityComparer : IEqualityComparer < Vertex >
288
- {
289
299
290
300
/// <summary>
291
301
/// Compares two instances of Vertex class to check for equality
292
302
/// </summary>
293
- public bool Equals ( Vertex x , Vertex y )
303
+ public override bool Equals ( Object other )
294
304
{
295
- if ( x == null && y == null )
296
- {
297
- return true ;
298
- }
299
- else if ( x == null || y == null )
305
+ var otherVertex = other as Vertex ;
306
+ if ( otherVertex == null )
300
307
{
301
308
return false ;
302
309
}
303
- else if ( x . name . Equals ( y . name , StringComparison . OrdinalIgnoreCase ) )
310
+
311
+ if ( name . Equals ( otherVertex . name , StringComparison . OrdinalIgnoreCase ) )
304
312
{
305
313
return true ;
306
314
}
307
- else
308
- {
309
- return false ;
310
- }
315
+
316
+ return false ;
311
317
}
312
318
313
319
/// <summary>
314
320
/// Returns the Hash code of the given Vertex instance
315
321
/// </summary>
316
- public int GetHashCode ( Vertex obj )
322
+ public override int GetHashCode ( )
317
323
{
318
- return obj . name . GetHashCode ( ) ;
324
+ return name . GetHashCode ( ) ;
319
325
}
320
326
}
321
327
322
-
323
328
/// <summary>
324
329
/// Class to encapsulate a function call graph and related actions
325
330
/// </summary>
@@ -358,7 +363,7 @@ public Digraph<Vertex> GetDigraph()
358
363
/// </summary>
359
364
public FunctionReferenceDigraph ( )
360
365
{
361
- digraph = new Digraph < Vertex > ( new VertexEqualityComparer ( ) ) ;
366
+ digraph = new Digraph < Vertex > ( ) ;
362
367
functionVisitStack = new Stack < Vertex > ( ) ;
363
368
}
364
369
@@ -380,7 +385,7 @@ public void AddVertex(Vertex name)
380
385
/// <param name="toV">end of the edge</param>
381
386
public void AddEdge ( Vertex fromV , Vertex toV )
382
387
{
383
- if ( ! digraph . GetNeighbors ( fromV ) . Contains ( toV , new VertexEqualityComparer ( ) ) )
388
+ if ( ! digraph . GetNeighbors ( fromV ) . Contains ( toV ) )
384
389
{
385
390
digraph . AddEdge ( fromV , toV ) ;
386
391
}
@@ -396,7 +401,7 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst ast
396
401
return AstVisitAction . SkipChildren ;
397
402
}
398
403
399
- var functionVertex = new Vertex { name = ast . Name , ast = ast } ;
404
+ var functionVertex = new Vertex ( ast . Name , ast ) ;
400
405
functionVisitStack . Push ( functionVertex ) ;
401
406
AddVertex ( functionVertex ) ;
402
407
ast . Body . Visit ( this ) ;
@@ -415,12 +420,18 @@ public override AstVisitAction VisitCommand(CommandAst ast)
415
420
}
416
421
417
422
var cmdName = ast . GetCommandName ( ) ;
418
- var vertex = new Vertex { name = cmdName , ast = ast } ;
423
+ if ( cmdName == null )
424
+ {
425
+ return AstVisitAction . Continue ;
426
+ }
427
+
428
+ var vertex = new Vertex ( cmdName , ast ) ;
419
429
AddVertex ( vertex ) ;
420
430
if ( IsWithinFunctionDefinition ( ) )
421
431
{
422
432
AddEdge ( GetCurrentFunctionContext ( ) , vertex ) ;
423
433
}
434
+
424
435
return AstVisitAction . Continue ;
425
436
}
426
437
@@ -452,8 +463,8 @@ public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressio
452
463
// necessarily a function, we do it because we are mainly interested in
453
464
// finding connection between a function and ShouldProcess and this approach
454
465
// prevents any unnecessary complexity.
455
- var exprVertex = new Vertex { name = expr , ast = ast . Expression } ;
456
- var memberVertex = new Vertex { name = memberExprAst . Value , ast = memberExprAst } ;
466
+ var exprVertex = new Vertex ( expr , ast . Expression ) ;
467
+ var memberVertex = new Vertex ( memberExprAst . Value , memberExprAst ) ;
457
468
AddVertex ( exprVertex ) ;
458
469
AddVertex ( memberVertex ) ;
459
470
AddEdge ( exprVertex , memberVertex ) ;
0 commit comments