Skip to content

Fix AvoidReservedParams does not work if function does not have cmdletbi... #22

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

Merged
merged 2 commits into from
Apr 10, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 24 additions & 29 deletions Rules/AvoidReservedParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,38 @@ public class AvoidReservedParams : IScriptRule
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);

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

string paramName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialize to null


PropertyInfo[] commonParams = typeof(CommonParameters).GetProperties();
List<string> commonParamNames = new List<string>();
List<string> commonParamNames = typeof(CommonParameters).GetProperties().Select(param => param.Name).ToList();

if (commonParams != null) {
foreach (PropertyInfo commonParam in commonParams) {
commonParamNames.Add("$" + commonParam.Name);
foreach (FunctionDefinitionAst funcAst in funcAsts)
{
IEnumerable<ParameterAst> parameters = null;
if (funcAst.Parameters != null)
{
parameters = funcAst.Parameters;
}
// Check param block
else
{
if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Parameters != null)
{
parameters = funcAst.Body.ParamBlock.Parameters;
}
}
}

if (paramAsts != null) {
foreach (ParameterAst paramAst in paramAsts) {
paramName = paramAst.Name.ToString();

if (commonParamNames.Contains(paramName, StringComparer.OrdinalIgnoreCase)) {
parentAst = paramAst.Parent;
while (parentAst != null && !(parentAst is FunctionDefinitionAst)) {
parentAst = parentAst.Parent;
}
if (parameters != null)
{
foreach (ParameterAst paramAst in parameters)
{
paramName = paramAst.Name.VariablePath.UserPath;

if (parentAst is FunctionDefinitionAst)
if (commonParamNames.Contains(paramName, StringComparer.OrdinalIgnoreCase))
{
IEnumerable<Ast> attrs = parentAst.FindAll(testAttr => testAttr is AttributeAst, true);
foreach (AttributeAst attr in attrs)
{
if (string.Equals(attr.Extent.Text, "[CmdletBinding()]",
StringComparison.OrdinalIgnoreCase))
{
string funcName = string.Format(CultureInfo.CurrentCulture,Strings.ReservedParamsCmdletPrefix, (parentAst as FunctionDefinitionAst).Name);
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcName,paramName), paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);

}
}
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcAst.Name, paramName),
paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/AvoidReservedParams.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Import-Module ScriptAnalyzer
$violationMessage = [regex]::Escape('The cmdlet Verb-Files defines the reserved common parameter $Verbose.')
$violationMessage = [regex]::Escape("Verb-Files' defines the reserved common parameter 'Verbose'.")
$violationName = "PSReservedParams"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\BadCmdlet.ps1 | Where-Object {$_.RuleName -eq $violationName}
Expand Down