-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathAvoidUsingNewObject.cs
More file actions
247 lines (225 loc) · 10.5 KB
/
AvoidUsingNewObject.cs
File metadata and controls
247 lines (225 loc) · 10.5 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// 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.Linq;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
/// <summary>
/// Rule that reports a warning when the New-Object cmdlet is used in a script.
/// The rule implements a correction that suggests using type-casting or type constructor.
///
/// Note:
/// In most cases if there isn't an automatic correction available,
/// the rule won't report any violation either.
/// This is because if there isn't an automatic correction available, it generally means
/// that there isn't a simple type-casting or type constructor that can be used that would
/// be more efficient or has a better syntax than using New-Object.
/// In other words, if the common `-Verbose` parameter is used, or both the parameters
/// `-ArgumentList` and `-Property` are used, there won't be a simple type initializer
/// available and the rule won't report any violation for the `New-Object` cmdlet.
///
/// Nevertheless, there are still some cases where the `New-Object` cmdlet might be
/// replaceable with a type initializer that would be more efficient or has a better syntax,
/// but an automatic correction can't be provided.
/// For example if the `-ArgumentList` parameter is used with a variable,
/// the rule will report a violation, but won't be able to provide a correction,
/// as it's not possible to determine from the AST alone whether the variable contains a
/// single value that can be used in a type initializer,
/// or if it contains multiple values that would require splatting.
/// </summary>
public class AvoidUsingNewObject : ConfigurableRule
{
/// <summary>
/// Construct an object of AvoidUsingNewObject type.
/// </summary>
public AvoidUsingNewObject() {
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>An enumerable type containing the violations</returns>
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
IEnumerable<CommandAst> newObjectAsts = ast.FindAll(testAst =>
testAst is CommandAst cmdAst &&
(cmdAst.GetCommandName() as string) is string commandName &&
commandName.Equals("New-Object", StringComparison.OrdinalIgnoreCase),
true
).Cast<CommandAst>();
foreach (CommandAst cmdAst in newObjectAsts)
{
// Use StaticParameterBinder to reliably get parameter values
var bindingResult = StaticParameterBinder.BindCommand(cmdAst, true);
// Check for `-TypeName` and either a `-ArgumentList` or `-Property`.
// But not both, as that would mean there isn't a simple
// type initializer available as a replacement.
if (
bindingResult.BoundParameters.Count <= 2 &&
bindingResult.BoundParameters.TryGetValue("TypeName", out ParameterBindingResult asTypeName) &&
asTypeName.ConstantValue is string typeName
) {
Boolean isProperty = bindingResult.BoundParameters.TryGetValue("Property", out ParameterBindingResult boundResult);
if (!isProperty)
{
Boolean isArgument = bindingResult.BoundParameters.TryGetValue("ArgumentList", out ParameterBindingResult argumentResult);
if (isArgument) { boundResult = argumentResult; }
}
string correction = null;
if (boundResult == null)
{
// No `-Property` or `-ArgumentList` parameter was used, so we suggest a parameterless constructor call.
correction = "[" + typeName + "]::new()";
}
else if (isProperty)
{
correction = "[" + typeName + "]" + boundResult.Value.Extent.Text;
}
else if (boundResult.ConstantValue != null)
{
string valueText = boundResult.Value.Extent.Text;
if (
boundResult.Value is StringConstantExpressionAst stringConstant &&
stringConstant.StringConstantType == StringConstantType.BareWord
)
{
valueText = '"' + valueText.Replace("\"", string.Empty) + '"'; // Test""123 --> "Test123"
}
correction = "[" + typeName + "]" + valueText;
}
else if (
boundResult.Value is ParenExpressionAst parenExpressionAst &&
!parenExpressionAst.Pipeline.Extent.Text.StartsWith(",")
)
{
correction = "[" +typeName + "]::new" + parenExpressionAst.Extent.Text;
}
else if (
boundResult.Value is ArrayExpressionAst arrayExpressionAst &&
!arrayExpressionAst.SubExpression.Extent.Text.StartsWith(",")
)
{
correction = "[" + typeName + "]::new(" + arrayExpressionAst.SubExpression.Extent.Text + ")";
}
else if (
boundResult.Value is SubExpressionAst subExpressionAst &&
!subExpressionAst.SubExpression.Extent.Text.StartsWith(",")
)
{
correction = "[" + typeName + "]::new(" + subExpressionAst.SubExpression.Extent.Text + ")";
}
else if (boundResult.Value is VariableExpressionAst)
{
// correction == $null
// The correction is inconclusive, as we can't be sure if the variable contains
// a single value that can be used in a type initializer,
// or if it contains multiple values that would require splatting,
// which can't be automatically determined from the AST.
}
else
{
correction = "[" + typeName + "]::new(" + boundResult.Value.Extent.Text + ")";
}
DiagnosticRecord diagnosticRecord = new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.AvoidUsingNewObjectError,
typeName
),
cmdAst.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName,
typeName
);
if (correction != null)
{
diagnosticRecord.SuggestedCorrections = new List<CorrectionExtent> {
new CorrectionExtent(
cmdAst.Extent.StartLineNumber,
cmdAst.Extent.EndLineNumber,
cmdAst.Extent.StartColumnNumber,
cmdAst.Extent.EndColumnNumber,
correction,
fileName,
string.Format(
CultureInfo.CurrentCulture,
Strings.AvoidUsingNewObjectCorrectionDescription,
typeName
)
)
};
}
yield return diagnosticRecord;
}
}
}
/// <summary>
/// Retrieves the common name of this rule.
/// </summary>
public override string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingNewObjectCommonName);
}
/// <summary>
/// Retrieves the description of this rule.
/// </summary>
public override string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingNewObjectDescription);
}
/// <summary>
/// Retrieves the name of this rule.
/// </summary>
public override string GetName()
{
return string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.AvoidUsingNewObjectName);
}
/// <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;
}
}
}