Skip to content

Commit ff665f3

Browse files
committed
Merge pull request #23 from PowerShell/BugFixes
Take bug fixes to Master
2 parents 995fb21 + f18d564 commit ff665f3

File tree

2 files changed

+20
-33
lines changed

2 files changed

+20
-33
lines changed

Rules/AvoidReservedParams.cs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,43 +30,30 @@ public class AvoidReservedParams : IScriptRule
3030
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
3131
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
3232

33-
IEnumerable<Ast> paramAsts = ast.FindAll(testAst => testAst is ParameterAst, true);
34-
Ast parentAst;
33+
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true);
3534

36-
string paramName;
35+
List<string> commonParamNames = typeof(CommonParameters).GetProperties().Select(param => param.Name).ToList();
3736

38-
PropertyInfo[] commonParams = typeof(CommonParameters).GetProperties();
39-
List<string> commonParamNames = new List<string>();
40-
41-
if (commonParams != null) {
42-
foreach (PropertyInfo commonParam in commonParams) {
43-
commonParamNames.Add("$" + commonParam.Name);
44-
}
45-
}
46-
47-
if (paramAsts != null) {
48-
foreach (ParameterAst paramAst in paramAsts) {
49-
paramName = paramAst.Name.ToString();
37+
foreach (FunctionDefinitionAst funcAst in funcAsts)
38+
{
39+
// this rule only applies to function with param block
40+
if (funcAst.Body != null && funcAst.Body.ParamBlock != null
41+
&& funcAst.Body.ParamBlock.Attributes != null && funcAst.Body.ParamBlock.Parameters != null)
42+
{
43+
// no cmdlet binding
44+
if (!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
45+
{
46+
continue;
47+
}
5048

51-
if (commonParamNames.Contains(paramName, StringComparer.OrdinalIgnoreCase)) {
52-
parentAst = paramAst.Parent;
53-
while (parentAst != null && !(parentAst is FunctionDefinitionAst)) {
54-
parentAst = parentAst.Parent;
55-
}
49+
foreach (ParameterAst paramAst in funcAst.Body.ParamBlock.Parameters)
50+
{
51+
string paramName = paramAst.Name.VariablePath.UserPath;
5652

57-
if (parentAst is FunctionDefinitionAst)
53+
if (commonParamNames.Contains(paramName, StringComparer.OrdinalIgnoreCase))
5854
{
59-
IEnumerable<Ast> attrs = parentAst.FindAll(testAttr => testAttr is AttributeAst, true);
60-
foreach (AttributeAst attr in attrs)
61-
{
62-
if (string.Equals(attr.Extent.Text, "[CmdletBinding()]",
63-
StringComparison.OrdinalIgnoreCase))
64-
{
65-
string funcName = string.Format(CultureInfo.CurrentCulture,Strings.ReservedParamsCmdletPrefix, (parentAst as FunctionDefinitionAst).Name);
66-
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcName,paramName), paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
67-
68-
}
69-
}
55+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcAst.Name, paramName),
56+
paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
7057
}
7158
}
7259
}

Tests/Rules/AvoidReservedParams.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Import-Module ScriptAnalyzer
2-
$violationMessage = [regex]::Escape('The cmdlet Verb-Files defines the reserved common parameter $Verbose.')
2+
$violationMessage = [regex]::Escape("Verb-Files' defines the reserved common parameter 'Verbose'.")
33
$violationName = "PSReservedParams"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
55
$violations = Invoke-ScriptAnalyzer $directory\BadCmdlet.ps1 | Where-Object {$_.RuleName -eq $violationName}

0 commit comments

Comments
 (0)