Skip to content

Commit 4bc41e9

Browse files
committed
Configurable (enable by default)
1 parent ad49041 commit 4bc41e9

3 files changed

Lines changed: 134 additions & 34 deletions

File tree

Rules/AvoidUsingArrayList.cs

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
54
using System;
65
using System.Collections.Generic;
7-
using System.Globalization;
8-
using System.Management.Automation.Language;
9-
using System.Text.RegularExpressions;
10-
116
#if !CORECLR
127
using System.ComponentModel.Composition;
138
#endif
9+
using System.Globalization;
10+
using System.Management.Automation.Language;
11+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
12+
using System.Text.RegularExpressions;
1413

1514
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
1615
{
16+
/// <summary>
17+
/// AvoidUsingArrayList: Checks for use of the ArrayList class
18+
/// </summary>
1719
#if !CORECLR
1820
[Export(typeof(IScriptRule))]
1921
#endif
20-
21-
/// <summary>
22-
/// Rule that warns when the ArrayList class is used in a PowerShell script.
23-
/// </summary>
24-
public class AvoidUsingArrayListAsFunctionNames : IScriptRule
22+
public class AvoidUsingArrayList : ConfigurableRule
2523
{
2624

2725
/// <summary>
28-
/// Analyzes the PowerShell AST for uses of the ArrayList class.
26+
/// Construct an object of AvoidUsingArrayList type.
2927
/// </summary>
30-
/// <param name="ast">The PowerShell Abstract Syntax Tree to analyze.</param>
31-
/// <param name="fileName">The name of the file being analyzed (for diagnostic reporting).</param>
32-
/// <returns>A collection of diagnostic records for each violation.</returns>
28+
public AvoidUsingArrayList() {
29+
Enable = true;
30+
}
3331

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

@@ -118,26 +122,69 @@ testAst is CommandAst cmdAst &&
118122
fileName
119123
);
120124
}
121-
122125
}
123-
124-
125126
}
126127

127-
public string GetCommonName() => Strings.AvoidUsingArrayListCommonName;
128+
/// <summary>
129+
/// Retrieves the common name of this rule.
130+
/// </summary>
131+
public override string GetCommonName()
132+
{
133+
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingArrayListCommonName);
134+
}
128135

129-
public string GetDescription() => Strings.AvoidUsingArrayListDescription;
136+
/// <summary>
137+
/// Retrieves the description of this rule.
138+
/// </summary>
139+
public override string GetDescription()
140+
{
141+
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingArrayListDescription);
142+
}
130143

131-
public string GetName() => string.Format(
144+
/// <summary>
145+
/// Retrieves the name of this rule.
146+
/// </summary>
147+
public override string GetName()
148+
{
149+
return string.Format(
132150
CultureInfo.CurrentCulture,
133151
Strings.NameSpaceFormat,
134152
GetSourceName(),
135153
Strings.AvoidUsingArrayListName);
154+
}
136155

137-
public RuleSeverity GetSeverity() => RuleSeverity.Warning;
156+
/// <summary>
157+
/// Retrieves the severity of the rule: error, warning or information.
158+
/// </summary>
159+
public override RuleSeverity GetSeverity()
160+
{
161+
return RuleSeverity.Warning;
162+
}
138163

139-
public string GetSourceName() => Strings.SourceName;
164+
/// <summary>
165+
/// Gets the severity of the returned diagnostic record: error, warning, or information.
166+
/// </summary>
167+
/// <returns></returns>
168+
public DiagnosticSeverity GetDiagnosticSeverity()
169+
{
170+
return DiagnosticSeverity.Warning;
171+
}
172+
173+
/// <summary>
174+
/// Retrieves the name of the module/assembly the rule is from.
175+
/// </summary>
176+
public override string GetSourceName()
177+
{
178+
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
179+
}
140180

141-
public SourceType GetSourceType() => SourceType.Builtin;
181+
/// <summary>
182+
/// Retrieves the type of the rule, Builtin, Managed or Module.
183+
/// </summary>
184+
public override SourceType GetSourceType()
185+
{
186+
return SourceType.Builtin;
187+
}
142188
}
143-
}
189+
}
190+

Tests/Rules/AvoidUsingArrayList.tests.ps1

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ param()
99
BeforeAll {
1010
$ruleName = "PSAvoidUsingArrayList"
1111
$ruleMessage = "The ArrayList class is used in '{0}'. Consider using a generic collection or a fixed array instead."
12+
$usingCollections = 'using namespace system.collections' + [Environment]::NewLine
13+
$usingGeneric = 'using namespace System.Collections.Generic' + [Environment]::NewLine
1214
}
1315

1416
Describe "AvoidArrayList" {
1517

1618
Context "When there are violations" {
1719

18-
BeforeAll {
19-
$usingCollections = 'using namespace system.collections' + [Environment]::NewLine
20-
}
21-
2220
It "Unquoted New-Object type" {
2321
$scriptDefinition = $usingCollections + {
2422
$List = New-Object ArrayList
@@ -166,10 +164,6 @@ Describe "AvoidArrayList" {
166164

167165
Context "When there are no violations" {
168166

169-
BeforeAll {
170-
$usingGeneric = 'using namespace System.Collections.Generic' + [Environment]::NewLine
171-
}
172-
173167
It "New-Object List[Object]" {
174168
$scriptDefinition = {
175169
$List = New-Object List[Object]
@@ -208,6 +202,65 @@ Describe "AvoidArrayList" {
208202
}
209203
}
210204

205+
Context "Disabled" {
206+
207+
BeforeAll {
208+
$settings = @{
209+
IncludeRules = @($ruleName)
210+
Rules = @{ $ruleName = @{ Enable = $false } }
211+
}
212+
}
213+
214+
It "New-Object type" {
215+
$scriptDefinition = $usingCollections + {
216+
$List = New-Object ArrayList
217+
1..3 | ForEach-Object { $null = $List.Add($_) }
218+
}.ToString()
219+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
220+
$violations | Should -BeNullOrEmpty
221+
}
222+
223+
It "Type initializer" {
224+
$scriptDefinition = $usingCollections + {
225+
$List = [ArrayList](1,2,3)
226+
1..3 | ForEach-Object { $null = $List.Add($_) }
227+
}.ToString()
228+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
229+
$violations | Should -BeNullOrEmpty
230+
}
231+
232+
It "New constructor" {
233+
$scriptDefinition = $usingCollections + {
234+
$List = [ArrayList]::new()
235+
1..3 | ForEach-Object { $null = $List.Add($_) }
236+
}.ToString()
237+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
238+
$violations | Should -BeNullOrEmpty
239+
}
240+
}
241+
242+
Context "Explicitly enabled" {
243+
244+
BeforeAll {
245+
$settings = @{
246+
IncludeRules = @($ruleName)
247+
Rules = @{ $ruleName = @{ Enable = $true } }
248+
}
249+
}
250+
251+
It "New-Object type" {
252+
$scriptDefinition = $usingCollections + {
253+
$List = New-Object ArrayList
254+
1..3 | ForEach-Object { $null = $List.Add($_) }
255+
}.ToString()
256+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
257+
$violations.Count | Should -Be 1
258+
$violations.Severity | Should -Be Warning
259+
$violations.Extent.Text | Should -Be {New-Object ArrayList}.ToString()
260+
$violations.Message | Should -Be ($ruleMessage -f {New-Object ArrayList})
261+
}
262+
}
263+
211264
Context "Test for potential errors" {
212265

213266
It "Dynamic types shouldn't error" {

docs/Rules/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The PSScriptAnalyzer contains the following rule definitions.
2828
| [AvoidShouldContinueWithoutForce](./AvoidShouldContinueWithoutForce.md) | Warning | Yes | |
2929
| [AvoidTrailingWhitespace](./AvoidTrailingWhitespace.md) | Warning | Yes | |
3030
| [AvoidUsingAllowUnencryptedAuthentication](./AvoidUsingAllowUnencryptedAuthentication.md) | Warning | Yes | |
31-
| [AvoidUsingArrayList](./AvoidUsingArrayList.md) | Warning | Yes | |
31+
| [AvoidUsingArrayList](./AvoidUsingArrayList.md) | Warning | Yes | Yes |
3232
| [AvoidUsingBrokenHashAlgorithms](./AvoidUsingBrokenHashAlgorithms.md) | Warning | Yes | |
3333
| [AvoidUsingCmdletAliases](./AvoidUsingCmdletAliases.md) | Warning | Yes | Yes<sup>2</sup> |
3434
| [AvoidUsingComputerNameHardcoded](./AvoidUsingComputerNameHardcoded.md) | Error | Yes | |

0 commit comments

Comments
 (0)