Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c34eb23
Implemented the AvoidUsingArrayList rule to warn when the ArrayList c…
iRon7 Apr 14, 2026
ba1167d
Testing-Commit-CSpell-issue
iRon7 Apr 16, 2026
77384e1
Apply suggestion from @liamjpeters
iRon7 Apr 16, 2026
77d1e6d
Update docs/Rules/AvoidUsingArrayList.md
iRon7 Apr 16, 2026
d9b88a8
Updated rule help
iRon7 Apr 16, 2026
1eb92e9
Merge branch '#2147AvoidArrayList' of https://github.com/iRon7/PSScri…
iRon7 Apr 16, 2026
c0aa82b
Changed "unintentionally"
iRon7 Apr 16, 2026
7bc3dfa
Update Rules/AvoidUsingArrayList.cs
iRon7 Apr 16, 2026
b35becb
Update Tests/Rules/AvoidUsingArrayList.tests.ps1
iRon7 Apr 16, 2026
9ed1355
ArrayListName could be null
iRon7 Apr 16, 2026
e1362e6
Resolved camelCase
iRon7 Apr 16, 2026
b80f01c
Remove ComponentModel namespace
iRon7 Apr 16, 2026
f913b1f
Fixed tests
iRon7 Apr 16, 2026
b3a5c3c
Updated Tests
iRon7 Apr 16, 2026
8aefe4c
fixed and tested empty (dynamic) BoundParameter
iRon7 Apr 16, 2026
ad49041
Robuster Pester tests
iRon7 Apr 17, 2026
4bc41e9
Configurable (enable by default)
iRon7 Apr 18, 2026
fb27627
Fixed ConstantValue null check test and rule
iRon7 Apr 21, 2026
5a66672
Merge branch 'main' into #2147AvoidArrayList
iRon7 May 5, 2026
973d2e7
Better UsingStatements handling and disable AvoidUsingArrayList by de…
iRon7 May 6, 2026
14ed9f5
`[ArrayList]::new()` without a `using namespace System.Collections`
iRon7 May 6, 2026
2a46b7f
Potential fix for pull request finding
iRon7 May 11, 2026
0646b3d
Resolve Copilot suggestions and resolved `Method not found: ScriptBlo…
iRon7 May 11, 2026
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
195 changes: 195 additions & 0 deletions Rules/AvoidUsingArrayList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
using System.Globalization;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System.Text.RegularExpressions;
using System.Linq;

namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// AvoidUsingArrayList: Checks for use of the ArrayList class
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class AvoidUsingArrayList : ConfigurableRule
{

/// <summary>
/// Construct an object of AvoidUsingArrayList type.
/// </summary>
public AvoidUsingArrayList() {
Enable = false;
}

/// <summary>
/// Analyzes the given ast to find the [violation]
/// </summary>
/// <param name="ast">AST to be analyzed. This should be non-null</param>
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
/// <returns>A an enumerable type containing the violations</returns>
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) { throw new ArgumentNullException(nameof(ast), Strings.NullAstErrorMessage); }

// If there is an using statement for the Collections namespace, check for the full typename.
// Otherwise also check for the bare ArrayList name.
Regex arrayListName = null;
if (ast is ScriptBlockAst sbAst) {
// sbAst.UsingStatements causes an error: Method not found: ScriptBlockAst.get_UsingStatements()
IEnumerable<Ast> usingStatements = usingStatements = sbAst.FindAll(testAst => testAst is UsingStatementAst, false);
foreach (UsingStatementAst usingAst in usingStatements.Cast<UsingStatementAst>())
{
if (
usingAst.UsingStatementKind == UsingStatementKind.Namespace &&
(
usingAst.Name.Value.Equals("Collections", StringComparison.OrdinalIgnoreCase) ||
usingAst.Name.Value.Equals("System.Collections", StringComparison.OrdinalIgnoreCase)
)
)
{
arrayListName = new Regex(@"^((System\.)?Collections\.)?ArrayList$", RegexOptions.IgnoreCase);
break;
}
}
}
if (arrayListName == null) { arrayListName = new Regex(@"^(System\.)?Collections\.ArrayList$", RegexOptions.IgnoreCase); }
Comment thread
iRon7 marked this conversation as resolved.

// Find all type initializers that create a new instance of the ArrayList class.
IEnumerable<Ast> typeAsts = ast.FindAll(testAst =>
(
testAst is ConvertExpressionAst convertAst &&
convertAst.StaticType != null &&
convertAst.StaticType.FullName == "System.Collections.ArrayList"
) ||
(
testAst is TypeExpressionAst typeAst &&
typeAst.TypeName != null &&
arrayListName.IsMatch(typeAst.TypeName.Name) &&
typeAst.Parent is InvokeMemberExpressionAst parentAst &&
parentAst.Member != null &&
parentAst.Member is StringConstantExpressionAst memberAst &&
memberAst.Value.Equals("new", StringComparison.OrdinalIgnoreCase)
),
true
);

foreach (Ast typeAst in typeAsts)
{
IScriptExtent Extent = typeAst is ConvertExpressionAst? typeAst.Extent : typeAst.Parent.Extent;
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.AvoidUsingArrayListError,
Extent.Text),
Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName
);
}

// Find all New-Object cmdlets that create a new instance of the ArrayList class.
var newObjectCommands = ast.FindAll(testAst =>
testAst is CommandAst cmdAst &&
cmdAst.GetCommandName() != null &&
cmdAst.GetCommandName().Equals("New-Object", StringComparison.OrdinalIgnoreCase),
true);

foreach (CommandAst cmd in newObjectCommands)
{
// Use StaticParameterBinder to reliably get parameter values
var bindingResult = StaticParameterBinder.BindCommand(cmd, true);

// Check for -TypeName parameter
if (
bindingResult.BoundParameters.TryGetValue("TypeName", out ParameterBindingResult typeNameBinding) &&
typeNameBinding.ConstantValue is string typeName &&
arrayListName.IsMatch(typeName)
)
Comment thread
iRon7 marked this conversation as resolved.
{
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.AvoidUsingArrayListError,
cmd.Extent.Text),
cmd.Extent,
GetName(),
DiagnosticSeverity.Warning,
Comment thread
bergmeister marked this conversation as resolved.
fileName
);
}
}
}

/// <summary>
/// Retrieves the common name of this rule.
/// </summary>
public override string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingArrayListCommonName);
}

/// <summary>
/// Retrieves the description of this rule.
/// </summary>
public override string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingArrayListDescription);
}

/// <summary>
/// Retrieves the name of this rule.
/// </summary>
public override string GetName()
{
return string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.AvoidUsingArrayListName);
}

/// <summary>
/// Retrieves the severity of the rule: error, warning or information.
/// </summary>
public override RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}

/// <summary>
/// Gets the severity of the returned diagnostic record: error, warning, or information.
/// </summary>
/// <returns></returns>
public DiagnosticSeverity GetDiagnosticSeverity()
{
return DiagnosticSeverity.Warning;
}

/// <summary>
/// Retrieves the name of the module/assembly the rule is from.
/// </summary>
public override string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}

/// <summary>
/// Retrieves the type of the rule, Builtin, Managed or Module.
/// </summary>
public override SourceType GetSourceType()
{
return SourceType.Builtin;
}
}
}

12 changes: 12 additions & 0 deletions Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,18 @@
<data name="AvoidSemicolonsAsLineTerminatorsError" xml:space="preserve">
<value>Line ends with a semicolon</value>
</data>
<data name="AvoidUsingArrayListCommonName" xml:space="preserve">
<value>Avoid using the ArrayList class</value>
</data>
<data name="AvoidUsingArrayListDescription" xml:space="preserve">
<value>Avoid using the ArrayList class in PowerShell scripts. Consider using generic collections or fixed arrays instead.</value>
</data>
<data name="AvoidUsingArrayListName" xml:space="preserve">
<value>AvoidUsingArrayList</value>
</data>
<data name="AvoidUsingArrayListError" xml:space="preserve">
<value>The ArrayList class is used in '{0}'. Consider using a generic collection or a fixed array instead.</value>
</data>
<data name="PlaceOpenBraceName" xml:space="preserve">
<value>PlaceOpenBrace</value>
</data>
Expand Down
Loading