Skip to content

Commit 95ab52a

Browse files
committed
Improved heuristics for Singular nouns rule
1 parent 86a5af4 commit 95ab52a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Rules/UseSingularNouns.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
1818
using System.ComponentModel.Composition;
1919
using System.Globalization;
20+
using System.Text.RegularExpressions;
2021

2122
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2223
{
@@ -46,6 +47,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
4647
{
4748
funcNamePieces = funcAst.Name.Split(funcSeperator);
4849
String noun = funcNamePieces[1];
50+
51+
// Convert the noun part of the function into a series of space delimited words
52+
// This helps the PluralizationService to provide an accurate determination about the plurality of the string
53+
noun = SplitCamelCaseString(noun);
54+
4955
var ps = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
5056

5157
if (!ps.IsSingular(noun) && ps.IsPlural(noun))
@@ -108,6 +114,19 @@ public string GetSourceName()
108114
{
109115
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
110116
}
117+
118+
/// <summary>
119+
/// SplitCamelCaseString: Splits a Camel Case'd string into individual words with space delimited
120+
/// </summary>
121+
private string SplitCamelCaseString(string input)
122+
{
123+
if (String.IsNullOrEmpty(input))
124+
{
125+
return String.Empty;
126+
}
127+
128+
return Regex.Replace(input, "([A-Z])", " $1", RegexOptions.Compiled).Trim();
129+
}
111130
}
112131

113132
}

Tests/Rules/GoodCmdlet.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,19 @@ function Get-Reserved*
260260
End
261261
{
262262
}
263+
}
264+
265+
<#
266+
.Synopsis
267+
function that has a noun that is singular
268+
.DESCRIPTION
269+
270+
.EXAMPLE
271+
272+
.EXAMPLE
273+
274+
#>
275+
function Get-MyWidgetStatus
276+
{
277+
263278
}

0 commit comments

Comments
 (0)