Skip to content

Fix Splatted Variable and Pipeline bug in UseCmdletCorrectly and AvoidUsingPositionalParameter #240

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 1 commit into from
Jun 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
36 changes: 28 additions & 8 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ item is TypeDefinitionAst
return false;
}

/// <summary>
/// Given a commandast, checks whether it uses splatted variable
/// </summary>
/// <param name="cmdAst"></param>
/// <returns></returns>
public bool HasSplattedVariable(CommandAst cmdAst)
{
if (cmdAst == null || cmdAst.CommandElements == null)
{
return false;
}

return cmdAst.CommandElements.Any(cmdElem => cmdElem is VariableExpressionAst && (cmdElem as VariableExpressionAst).Splatted);
}

/// <summary>
/// Given a commandast, checks whether positional parameters are used or not.
/// </summary>
Expand All @@ -237,6 +252,11 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
IEnumerable<CommandParameterSetInfo> scriptBlocks = null;
bool hasScriptBlockSet = false;

if (HasSplattedVariable(cmdAst))
{
return false;
}

if (commandInfo != null && commandInfo.CommandType == System.Management.Automation.CommandTypes.Cmdlet)
{
try
Expand Down Expand Up @@ -286,19 +306,19 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
}
else
{
//Skip if splatting "@" is used
if (ceAst is VariableExpressionAst)
{
if ((ceAst as VariableExpressionAst).Splatted)
{
continue;
}
}
arguments += 1;
}
}
}

// if not the first element in a pipeline, increase the number of arguments by 1
PipelineAst parent = cmdAst.Parent as PipelineAst;

if (parent != null && parent.PipelineElements.Count > 1 && parent.PipelineElements[0] != cmdAst)
{
arguments += 1;
}

return arguments > parameters;
}

Expand Down
19 changes: 17 additions & 2 deletions Rules/AvoidPositionalParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,23 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
if (Helper.Instance.GetCommandInfo(cmdAst.GetCommandName()) != null
&& Helper.Instance.PositionalParameterUsed(cmdAst))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersError, cmdAst.GetCommandName()),
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, cmdAst.GetCommandName());
PipelineAst parent = cmdAst.Parent as PipelineAst;

if (parent != null && parent.PipelineElements.Count > 1)
{
// raise if it's the first element in pipeline. otherwise no.
if (parent.PipelineElements[0] == cmdAst)
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersError, cmdAst.GetCommandName()),
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, cmdAst.GetCommandName());
}
}
// not in pipeline so just raise it normally
else
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersError, cmdAst.GetCommandName()),
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, cmdAst.GetCommandName());
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Rules/UseCmdletCorrectly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ private bool IsMandatoryParameterExisted(CommandAst cmdAst)
return true;
}

// ignores if splatted variable is used
if (Helper.Instance.HasSplattedVariable(cmdAst))
{
return true;
}

// Gets parameters from command elements.
ceAsts = cmdAst.CommandElements.Where<CommandElementAst>(foundParamASTs);

Expand Down
6 changes: 6 additions & 0 deletions Tests/Rules/GoodCmdlet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ function Get-File
$a
}

$a | Write-Warning

$b = @{"hash"="table"}

Write-Debug @b

return @{"hash"="true"}
}
End
Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/UseCmdletCorrectly.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Wrong-Cmd
Write-Verbose -Message "Write Verbose"
Write-Verbose "Warning" -OutVariable $test
Write-Verbose "Warning"
Write-Verbose "Warning" | PipeLineCmdlet