Skip to content

Add ImplementingType to RuleInfo object #1250

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
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
2 changes: 1 addition & 1 deletion Engine/Commands/GetScriptAnalyzerRuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected override void ProcessRecord()
foreach (IRule rule in rules)
{
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(),
rule.GetSourceType(), rule.GetSourceName(), rule.GetSeverity()));
rule.GetSourceType(), rule.GetSourceName(), rule.GetSeverity(), rule.GetType()));
}
}
}
Expand Down
38 changes: 35 additions & 3 deletions Engine/Generic/RuleInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic
Expand All @@ -16,6 +17,7 @@ public class RuleInfo
private SourceType sourceType;
private string sourceName;
private RuleSeverity ruleSeverity;
private Type implementingType;

/// <summary>
/// Name: The name of the rule.
Expand Down Expand Up @@ -50,7 +52,7 @@ public string Description
/// <summary>
/// SourceType: The source type of the rule.
/// </summary>
///
///
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public SourceType SourceType
{
Expand Down Expand Up @@ -78,6 +80,16 @@ public RuleSeverity Severity
private set { ruleSeverity = value; }
}

/// <summary>
/// ImplementingType : The type which implements the rule.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the CA rule is right to call this out. C# has auto-properties now that can simplify this property to public Type { get; } whereby the private setter is included by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah - I didn't want to change them all, so I just followed the previous example for consistency. I'm certainly happy to change all of them.

public Type ImplementingType
{
get { return implementingType; }
private set { implementingType = value; }
}

/// <summary>
/// Constructor for a RuleInfo.
/// </summary>
Expand All @@ -93,12 +105,32 @@ public RuleInfo(string name, string commonName, string description, SourceType s
Description = description;
SourceType = sourceType;
SourceName = sourceName;
Severity = severity;
Severity = severity;
}

/// <summary>
/// Constructor for a RuleInfo.
/// </summary>
/// <param name="name">Name of the rule.</param>
/// <param name="commonName">Common Name of the rule.</param>
/// <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>
/// <param name="implementingType">The dotnet type of the rule.</param>
public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName, RuleSeverity severity, Type implementingType)
{
RuleName = name;
CommonName = commonName;
Description = description;
SourceType = sourceType;
SourceName = sourceName;
Severity = severity;
ImplementingType = implementingType;
}

public override string ToString()
{
return RuleName;
}
}
}
}
8 changes: 8 additions & 0 deletions Tests/Engine/GetScriptAnalyzerRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,11 @@ Describe "TestWildCard" {
$rules.Count | Should -Be 4
}
}

Describe "TestImplementingType" {
It "retrieves rule which have an implementing type" {
$rule = Get-ScriptAnalyzerRule PSPlaceCloseBrace
$type = $rule.ImplementingType
$type.BaseType.Name | Should -Be "ConfigurableRule"
}
}