Skip to content

Commit d3b6b75

Browse files
author
Quoc Truong
committed
Add rule suppression for classes and functions in classes
1 parent 509daa3 commit d3b6b75

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

Engine/Generic/RuleSuppression.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Management.Automation.Language;
4+
using System.Collections.Generic;
45

56
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
67
{
@@ -143,5 +144,38 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
143144
StartOffSet = start;
144145
EndOffset = end;
145146
}
147+
148+
/// <summary>
149+
/// Given a list of attribute asts, return a list of rule suppression
150+
/// with startoffset at start and endoffset at end
151+
/// </summary>
152+
/// <param name="attrAsts"></param>
153+
/// <param name="start"></param>
154+
/// <param name="end"></param>
155+
/// <returns></returns>
156+
public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> attrAsts, int start, int end)
157+
{
158+
List<RuleSuppression> result = new List<RuleSuppression>();
159+
160+
if (attrAsts == null)
161+
{
162+
return result;
163+
}
164+
165+
IEnumerable<AttributeAst> suppressionAttribute = attrAsts.Where(
166+
item => item.TypeName.GetReflectionType() == typeof(System.Diagnostics.CodeAnalysis.SuppressMessageAttribute));
167+
168+
foreach (var attributeAst in suppressionAttribute)
169+
{
170+
RuleSuppression ruleSupp = new RuleSuppression(attributeAst, start, end);
171+
172+
if (string.IsNullOrWhiteSpace(ruleSupp.Error))
173+
{
174+
result.Add(ruleSupp);
175+
}
176+
}
177+
178+
return result;
179+
}
146180
}
147181
}

Engine/Helper.cs

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,21 @@ public bool HasSpecialVars(string varName)
615615
public Dictionary<string, LinkedList<Tuple<int, int>>> GetRuleSuppression(Ast ast)
616616
{
617617
List<RuleSuppression> ruleSuppressionList = new List<RuleSuppression>();
618+
619+
// Get rule suppression from functions
618620
IEnumerable<FunctionDefinitionAst> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true).Cast<FunctionDefinitionAst>();
621+
619622
foreach (var funcAst in funcAsts)
620623
{
621-
ruleSuppressionList.AddRange(GetSuppressionFunction(funcAst));
624+
ruleSuppressionList.AddRange(GetSuppressionsFunction(funcAst));
625+
}
626+
627+
// Get rule suppression from classes
628+
IEnumerable<TypeDefinitionAst> typeAsts = ast.FindAll(item => item is TypeDefinitionAst, true).Cast<TypeDefinitionAst>();
629+
630+
foreach (var typeAst in typeAsts)
631+
{
632+
ruleSuppressionList.AddRange(GetSuppressionsClass(typeAst));
622633
}
623634

624635
Dictionary<string, LinkedList<Tuple<int, int>>> results = new Dictionary<string, LinkedList<Tuple<int, int>>>(StringComparer.OrdinalIgnoreCase);
@@ -664,24 +675,48 @@ public Dictionary<string, LinkedList<Tuple<int, int>>> GetRuleSuppression(Ast as
664675
/// </summary>
665676
/// <param name="funcAst"></param>
666677
/// <returns></returns>
667-
internal List<RuleSuppression> GetSuppressionFunction(FunctionDefinitionAst funcAst)
678+
internal List<RuleSuppression> GetSuppressionsFunction(FunctionDefinitionAst funcAst)
668679
{
669680
List<RuleSuppression> result = new List<RuleSuppression>();
670681

671682
if (funcAst != null && funcAst.Body != null
672683
&& funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Attributes != null)
673684
{
674-
IEnumerable<AttributeAst> suppressionAttribute = funcAst.Body.ParamBlock.Attributes.Where(
675-
item => item.TypeName.GetReflectionType() == typeof(System.Diagnostics.CodeAnalysis.SuppressMessageAttribute));
685+
result.AddRange(RuleSuppression.GetSuppressions(funcAst.Body.ParamBlock.Attributes, funcAst.Extent.StartOffset, funcAst.Extent.EndOffset));
686+
}
687+
688+
return result;
689+
}
690+
691+
/// <summary>
692+
/// Returns a list of rule suppression from the class
693+
/// </summary>
694+
/// <param name="typeAst"></param>
695+
/// <returns></returns>
696+
internal List<RuleSuppression> GetSuppressionsClass(TypeDefinitionAst typeAst)
697+
{
698+
List<RuleSuppression> result = new List<RuleSuppression>();
699+
700+
if (typeAst != null && typeAst.Attributes != null && typeAst.Attributes.Count != 0)
701+
{
702+
result.AddRange(RuleSuppression.GetSuppressions(typeAst.Attributes, typeAst.Extent.StartOffset, typeAst.Extent.EndOffset));
703+
}
676704

677-
foreach (var attributeAst in suppressionAttribute)
705+
if (typeAst.Members == null)
706+
{
707+
return result;
708+
}
709+
710+
foreach (var member in typeAst.Members)
711+
{
712+
FunctionMemberAst funcMemb = member as FunctionMemberAst;
713+
714+
if (funcMemb == null)
678715
{
679-
RuleSuppression ruleSupp = new RuleSuppression(attributeAst, funcAst.Extent.StartOffset, funcAst.Extent.EndOffset);
680-
if (String.IsNullOrWhiteSpace(ruleSupp.Error))
681-
{
682-
result.Add(ruleSupp);
683-
}
716+
continue;
684717
}
718+
719+
result.AddRange(RuleSuppression.GetSuppressions(funcMemb.Attributes, funcMemb.Extent.StartOffset, funcMemb.Extent.EndOffset));
685720
}
686721

687722
return result;
@@ -844,6 +879,16 @@ public object VisitScriptBlock(ScriptBlockAst scriptBlockAst)
844879
return null;
845880
}
846881

882+
/// <summary>
883+
/// Do nothing
884+
/// </summary>
885+
/// <param name="baseCtorInvokeMemberExpressionAst"></param>
886+
/// <returns></returns>
887+
public object VisitBaseCtorInvokeMemberExpression(BaseCtorInvokeMemberExpressionAst baseCtorInvokeMemberExpressionAst)
888+
{
889+
return null;
890+
}
891+
847892
/// <summary>
848893
/// Do nothing
849894
/// </summary>

0 commit comments

Comments
 (0)