Skip to content
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

Convert UseSingularNouns to configurable rule and add Windows to allowlist #1858

Merged
merged 6 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 14 additions & 12 deletions Rules/UseSingularNouns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,23 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class CmdletSingularNoun : IScriptRule
public class CmdletSingularNoun : ConfigurableRule
{
[ConfigurableRuleProperty(defaultValue: new string[] { "Data", "Windows" })]
public string[] NounAllowList { get; set; }

private readonly string[] nounAllowList =
public CmdletSingularNoun()
{
"Data"
};
Enable = true;
}

/// <summary>
/// Checks that all defined cmdlet use singular noun
/// </summary>
/// <param name="ast"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullCommandInfoError);

Expand All @@ -70,7 +72,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)

if (pluralizer.CanOnlyBePlural(noun))
{
if (nounAllowList.Contains(noun, StringComparer.OrdinalIgnoreCase))
if (NounAllowList.Contains(noun, StringComparer.OrdinalIgnoreCase))
{
continue;
}
Expand Down Expand Up @@ -98,7 +100,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
/// GetName: Retrieves the name of this rule.
/// </summary>
/// <returns>The name of this rule</returns>
public string GetName()
public override string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseSingularNounsName);
}
Expand All @@ -107,7 +109,7 @@ public string GetName()
/// GetName: Retrieves the common name of this rule.
/// </summary>
/// <returns>The common name of this rule</returns>
public string GetCommonName()
public override string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsCommonName);
}
Expand All @@ -116,15 +118,15 @@ public string GetCommonName()
/// GetDescription: Retrieves the description of this rule.
/// </summary>
/// <returns>The description of this rule</returns>
public string GetDescription()
public override string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsDescription);
}

/// <summary>
/// GetSourceType: Retrieves the type of the rule: builtin, managed or module.
/// </summary>
public SourceType GetSourceType()
public override SourceType GetSourceType()
{
return SourceType.Builtin;
}
Expand All @@ -133,15 +135,15 @@ public SourceType GetSourceType()
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public RuleSeverity GetSeverity()
public override RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}

/// <summary>
/// GetSourceName: Retrieves the module/assembly name the rule is from.
/// </summary>
public string GetSourceName()
public override string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}
Expand Down
29 changes: 28 additions & 1 deletion Tests/Rules/UseSingularNounsReservedVerbs.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Describe "UseSingularNouns" {

Context "When function names have nouns from allowlist" {

It "ignores function name ending with Data" {
It "ignores function name ending with Data by default" {
$nounViolationScript = @'
Function Add-SomeData
{
Expand All @@ -44,6 +44,33 @@ Write-Output "Adding some data"
-OutVariable violations
$violations.Count | Should -Be 0
}

It "ignores function name ending with Windows by default" {
$nounViolationScript = @'
Function Test-Windows
{
Write-Output "Testing Microsoft Windows"
}
'@
Invoke-ScriptAnalyzer -ScriptDefinition $nounViolationScript `
-IncludeRule "PSUseSingularNouns" `
-OutVariable violations
$violations.Count | Should -Be 0
}

It "ignores function names defined in settings" {
$nounViolationScript = @'
Function Get-Bananas
{
Write-Output "Bananas"
}
'@
Invoke-ScriptAnalyzer -ScriptDefinition $nounViolationScript -Settings @{
IncludeRules = @("PSUseSingularNouns")
Rules = @{ PSUseSingularNouns = @{ NounAllowList = "Bananas" } }
} | Should -BeNullOrEmpty
}

}

Context "When there are no violations" {
Expand Down
2 changes: 1 addition & 1 deletion docs/Rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ The PSScriptAnalyzer contains the following rule definitions.
| [UseProcessBlockForPipelineCommand](./UseProcessBlockForPipelineCommand.md) | Warning | Yes | |
| [UsePSCredentialType](./UsePSCredentialType.md) | Warning | Yes | |
| [UseShouldProcessForStateChangingFunctions](./UseShouldProcessForStateChangingFunctions.md) | Warning | Yes | |
| [UseSingularNouns](./UseSingularNouns.md) | Warning | Yes | |
| [UseSingularNouns](./UseSingularNouns.md) | Warning | Yes | Yes |
| [UseSupportsShouldProcess](./UseSupportsShouldProcess.md) | Warning | Yes | |
| [UseToExportFieldsInManifest](./UseToExportFieldsInManifest.md) | Warning | Yes | |
| [UseUsingScopeModifierInNewRunspaces](./UseUsingScopeModifierInNewRunspaces.md) | Warning | Yes | |
Expand Down
21 changes: 21 additions & 0 deletions docs/Rules/UseSingularNouns.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ title: UseSingularNouns

MJVL marked this conversation as resolved.
Show resolved Hide resolved
PowerShell team best practices state cmdlets should use singular nouns and not plurals.

## Configuration

```powershell
Rules = @{
UseSingularNouns = @{
NounAllowList = 'Data', 'Windows', 'Foos'
Enable = $true
}
}
```

### Parameters

#### `UseSingularNouns: string[]` (Default value is `{'Data', 'Windows'}`)

Commands to be excluded from this rule. `Data` and `Windows` are common false positives and are excluded by default

#### Enable: `bool` (Default value is `$true`)

Enable or disable the rule during ScriptAnalyzer invocation.

## How

Change plurals to singular.
Expand Down