-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathAvoidUsingArrayList.cs
More file actions
144 lines (122 loc) · 5.67 KB
/
AvoidUsingArrayList.cs
File metadata and controls
144 lines (122 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management.Automation.Language;
using System.Text.RegularExpressions;
using System.ComponentModel;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
/// <summary>
/// Rule that warns when the ArrayList class is used in a PowerShell script.
/// </summary>
public class AvoidUsingArrayListAsFunctionNames : IScriptRule
{
/// <summary>
/// Analyzes the PowerShell AST for uses of the ArrayList class.
/// </summary>
/// <param name="ast">The PowerShell Abstract Syntax Tree to analyze.</param>
/// <param name="fileName">The name of the file being analyzed (for diagnostic reporting).</param>
/// <returns>A collection of diagnostic records for each violation.</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) { throw new ArgumentNullException(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;
var sbAst = ast as ScriptBlockAst;
foreach (UsingStatementAst usingAst in sbAst.UsingStatements)
{
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.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") &&
ArrayListName.IsMatch(bindingResult.BoundParameters["TypeName"].ConstantValue as string)
)
{
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.AvoidUsingArrayListError,
cmd.Extent.Text),
bindingResult.BoundParameters["TypeName"].Value.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName
);
}
}
}
public string GetCommonName() => Strings.AvoidUsingArrayListCommonName;
public string GetDescription() => Strings.AvoidUsingArrayListDescription;
public string GetName() => string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.AvoidUsingArrayListName);
public RuleSeverity GetSeverity() => RuleSeverity.Warning;
public string GetSourceName() => Strings.SourceName;
public SourceType GetSourceType() => SourceType.Builtin;
}
}