Skip to content

Commit 04c5c63

Browse files
committed
Merge pull request #2 from yutingc/master
Finalize rule severity
2 parents 61613db + e39b02e commit 04c5c63

File tree

5 files changed

+196
-86
lines changed

5 files changed

+196
-86
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Management.Automation;
8+
using System.Management.Automation.Language;
9+
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
10+
using System.ComponentModel.Composition;
11+
using System.Resources;
12+
using System.Globalization;
13+
using System.Threading;
14+
using System.Reflection;
15+
16+
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
17+
{
18+
/// <summary>
19+
/// AvoidShouldContinueWithoutForceParameter: Check that if ShouldContinue is used,
20+
/// the function should have a boolean force parameter
21+
/// </summary>
22+
[Export(typeof(IScriptRule))]
23+
public class AvoidShouldContinueWithoutForce : IScriptRule
24+
{
25+
/// <summary>
26+
/// AvoidShouldContinueWithoutForceCheck that if ShouldContinue is used,
27+
/// the function should have a boolean force parameter
28+
/// </summary>
29+
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
30+
{
31+
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
32+
33+
// Finds all ParamAsts.
34+
IEnumerable<Ast> funcAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
35+
36+
// Iterrates all ParamAsts and check if there are any force.
37+
foreach (FunctionDefinitionAst funcAst in funcAsts)
38+
{
39+
IEnumerable<Ast> paramAsts = funcAst.FindAll(testAst => testAst is ParameterAst, true);
40+
bool hasForce = false;
41+
42+
foreach (ParameterAst paramAst in paramAsts)
43+
{
44+
if (String.Equals(paramAst.Name.VariablePath.ToString(), "force", StringComparison.OrdinalIgnoreCase)
45+
&& String.Equals(paramAst.StaticType.FullName, "System.Boolean", StringComparison.OrdinalIgnoreCase))
46+
{
47+
hasForce = true;
48+
break;
49+
}
50+
}
51+
52+
if (hasForce)
53+
{
54+
continue;
55+
}
56+
57+
IEnumerable<Ast> imeAsts = funcAst.FindAll(testAst => testAst is InvokeMemberExpressionAst, true);
58+
59+
foreach (InvokeMemberExpressionAst imeAst in imeAsts)
60+
{
61+
VariableExpressionAst typeAst = imeAst.Expression as VariableExpressionAst;
62+
if (typeAst == null) continue;
63+
64+
if (String.Equals(typeAst.VariablePath.UserPath, "pscmdlet", StringComparison.OrdinalIgnoreCase)
65+
&& (String.Equals(imeAst.Member.Extent.Text, "shouldcontinue", StringComparison.OrdinalIgnoreCase)))
66+
{
67+
yield return new DiagnosticRecord(
68+
String.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceError, funcAst.Name, System.IO.Path.GetFileName(fileName)),
69+
imeAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
70+
}
71+
}
72+
}
73+
}
74+
75+
/// <summary>
76+
/// GetName: Retrieves the name of this rule.
77+
/// </summary>
78+
/// <returns>The name of this rule</returns>
79+
public string GetName()
80+
{
81+
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidShouldContinueWithoutForceName);
82+
}
83+
84+
/// <summary>
85+
/// GetCommonName: Retrieves the common name of this rule.
86+
/// </summary>
87+
/// <returns>The common name of this rule</returns>
88+
public string GetCommonName()
89+
{
90+
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceCommonName);
91+
}
92+
93+
/// <summary>
94+
/// GetDescription: Retrieves the description of this rule.
95+
/// </summary>
96+
/// <returns>The description of this rule</returns>
97+
public string GetDescription()
98+
{
99+
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidShouldContinueWithoutForceDescription);
100+
}
101+
102+
/// <summary>
103+
/// GetSourceType: Retrieves the type of the rule: builtin, managed or module.
104+
/// </summary>
105+
public SourceType GetSourceType()
106+
{
107+
return SourceType.Builtin;
108+
}
109+
110+
/// <summary>
111+
/// GetSourceName: Retrieves the module/assembly name the rule is from.
112+
/// </summary>
113+
public string GetSourceName()
114+
{
115+
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
116+
}
117+
}
118+
}

Rules/AvoidUserNameAndPasswordParams.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
1717
{
1818
/// <summary>
19-
/// AvoidUserNameAndPasswordParams: Check that a function does not use both username and password
19+
/// AvoidUsernameAndPasswordParams: Check that a function does not use both username and password
2020
/// parameters.
2121
/// </summary>
2222
[Export(typeof(IScriptRule))]
23-
public class AvoidUserNameAndPasswordParams : IScriptRule
23+
public class AvoidUsernameAndPasswordParams : IScriptRule
2424
{
2525
/// <summary>
2626
/// AnalyzeScript: Check that a function does not use both username
@@ -77,8 +77,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7777
if (hasUserName && hasPwd)
7878
{
7979
yield return new DiagnosticRecord(
80-
String.Format(CultureInfo.CurrentCulture, Strings.AvoidUserNameAndPasswordParamsError, funcAst.Name),
81-
funcAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
80+
String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsError, funcAst.Name),
81+
funcAst.Extent, GetName(), DiagnosticSeverity.Error, fileName);
8282
}
8383
}
8484
}
@@ -89,7 +89,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
8989
/// <returns>The name of this rule</returns>
9090
public string GetName()
9191
{
92-
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUserNameAndPasswordParamsName);
92+
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsernameAndPasswordParamsName);
9393
}
9494

9595
/// <summary>
@@ -98,7 +98,7 @@ public string GetName()
9898
/// <returns>The common name of this rule</returns>
9999
public string GetCommonName()
100100
{
101-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUserNameAndPasswordParamsCommonName);
101+
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsCommonName);
102102
}
103103

104104
/// <summary>
@@ -107,7 +107,7 @@ public string GetCommonName()
107107
/// <returns>The description of this rule</returns>
108108
public string GetDescription()
109109
{
110-
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUserNameAndPasswordParamsDescription);
110+
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsernameAndPasswordParamsDescription);
111111
}
112112

113113
/// <summary>

Rules/ScriptAnalyzerBuiltinRules.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<Compile Include="AvoidPositionalParameters.cs" />
5959
<Compile Include="AvoidReservedCharInCmdlet.cs" />
6060
<Compile Include="AvoidReservedParams.cs" />
61-
<Compile Include="AvoidShouldContinueShouldProcessWithoutForce.cs" />
61+
<Compile Include="AvoidShouldContinueWithoutForce.cs" />
6262
<Compile Include="AvoidTrapStatement.cs" />
6363
<Compile Include="AvoidUnitializedVariable.cs" />
6464
<Compile Include="AvoidUserNameAndPasswordParams.cs" />
@@ -73,6 +73,11 @@
7373
<Compile Include="PossibleIncorrectComparisonWithNull.cs" />
7474
<Compile Include="ProvideCommentHelp.cs" />
7575
<Compile Include="ProvideVerboseMessage.cs" />
76+
<Compile Include="Strings.Designer.cs">
77+
<AutoGen>True</AutoGen>
78+
<DesignTime>True</DesignTime>
79+
<DependentUpon>Strings.resx</DependentUpon>
80+
</Compile>
7681
<Compile Include="UseApprovedVerbs.cs" />
7782
<Compile Include="UseCmdletCorrectly.cs" />
7883
<Compile Include="UseDeclaredVarsMoreThanAssignments.cs" />
@@ -82,17 +87,12 @@
8287
<Compile Include="UseShouldProcessCorrectly.cs" />
8388
<Compile Include="UseSingularNouns.cs" />
8489
<Compile Include="UseStandardDSCFunctionsInResource.cs" />
85-
<Compile Include="Strings.cs">
86-
<AutoGen>True</AutoGen>
87-
<DesignTime>True</DesignTime>
88-
<DependentUpon>Strings.resx</DependentUpon>
89-
</Compile>
9090
</ItemGroup>
9191
<ItemGroup>
9292
<EmbeddedResource Include="Strings.resx">
9393
<SubType>Designer</SubType>
9494
<Generator>ResXFileCodeGenerator</Generator>
95-
<LastGenOutput>Strings.cs</LastGenOutput>
95+
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
9696
</EmbeddedResource>
9797
</ItemGroup>
9898
<ItemGroup>

0 commit comments

Comments
 (0)