|
15 | 15 | using System.Linq;
|
16 | 16 | using System.Management.Automation.Language;
|
17 | 17 | using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
|
18 |
| -#if !CORECLR |
19 |
| -using System.ComponentModel.Composition; |
20 |
| -#else |
| 18 | +#if CORECLR |
21 | 19 | using Pluralize.NET;
|
| 20 | +#else |
| 21 | +using System.ComponentModel.Composition; |
22 | 22 | #endif
|
23 | 23 | using System.Globalization;
|
24 | 24 | using System.Text.RegularExpressions;
|
@@ -55,48 +55,42 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
|
55 | 55 | char[] funcSeperator = { '-' };
|
56 | 56 | string[] funcNamePieces = new string[2];
|
57 | 57 |
|
58 |
| -#if !CORECLR |
59 |
| - var usCultureInfo = CultureInfo.GetCultureInfo("en-us"); |
60 |
| - var pluralizationService = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(usCultureInfo); |
61 |
| -#else |
62 |
| - var pluralizationService = new Pluralizer(); |
63 |
| -#endif |
| 58 | + var pluralizer = new PluralizerProxy(); |
64 | 59 |
|
65 | 60 | foreach (FunctionDefinitionAst funcAst in funcAsts)
|
66 | 61 | {
|
67 |
| - if (funcAst.Name != null && funcAst.Name.Contains('-')) |
| 62 | + if (funcAst.Name == null || !funcAst.Name.Contains('-')) |
| 63 | + { |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + string noun = GetLastWordInCmdlet(funcAst.Name); |
| 68 | + |
| 69 | + if (noun is null) |
68 | 70 | {
|
69 |
| - funcNamePieces = funcAst.Name.Split(funcSeperator); |
70 |
| - String nounPart = funcNamePieces[1]; |
71 |
| - |
72 |
| - // Convert the noun part of the function into a series of space delimited words |
73 |
| - // This helps the PluralizationService to provide an accurate determination about the plurality of the string |
74 |
| - nounPart = SplitCamelCaseString(nounPart); |
75 |
| - var words = nounPart.Split(new char[] { ' ' }); |
76 |
| - var noun = words.LastOrDefault(); |
77 |
| - if (noun == null) |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if (pluralizer.CanOnlyBePlural(noun)) |
| 75 | + { |
| 76 | + IScriptExtent extent = Helper.Instance.GetScriptExtentForFunctionName(funcAst); |
| 77 | + |
| 78 | + if (nounAllowList.Contains(noun, StringComparer.OrdinalIgnoreCase)) |
78 | 79 | {
|
79 | 80 | continue;
|
80 | 81 | }
|
81 | 82 |
|
82 |
| -#if !CORECLR |
83 |
| - if (!pluralizationService.IsSingular(noun) && pluralizationService.IsPlural(noun)) |
84 |
| -#else |
85 |
| - if (!pluralizationService.IsSingular(noun) && pluralizationService.IsPlural(noun)) |
86 |
| -#endif |
| 83 | + if (extent is null) |
87 | 84 | {
|
88 |
| - IScriptExtent extent = Helper.Instance.GetScriptExtentForFunctionName(funcAst); |
89 |
| - if (nounAllowList.Contains(noun, StringComparer.OrdinalIgnoreCase)) |
90 |
| - { |
91 |
| - continue; |
92 |
| - } |
93 |
| - if (null == extent) |
94 |
| - { |
95 |
| - extent = funcAst.Extent; |
96 |
| - } |
97 |
| - yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsError, funcAst.Name), |
98 |
| - extent, GetName(), DiagnosticSeverity.Warning, fileName); |
| 85 | + extent = funcAst.Extent; |
99 | 86 | }
|
| 87 | + |
| 88 | + yield return new DiagnosticRecord( |
| 89 | + string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsError, funcAst.Name), |
| 90 | + extent, |
| 91 | + GetName(), |
| 92 | + DiagnosticSeverity.Warning, |
| 93 | + fileName); |
100 | 94 | }
|
101 | 95 | }
|
102 | 96 |
|
@@ -155,17 +149,71 @@ public string GetSourceName()
|
155 | 149 | }
|
156 | 150 |
|
157 | 151 | /// <summary>
|
158 |
| - /// SplitCamelCaseString: Splits a Camel Case'd string into individual words with space delimited |
| 152 | + /// Gets the last word in a standard syntax, CamelCase cmdlet. |
| 153 | + /// If the cmdlet name is non-standard, returns null. |
159 | 154 | /// </summary>
|
160 |
| - private string SplitCamelCaseString(string input) |
| 155 | + private string GetLastWordInCmdlet(string cmdletName) |
| 156 | + { |
| 157 | + if (string.IsNullOrEmpty(cmdletName)) |
| 158 | + { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + // Cmdlet doesn't use CamelCase, so assume it's something like an initialism that shouldn't be singularized |
| 163 | + if (!char.IsLower(cmdletName[^1])) |
| 164 | + { |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | + for (int i = cmdletName.Length - 1; i >= 0; i--) |
| 169 | + { |
| 170 | + if (cmdletName[i] == '-') |
| 171 | + { |
| 172 | + // Cmdlet name ends in '-' -- we give up |
| 173 | + if (i == cmdletName.Length - 1) |
| 174 | + { |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 178 | + // Return everything after the dash |
| 179 | + return cmdletName.Substring(i + 1); |
| 180 | + } |
| 181 | + |
| 182 | + // We just changed from lower case to upper, so we have the end word |
| 183 | + if (char.IsUpper(cmdletName[i])) |
| 184 | + { |
| 185 | + return cmdletName.Substring(i); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // We shouldn't ever get here since we should always eventually hit a '-' |
| 190 | + // But if we do, assume this isn't supported cmdlet name |
| 191 | + return null; |
| 192 | + } |
| 193 | + |
| 194 | +#if CoreCLR |
| 195 | + private class PluralizerProxy |
161 | 196 | {
|
162 |
| - if (String.IsNullOrEmpty(input)) |
| 197 | + private readonly Pluralizer _pluralizer; |
| 198 | + |
| 199 | + public PluralizerProxy() |
163 | 200 | {
|
164 |
| - return String.Empty; |
| 201 | + _pluralizer = new Pluralizer(); |
165 | 202 | }
|
166 | 203 |
|
167 |
| - return Regex.Replace(input, "([A-Z])", " $1", RegexOptions.Compiled).Trim(); |
| 204 | + public bool CanOnlyBePlural(string noun) => |
| 205 | + !_pluralizer.IsSingular(noun) && _pluralizer.IsPlural(noun); |
168 | 206 | }
|
| 207 | +#else |
| 208 | + private class PluralizerProxy |
| 209 | + { |
| 210 | + private static readonly PluralizationService s_pluralizationService = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService( |
| 211 | + CultureInfo.GetCultureInfo('en-us')); |
| 212 | + |
| 213 | + public bool CanOnlyBePlural(string noun) => |
| 214 | + !s_pluralizationService.IsSingular(noun) && s_pluralizationService.IsPlural(noun); |
| 215 | + } |
| 216 | +#endif |
169 | 217 | }
|
170 | 218 |
|
171 | 219 | }
|
0 commit comments