Skip to content

Commit 2a8ddcf

Browse files
Merge branch 'main' into #396ParameterSetNameMismatch
2 parents 374afec + f3e9134 commit 2a8ddcf

12 files changed

+1659
-354
lines changed

Engine/Generic/RuleSuppression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public static List<RuleSuppression> GetSuppressions(IEnumerable<AttributeAst> at
340340

341341
if (targetAsts != null)
342342
{
343-
if (targetAsts.Count() == 0)
343+
if (!targetAsts.Any())
344344
{
345345
if (String.IsNullOrWhiteSpace(scopeAst.Extent.File))
346346
{

Engine/ScriptAnalyzer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ internal bool ParseProfile(object profileObject, PathIntrinsics path, IOutputWri
267267
return false;
268268
}
269269

270-
this.severity = (severityList.Count() == 0) ? null : severityList.ToArray();
271-
this.includeRule = (includeRuleList.Count() == 0) ? null : includeRuleList.ToArray();
272-
this.excludeRule = (excludeRuleList.Count() == 0) ? null : excludeRuleList.ToArray();
270+
this.severity = (severityList.Count == 0) ? null : severityList.ToArray();
271+
this.includeRule = (includeRuleList.Count == 0) ? null : includeRuleList.ToArray();
272+
this.excludeRule = (excludeRuleList.Count == 0) ? null : excludeRuleList.ToArray();
273273
if (settings != null
274274
&& settings.ContainsKey("Rules"))
275275
{
@@ -609,7 +609,7 @@ private bool ParseProfileString(string profile, PathIntrinsics path, IOutputWrit
609609
IEnumerable<Ast> hashTableAsts = profileAst.FindAll(item => item is HashtableAst, false);
610610

611611
// no hashtable, raise warning
612-
if (hashTableAsts.Count() == 0)
612+
if (!hashTableAsts.Any())
613613
{
614614
writer.WriteError(new ErrorRecord(new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, profile)),
615615
Strings.ConfigurationFileHasNoHashTable, ErrorCategory.ResourceUnavailable, profile));

Engine/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ private void parseSettingsFile(string settingsFilePath)
453453
IEnumerable<Ast> hashTableAsts = profileAst.FindAll(item => item is HashtableAst, false);
454454

455455
// no hashtable, raise warning
456-
if (hashTableAsts.Count() == 0)
456+
if (!hashTableAsts.Any())
457457
{
458458
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Strings.InvalidProfile, settingsFilePath));
459459
}

Rules/AlignAssignmentStatement.cs

Lines changed: 587 additions & 213 deletions
Large diffs are not rendered by default.

Rules/AvoidMultipleTypeAttributes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3737
// Iterates all ParamAsts and check the number of its types.
3838
foreach (ParameterAst paramAst in paramAsts)
3939
{
40-
if (paramAst.Attributes.Where(typeAst => typeAst is TypeConstraintAst).Count() > 1)
40+
if (paramAst.Attributes.OfType<TypeConstraintAst>().Skip(1).Any())
4141
{
4242
yield return new DiagnosticRecord(
4343
String.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypeAttributesError, paramAst.Name),

Rules/PossibleIncorrectComparisonWithNull.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private IEnumerable<CorrectionExtent> GetCorrectionExtent(BinaryExpressionAst bi
104104
binaryExpressionAst.Extent.EndColumnNumber,
105105
$"{binaryExpressionAst.Right.Extent.Text} {binaryExpressionAst.ErrorPosition.Text} {binaryExpressionAst.Left.Extent.Text}",
106106
binaryExpressionAst.Extent.File,
107-
Strings.PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription
107+
Strings.PossibleIncorrectComparisonWithNullSuggestedCorrectionDescription
108108
);
109109

110110
yield return correction;

Rules/Strings.resx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
<value>Avoid Using Cmdlet Aliases or omitting the 'Get-' prefix.</value>
125125
</data>
126126
<data name="AvoidUsingEmptyCatchBlockDescription" xml:space="preserve">
127-
<value>Empty catch blocks are considered poor design decisions because if an error occurs in the try block, this error is simply swallowed and not acted upon. While this does not inherently lead to bad things. It can and this should be avoided if possible. To fix a violation of this rule, using Write-Error or throw statements in catch blocks.</value>
127+
<value>Empty catch blocks are considered poor design decisions because if an error occurs in the try block, this error is simply swallowed and not acted upon. While this does not inherently cause problems, it can, so it should be avoided where possible. To fix a violation of this rule, use Write-Error or throw statements in catch blocks.</value>
128128
</data>
129129
<data name="AvoidUsingEmptyCatchBlockCommonName" xml:space="preserve">
130130
<value>Avoid Using Empty Catch Block</value>
@@ -136,7 +136,7 @@
136136
<value>Avoid Using Invoke-Expression</value>
137137
</data>
138138
<data name="AvoidUsingPositionalParametersDescription" xml:space="preserve">
139-
<value>Readability and clarity should be the goal of any script we expect to maintain over time. When calling a command that takes parameters, where possible consider using name parameters as opposed to positional parameters. To fix a violation of this rule, please use named parameters instead of positional parameters when calling a command.</value>
139+
<value>Readability and clarity should be the goal of any script we expect to maintain over time. When calling a command that takes parameters, where possible consider using named parameters as opposed to positional parameters. To fix a violation of this rule, please use named parameters instead of positional parameters when calling a command.</value>
140140
</data>
141141
<data name="AvoidUsingPositionalParametersCommonName" xml:space="preserve">
142142
<value>Avoid Using Positional Parameters</value>
@@ -178,7 +178,7 @@
178178
<value>No Global Variables</value>
179179
</data>
180180
<data name="PossibleIncorrectComparisonWithNullDescription" xml:space="preserve">
181-
<value>Checks that $null is on the left side of any equaltiy comparisons (eq, ne, ceq, cne, ieq, ine). When there is an array on the left side of a null equality comparison, PowerShell will check for a $null IN the array rather than if the array is null. If the two sides of the comaprision are switched this is fixed. Therefore, $null should always be on the left side of equality comparisons just in case.</value>
181+
<value>Checks that $null is on the left side of any equality comparisons (eq, ne, ceq, cne, ieq, ine). When there is an array on the left side of a null equality comparison, PowerShell will check for a $null IN the array rather than if the array is null. If the two sides of the comparison are switched this is fixed. Therefore, $null should always be on the left side of equality comparisons just in case.</value>
182182
</data>
183183
<data name="PossibleIncorrectComparisonWithNullError" xml:space="preserve">
184184
<value>$null should be on the left side of equality comparisons.</value>
@@ -618,7 +618,7 @@
618618
<data name="UseShouldProcessForStateChangingFunctionsCommonName" xml:space="preserve">
619619
<value>Use ShouldProcess For State Changing Functions</value>
620620
</data>
621-
<data name="UseShouldProcessForStateChangingFunctionsDescrption" xml:space="preserve">
621+
<data name="UseShouldProcessForStateChangingFunctionsDescription" xml:space="preserve">
622622
<value>Functions that have verbs like New, Start, Stop, Set, Reset, Restart that change system state should support 'ShouldProcess'.</value>
623623
</data>
624624
<data name="UseShouldProcessForStateChangingFunctionsError" xml:space="preserve">
@@ -703,7 +703,7 @@
703703
<value>PowerShell help file needs to use UTF8 Encoding.</value>
704704
</data>
705705
<data name="UseUTF8EncodingForHelpFileError" xml:space="preserve">
706-
<value>File {0} has to use UTF8 instead of {1} encoding because it is a powershell help file.</value>
706+
<value>File {0} has to use UTF8 instead of {1} encoding because it is a PowerShell help file.</value>
707707
</data>
708708
<data name="UseUTF8EncodingForHelpFileName" xml:space="preserve">
709709
<value>UseUTF8EncodingForHelpFile</value>
@@ -742,7 +742,7 @@
742742
<value>Misleading Backtick</value>
743743
</data>
744744
<data name="MisleadingBacktickDescription" xml:space="preserve">
745-
<value>Ending a line with an escaped whitepsace character is misleading. A trailing backtick is usually used for line continuation. Users typically don't intend to end a line with escaped whitespace.</value>
745+
<value>Ending a line with an escaped whitespace character is misleading. A trailing backtick is usually used for line continuation. Users typically don't intend to end a line with escaped whitespace.</value>
746746
</data>
747747
<data name="MisleadingBacktickName" xml:space="preserve">
748748
<value>MisleadingBacktick</value>
@@ -786,16 +786,16 @@
786786
<data name="UseToExportFieldsInManifestCorrectionDescription" xml:space="preserve">
787787
<value>Replace {0} with {1}</value>
788788
</data>
789-
<data name="UseLiteralInitilializerForHashtableCommonName" xml:space="preserve">
789+
<data name="UseLiteralInitializerForHashtableCommonName" xml:space="preserve">
790790
<value>Create hashtables with literal initializers</value>
791791
</data>
792-
<data name="UseLiteralInitilializerForHashtableDescription" xml:space="preserve">
792+
<data name="UseLiteralInitializerForHashtableDescription" xml:space="preserve">
793793
<value>Use literal initializer, @{{}}, for creating a hashtable as they are case-insensitive by default</value>
794794
</data>
795-
<data name="UseLiteralInitilializerForHashtableError" xml:space="preserve">
796-
<value>Create hashtables with literal initliazers</value>
795+
<data name="UseLiteralInitializerForHashtableError" xml:space="preserve">
796+
<value>Create hashtables with literal initializers</value>
797797
</data>
798-
<data name="UseLiteralInitilializerForHashtableName" xml:space="preserve">
798+
<data name="UseLiteralInitializerForHashtableName" xml:space="preserve">
799799
<value>UseLiteralInitializerForHashtable</value>
800800
</data>
801801
<data name="UseCompatibleCmdletsName" xml:space="preserve">
@@ -874,7 +874,7 @@
874874
<value>The type accelerator '{0}' is not available by default in PowerShell version '{1}' on platform '{2}'</value>
875875
</data>
876876
<data name="AvoidGlobalFunctionsCommonName" xml:space="preserve">
877-
<value>Avoid global functiosn and aliases</value>
877+
<value>Avoid global functions and aliases</value>
878878
</data>
879879
<data name="AvoidGlobalFunctionsDescription" xml:space="preserve">
880880
<value>Checks that global functions and aliases are not used. Global functions are strongly discouraged as they can cause errors across different systems.</value>
@@ -979,7 +979,7 @@
979979
<value>Use consistent indentation</value>
980980
</data>
981981
<data name="UseConsistentIndentationDescription" xml:space="preserve">
982-
<value>Each statement block should have a consistent indenation.</value>
982+
<value>Each statement block should have a consistent indentation.</value>
983983
</data>
984984
<data name="UseConsistentIndentationError" xml:space="preserve">
985985
<value>Indentation not consistent</value>
@@ -991,7 +991,7 @@
991991
<value>Use whitespaces</value>
992992
</data>
993993
<data name="UseConsistentWhitespaceDescription" xml:space="preserve">
994-
<value>Check for whitespace between keyword and open paren/curly, around assigment operator ('='), around arithmetic operators and after separators (',' and ';')</value>
994+
<value>Check for whitespace between keyword and open paren/curly, around assignment operator ('='), around arithmetic operators and after separators (',' and ';')</value>
995995
</data>
996996
<data name="UseConsistentWhitespaceErrorBeforeOpeningBrace" xml:space="preserve">
997997
<value>Use space before open brace.</value>
@@ -1015,10 +1015,10 @@
10151015
<value>Use SupportsShouldProcess</value>
10161016
</data>
10171017
<data name="UseSupportsShouldProcessDescription" xml:space="preserve">
1018-
<value>Commands typically provide Confirm and Whatif parameters to give more control on its execution in an interactive environment. In PowerShell, a command can use a SupportsShouldProcess attribute to provide this capability. Hence, manual addition of these parameters to a command is discouraged. If a commands need Confirm and Whatif parameters, then it should support ShouldProcess.</value>
1018+
<value>Commands typically provide Confirm and WhatIf parameters to give more control on its execution in an interactive environment. In PowerShell, a command can use a SupportsShouldProcess attribute to provide this capability. Hence, manual addition of these parameters to a command is discouraged. If a command needs Confirm and WhatIf parameters, then it should support ShouldProcess.</value>
10191019
</data>
10201020
<data name="UseSupportsShouldProcessError" xml:space="preserve">
1021-
<value>Whatif and/or Confirm manually defined in function {0}. Instead, please use SupportsShouldProcess attribute.</value>
1021+
<value>WhatIf and/or Confirm manually defined in function {0}. Instead, please use SupportsShouldProcess attribute.</value>
10221022
</data>
10231023
<data name="AlignAssignmentStatementName" xml:space="preserve">
10241024
<value>AlignAssignmentStatement</value>
@@ -1042,10 +1042,10 @@
10421042
<value>Use a different variable name</value>
10431043
</data>
10441044
<data name="AvoidAssignmentToReadOnlyAutomaticVariableCommonName" xml:space="preserve">
1045-
<value>Changing automtic variables might have undesired side effects</value>
1045+
<value>Changing automatic variables might have undesired side effects</value>
10461046
</data>
10471047
<data name="AvoidAssignmentToReadOnlyAutomaticVariableDescription" xml:space="preserve">
1048-
<value>This automatic variables is built into PowerShell and readonly.</value>
1048+
<value>This automatic variable is built into PowerShell and readonly.</value>
10491049
</data>
10501050
<data name="AvoidAssignmentToReadOnlyAutomaticVariableError" xml:space="preserve">
10511051
<value>The Variable '{0}' cannot be assigned since it is a readonly automatic variable that is built into PowerShell, please use a different name.</value>
@@ -1077,7 +1077,7 @@
10771077
<data name="PossibleIncorrectUsageOfRedirectionOperatorName" xml:space="preserve">
10781078
<value>PossibleIncorrectUsageOfRedirectionOperator</value>
10791079
</data>
1080-
<data name="PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription" xml:space="preserve">
1080+
<data name="PossibleIncorrectComparisonWithNullSuggestedCorrectionDescription" xml:space="preserve">
10811081
<value>Use $null on the left hand side for safe comparison with $null.</value>
10821082
</data>
10831083
<data name="UseConsistentWhitespaceErrorAfterOpeningBrace" xml:space="preserve">
@@ -1177,7 +1177,7 @@
11771177
<value>Avoid multiple type specifiers on parameters</value>
11781178
</data>
11791179
<data name="AvoidMultipleTypeAttributesDescription" xml:space="preserve">
1180-
<value>Prameter should not have more than one type specifier.</value>
1180+
<value>Parameter should not have more than one type specifier.</value>
11811181
</data>
11821182
<data name="AvoidMultipleTypeAttributesError" xml:space="preserve">
11831183
<value>Parameter '{0}' has more than one type specifier.</value>
@@ -1219,7 +1219,7 @@
12191219
<value>Avoid sending credentials and secrets over unencrypted connections.</value>
12201220
</data>
12211221
<data name="AvoidUsingAllowUnencryptedAuthenticationError" xml:space="preserve">
1222-
<value>The insecure AllowUsingUnencryptedAuthentication switch was used. This should be avoided except for compatability with legacy systems.</value>
1222+
<value>The insecure AllowUnencryptedAuthentication switch was used. This should be avoided except for compatibility with legacy systems.</value>
12231223
</data>
12241224
<data name="AvoidUsingAllowUnencryptedAuthenticationName" xml:space="preserve">
12251225
<value>AvoidUsingAllowUnencryptedAuthentication</value>

Rules/UseLiteralInitializerForHashtable.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6262
/// </summary>
6363
public string GetCommonName()
6464
{
65-
return string.Format(CultureInfo.CurrentCulture, Strings.UseLiteralInitilializerForHashtableCommonName);
65+
return string.Format(CultureInfo.CurrentCulture, Strings.UseLiteralInitializerForHashtableCommonName);
6666
}
6767

6868
/// <summary>
6969
/// Retrieves the description of this rule.
7070
/// </summary>
7171
public string GetDescription()
7272
{
73-
return string.Format(CultureInfo.CurrentCulture, Strings.UseLiteralInitilializerForHashtableDescription);
73+
return string.Format(CultureInfo.CurrentCulture, Strings.UseLiteralInitializerForHashtableDescription);
7474
}
7575

7676
/// <summary>
@@ -82,7 +82,7 @@ public string GetName()
8282
CultureInfo.CurrentCulture,
8383
Strings.NameSpaceFormat,
8484
GetSourceName(),
85-
Strings.UseLiteralInitilializerForHashtableName);
85+
Strings.UseLiteralInitializerForHashtableName);
8686
}
8787

8888
/// <summary>
@@ -170,7 +170,7 @@ public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressio
170170
|| !HasIgnoreCaseComparerArg(methodCallAst.Arguments))
171171
{
172172
var dr = new DiagnosticRecord(
173-
Strings.UseLiteralInitilializerForHashtableDescription,
173+
Strings.UseLiteralInitializerForHashtableDescription,
174174
methodCallAst.Extent,
175175
GetName(),
176176
GetDiagnosticSeverity(),
@@ -205,7 +205,7 @@ private void AnalyzeNewObjectCommand(CommandAst commandAst)
205205
}
206206

207207
var dr = new DiagnosticRecord(
208-
Strings.UseLiteralInitilializerForHashtableDescription,
208+
Strings.UseLiteralInitializerForHashtableDescription,
209209
commandAst.Extent,
210210
GetName(),
211211
GetDiagnosticSeverity(),

Rules/UseShouldProcessForStateChangingFunctions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public string GetCommonName()
102102
/// <returns>The description of this rule</returns>
103103
public string GetDescription()
104104
{
105-
return string.Format(CultureInfo.CurrentCulture, Strings.UseShouldProcessForStateChangingFunctionsDescrption);
105+
return string.Format(CultureInfo.CurrentCulture, Strings.UseShouldProcessForStateChangingFunctionsDescription);
106106
}
107107

108108
/// <summary>

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,13 @@ Describe "Test Name parameters" {
6363

6464
It "get Rules with no parameters supplied" {
6565
$defaultRules = Get-ScriptAnalyzerRule
66-
$expectedNumRules = 73
67-
$defaultRules.Count | Should -Be $expectedNumRules
66+
# Dynamically count the expected number of rules from source files
67+
# by finding all C# files with [Export(typeof(I...Rule))] attributes
68+
$rulesRoot = Resolve-Path "$PSScriptRoot/../../Rules"
69+
$expectedNumRules = (Get-ChildItem -Path $rulesRoot -Filter '*.cs' -Recurse |
70+
Select-String -Pattern 'Export\(typeof\s*\(I\w+Rule\)\)' |
71+
Select-Object -ExpandProperty Path -Unique).Count
72+
$defaultRules.Count | Should -Be $expectedNumRules
6873
}
6974

7075
It "is a positional parameter" {

0 commit comments

Comments
 (0)