diff --git a/Engine/Helper.cs b/Engine/Helper.cs
index 373f4bd58..9716db704 100644
--- a/Engine/Helper.cs
+++ b/Engine/Helper.cs
@@ -219,6 +219,21 @@ item is TypeDefinitionAst
return false;
}
+ ///
+ /// Given a commandast, checks whether it uses splatted variable
+ ///
+ ///
+ ///
+ 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);
+ }
+
///
/// Given a commandast, checks whether positional parameters are used or not.
///
@@ -237,6 +252,11 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
IEnumerable scriptBlocks = null;
bool hasScriptBlockSet = false;
+ if (HasSplattedVariable(cmdAst))
+ {
+ return false;
+ }
+
if (commandInfo != null && commandInfo.CommandType == System.Management.Automation.CommandTypes.Cmdlet)
{
try
@@ -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;
}
diff --git a/Rules/AvoidPositionalParameters.cs b/Rules/AvoidPositionalParameters.cs
index 248dc4f71..a52ac550b 100644
--- a/Rules/AvoidPositionalParameters.cs
+++ b/Rules/AvoidPositionalParameters.cs
@@ -47,8 +47,23 @@ public IEnumerable 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());
+ }
}
}
}
diff --git a/Rules/UseCmdletCorrectly.cs b/Rules/UseCmdletCorrectly.cs
index 430e9a2d4..691f4f743 100644
--- a/Rules/UseCmdletCorrectly.cs
+++ b/Rules/UseCmdletCorrectly.cs
@@ -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(foundParamASTs);
diff --git a/Tests/Rules/GoodCmdlet.ps1 b/Tests/Rules/GoodCmdlet.ps1
index 24106a3f5..0fceccd60 100644
--- a/Tests/Rules/GoodCmdlet.ps1
+++ b/Tests/Rules/GoodCmdlet.ps1
@@ -85,6 +85,12 @@ function Get-File
$a
}
+ $a | Write-Warning
+
+ $b = @{"hash"="table"}
+
+ Write-Debug @b
+
return @{"hash"="true"}
}
End
diff --git a/Tests/Rules/UseCmdletCorrectly.ps1 b/Tests/Rules/UseCmdletCorrectly.ps1
index 8fb8392c2..373078af8 100644
--- a/Tests/Rules/UseCmdletCorrectly.ps1
+++ b/Tests/Rules/UseCmdletCorrectly.ps1
@@ -2,4 +2,4 @@
Wrong-Cmd
Write-Verbose -Message "Write Verbose"
Write-Verbose "Warning" -OutVariable $test
-Write-Verbose "Warning"
\ No newline at end of file
+Write-Verbose "Warning" | PipeLineCmdlet
\ No newline at end of file