Skip to content

Take bug fixes to Master #23

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 3 commits into from
Apr 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
51 changes: 19 additions & 32 deletions Rules/AvoidReservedParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,30 @@ 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;
List<string> commonParamNames = typeof(CommonParameters).GetProperties().Select(param => param.Name).ToList();

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

if (commonParams != null) {
foreach (PropertyInfo commonParam in commonParams) {
commonParamNames.Add("$" + commonParam.Name);
}
}

if (paramAsts != null) {
foreach (ParameterAst paramAst in paramAsts) {
paramName = paramAst.Name.ToString();
foreach (FunctionDefinitionAst funcAst in funcAsts)
{
// this rule only applies to function with param block
if (funcAst.Body != null && funcAst.Body.ParamBlock != null
&& funcAst.Body.ParamBlock.Attributes != null && funcAst.Body.ParamBlock.Parameters != null)
{
// no cmdlet binding
if (!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
{
continue;
}

if (commonParamNames.Contains(paramName, StringComparer.OrdinalIgnoreCase)) {
parentAst = paramAst.Parent;
while (parentAst != null && !(parentAst is FunctionDefinitionAst)) {
parentAst = parentAst.Parent;
}
foreach (ParameterAst paramAst in funcAst.Body.ParamBlock.Parameters)
{
string 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