Skip to content

Finalize rule severity #2

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
Apr 1, 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
3 changes: 0 additions & 3 deletions Engine/ScriptAnalyzerEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@
<Compile Include="VariableAnalysis.cs" />
<Compile Include="VariableAnalysisBase.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Microsoft.Windows.Powershell.ScriptAnalyzer.dll-Help.xml" />
</ItemGroup>
<ItemGroup>
<None Include="PSScriptAnalyzer.psd1" />
<None Include="ScriptAnalyzer.format.ps1xml" />
Expand Down
118 changes: 118 additions & 0 deletions Rules/AvoidShouldContinueWithoutForce.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Language;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System.ComponentModel.Composition;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.Reflection;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// AvoidShouldContinueWithoutForceParameter: Check that if ShouldContinue is used,
/// the function should have a boolean force parameter
/// </summary>
[Export(typeof(IScriptRule))]
public class AvoidShouldContinueWithoutForce : IScriptRule
{
/// <summary>
/// AvoidShouldContinueWithoutForceCheck that if ShouldContinue is used,
/// the function should have a boolean force parameter
/// </summary>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);

// Finds all ParamAsts.
IEnumerable<Ast> funcAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);

// Iterrates all ParamAsts and check if there are any force.
foreach (FunctionDefinitionAst funcAst in funcAsts)
{
IEnumerable<Ast> paramAsts = funcAst.FindAll(testAst => testAst is ParameterAst, true);
bool hasForce = false;

foreach (ParameterAst paramAst in paramAsts)
{
if (String.Equals(paramAst.Name.VariablePath.ToString(), "force", StringComparison.OrdinalIgnoreCase)
&& String.Equals(paramAst.StaticType.FullName, "System.Boolean", StringComparison.OrdinalIgnoreCase))
{
hasForce = true;
break;
}
}

if (hasForce)
{
continue;
}

IEnumerable<Ast> imeAsts = funcAst.FindAll(testAst => testAst is InvokeMemberExpressionAst, true);

foreach (InvokeMemberExpressionAst imeAst in imeAsts)
{
VariableExpressionAst typeAst = imeAst.Expression as VariableExpressionAst;
if (typeAst == null) continue;

if (String.Equals(typeAst.VariablePath.UserPath, "pscmdlet", StringComparison.OrdinalIgnoreCase)
&& (String.Equals(imeAst.Member.Extent.Text, "shouldcontinue", StringComparison.OrdinalIgnoreCase)))
{
yield return new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceError, funcAst.Name, System.IO.Path.GetFileName(fileName)),
imeAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}

/// <summary>
/// GetName: Retrieves the name of this rule.
/// </summary>
/// <returns>The name of this rule</returns>
public string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidShouldContinueWithoutForceName);
}

/// <summary>
/// GetCommonName: Retrieves the common name of this rule.
/// </summary>
/// <returns>The common name of this rule</returns>
public string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceCommonName);
}

/// <summary>
/// GetDescription: Retrieves the description of this rule.
/// </summary>
/// <returns>The description of this rule</returns>
public string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceDescription);
}

/// <summary>
/// GetSourceType: Retrieves the type of the rule: builtin, managed or module.
/// </summary>
public SourceType GetSourceType()
{
return SourceType.Builtin;
}

/// <summary>
/// GetSourceName: Retrieves the module/assembly name the rule is from.
/// </summary>
public string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}
}
}
14 changes: 7 additions & 7 deletions Rules/AvoidUserNameAndPasswordParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// AvoidUserNameAndPasswordParams: Check that a function does not use both username and password
/// AvoidUsernameAndPasswordParams: Check that a function does not use both username and password
/// parameters.
/// </summary>
[Export(typeof(IScriptRule))]
public class AvoidUserNameAndPasswordParams : IScriptRule
public class AvoidUsernameAndPasswordParams : IScriptRule
{
/// <summary>
/// AnalyzeScript: Check that a function does not use both username
Expand Down Expand Up @@ -77,8 +77,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
if (hasUserName && hasPwd)
{
yield return new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, Strings.AvoidUserNameAndPasswordParamsError, funcAst.Name),
funcAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsError, funcAst.Name),
funcAst.Extent, GetName(), DiagnosticSeverity.Error, fileName);
}
}
}
Expand All @@ -89,7 +89,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
/// <returns>The name of this rule</returns>
public string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUserNameAndPasswordParamsName);
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsernameAndPasswordParamsName);
}

/// <summary>
Expand All @@ -98,7 +98,7 @@ public string GetName()
/// <returns>The common name of this rule</returns>
public string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUserNameAndPasswordParamsCommonName);
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsCommonName);
}

/// <summary>
Expand All @@ -107,7 +107,7 @@ public string GetCommonName()
/// <returns>The description of this rule</returns>
public string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUserNameAndPasswordParamsDescription);
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsDescription);
}

/// <summary>
Expand Down
14 changes: 7 additions & 7 deletions Rules/ScriptAnalyzerBuiltinRules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<Compile Include="AvoidPositionalParameters.cs" />
<Compile Include="AvoidReservedCharInCmdlet.cs" />
<Compile Include="AvoidReservedParams.cs" />
<Compile Include="AvoidShouldContinueShouldProcessWithoutForce.cs" />
<Compile Include="AvoidShouldContinueWithoutForce.cs" />
<Compile Include="AvoidTrapStatement.cs" />
<Compile Include="AvoidUnitializedVariable.cs" />
<Compile Include="AvoidUserNameAndPasswordParams.cs" />
Expand All @@ -73,6 +73,11 @@
<Compile Include="PossibleIncorrectComparisonWithNull.cs" />
<Compile Include="ProvideCommentHelp.cs" />
<Compile Include="ProvideVerboseMessage.cs" />
<Compile Include="Strings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
<Compile Include="UseApprovedVerbs.cs" />
<Compile Include="UseCmdletCorrectly.cs" />
<Compile Include="UseDeclaredVarsMoreThanAssignments.cs" />
Expand All @@ -82,17 +87,12 @@
<Compile Include="UseShouldProcessCorrectly.cs" />
<Compile Include="UseSingularNouns.cs" />
<Compile Include="UseStandardDSCFunctionsInResource.cs" />
<Compile Include="Strings.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Strings.resx">
<SubType>Designer</SubType>
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Strings.cs</LastGenOutput>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
Expand Down
Loading