Skip to content

Commit d4e04ec

Browse files
author
Quoc Truong
committed
Add support for rule suppression id with uninitializedvariable
1 parent 1f0927c commit d4e04ec

File tree

7 files changed

+64
-273
lines changed

7 files changed

+64
-273
lines changed

Engine/Commands/InvokeScriptAnalyzerCommand.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private void AnalyzeFile(string filePath)
292292
return;
293293
}
294294

295-
Dictionary<string, LinkedList<Tuple<int, int>>> ruleSuppressions = Helper.Instance.GetRuleSuppression(ast);
295+
Dictionary<string, List<RuleSuppression>> ruleSuppressions = Helper.Instance.GetRuleSuppression(ast);
296296

297297
#region Run VariableAnalysis
298298
try
@@ -324,7 +324,7 @@ private void AnalyzeFile(string filePath)
324324
}
325325
catch (Exception scriptRuleException)
326326
{
327-
WriteError(new ErrorRecord(scriptRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
327+
WriteError(new ErrorRecord(scriptRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
328328
continue;
329329
}
330330
}
@@ -385,7 +385,7 @@ private void AnalyzeFile(string filePath)
385385
}
386386
catch (Exception commandRuleException)
387387
{
388-
WriteError(new ErrorRecord(commandRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, fileName));
388+
WriteError(new ErrorRecord(commandRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, fileName));
389389
continue;
390390
}
391391
}
@@ -414,7 +414,7 @@ private void AnalyzeFile(string filePath)
414414
}
415415
catch (Exception tokenRuleException)
416416
{
417-
WriteError(new ErrorRecord(tokenRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, fileName));
417+
WriteError(new ErrorRecord(tokenRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, fileName));
418418
continue;
419419
}
420420
}
@@ -442,7 +442,7 @@ private void AnalyzeFile(string filePath)
442442
}
443443
catch (Exception dscResourceRuleException)
444444
{
445-
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
445+
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
446446
continue;
447447
}
448448
}
@@ -484,7 +484,7 @@ private void AnalyzeFile(string filePath)
484484
}
485485
catch (Exception dscResourceRuleException)
486486
{
487-
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
487+
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
488488
continue;
489489
}
490490
}
@@ -519,7 +519,7 @@ private void AnalyzeFile(string filePath)
519519
}
520520
catch (Exception externalRuleException)
521521
{
522-
WriteError(new ErrorRecord(externalRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, fileName));
522+
WriteError(new ErrorRecord(externalRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, fileName));
523523
}
524524
}
525525
}

Engine/Generic/RuleSuppression.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class RuleSuppression
1313
/// <summary>
1414
/// The start offset of the rule suppression
1515
/// </summary>
16-
public int StartOffSet
16+
public int StartOffset
1717
{
1818
get;
1919
set;
@@ -79,7 +79,7 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
7979

8080
if (positionalArguments.Any(item => !(item is StringConstantExpressionAst)))
8181
{
82-
Error = "All the arguments of the suppression message attribute should be string constant";
82+
Error = Strings.StringConstantArgumentsSuppressionAttributeError;
8383
}
8484
else
8585
{
@@ -105,12 +105,12 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
105105
{
106106
if (name.Extent.StartOffset < lastPositionalArgumentsOffset)
107107
{
108-
Error = "Named arguments must always come after positional arguments";
108+
Error = Strings.NamedArgumentsBeforePositionalError;
109109
break;
110110
}
111111
else if (!(name.Argument is StringConstantExpressionAst))
112112
{
113-
Error = "All the arguments of the suppression message attribute should be string constant";
113+
Error = Strings.StringConstantArgumentsSuppressionAttributeError;
114114
break;
115115
}
116116

@@ -119,7 +119,7 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
119119
case "rulename":
120120
if (!String.IsNullOrWhiteSpace(RuleName))
121121
{
122-
Error = "RuleName cannot be set by both positional and named arguments";
122+
Error = String.Format(Strings.NamedAndPositionalArgumentsConflictError, name);
123123
}
124124

125125
RuleName = (name.Argument as StringConstantExpressionAst).Value;
@@ -128,7 +128,7 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
128128
case "rulesuppressionid":
129129
if (!String.IsNullOrWhiteSpace(RuleSuppressionID))
130130
{
131-
Error = "RuleSuppressionID cannot be set by both positional and named arguments";
131+
Error = String.Format(Strings.NamedAndPositionalArgumentsConflictError, name);
132132
}
133133

134134
RuleSuppressionID = (name.Argument as StringConstantExpressionAst).Value;
@@ -141,7 +141,7 @@ public RuleSuppression(AttributeAst attrAst, int start, int end)
141141
}
142142
}
143143

144-
StartOffSet = start;
144+
StartOffset = start;
145145
EndOffset = end;
146146
}
147147

Engine/Helper.cs

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ public bool HasSpecialVars(string varName)
612612
/// </summary>
613613
/// <param name="ast"></param>
614614
/// <returns></returns>
615-
public Dictionary<string, LinkedList<Tuple<int, int>>> GetRuleSuppression(Ast ast)
615+
public Dictionary<string, List<RuleSuppression>> GetRuleSuppression(Ast ast)
616616
{
617617
List<RuleSuppression> ruleSuppressionList = new List<RuleSuppression>();
618618

@@ -632,39 +632,18 @@ public Dictionary<string, LinkedList<Tuple<int, int>>> GetRuleSuppression(Ast as
632632
ruleSuppressionList.AddRange(GetSuppressionsClass(typeAst));
633633
}
634634

635-
Dictionary<string, LinkedList<Tuple<int, int>>> results = new Dictionary<string, LinkedList<Tuple<int, int>>>(StringComparer.OrdinalIgnoreCase);
636-
ruleSuppressionList.Sort((item, item2) => item.StartOffSet.CompareTo(item2.StartOffSet));
635+
Dictionary<string, List<RuleSuppression>> results = new Dictionary<string, List<RuleSuppression>>(StringComparer.OrdinalIgnoreCase);
636+
ruleSuppressionList.Sort((item, item2) => item.StartOffset.CompareTo(item2.StartOffset));
637637

638638
foreach (RuleSuppression ruleSuppression in ruleSuppressionList)
639639
{
640640
if (!results.ContainsKey(ruleSuppression.RuleName))
641641
{
642-
LinkedList<Tuple<int, int>> intervals = new LinkedList<Tuple<int, int>>();
643-
intervals.AddLast(Tuple.Create(ruleSuppression.StartOffSet, ruleSuppression.EndOffset));
644-
results.Add(ruleSuppression.RuleName, intervals);
645-
}
646-
else
647-
{
648-
LinkedList<Tuple<int, int>> intervals = results[ruleSuppression.RuleName];
649-
int previousEnd = intervals.Last.Value.Item2;
650-
651-
// proccess the intervals so they do not overlap and is sorted in order
652-
if (ruleSuppression.StartOffSet <= previousEnd)
653-
{
654-
if (ruleSuppression.EndOffset <= previousEnd)
655-
{
656-
continue;
657-
}
658-
else
659-
{
660-
intervals.Last.Value = Tuple.Create(intervals.Last.Value.Item1, ruleSuppression.EndOffset);
661-
}
662-
}
663-
else
664-
{
665-
intervals.AddLast(Tuple.Create(ruleSuppression.StartOffSet, ruleSuppression.EndOffset));
666-
}
642+
List<RuleSuppression> ruleSuppressions = new List<RuleSuppression>();
643+
results.Add(ruleSuppression.RuleName, ruleSuppressions);
667644
}
645+
646+
results[ruleSuppression.RuleName].Add(ruleSuppression);
668647
}
669648

670649
return results;
@@ -727,59 +706,62 @@ internal List<RuleSuppression> GetSuppressionsClass(TypeDefinitionAst typeAst)
727706
/// </summary>
728707
/// <param name="ruleSuppressions"></param>
729708
/// <param name="diagnostics"></param>
730-
public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, LinkedList<Tuple<int, int>>> ruleSuppressions, List<DiagnosticRecord> diagnostics)
709+
public List<DiagnosticRecord> SuppressRule(string ruleName, Dictionary<string, List<RuleSuppression>> ruleSuppressionsDict, List<DiagnosticRecord> diagnostics)
731710
{
732711
List<DiagnosticRecord> results = new List<DiagnosticRecord>();
733-
if (!ruleSuppressions.ContainsKey(ruleName) || diagnostics.Count == 0)
712+
713+
if (!ruleSuppressionsDict.ContainsKey(ruleName) || diagnostics.Count == 0)
734714
{
735715
return diagnostics;
736716
}
737717

738-
LinkedList<Tuple<int, int>> intervals = ruleSuppressions[ruleName];
718+
List<RuleSuppression> ruleSuppressions = ruleSuppressionsDict[ruleName];
739719

740-
if (intervals.Count == 0)
720+
if (ruleSuppressions.Count == 0)
741721
{
742722
return diagnostics;
743723
}
744724

745725
int recordIndex = 0;
726+
int ruleSuppressionIndex = 0;
746727
DiagnosticRecord record = diagnostics.First();
747-
LinkedListNode<Tuple<int, int>> interval = intervals.First;
728+
RuleSuppression ruleSuppression = ruleSuppressions.First();
748729

749730
while (recordIndex < diagnostics.Count)
750731
{
751-
// the record precedes the interval so don't apply the suppression
752-
if (record.Extent.StartOffset < interval.Value.Item1)
732+
// the record precedes the rule suppression so don't apply the suppression
733+
if (record.Extent.StartOffset < ruleSuppression.StartOffset)
753734
{
754735
results.Add(record);
755-
recordIndex += 1;
756-
757-
if (recordIndex == diagnostics.Count)
758-
{
759-
break;
760-
}
761-
else
762-
{
763-
// move on to the next record
764-
record = diagnostics[recordIndex];
765-
continue;
766-
}
767736
}
768-
769-
// end of the rule suppression is less than the record start offset so move on to next interval
770-
if (interval.Value.Item2 < record.Extent.StartOffset)
737+
// end of the rule suppression is less than the record start offset so move on to next rule suppression
738+
else if (ruleSuppression.EndOffset < record.Extent.StartOffset)
771739
{
772-
interval = interval.Next;
740+
ruleSuppressionIndex += 1;
773741

774-
if (interval == null)
742+
if (ruleSuppressionIndex == ruleSuppressions.Count)
775743
{
776744
break;
777745
}
778746

747+
ruleSuppression = ruleSuppressions[ruleSuppressionIndex];
748+
779749
continue;
780750
}
751+
// at this point, the record is inside the interval
752+
else
753+
{
754+
// if the rule suppression id from the rule suppression is not null and the one from diagnostic record is not null
755+
// and they are they are not the same then we cannot ignore the record
756+
if (!string.IsNullOrWhiteSpace(ruleSuppression.RuleSuppressionID) && !string.IsNullOrWhiteSpace(record.RuleSuppressionID)
757+
&& !string.Equals(ruleSuppression.RuleSuppressionID, record.RuleSuppressionID, StringComparison.OrdinalIgnoreCase))
758+
{
759+
results.Add(record);
760+
}
761+
// otherwise, we ignore the record, move on to the next.
762+
}
781763

782-
// here the record is inside the interval so we don't add it to the result
764+
// important assumption: this point is reached only if we want to move to the next record
783765
recordIndex += 1;
784766

785767
if (recordIndex == diagnostics.Count)

Engine/ScriptAnalyzerEngine.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -71,7 +71,7 @@
7171
<Compile Include="Loggers\WriteObjectsLogger.cs" />
7272
<Compile Include="ScriptAnalyzer.cs" />
7373
<Compile Include="SpecialVars.cs" />
74-
<Compile Include="Strings.cs">
74+
<Compile Include="Strings.Designer.cs">
7575
<AutoGen>True</AutoGen>
7676
<DesignTime>True</DesignTime>
7777
<DependentUpon>Strings.resx</DependentUpon>
@@ -87,7 +87,7 @@
8787
<ItemGroup>
8888
<EmbeddedResource Include="Strings.resx">
8989
<Generator>ResXFileCodeGenerator</Generator>
90-
<LastGenOutput>Strings.cs</LastGenOutput>
90+
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
9191
</EmbeddedResource>
9292
</ItemGroup>
9393
<Import Project="$(MSBuildToolsPath)\Microsoft.CSHARP.Targets" />
@@ -100,4 +100,4 @@ copy /y "$(ProjectDir)\*.ps1xml" "$(SolutionDir)$(SolutionName)"
100100
copy /y "$(ProjectDir)\*.psd1" "$(SolutionDir)$(SolutionName)"
101101
copy /y "$(TargetPath)" "$(SolutionDir)$(SolutionName)"</PostBuildEvent>
102102
</PropertyGroup>
103-
</Project>
103+
</Project>

0 commit comments

Comments
 (0)