Skip to content

Commit 0646b3d

Browse files
committed
Resolve Copilot suggestions and resolved Method not found: ScriptBlockAst.get_UsingStatements() error
1 parent 2a46b7f commit 0646b3d

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

Rules/AvoidUsingArrayList.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
4444
// Otherwise also check for the bare ArrayList name.
4545
Regex arrayListName = null;
4646
if (ast is ScriptBlockAst sbAst) {
47-
foreach (UsingStatementAst usingAst in sbAst.UsingStatements.Cast<UsingStatementAst>())
47+
// sbAst.UsingStatements causes an error: Method not found: ScriptBlockAst.get_UsingStatements()
48+
IEnumerable<Ast> usingStatements = usingStatements = sbAst.FindAll(testAst => testAst is UsingStatementAst, false);
49+
foreach (UsingStatementAst usingAst in usingStatements.Cast<UsingStatementAst>())
4850
{
4951
if (
5052
usingAst.UsingStatementKind == UsingStatementKind.Namespace &&
@@ -82,12 +84,13 @@ parentAst.Member is StringConstantExpressionAst memberAst &&
8284

8385
foreach (Ast typeAst in typeAsts)
8486
{
87+
IScriptExtent Extent = typeAst is ConvertExpressionAst? typeAst.Extent : typeAst.Parent.Extent;
8588
yield return new DiagnosticRecord(
8689
string.Format(
8790
CultureInfo.CurrentCulture,
8891
Strings.AvoidUsingArrayListError,
89-
typeAst.Parent.Extent.Text),
90-
typeAst.Parent.Extent,
92+
Extent.Text),
93+
Extent,
9194
GetName(),
9295
DiagnosticSeverity.Warning,
9396
fileName
@@ -108,9 +111,9 @@ testAst is CommandAst cmdAst &&
108111

109112
// Check for -TypeName parameter
110113
if (
111-
bindingResult.BoundParameters.ContainsKey("TypeName") &&
112-
bindingResult.BoundParameters["TypeName"].ConstantValue != null &&
113-
arrayListName.IsMatch(bindingResult.BoundParameters["TypeName"].ConstantValue as string)
114+
bindingResult.BoundParameters.TryGetValue("TypeName", out ParameterBindingResult typeNameBinding) &&
115+
typeNameBinding.ConstantValue is string typeName &&
116+
arrayListName.IsMatch(typeName)
114117
)
115118
{
116119
yield return new DiagnosticRecord(

docs/Rules/AvoidUsingArrayList.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ title: AvoidUsingArrayList
1010

1111
## Description
1212

13-
Per dotnet best practices, the
14-
[`ArrayList` class](https://learn.microsoft.com/dotnet/api/system.collections.arraylist)
15-
is not recommended for new development, the same recommendation applies to PowerShell:
13+
Per .NET best practices, the [`ArrayList` class][1] is not recommended for new development,
14+
the same recommendation applies to PowerShell:
1615

1716
Avoid the ArrayList class for new development.
18-
The `ArrayList` class is a non-generic collection that can hold objects of any type. This is inline with the fact
19-
that PowerShell is a weakly typed language. However, the `ArrayList` class does not provide any explicit type
20-
safety and performance benefits of generic collections. Instead of using an `ArrayList`, consider using either a
21-
[`System.Collections.Generic.List[Object]`](https://learn.microsoft.com/dotnet/api/system.collections.generic.list-1)
22-
class or a fixed PowerShell array.
23-
Besides, the `ArrayList.Add` method returns the index of the added element which often unintentionally pollutes the
24-
PowerShell pipeline and therefore might cause unexpected issues.
17+
The `ArrayList` class is a non-generic collection that can hold objects of any type.
18+
This is in line with the fact that PowerShell is a weakly typed language. However, the
19+
`ArrayList` class does not provide any explicit type safety and performance benefits
20+
of generic collections. Instead of using an `ArrayList`, consider using either a
21+
[`System.Collections.Generic.List[Object]`][2] class or a fixed PowerShell array.
22+
Besides, the `ArrayList.Add` method returns the index of the added element which often
23+
unintentionally pollutes the PowerShell pipeline and therefore might cause unexpected issues.
2524

2625
## How to Fix
2726

28-
In cases where only the `Add` method is used, you might just replace the `ArrayList` class with a generic
29-
`List[Object]` class but you could also consider using the idiomatic PowerShell pipeline syntax instead.
27+
In cases where only the `Add` method is used, you might just replace the `ArrayList` class
28+
with a generic `List[Object]` class but you could also consider using the idiomatic PowerShell
29+
pipeline syntax instead.
3030

3131
## Example
3232

@@ -49,4 +49,13 @@ $List = [System.Collections.Generic.List[Object]]::new()
4949
```powershell
5050
# Creating a fixed array by using the PowerShell pipeline
5151
$List = 1..3 | ForEach-Object { $_ }
52-
```
52+
```
53+
54+
### Parameters
55+
56+
- `Enable`: **bool** (Default value is `$false`)
57+
58+
Enable or disable the rule during ScriptAnalyzer invocation.
59+
60+
[1]: https://learn.microsoft.com/dotnet/api/system.collections.arraylist "ArrayList Class"
61+
[2]: https://learn.microsoft.com/dotnet/api/system.collections.generic.list-1 "List<T> Class"

0 commit comments

Comments
 (0)