Skip to content

Take Bug fixes to Master #242

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 12 commits into from
Jun 12, 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
10 changes: 8 additions & 2 deletions Engine/SpecialVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal class SpecialVars
internal const string PSScriptRoot = "PSScriptRoot";
internal const string PSCommandPath = "PSCommandPath";
internal const string ExecutionContext = "ExecutionContext";
internal const string Matches = "Matches";
internal const string PSVersionTable = "PSVersionTable";

internal static readonly string[] InitializedVariables;

Expand Down Expand Up @@ -63,7 +65,9 @@ static SpecialVars()
MyInvocation,
PSScriptRoot,
PSCommandPath,
ExecutionContext
ExecutionContext,
Matches,
PSVersionTable
};
internal static readonly Type[] AutomaticVariableTypes = new Type[]
{
Expand All @@ -76,7 +80,9 @@ static SpecialVars()
/* MyInvocation */ typeof(InvocationInfo),
/* PSScriptRoot */ typeof(string),
/* PSCommandPath */ typeof(string),
/* ExecutionContext */ typeof(EngineIntrinsics),
/* ExecutionContext */ typeof(EngineIntrinsics),
/* Matches */ typeof(System.Collections.Hashtable),
/* PSVersionTable */ typeof(System.Collections.Hashtable)
};


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
2 changes: 1 addition & 1 deletion Rules/ScriptAnalyzerBuiltinRules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<Compile Include="MissingModuleManifestField.cs" />
<Compile Include="PossibleIncorrectComparisonWithNull.cs" />
<Compile Include="ProvideCommentHelp.cs" />
<Compile Include="ProvideVerboseMessage.cs" />
<Compile Include="UseVerboseMessageInDSCResource.cs" />
<Compile Include="Strings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down
83 changes: 37 additions & 46 deletions Rules/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,14 @@
<data name="SourceName" xml:space="preserve">
<value>PS</value>
</data>
<data name="ProvideVerboseMessageDescription" xml:space="preserve">
<value>Checks that Write-Verbose is called at least once in every cmdlet or script. This is in line with the PowerShell best practices.</value>
<data name="UseVerboseMessageInDSCResourceDescription" xml:space="preserve">
<value>It is a best practice to emit informative, verbose messages in DSC resource functions. This helps in debugging issues when a DSC configuration is executed.</value>
</data>
<data name="ProvideVerboseMessageErrorFunction" xml:space="preserve">
<value>There is no call to Write-Verbose in the function ‘{0}’.</value>
<data name="UseVerboseMessageInDSCResourceErrorFunction" xml:space="preserve">
<value>There is no call to Write-Verbose in DSC function ‘{0}’. If you are using Write-Verbose in a helper function, suppress this rule application.</value>
</data>
<data name="ProvideVerboseMessageCommonName" xml:space="preserve">
<value>Verbose</value>
</data>
<data name="ProvideVerboseMessageScript" xml:space="preserve">
<value>There is no call to Write-Verbose in the script.</value>
<data name="UseVerboseMessageInDSCResourceCommonName" xml:space="preserve">
<value>Use verbose message in DSC resource</value>
</data>
<data name="MissingModuleManifestFieldDescription" xml:space="preserve">
<value>Some fields of the module manifest (such as ModuleVersion) are required.</value>
Expand Down Expand Up @@ -459,8 +456,8 @@
<data name="MissingModuleManifestFieldName" xml:space="preserve">
<value>MissingModuleManifestField</value>
</data>
<data name="ProvideVerboseMessageName" xml:space="preserve">
<value>ProvideVerboseMessage</value>
<data name="UseVerboseMessageInDSCResourceName" xml:space="preserve">
<value>UseVerboseMessageInDSCResource</value>
</data>
<data name="CommandNotFoundCommonName" xml:space="preserve">
<value>Command Not Found</value>
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
3 changes: 1 addition & 2 deletions Rules/UseDeclaredVarsMoreThanAssignments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);

IEnumerable<Ast> assignmentAsts = ast.FindAll(testAst => testAst is AssignmentStatementAst, true);
IEnumerable<Ast> assingmentVarAsts;
IEnumerable<Ast> assignmentAsts = ast.FindAll(testAst => testAst is AssignmentStatementAst, true);
IEnumerable<Ast> varAsts = ast.FindAll(testAst => testAst is VariableExpressionAst, true);
IEnumerable<Ast> varsInAssignment;

Expand Down
Loading