Skip to content

Positional parameter #246

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
Jun 17, 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
15 changes: 1 addition & 14 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
CommandInfo commandInfo = GetCommandInfo(GetCmdletNameFromAlias(cmdAst.GetCommandName())) ?? GetCommandInfo(cmdAst.GetCommandName());

IEnumerable<ParameterMetadata> switchParams = null;
IEnumerable<CommandParameterSetInfo> scriptBlocks = null;
bool hasScriptBlockSet = false;

if (HasSplattedVariable(cmdAst))
{
Expand All @@ -262,15 +260,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
try
{
switchParams = commandInfo.Parameters.Values.Where<ParameterMetadata>(pm => pm.SwitchParameter);
scriptBlocks = commandInfo.ParameterSets;
foreach (CommandParameterSetInfo cmdParaset in scriptBlocks)
{
if (String.Equals(cmdParaset.Name, "ScriptBlockSet", StringComparison.OrdinalIgnoreCase))
{
hasScriptBlockSet = true;
}
}

}
catch (Exception)
{
Expand All @@ -284,8 +273,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst)

foreach (CommandElementAst ceAst in cmdAst.CommandElements)
{
if (!hasScriptBlockSet)
{
if (ceAst is CommandParameterAst)
{
// Skip if it's a switch parameter
Expand All @@ -308,7 +295,7 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
{
arguments += 1;
}
}

}

// if not the first element in a pipeline, increase the number of arguments by 1
Expand Down
3 changes: 2 additions & 1 deletion Tests/Rules/AvoidPositionalParametersNoViolations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Get-ChildItem -Path "Tests"
Clear-Host
Split-Path -Path "Random" -leaf
Get-Process | Where-Object {$_.handles -gt 200}
get-service-computername localhost | where {($_.status -eq "Running") -and ($_.CanStop -eq $true)}
get-service-computername localhost | where {($_.status -eq "Running") -and ($_.CanStop -eq $true)}
1, 2, $null, 4 | ForEach-Object {"Hello"}