Skip to content

Commit 1c711cf

Browse files
author
Kapil Borle
committed
Change Vertex class implementation
1 parent 0d5b12f commit 1c711cf

File tree

2 files changed

+47
-36
lines changed

2 files changed

+47
-36
lines changed

Engine/Helper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3611,7 +3611,7 @@ public IEnumerable<T> GetVertices()
36113611
/// <returns>True if the graph contains the vertex, otherwise false</returns>
36123612
public bool ContainsVertex(T vertex)
36133613
{
3614-
return vertexIndexMap.Keys.Contains(vertex);
3614+
return vertexIndexMap.ContainsKey(vertex);
36153615
}
36163616

36173617
/// <summary>

Rules/UseShouldProcessCorrectly.cs

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class UseShouldProcessCorrectly : IScriptRule
4141
public UseShouldProcessCorrectly()
4242
{
4343
diagnosticRecords = new List<DiagnosticRecord>();
44-
shouldProcessVertex = new Vertex {name="ShouldProcess", ast=null};
44+
shouldProcessVertex = new Vertex("ShouldProcess", null);
4545
}
4646

4747
/// <summary>
@@ -141,7 +141,7 @@ private void FindViolations()
141141
private DiagnosticRecord GetViolation(Vertex v)
142142
{
143143
bool callsShouldProcess = funcDigraph.IsConnected(v, shouldProcessVertex);
144-
FunctionDefinitionAst fast = v.ast as FunctionDefinitionAst;
144+
FunctionDefinitionAst fast = v.Ast as FunctionDefinitionAst;
145145
if (fast == null)
146146
{
147147
return null;
@@ -179,7 +179,7 @@ private DiagnosticRecord GetViolation(Vertex v)
179179
CultureInfo.CurrentCulture,
180180
Strings.ShouldProcessErrorHasCmdlet,
181181
fast.Name),
182-
v.ast.Extent,
182+
v.Ast.Extent,
183183
GetName(),
184184
DiagnosticSeverity.Warning,
185185
fileName);
@@ -196,15 +196,14 @@ private DiagnosticRecord GetViolation(Vertex v)
196196
/// <returns>true if an upstream function declares SupportsShouldProcess, otherwise false</returns>
197197
private bool UpstreamDeclaresShouldProcess(Vertex v)
198198
{
199-
var equalityComparer = new VertexEqualityComparer();
200199
foreach (var vertex in funcDigraph.GetVertices())
201200
{
202-
if (equalityComparer.Equals(vertex, v))
201+
if (v.Equals(vertex))
203202
{
204203
continue;
205204
}
206205

207-
var fast = vertex.ast as FunctionDefinitionAst;
206+
var fast = vertex.Ast as FunctionDefinitionAst;
208207
if (fast == null)
209208
{
210209
continue;
@@ -269,8 +268,26 @@ private bool DeclaresSupportsShouldProcess(FunctionDefinitionAst ast)
269268
/// </summary>
270269
class Vertex
271270
{
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+
}
274291

275292
/// <summary>
276293
/// Returns string representation of a Vertex instance
@@ -279,47 +296,35 @@ public override string ToString()
279296
{
280297
return name;
281298
}
282-
}
283-
284-
/// <summary>
285-
/// Implements IEqualityComparer interface for Vertex class
286-
/// </summary>
287-
class VertexEqualityComparer : IEqualityComparer<Vertex>
288-
{
289299

290300
/// <summary>
291301
/// Compares two instances of Vertex class to check for equality
292302
/// </summary>
293-
public bool Equals(Vertex x, Vertex y)
303+
public override bool Equals(Object other)
294304
{
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)
300307
{
301308
return false;
302309
}
303-
else if (x.name.Equals(y.name, StringComparison.OrdinalIgnoreCase))
310+
311+
if (name.Equals(otherVertex.name, StringComparison.OrdinalIgnoreCase))
304312
{
305313
return true;
306314
}
307-
else
308-
{
309-
return false;
310-
}
315+
316+
return false;
311317
}
312318

313319
/// <summary>
314320
/// Returns the Hash code of the given Vertex instance
315321
/// </summary>
316-
public int GetHashCode(Vertex obj)
322+
public override int GetHashCode()
317323
{
318-
return obj.name.GetHashCode();
324+
return name.GetHashCode();
319325
}
320326
}
321327

322-
323328
/// <summary>
324329
/// Class to encapsulate a function call graph and related actions
325330
/// </summary>
@@ -358,7 +363,7 @@ public Digraph<Vertex> GetDigraph()
358363
/// </summary>
359364
public FunctionReferenceDigraph()
360365
{
361-
digraph = new Digraph<Vertex>(new VertexEqualityComparer());
366+
digraph = new Digraph<Vertex>();
362367
functionVisitStack = new Stack<Vertex>();
363368
}
364369

@@ -380,7 +385,7 @@ public void AddVertex(Vertex name)
380385
/// <param name="toV">end of the edge</param>
381386
public void AddEdge(Vertex fromV, Vertex toV)
382387
{
383-
if (!digraph.GetNeighbors(fromV).Contains(toV, new VertexEqualityComparer()))
388+
if (!digraph.GetNeighbors(fromV).Contains(toV))
384389
{
385390
digraph.AddEdge(fromV, toV);
386391
}
@@ -396,7 +401,7 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst ast
396401
return AstVisitAction.SkipChildren;
397402
}
398403

399-
var functionVertex = new Vertex { name = ast.Name, ast = ast };
404+
var functionVertex = new Vertex (ast.Name, ast);
400405
functionVisitStack.Push(functionVertex);
401406
AddVertex(functionVertex);
402407
ast.Body.Visit(this);
@@ -415,12 +420,18 @@ public override AstVisitAction VisitCommand(CommandAst ast)
415420
}
416421

417422
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);
419429
AddVertex(vertex);
420430
if (IsWithinFunctionDefinition())
421431
{
422432
AddEdge(GetCurrentFunctionContext(), vertex);
423433
}
434+
424435
return AstVisitAction.Continue;
425436
}
426437

@@ -452,8 +463,8 @@ public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressio
452463
// necessarily a function, we do it because we are mainly interested in
453464
// finding connection between a function and ShouldProcess and this approach
454465
// 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);
457468
AddVertex(exprVertex);
458469
AddVertex(memberVertex);
459470
AddEdge(exprVertex, memberVertex);

0 commit comments

Comments
 (0)