Skip to content

Commit 974b1d1

Browse files
author
Kapil Borle
committed
Add more type check to UseLiteralInitializerForHashtable rule
1 parent 3168d36 commit 974b1d1

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

Rules/UseLiteralInitializerForHashtable.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public UseLiteralInitializerForHashtable()
3838
{
3939
var presetTypeNames = new string[]
4040
{
41-
"system.collection.hashtable",
41+
"system.collections.hashtable",
42+
"collections.hashtable",
4243
"hashtable"
4344
};
4445
presetTypeNameSet = new HashSet<string>(presetTypeNames, StringComparer.OrdinalIgnoreCase);
@@ -204,12 +205,11 @@ private void AnalyzeNewObjectCommand(CommandAst commandAst)
204205
return;
205206
}
206207

207-
if (argumentList != null)
208+
209+
if (argumentList != null
210+
&& HasIgnoreCaseComparerArg(argumentList))
208211
{
209-
if (argumentList.Any(arg => arg != null && arg.EndsWith("ignorecase", StringComparison.OrdinalIgnoreCase)))
210-
{
211-
return;
212-
}
212+
return;
213213
}
214214

215215
var dr = new DiagnosticRecord(
@@ -225,6 +225,9 @@ private void AnalyzeNewObjectCommand(CommandAst commandAst)
225225

226226
/// <summary>
227227
/// Interpret the named and unnamed arguments and assign them their corresponding parameters
228+
///
229+
/// PSv4 onwards there exists System.Management.Automation.Language.StaticParameterBinder.BindCommand to
230+
/// achieve identical objective. But since we support PSSA on PSv3 too we need this implementation.
228231
/// </summary>
229232
/// <param name="commandAst">An non-null instance of CommandAst. Expects it be commandast of "new-object" command</param>
230233
/// <param name="typeName">Returns the TypeName argument</param>
@@ -356,9 +359,11 @@ private List<CommandElementAst> GetNamedArguments(ReadOnlyCollection<CommandElem
356359
/// <param name="arguments">A collection of argument asts. Neither this nor any elements within it should be null</param>
357360
private bool HasIgnoreCaseComparerArg(ReadOnlyCollection<ExpressionAst> arguments)
358361
{
362+
var argumentsAsStrings = new List<string>();
359363
foreach (var arg in arguments)
360364
{
361365
var memberExprAst = arg as MemberExpressionAst;
366+
argumentsAsStrings.Add(null);
362367
if (memberExprAst == null)
363368
{
364369
continue;
@@ -369,13 +374,30 @@ private bool HasIgnoreCaseComparerArg(ReadOnlyCollection<ExpressionAst> argument
369374
{
370375
continue;
371376
}
377+
argumentsAsStrings[argumentsAsStrings.Count - 1] = strConstExprAst.Value;
378+
}
379+
return HasIgnoreCaseComparerArg(argumentsAsStrings);
380+
}
372381

373-
if (strConstExprAst.Value.EndsWith("ignorecase"))
382+
/// <summary>
383+
/// Checks if any argument in the given collection ends with "ignorecase"
384+
/// </summary>
385+
/// <param name="arguments">An enumerable of type string. Elements can be null but the collection must be non-null .</param>
386+
/// <returns></returns>
387+
private bool HasIgnoreCaseComparerArg(IEnumerable<string> arguments)
388+
{
389+
390+
foreach (var arg in arguments)
391+
{
392+
if (arg == null)
393+
{
394+
continue;
395+
}
396+
if (arg.EndsWith("ignorecase", StringComparison.OrdinalIgnoreCase))
374397
{
375398
return true;
376399
}
377400
}
378-
379401
return false;
380402
}
381403

Tests/Rules/UseLiteralInitializerForHashtable.tests.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ Describe "UseLiteralInitlializerForHashtable" {
66
It "has violation" {
77
$violationScriptDef = @'
88
$htable1 = new-object hashtable
9-
$htable2 = new-object system.collection.hashtable
9+
$htable2 = new-object system.collections.hashtable
1010
$htable3 = new-object -Typename hashtable -ArgumentList 10
11+
$htable4 = new-object collections.hashtable
1112
'@
1213
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $violationScriptDef -IncludeRule $ruleName
13-
$violations.Count | Should Be 3
14+
$violations.Count | Should Be 4
1415
}
1516

1617
It "does not detect violation if arguments given to new-object contain ignore case string comparer" {
@@ -37,11 +38,12 @@ Describe "UseLiteralInitlializerForHashtable" {
3738
It "has violation" {
3839
$violationScriptDef = @'
3940
$htable1 = [hashtable]::new()
40-
$htable2 = [system.collection.hashtable]::new()
41+
$htable2 = [system.collections.hashtable]::new()
4142
$htable3 = [hashtable]::new(10)
43+
$htable4 = [collections.hashtable]::new()
4244
'@
4345
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $violationScriptDef -IncludeRule $ruleName
44-
$violations.Count | Should Be 3
46+
$violations.Count | Should Be 4
4547
}
4648

4749
It "does not detect violation if arguments given to [hashtable]::new contain ignore case string comparer" {

0 commit comments

Comments
 (0)