-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathUseConsistentParameterSetName.cs
More file actions
453 lines (414 loc) · 21.5 KB
/
Copy pathUseConsistentParameterSetName.cs
File metadata and controls
453 lines (414 loc) · 21.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Management.Automation.Language;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// UseConsistentParameterSetName: Check for case-sensitive parameter set
/// name mismatches, missing default parameter set names, and parameter set
/// names containing new lines.
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class UseConsistentParameterSetName : ConfigurableRule
{
private const string AllParameterSetsName = "__AllParameterSets";
/// <summary>
/// AnalyzeScript: Check for parameter set name issues.
/// </summary>
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null)
{
throw new ArgumentNullException(Strings.NullAstErrorMessage);
}
var allParameterBlocks = ast
.FindAll(testAst => testAst is ParamBlockAst, true)
.Cast<ParamBlockAst>()
.Where(pb => pb.Parameters?.Count > 0);
foreach (var paramBlock in allParameterBlocks)
{
// If the paramblock has no parameters, skip it
if (paramBlock.Parameters.Count == 0)
{
continue;
}
// Get the CmdletBinding attribute and default parameter set name
// Or null if not present
var cmdletBindingAttr = Helper.Instance.GetCmdletBindingAttributeAst(paramBlock.Attributes);
var defaultParamSetName = GetNamedArgumentValue(cmdletBindingAttr, "DefaultParameterSetName");
// For each parameter block, build up a list of all the parameters
// and the parameter sets in which they appear.
List<ParameterSetInfo> paramBlockInfo = new List<ParameterSetInfo>();
foreach (var parameter in paramBlock.Parameters)
{
// If the parameter has no attributes, it is part of all
// parameter sets. We can ignore it for these checks.
if (parameter.Attributes.Count == 0)
{
continue;
}
// For each parameter attribute a parameter has, extract
// the parameter set and add it to our knowledge of the
// param block.
foreach (var attribute in parameter.Attributes.Where(attr => attr is AttributeAst).Cast<AttributeAst>())
{
if (string.Equals(attribute.TypeName?.Name, "Parameter", StringComparison.OrdinalIgnoreCase))
{
var parameterSetName = GetNamedArgumentValue(attribute, "ParameterSetName", AllParameterSetsName);
paramBlockInfo.Add(new ParameterSetInfo(parameter.Name.VariablePath.UserPath, parameterSetName, attribute));
}
}
}
// We now have a picture of the parameters and parameterset
// usage of this paramblock. We can make each check.
// Check 1: Default parameter set name
// -------------------------------------------------------------
// If we have parameter sets in use and the CmdletBinding
// attribute, but no default specified, warn about this.
if (string.IsNullOrEmpty(defaultParamSetName) &&
cmdletBindingAttr != null &&
paramBlockInfo.Any(p => p.ParameterSetName != AllParameterSetsName)
)
{
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameMissingDefaultError),
cmdletBindingAttr?.Extent ?? paramBlock.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName);
}
// Check 2: Parameter Declared Multiple Times in Same Set
// -------------------------------------------------------------
// If any parameter has more than one parameter attribute for
// the same parameterset, warn about each instance.
// Parameters cannot be declared multiple times in the same set.
// Calling a function that has a parameter declared multiple
// times in the same parameterset is a runtime exception -
// specifically a [System.Management.Automation.MetadataException]
// It'd be better to know before runtime.
// We use the same message text as the MetadataException for
// consistency
var duplicateAttributes = paramBlockInfo
.GroupBy(p => new { p.ParameterName, p.ParameterSetName })
.Where(g => g.Count() > 1)
.SelectMany(g => g);
foreach (var duplicate in duplicateAttributes)
{
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameMultipleDeclarationsError,
duplicate.ParameterName,
duplicate.ParameterSetName),
duplicate.ParameterAttributeAst.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName);
}
// Check 3: Validate Default Parameter Set
// -------------------------------------------------------------
// If a default parameter set is specified and matches one of
// the used parameter set names ignoring case, but not otherwise
// then we should warn about this
if (!string.IsNullOrEmpty(defaultParamSetName))
{
// Look for an exact (case-sensitive) match
var exactMatch = paramBlockInfo
.FirstOrDefault(p =>
string.Equals(
p.ParameterSetName,
defaultParamSetName,
StringComparison.Ordinal
)
);
if (exactMatch == null)
{
// No exact match, look for a case-insensitive match
var caseInsensitiveMatch = paramBlockInfo
.FirstOrDefault(p =>
string.Equals(
p.ParameterSetName,
defaultParamSetName,
StringComparison.OrdinalIgnoreCase
)
);
if (caseInsensitiveMatch != null)
{
var defaultParameterSetNameExtents = GetDefaultParameterSetNameValueExtent(cmdletBindingAttr);
// Emit a diagnostic for the first case-insensitive match
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameCaseMismatchDefaultError,
defaultParamSetName,
caseInsensitiveMatch.ParameterSetName),
defaultParameterSetNameExtents ?? cmdletBindingAttr?.Extent ?? paramBlock.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName);
}
}
}
// Check 4: Parameter Set Name Consistency
// -------------------------------------------------------------
// If a parameter set name is used in multiple places, it must
// be consistently used across all usages. This means the casing
// must match exactly. We should warn about any inconsistencies
// found.
var paramSetGroups = paramBlockInfo
.GroupBy(p => p.ParameterSetName, StringComparer.OrdinalIgnoreCase)
.Where(g =>
g.Select(p => p.ParameterSetName)
.Distinct(StringComparer.Ordinal)
.Skip(1).Any()
);
foreach (var group in paramSetGroups)
{
// Take the first instance as the canonical casing
var canonical = group.First();
foreach (var entry in group.Skip(1))
{
if (!string.Equals(
entry.ParameterSetName,
canonical.ParameterSetName,
StringComparison.Ordinal
)
)
{
var parameterSetNameExtents = GetParameterSetNameValueExtent(entry.ParameterAttributeAst);
if (parameterSetNameExtents != null)
{
var correction = new CorrectionExtent(
parameterSetNameExtents.StartLineNumber,
parameterSetNameExtents.EndLineNumber,
parameterSetNameExtents.StartColumnNumber,
parameterSetNameExtents.EndColumnNumber,
$"'{canonical.ParameterSetName}'",
fileName,
string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameCaseMismatchSuggestedCorrectionDescription,
entry.ParameterSetName,
canonical.ParameterSetName
)
);
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameCaseMismatchParameterError,
entry.ParameterSetName,
canonical.ParameterSetName),
parameterSetNameExtents,
GetName(),
DiagnosticSeverity.Warning,
fileName,
null,
new List<CorrectionExtent> { correction });
}
else
{
// If we couldn't find the parameter set name extents, we can't create a correction
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameCaseMismatchParameterError,
entry.ParameterSetName,
canonical.ParameterSetName),
entry.ParameterAttributeAst.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName);
}
}
}
}
// Check 5: Parameter Set Names should not contain New Lines
// -------------------------------------------------------------
// There is no practical purpose for parameterset names to
// contain a newline
foreach (var entry in paramBlockInfo)
{
if (entry.ParameterSetName.Contains('\n') || entry.ParameterSetName.Contains('\r'))
{
var parameterSetNameExtents = GetParameterSetNameValueExtent(entry.ParameterAttributeAst);
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameNewLineError),
parameterSetNameExtents ?? entry.ParameterAttributeAst.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName);
}
}
if (defaultParamSetName != null &&
(defaultParamSetName.Contains('\n') || defaultParamSetName.Contains('\r')))
{
// If the default parameter set name contains new lines, warn about it
var defaultParameterSetNameExtents = GetDefaultParameterSetNameValueExtent(cmdletBindingAttr);
yield return new DiagnosticRecord(
string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameNewLineError,
defaultParamSetName),
defaultParameterSetNameExtents ?? cmdletBindingAttr?.Extent ?? paramBlock.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName);
}
}
}
/// <summary>
/// Retrieves the value of a named argument from an AttributeAst's NamedArguments collection.
/// If the named argument is not found, returns the provided default value.
/// If the argument value is a constant, returns its string representation; otherwise, returns the argument's text.
/// </summary>
/// <param name="attributeAst">The AttributeAst to search for the named argument.</param>
/// <param name="argumentName">The name of the argument to look for (case-insensitive).</param>
/// <param name="defaultValue">The value to return if the named argument is not found. Defaults to null.</param>
/// <returns>
/// The value of the named argument as a string if found; otherwise, the default value.
/// </returns>
private static string GetNamedArgumentValue(AttributeAst attributeAst, string argumentName, string defaultValue = null)
{
if (attributeAst == null || attributeAst.NamedArguments == null)
{
return defaultValue;
}
foreach (var namedArg in attributeAst.NamedArguments)
{
if (namedArg?.ArgumentName == null) continue;
if (string.Equals(namedArg.ArgumentName, argumentName, StringComparison.OrdinalIgnoreCase))
{
// Try to evaluate the argument value as a constant string
if (namedArg.Argument is ConstantExpressionAst constAst)
{
return constAst.Value?.ToString();
}
// If not a constant, try to get the string representation
return namedArg.Argument.Extent.Text;
}
}
return defaultValue;
}
/// <summary>
/// Finds the IScriptExtent of the value assigned to the ParameterSetName argument
/// in the given AttributeAst (if it is a [Parameter()] attribute).
/// Returns null if not found.
/// </summary>
/// <param name="attributeAst">The AttributeAst to search.</param>
/// <returns>The IScriptExtent of the ParameterSetName value, or null if not found.</returns>
private static IScriptExtent GetParameterSetNameValueExtent(AttributeAst attributeAst)
{
return GetAttributeNamedArgumentValueExtent(attributeAst, "ParameterSetName", "Parameter");
}
/// <summary>
/// Finds the IScriptExtent of the value assigned to the DefaultParameterSetName argument
/// in the given AttributeAst (if it is a [CmdletBinding()] attribute).
/// Returns null if not found.
/// </summary>
/// <param name="attributeAst">The AttributeAst to search.</param>
/// <returns>The IScriptExtent of the DefaultParameterSetName value, or null if not found.</returns>
private static IScriptExtent GetDefaultParameterSetNameValueExtent(AttributeAst attributeAst)
{
return GetAttributeNamedArgumentValueExtent(attributeAst, "DefaultParameterSetName", "CmdletBinding");
}
/// <summary>
/// Finds the IScriptExtent of the value of a named argument in the given AttributeAst.
/// Returns null if not found.
/// </summary>
/// <param name="attributeAst">The AttributeAst to search.</param>
/// <param name="argumentName">The name of the argument to find.</param>
/// <param name="expectedAttributeName">The expected type name of the attribute. i.e. <c>Parameter</c> (optional).</param>
/// <returns>The IScriptExtent of the named argument value, or null if not found.</returns>
private static IScriptExtent GetAttributeNamedArgumentValueExtent(AttributeAst attributeAst, string argumentName, string expectedAttributeName = null)
{
if (attributeAst == null || attributeAst.NamedArguments == null)
return null;
if (!string.IsNullOrEmpty(expectedAttributeName) &&
!string.Equals(
attributeAst.TypeName?.Name,
expectedAttributeName,
StringComparison.OrdinalIgnoreCase)
)
return null;
foreach (var namedArg in attributeAst.NamedArguments)
{
if (string.Equals(namedArg.ArgumentName, argumentName, StringComparison.OrdinalIgnoreCase))
{
return namedArg.Argument?.Extent;
}
}
return null;
}
/// <summary>
/// Represents information about a parameter and its parameter set.
/// </summary>
private class ParameterSetInfo
{
public string ParameterName { get; }
public string ParameterSetName { get; }
public AttributeAst ParameterAttributeAst { get; }
public ParameterSetInfo(string parameterName, string parameterSetName, AttributeAst parameterAttributeAst)
{
ParameterName = parameterName;
ParameterSetName = parameterSetName;
ParameterAttributeAst = parameterAttributeAst;
}
}
/// <summary>
/// GetName: Retrieves the name of this rule.
/// </summary>
/// <returns>The name of this rule</returns>
public override string GetName() => string.Format(
CultureInfo.CurrentCulture,
Strings.NameSpaceFormat,
GetSourceName(),
Strings.UseConsistentParameterSetNameName
);
/// <summary>
/// GetCommonName: Retrieves the common name of this rule.
/// </summary>
/// <returns>The common name of this rule</returns>
public override string GetCommonName() => string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameCommonName
);
/// <summary>
/// GetDescription: Retrieves the description of this rule.
/// </summary>
/// <returns>The description of this rule</returns>
public override string GetDescription() => string.Format(
CultureInfo.CurrentCulture,
Strings.UseConsistentParameterSetNameDescription
);
/// <summary>
/// Method: Retrieves the type of the rule: builtin, managed or module.
/// </summary>
public override SourceType GetSourceType() => SourceType.Builtin;
/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public override RuleSeverity GetSeverity() => RuleSeverity.Warning;
/// <summary>
/// Method: Retrieves the module/assembly name the rule is from.
/// </summary>
public override string GetSourceName() => string.Format(
CultureInfo.CurrentCulture, Strings.SourceName
);
}
}