Skip to content

Suppression #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Apr 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ private void AnalyzeFile(string filePath)
Token[] tokens = null;
ParseError[] errors = null;
List<DiagnosticRecord> diagnostics = new List<DiagnosticRecord>();

IEnumerable<Ast> funcDefAsts;

// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
Expand Down Expand Up @@ -291,6 +292,8 @@ private void AnalyzeFile(string filePath)
return;
}

Dictionary<string, List<RuleSuppression>> ruleSuppressions = Helper.Instance.GetRuleSuppression(ast);

#region Run VariableAnalysis
try
{
Expand All @@ -317,11 +320,11 @@ private void AnalyzeFile(string filePath)
// We want the Engine to continue functioning even if one or more Rules throws an exception
try
{
diagnostics.AddRange(scriptRule.AnalyzeScript(ast, filePath));
diagnostics.AddRange(Helper.Instance.SuppressRule(scriptRule.GetName(), ruleSuppressions, scriptRule.AnalyzeScript(ast, filePath).ToList()));
}
catch (Exception scriptRuleException)
{
WriteError(new ErrorRecord(scriptRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
WriteError(new ErrorRecord(scriptRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
continue;
}
}
Expand Down Expand Up @@ -382,7 +385,7 @@ private void AnalyzeFile(string filePath)
}
catch (Exception commandRuleException)
{
WriteError(new ErrorRecord(commandRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, fileName));
WriteError(new ErrorRecord(commandRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, fileName));
continue;
}
}
Expand Down Expand Up @@ -411,7 +414,7 @@ private void AnalyzeFile(string filePath)
}
catch (Exception tokenRuleException)
{
WriteError(new ErrorRecord(tokenRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, fileName));
WriteError(new ErrorRecord(tokenRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, fileName));
continue;
}
}
Expand Down Expand Up @@ -439,7 +442,7 @@ private void AnalyzeFile(string filePath)
}
catch (Exception dscResourceRuleException)
{
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
continue;
}
}
Expand Down Expand Up @@ -481,7 +484,7 @@ private void AnalyzeFile(string filePath)
}
catch (Exception dscResourceRuleException)
{
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, filePath));
continue;
}
}
Expand Down Expand Up @@ -516,7 +519,7 @@ private void AnalyzeFile(string filePath)
}
catch (Exception externalRuleException)
{
WriteError(new ErrorRecord(externalRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, fileName));
WriteError(new ErrorRecord(externalRuleException, Strings.RuleErrorMessage, ErrorCategory.InvalidOperation, fileName));
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions Engine/Generic/DiagnosticRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class DiagnosticRecord
private string ruleName;
private DiagnosticSeverity severity;
private string scriptName;
private string ruleSuppressionId;

/// <summary>
/// Represents a string from the rule about why this diagnostic was created.
Expand Down Expand Up @@ -62,7 +63,16 @@ public string ScriptName
{
get { return scriptName; }
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
set { scriptName = System.IO.Path.GetFileName(value); ; }
set { scriptName = System.IO.Path.GetFileName(value); }
}

/// <summary>
/// Returns the rule id for this record
/// </summary>
public string RuleSuppressionID
{
get { return ruleSuppressionId; }
set { ruleSuppressionId = value; }
}

/// <summary>
Expand All @@ -81,13 +91,14 @@ public DiagnosticRecord()
/// <param name="ruleName">The name of the rule that created this diagnostic</param>
/// <param name="severity">The severity of this diagnostic</param>
/// <param name="scriptName">The name of the script file being analyzed</param>
public DiagnosticRecord(string message, IScriptExtent extent, string ruleName, DiagnosticSeverity severity, string scriptName)
public DiagnosticRecord(string message, IScriptExtent extent, string ruleName, DiagnosticSeverity severity, string scriptName, string ruleId = null)
{
Message = string.IsNullOrEmpty(message) ? string.Empty : message;
RuleName = string.IsNullOrEmpty(ruleName) ? string.Empty : ruleName;
Extent = extent;
Severity = severity;
ScriptName = string.IsNullOrEmpty(scriptName) ? string.Empty : scriptName;
ruleSuppressionId = ruleId;
}
}

Expand Down
181 changes: 181 additions & 0 deletions Engine/Generic/RuleSuppression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using System;
using System.Linq;
using System.Management.Automation.Language;
using System.Collections.Generic;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
{
/// <summary>
///
/// </summary>
public class RuleSuppression
{
/// <summary>
/// The start offset of the rule suppression
/// </summary>
public int StartOffset
{
get;
set;
}

/// <summary>
/// The end offset of the rule suppression
/// </summary>
public int EndOffset
{
get;
set;
}

/// <summary>
/// Name of the rule being suppressed
/// </summary>
public string RuleName
{
get;
set;
}

/// <summary>
/// ID of the violation instance
/// </summary>
public string RuleSuppressionID
{
get;
set;
}

/// <summary>
/// Returns error occurred in trying to parse the attribute
/// </summary>
public string Error
{
get;
set;
}

/// <summary>
/// Returns rule suppression from an attribute ast that has the type suppressmessageattribute
/// </summary>
/// <param name="attrAst"></param>
/// <param name="start"></param>
/// <param name="end"></param>
public RuleSuppression(AttributeAst attrAst, int start, int end)
{
Error = String.Empty;

if (attrAst != null)
{
var positionalArguments = attrAst.PositionalArguments;
var namedArguments = attrAst.NamedArguments;

int lastPositionalArgumentsOffset = -1;

if (positionalArguments != null && positionalArguments.Count != 0)
{
int count = positionalArguments.Count;
lastPositionalArgumentsOffset = positionalArguments[positionalArguments.Count - 1].Extent.StartOffset;

if (positionalArguments.Any(item => !(item is StringConstantExpressionAst)))
{
Error = Strings.StringConstantArgumentsSuppressionAttributeError;
}
else
{
switch (count)
{
case 2:
RuleSuppressionID = (positionalArguments[1] as StringConstantExpressionAst).Value;
goto case 1;

case 1:
RuleName = (positionalArguments[0] as StringConstantExpressionAst).Value;
goto default;

default:
break;
}
}
}

if (namedArguments != null && namedArguments.Count != 0)
{
foreach (var name in namedArguments)
{
if (name.Extent.StartOffset < lastPositionalArgumentsOffset)
{
Error = Strings.NamedArgumentsBeforePositionalError;
break;
}
else if (!(name.Argument is StringConstantExpressionAst))
{
Error = Strings.StringConstantArgumentsSuppressionAttributeError;
break;
}

switch (name.ArgumentName.ToLower())
{
case "rulename":
if (!String.IsNullOrWhiteSpace(RuleName))
{
Error = String.Format(Strings.NamedAndPositionalArgumentsConflictError, name);
}

RuleName = (name.Argument as StringConstantExpressionAst).Value;
goto default;

case "rulesuppressionid":
if (!String.IsNullOrWhiteSpace(RuleSuppressionID))
{
Error = String.Format(Strings.NamedAndPositionalArgumentsConflictError, name);
}

RuleSuppressionID = (name.Argument as StringConstantExpressionAst).Value;
goto default;

default:
break;
}
}
}
}

StartOffset = start;
EndOffset = end;
}

/// <summary>
/// Given a list of attribute asts, return a list of rule suppression
/// with startoffset at start and endoffset at end
/// </summary>
/// <param name="attrAsts"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> attrAsts, int start, int end)
{
List<RuleSuppression> result = new List<RuleSuppression>();

if (attrAsts == null)
{
return result;
}

IEnumerable<AttributeAst> suppressionAttribute = attrAsts.Where(
item => item.TypeName.GetReflectionType() == typeof(System.Diagnostics.CodeAnalysis.SuppressMessageAttribute));

foreach (var attributeAst in suppressionAttribute)
{
RuleSuppression ruleSupp = new RuleSuppression(attributeAst, start, end);

if (string.IsNullOrWhiteSpace(ruleSupp.Error))
{
result.Add(ruleSupp);
}
}

return result;
}
}
}
Loading