Skip to content

Add rule severity support for Get-ScriptAnalyzerRule #52

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 3 commits into from
Apr 22, 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
29 changes: 24 additions & 5 deletions Engine/Commands/GetScriptAnalyzerRuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
// THE SOFTWARE.
//

using Microsoft.PowerShell.Commands;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using System.Resources;
using System.Threading;
using System.Reflection;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Commands
{
Expand Down Expand Up @@ -56,6 +53,21 @@ public string[] Name
set { name = value; }
}
private string[] name;

/// <summary>
/// Severity: Array of the severity types to be enabled.
/// </summary>
/// </summary>
[ValidateSet("Warning", "Error", "Information", IgnoreCase = true)]
[Parameter(Mandatory = false)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Severity
{
get { return severity; }
set { severity = value; }
}
private string[] severity;

#endregion Parameters

#region Private Members
Expand Down Expand Up @@ -128,9 +140,16 @@ protected override void ProcessRecord()
}
else
{
if (severity != null)
{
var ruleSeverity = severity.Select(item => Enum.Parse(typeof (RuleSeverity), item));
rules = rules.Where(item => ruleSeverity.Contains(item.GetSeverity())).ToList();
}

foreach (IRule rule in rules)
{
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(), rule.GetSourceType(), rule.GetSourceName()));
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(),
rule.GetSourceType(), rule.GetSourceName(), rule.GetSeverity()));
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Engine/Generic/AvoidCmdletGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
/// </summary>
/// <returns>The source type of the rule.</returns>
public abstract SourceType GetSourceType();

/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public abstract RuleSeverity GetSeverity();
}
}
2 changes: 2 additions & 0 deletions Engine/Generic/AvoidParameterGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
/// </summary>
/// <returns>The source type of the rule.</returns>
public abstract SourceType GetSourceType();

public abstract RuleSeverity GetSeverity();
}
}
6 changes: 6 additions & 0 deletions Engine/Generic/ExternalRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public SourceType GetSourceType()
return SourceType.Module;
}

//Set the community rule level as warning as the current implementation does not require user to specify rule severity when defining their functions in PS scripts
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}

public string GetSourceName()
{
return this.srcName;
Expand Down
13 changes: 7 additions & 6 deletions Engine/Generic/IRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
// THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
{
/// <summary>
Expand Down Expand Up @@ -52,5 +46,12 @@ public interface IRule
/// </summary>
/// <returns>The source type of the rule.</returns>
SourceType GetSourceType();

/// <summary>
/// GetSeverity: Retrieves severity of the rule.
/// </summary>
/// <returns></returns>
RuleSeverity GetSeverity();

}
}
14 changes: 13 additions & 1 deletion Engine/Generic/RuleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class RuleInfo
private string description;
private SourceType sourceType;
private string sourceName;
private RuleSeverity ruleSeverity;

/// <summary>
/// Name: The name of the rule.
Expand Down Expand Up @@ -81,6 +82,16 @@ public string SourceName
private set { sourceName = value; }
}

/// <summary>
/// Severity : The severity of the rule violation.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public RuleSeverity Severity
{
get { return ruleSeverity; }
private set { ruleSeverity = value; }
}

/// <summary>
/// Constructor for a RuleInfo.
/// </summary>
Expand All @@ -89,13 +100,14 @@ public string SourceName
/// <param name="description">Description of the rule.</param>
/// <param name="sourceType">Source type of the rule.</param>
/// <param name="sourceName">Source name of the rule.</param>
public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName)
public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName, RuleSeverity severity)
{
Name = name;
CommonName = commonName;
Description = description;
SourceType = sourceType;
SourceName = sourceName;
Severity = severity;
}
}
}
35 changes: 35 additions & 0 deletions Engine/Generic/RuleSeverity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright (c) Microsoft Corporation.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
{
/// <summary>
/// Represents the severity of a PSScriptAnalyzer rule
/// </summary>
public enum RuleSeverity : uint
{
/// <summary>
/// Information: This warning is trivial, but may be useful. They are recommended by PowerShell best practice.
/// </summary>
Information = 0,

/// <summary>
/// WARNING: This warning may cause a problem or does not follow PowerShell's recommended guidelines.
/// </summary>
Warning = 1,

/// <summary>
/// ERROR: This warning is likely to cause a problem or does not follow PowerShell's required guidelines.
/// </summary>
Error = 2,
};
}
5 changes: 2 additions & 3 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ internal class ScriptAnalyzer
#region Private memebers

private CompositionContainer container;
private const string baseName = "Microsoft.Windows.Powershell.ScriptAnalyzer.Properties.Strings";

#endregion

Expand Down Expand Up @@ -91,7 +90,7 @@ public static ScriptAnalyzer Instance
public void Initialize()
{
// Clear external rules for each invoke.
this.ExternalRules = new List<ExternalRule>();
ExternalRules = new List<ExternalRule>();

// Initialize helper
Helper.Instance.Initialize();
Expand Down Expand Up @@ -129,7 +128,7 @@ public void Initilaize(Dictionary<string, List<string>> result)
List<string> paths = new List<string>();

// Clear external rules for each invoke.
this.ExternalRules = new List<ExternalRule>();
ExternalRules = new List<ExternalRule>();

// Initialize helper
Helper.Instance.Initialize();
Expand Down
16 changes: 8 additions & 8 deletions Engine/ScriptAnalyzer.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Label>Rule Name</Label>
</TableColumnHeader>
<TableColumnHeader>
<Width>10</Width>
<Width>12</Width>
<Label>Severity</Label>
</TableColumnHeader>
<TableColumnHeader>
Expand Down Expand Up @@ -62,19 +62,19 @@
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>30</Width>
<Label>Name</Label>
<Width>35</Width>
<Label>Rule Name</Label>
</TableColumnHeader>
<TableColumnHeader>
<Width>33</Width>
<Label>CommonName</Label>
<Width>15</Width>
<Label>Severity</Label>
</TableColumnHeader>
<TableColumnHeader>
<Width>45</Width>
<Width>60</Width>
<Label>Description</Label>
</TableColumnHeader>
<TableColumnHeader>
<Width>8</Width>
<Width>10</Width>
<Label>Source</Label>
</TableColumnHeader>
</TableHeaders>
Expand All @@ -86,7 +86,7 @@
<PropertyName>Name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>CommonName</PropertyName>
<PropertyName>Severity</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Description</PropertyName>
Expand Down
2 changes: 1 addition & 1 deletion Engine/ScriptAnalyzer.types.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<Name>DefaultDisplayPropertySet</Name>
<ReferencedProperties>
<Name>Name</Name>
<Name>CommonName</Name>
<Name>Severity</Name>
<Name>Description</Name>
<Name>SourceName</Name>
</ReferencedProperties>
Expand Down
17 changes: 9 additions & 8 deletions Rules/AvoidAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@
//

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Language;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System.ComponentModel.Composition;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.Reflection;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
{
Expand Down Expand Up @@ -97,6 +89,15 @@ public SourceType GetSourceType()
return SourceType.Builtin;
}

/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}

/// <summary>
/// GetSourceName: Retrieves the name of the module/assembly the rule is from.
/// </summary>
Expand Down
16 changes: 9 additions & 7 deletions Rules/AvoidDefaultTrueValueSwitchParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@
//

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Language;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System.ComponentModel.Composition;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.Reflection;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
{
Expand Down Expand Up @@ -91,6 +84,15 @@ public SourceType GetSourceType()
return SourceType.Builtin;
}

/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}

/// <summary>
/// GetSourceName: Retrieves the module/assembly name the rule is from.
/// </summary>
Expand Down
17 changes: 9 additions & 8 deletions Rules/AvoidEmptyCatchBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@
//

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Language;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System.ComponentModel.Composition;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.Reflection;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
{
Expand Down Expand Up @@ -91,6 +83,15 @@ public SourceType GetSourceType()
return SourceType.Builtin;
}

/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}

/// <summary>
/// Method: Retrieves the module/assembly name the rule is from.
/// </summary>
Expand Down
Loading