-
Notifications
You must be signed in to change notification settings - Fork 410
Add new AvoidUsingArrayList rule #2174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iRon7
wants to merge
21
commits into
PowerShell:main
Choose a base branch
from
iRon7:#2147AvoidArrayList
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c34eb23
Implemented the AvoidUsingArrayList rule to warn when the ArrayList c…
iRon7 ba1167d
Testing-Commit-CSpell-issue
iRon7 77384e1
Apply suggestion from @liamjpeters
iRon7 77d1e6d
Update docs/Rules/AvoidUsingArrayList.md
iRon7 d9b88a8
Updated rule help
iRon7 1eb92e9
Merge branch '#2147AvoidArrayList' of https://github.com/iRon7/PSScri…
iRon7 c0aa82b
Changed "unintentionally"
iRon7 7bc3dfa
Update Rules/AvoidUsingArrayList.cs
iRon7 b35becb
Update Tests/Rules/AvoidUsingArrayList.tests.ps1
iRon7 9ed1355
ArrayListName could be null
iRon7 e1362e6
Resolved camelCase
iRon7 b80f01c
Remove ComponentModel namespace
iRon7 f913b1f
Fixed tests
iRon7 b3a5c3c
Updated Tests
iRon7 8aefe4c
fixed and tested empty (dynamic) BoundParameter
iRon7 ad49041
Robuster Pester tests
iRon7 4bc41e9
Configurable (enable by default)
iRon7 fb27627
Fixed ConstantValue null check test and rule
iRon7 5a66672
Merge branch 'main' into #2147AvoidArrayList
iRon7 973d2e7
Better UsingStatements handling and disable AvoidUsingArrayList by de…
iRon7 14ed9f5
`[ArrayList]::new()` without a `using namespace System.Collections`
iRon7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| // 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) { | ||
| foreach (UsingStatementAst usingAst in sbAst.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); } | ||
|
|
||
|
|
||
| // 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) | ||
| { | ||
| yield return new DiagnosticRecord( | ||
| string.Format( | ||
| CultureInfo.CurrentCulture, | ||
| Strings.AvoidUsingArrayListError, | ||
| typeAst.Parent.Extent.Text), | ||
| typeAst.Parent.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.ContainsKey("TypeName") && | ||
| bindingResult.BoundParameters["TypeName"].ConstantValue != null && | ||
| arrayListName.IsMatch(bindingResult.BoundParameters["TypeName"].ConstantValue as string) | ||
| ) | ||
| { | ||
| yield return new DiagnosticRecord( | ||
| string.Format( | ||
| CultureInfo.CurrentCulture, | ||
| Strings.AvoidUsingArrayListError, | ||
| cmd.Extent.Text), | ||
| cmd.Extent, | ||
| GetName(), | ||
| DiagnosticSeverity.Warning, | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.