Skip to content

Commit 9f70999

Browse files
committed
Merge pull request #52 from PowerShell/ruleseverity
Add rule severity support for Get-ScriptAnalyzerRule
2 parents 6e831b1 + 7a7b4ca commit 9f70999

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+427
-242
lines changed

Engine/Commands/GetScriptAnalyzerRuleCommand.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@
1010
// THE SOFTWARE.
1111
//
1212

13+
using Microsoft.PowerShell.Commands;
1314
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1415
using System;
1516
using System.Collections.Generic;
16-
using System.ComponentModel.Composition;
1717
using System.Diagnostics.CodeAnalysis;
1818
using System.Globalization;
1919
using System.Linq;
2020
using System.Management.Automation;
21-
using System.Resources;
22-
using System.Threading;
23-
using System.Reflection;
2421

2522
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Commands
2623
{
@@ -56,6 +53,21 @@ public string[] Name
5653
set { name = value; }
5754
}
5855
private string[] name;
56+
57+
/// <summary>
58+
/// Severity: Array of the severity types to be enabled.
59+
/// </summary>
60+
/// </summary>
61+
[ValidateSet("Warning", "Error", "Information", IgnoreCase = true)]
62+
[Parameter(Mandatory = false)]
63+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
64+
public string[] Severity
65+
{
66+
get { return severity; }
67+
set { severity = value; }
68+
}
69+
private string[] severity;
70+
5971
#endregion Parameters
6072

6173
#region Private Members
@@ -128,9 +140,16 @@ protected override void ProcessRecord()
128140
}
129141
else
130142
{
143+
if (severity != null)
144+
{
145+
var ruleSeverity = severity.Select(item => Enum.Parse(typeof (RuleSeverity), item));
146+
rules = rules.Where(item => ruleSeverity.Contains(item.GetSeverity())).ToList();
147+
}
148+
131149
foreach (IRule rule in rules)
132150
{
133-
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(), rule.GetSourceType(), rule.GetSourceName()));
151+
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(),
152+
rule.GetSourceType(), rule.GetSourceName(), rule.GetSeverity()));
134153
}
135154
}
136155
}

Engine/Generic/AvoidCmdletGeneric.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
9393
/// </summary>
9494
/// <returns>The source type of the rule.</returns>
9595
public abstract SourceType GetSourceType();
96+
97+
/// <summary>
98+
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
99+
/// </summary>
100+
/// <returns></returns>
101+
public abstract RuleSeverity GetSeverity();
96102
}
97103
}

Engine/Generic/AvoidParameterGeneric.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
104104
/// </summary>
105105
/// <returns>The source type of the rule.</returns>
106106
public abstract SourceType GetSourceType();
107+
108+
public abstract RuleSeverity GetSeverity();
107109
}
108110
}

Engine/Generic/ExternalRule.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public SourceType GetSourceType()
5555
return SourceType.Module;
5656
}
5757

58+
//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
59+
public RuleSeverity GetSeverity()
60+
{
61+
return RuleSeverity.Warning;
62+
}
63+
5864
public string GetSourceName()
5965
{
6066
return this.srcName;

Engine/Generic/IRule.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
// THE SOFTWARE.
1111
//
1212

13-
using System;
14-
using System.Collections.Generic;
15-
using System.Linq;
16-
using System.Text;
17-
using System.Threading.Tasks;
18-
1913
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
2014
{
2115
/// <summary>
@@ -52,5 +46,12 @@ public interface IRule
5246
/// </summary>
5347
/// <returns>The source type of the rule.</returns>
5448
SourceType GetSourceType();
49+
50+
/// <summary>
51+
/// GetSeverity: Retrieves severity of the rule.
52+
/// </summary>
53+
/// <returns></returns>
54+
RuleSeverity GetSeverity();
55+
5556
}
5657
}

Engine/Generic/RuleInfo.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class RuleInfo
2929
private string description;
3030
private SourceType sourceType;
3131
private string sourceName;
32+
private RuleSeverity ruleSeverity;
3233

3334
/// <summary>
3435
/// Name: The name of the rule.
@@ -81,6 +82,16 @@ public string SourceName
8182
private set { sourceName = value; }
8283
}
8384

85+
/// <summary>
86+
/// Severity : The severity of the rule violation.
87+
/// </summary>
88+
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
89+
public RuleSeverity Severity
90+
{
91+
get { return ruleSeverity; }
92+
private set { ruleSeverity = value; }
93+
}
94+
8495
/// <summary>
8596
/// Constructor for a RuleInfo.
8697
/// </summary>
@@ -89,13 +100,14 @@ public string SourceName
89100
/// <param name="description">Description of the rule.</param>
90101
/// <param name="sourceType">Source type of the rule.</param>
91102
/// <param name="sourceName">Source name of the rule.</param>
92-
public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName)
103+
public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName, RuleSeverity severity)
93104
{
94105
Name = name;
95106
CommonName = commonName;
96107
Description = description;
97108
SourceType = sourceType;
98109
SourceName = sourceName;
110+
Severity = severity;
99111
}
100112
}
101113
}

Engine/Generic/RuleSeverity.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Copyright (c) Microsoft Corporation.
3+
//
4+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
// THE SOFTWARE.
11+
//
12+
13+
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
14+
{
15+
/// <summary>
16+
/// Represents the severity of a PSScriptAnalyzer rule
17+
/// </summary>
18+
public enum RuleSeverity : uint
19+
{
20+
/// <summary>
21+
/// Information: This warning is trivial, but may be useful. They are recommended by PowerShell best practice.
22+
/// </summary>
23+
Information = 0,
24+
25+
/// <summary>
26+
/// WARNING: This warning may cause a problem or does not follow PowerShell's recommended guidelines.
27+
/// </summary>
28+
Warning = 1,
29+
30+
/// <summary>
31+
/// ERROR: This warning is likely to cause a problem or does not follow PowerShell's required guidelines.
32+
/// </summary>
33+
Error = 2,
34+
};
35+
}

Engine/ScriptAnalyzer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ internal class ScriptAnalyzer
3333
#region Private memebers
3434

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

3837
#endregion
3938

@@ -91,7 +90,7 @@ public static ScriptAnalyzer Instance
9190
public void Initialize()
9291
{
9392
// Clear external rules for each invoke.
94-
this.ExternalRules = new List<ExternalRule>();
93+
ExternalRules = new List<ExternalRule>();
9594

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

131130
// Clear external rules for each invoke.
132-
this.ExternalRules = new List<ExternalRule>();
131+
ExternalRules = new List<ExternalRule>();
133132

134133
// Initialize helper
135134
Helper.Instance.Initialize();

Engine/ScriptAnalyzer.format.ps1xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<Label>Rule Name</Label>
1414
</TableColumnHeader>
1515
<TableColumnHeader>
16-
<Width>10</Width>
16+
<Width>12</Width>
1717
<Label>Severity</Label>
1818
</TableColumnHeader>
1919
<TableColumnHeader>
@@ -62,19 +62,19 @@
6262
<TableControl>
6363
<TableHeaders>
6464
<TableColumnHeader>
65-
<Width>30</Width>
66-
<Label>Name</Label>
65+
<Width>35</Width>
66+
<Label>Rule Name</Label>
6767
</TableColumnHeader>
6868
<TableColumnHeader>
69-
<Width>33</Width>
70-
<Label>CommonName</Label>
69+
<Width>15</Width>
70+
<Label>Severity</Label>
7171
</TableColumnHeader>
7272
<TableColumnHeader>
73-
<Width>45</Width>
73+
<Width>60</Width>
7474
<Label>Description</Label>
7575
</TableColumnHeader>
7676
<TableColumnHeader>
77-
<Width>8</Width>
77+
<Width>10</Width>
7878
<Label>Source</Label>
7979
</TableColumnHeader>
8080
</TableHeaders>
@@ -86,7 +86,7 @@
8686
<PropertyName>Name</PropertyName>
8787
</TableColumnItem>
8888
<TableColumnItem>
89-
<PropertyName>CommonName</PropertyName>
89+
<PropertyName>Severity</PropertyName>
9090
</TableColumnItem>
9191
<TableColumnItem>
9292
<PropertyName>Description</PropertyName>

Engine/ScriptAnalyzer.types.ps1xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<Name>DefaultDisplayPropertySet</Name>
4343
<ReferencedProperties>
4444
<Name>Name</Name>
45-
<Name>CommonName</Name>
45+
<Name>Severity</Name>
4646
<Name>Description</Name>
4747
<Name>SourceName</Name>
4848
</ReferencedProperties>

Rules/AvoidAlias.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,11 @@
1111
//
1212

1313
using System;
14-
using System.Collections.ObjectModel;
1514
using System.Collections.Generic;
16-
using System.Linq;
17-
using System.Text;
18-
using System.Threading.Tasks;
19-
using System.Management.Automation;
2015
using System.Management.Automation.Language;
2116
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
2217
using System.ComponentModel.Composition;
23-
using System.Resources;
2418
using System.Globalization;
25-
using System.Threading;
26-
using System.Reflection;
2719

2820
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
2921
{
@@ -97,6 +89,15 @@ public SourceType GetSourceType()
9789
return SourceType.Builtin;
9890
}
9991

92+
/// <summary>
93+
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
94+
/// </summary>
95+
/// <returns></returns>
96+
public RuleSeverity GetSeverity()
97+
{
98+
return RuleSeverity.Warning;
99+
}
100+
100101
/// <summary>
101102
/// GetSourceName: Retrieves the name of the module/assembly the rule is from.
102103
/// </summary>

Rules/AvoidDefaultTrueValueSwitchParameter.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,12 @@
1111
//
1212

1313
using System;
14-
using System.Collections.ObjectModel;
1514
using System.Collections.Generic;
1615
using System.Linq;
17-
using System.Text;
18-
using System.Threading.Tasks;
19-
using System.Management.Automation;
2016
using System.Management.Automation.Language;
2117
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
2218
using System.ComponentModel.Composition;
23-
using System.Resources;
2419
using System.Globalization;
25-
using System.Threading;
26-
using System.Reflection;
2720

2821
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
2922
{
@@ -91,6 +84,15 @@ public SourceType GetSourceType()
9184
return SourceType.Builtin;
9285
}
9386

87+
/// <summary>
88+
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
89+
/// </summary>
90+
/// <returns></returns>
91+
public RuleSeverity GetSeverity()
92+
{
93+
return RuleSeverity.Warning;
94+
}
95+
9496
/// <summary>
9597
/// GetSourceName: Retrieves the module/assembly name the rule is from.
9698
/// </summary>

Rules/AvoidEmptyCatchBlock.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,11 @@
1111
//
1212

1313
using System;
14-
using System.Collections.ObjectModel;
1514
using System.Collections.Generic;
16-
using System.Linq;
17-
using System.Text;
18-
using System.Threading.Tasks;
19-
using System.Management.Automation;
2015
using System.Management.Automation.Language;
2116
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
2217
using System.ComponentModel.Composition;
23-
using System.Resources;
2418
using System.Globalization;
25-
using System.Threading;
26-
using System.Reflection;
2719

2820
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
2921
{
@@ -91,6 +83,15 @@ public SourceType GetSourceType()
9183
return SourceType.Builtin;
9284
}
9385

86+
/// <summary>
87+
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
88+
/// </summary>
89+
/// <returns></returns>
90+
public RuleSeverity GetSeverity()
91+
{
92+
return RuleSeverity.Warning;
93+
}
94+
9495
/// <summary>
9596
/// Method: Retrieves the module/assembly name the rule is from.
9697
/// </summary>

0 commit comments

Comments
 (0)