Skip to content

Commit 113f7c8

Browse files
committed
Merge pull request #246 from PowerShell/PositionalParameter
Positional parameter
2 parents a05dbae + 3e2555c commit 113f7c8

File tree

2 files changed

+3
-15
lines changed

2 files changed

+3
-15
lines changed

Engine/Helper.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
249249
CommandInfo commandInfo = GetCommandInfo(GetCmdletNameFromAlias(cmdAst.GetCommandName())) ?? GetCommandInfo(cmdAst.GetCommandName());
250250

251251
IEnumerable<ParameterMetadata> switchParams = null;
252-
IEnumerable<CommandParameterSetInfo> scriptBlocks = null;
253-
bool hasScriptBlockSet = false;
254252

255253
if (HasSplattedVariable(cmdAst))
256254
{
@@ -262,15 +260,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
262260
try
263261
{
264262
switchParams = commandInfo.Parameters.Values.Where<ParameterMetadata>(pm => pm.SwitchParameter);
265-
scriptBlocks = commandInfo.ParameterSets;
266-
foreach (CommandParameterSetInfo cmdParaset in scriptBlocks)
267-
{
268-
if (String.Equals(cmdParaset.Name, "ScriptBlockSet", StringComparison.OrdinalIgnoreCase))
269-
{
270-
hasScriptBlockSet = true;
271-
}
272-
}
273-
274263
}
275264
catch (Exception)
276265
{
@@ -284,8 +273,6 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
284273

285274
foreach (CommandElementAst ceAst in cmdAst.CommandElements)
286275
{
287-
if (!hasScriptBlockSet)
288-
{
289276
if (ceAst is CommandParameterAst)
290277
{
291278
// Skip if it's a switch parameter
@@ -308,7 +295,7 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
308295
{
309296
arguments += 1;
310297
}
311-
}
298+
312299
}
313300

314301
// if not the first element in a pipeline, increase the number of arguments by 1

Tests/Rules/AvoidPositionalParametersNoViolations.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ Get-ChildItem -Path "Tests"
33
Clear-Host
44
Split-Path -Path "Random" -leaf
55
Get-Process | Where-Object {$_.handles -gt 200}
6-
get-service-computername localhost | where {($_.status -eq "Running") -and ($_.CanStop -eq $true)}
6+
get-service-computername localhost | where {($_.status -eq "Running") -and ($_.CanStop -eq $true)}
7+
1, 2, $null, 4 | ForEach-Object {"Hello"}

0 commit comments

Comments
 (0)