Skip to content

Commit b5be992

Browse files
committed
Modify AvoidUsernameAndPassword and AvoidReservedParams
Raise error when [cmdletbinding()] is used in AvoidReservedParamsRule Modify the list of strings to search in AvoidUsernameAndPassword. Also change the name of the rule to CamelCase.
1 parent 67fe204 commit b5be992

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

Rules/AvoidReservedParams.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
3333
IEnumerable<Ast> paramAsts = ast.FindAll(testAst => testAst is ParameterAst, true);
3434
Ast parentAst;
3535

36-
string funcName;
3736
string paramName;
3837

3938
PropertyInfo[] commonParams = typeof(CommonParameters).GetProperties();
@@ -55,14 +54,20 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
5554
parentAst = parentAst.Parent;
5655
}
5756

58-
if (parentAst is FunctionDefinitionAst) {
59-
funcName = string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsCmdletPrefix, (parentAst as FunctionDefinitionAst).Name);
57+
if (parentAst is FunctionDefinitionAst)
58+
{
59+
IEnumerable<Ast> attrs = parentAst.FindAll(testAttr => testAttr is AttributeAst, true);
60+
foreach (AttributeAst attr in attrs)
61+
{
62+
if (string.Equals(attr.Extent.Text, "[CmdletBinding()]",
63+
StringComparison.OrdinalIgnoreCase))
64+
{
65+
string funcName = string.Format(CultureInfo.CurrentCulture,Strings.ReservedParamsCmdletPrefix, (parentAst as FunctionDefinitionAst).Name);
66+
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcName,paramName), paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
67+
68+
}
69+
}
6070
}
61-
else {
62-
funcName = Strings.ReservedParamsScriptPrefix;
63-
}
64-
65-
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcName, paramName), paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
6671
}
6772
}
6873
}

Rules/AvoidUserNameAndPasswordParams.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3434
IEnumerable<Ast> functionAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
3535

3636
List<String> passwords = new List<String>() {"Password", "Passphrase"};
37-
List<String> usernames = new List<String>() { "Username", "User", "ID", "APIKey", "Key",
38-
"Account", "Name" };
37+
List<String> usernames = new List<String>() { "Username", "User"};
3938

4039
foreach (FunctionDefinitionAst funcAst in functionAsts)
4140
{

Rules/ScriptAnalyzerBuiltinRules.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<Compile Include="AvoidShouldContinueWithoutForce.cs" />
6262
<Compile Include="AvoidTrapStatement.cs" />
6363
<Compile Include="AvoidUnitializedVariable.cs" />
64-
<Compile Include="AvoidUserNameAndPasswordParams.cs" />
64+
<Compile Include="AvoidUsernameAndPasswordParams.cs" />
6565
<Compile Include="AvoidUsingClearHost.cs" />
6666
<Compile Include="AvoidUsingComputerNameHardcoded.cs" />
6767
<Compile Include="AvoidUsingConvertToSecureStringWithPlainText.cs" />

0 commit comments

Comments
 (0)