Skip to content

Commit e49e618

Browse files
author
Quoc Truong
committed
Merge pull request #306 from PowerShell/FixPositionalParameter
Change positional parameter rule so it will only be triggered if we h…
2 parents b00bfff + 85c8b9d commit e49e618

8 files changed

+30
-20
lines changed

Engine/Helper.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,9 @@ public bool HasSplattedVariable(CommandAst cmdAst)
283283
/// Given a commandast, checks whether positional parameters are used or not.
284284
/// </summary>
285285
/// <param name="cmdAst"></param>
286+
/// <param name="moreThanThreePositional">only return true if more than three positional parameters are used</param>
286287
/// <returns></returns>
287-
public bool PositionalParameterUsed(CommandAst cmdAst)
288+
public bool PositionalParameterUsed(CommandAst cmdAst, bool moreThanThreePositional = false)
288289
{
289290
if (cmdAst == null || cmdAst.GetCommandName() == null)
290291
{
@@ -351,6 +352,11 @@ public bool PositionalParameterUsed(CommandAst cmdAst)
351352
arguments += 1;
352353
}
353354

355+
if (moreThanThreePositional && arguments < 3)
356+
{
357+
return false;
358+
}
359+
354360
return arguments > parameters;
355361
}
356362

Rules/AvoidPositionalParameters.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
4545
if (cmdAst.GetCommandName() == null) continue;
4646

4747
if (Helper.Instance.GetCommandInfo(cmdAst.GetCommandName()) != null
48-
&& Helper.Instance.PositionalParameterUsed(cmdAst))
48+
&& Helper.Instance.PositionalParameterUsed(cmdAst, true))
4949
{
5050
PipelineAst parent = cmdAst.Parent as PipelineAst;
5151

@@ -55,14 +55,14 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5555
if (parent.PipelineElements[0] == cmdAst)
5656
{
5757
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersError, cmdAst.GetCommandName()),
58-
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, cmdAst.GetCommandName());
58+
cmdAst.Extent, GetName(), DiagnosticSeverity.Information, fileName, cmdAst.GetCommandName());
5959
}
6060
}
6161
// not in pipeline so just raise it normally
6262
else
6363
{
6464
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPositionalParametersError, cmdAst.GetCommandName()),
65-
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName, cmdAst.GetCommandName());
65+
cmdAst.Extent, GetName(), DiagnosticSeverity.Information, fileName, cmdAst.GetCommandName());
6666
}
6767
}
6868
}
@@ -109,7 +109,7 @@ public SourceType GetSourceType()
109109
/// <returns></returns>
110110
public RuleSeverity GetSeverity()
111111
{
112-
return RuleSeverity.Warning;
112+
return RuleSeverity.Information;
113113
}
114114

115115
/// <summary>

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Describe "TestSeverity" {
118118

119119
It "filters rules based on multiple severity inputs"{
120120
$rules = Get-ScriptAnalyzerRule -Severity Error,Information
121-
$rules.Count | Should be 13
121+
$rules.Count | Should be 14
122122
}
123123

124124
It "takes lower case inputs" {

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

+4-4
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ Describe "Test IncludeRule" {
143143
Context "IncludeRule supports wild card" {
144144
It "includes 1 wildcard rule"{
145145
$includeWildcard = Invoke-ScriptAnalyzer $directory\..\Rules\BadCmdlet.ps1 -IncludeRule $avoidRules
146-
$includeWildcard.Count | Should be 3
146+
$includeWildcard.Count | Should be 0
147147
}
148148

149149
it "includes 2 wildcardrules" {
150150
$includeWildcard = Invoke-ScriptAnalyzer $directory\..\Rules\BadCmdlet.ps1 -IncludeRule $avoidRules, $useRules
151-
$includeWildcard.Count | Should be 7
151+
$includeWildcard.Count | Should be 4
152152
}
153153
}
154154
}
@@ -174,12 +174,12 @@ Describe "Test Severity" {
174174

175175
It "works with 2 arguments" {
176176
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity Information, Warning
177-
$errors.Count | Should Be 2
177+
$errors.Count | Should Be 1
178178
}
179179

180180
It "works with lowercase argument"{
181181
$errors = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -Severity information, warning
182-
$errors.Count | Should Be 2
182+
$errors.Count | Should Be 1
183183
}
184184
}
185185

Tests/Engine/RuleSuppression.tests.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Describe "RuleSuppressionWithScope" {
5050
Context "FunctionScope" {
5151
It "Does not raise violations" {
5252
$suppression = $violations | Where-Object {$_.RuleName -eq "PSAvoidUsingPositionalParameters" }
53-
$suppression.Count | Should Be 1
53+
$suppression.Count | Should Be 0
5454
}
5555
}
5656

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
Get-Content Test
2-
Get-ChildItem Tests
3-
Write-Output "I don't want to use positional parameters"
4-
Split-Path "RandomPath" -Leaf
5-
Get-Process | ForEach-Object {Write-Host $_.name -foregroundcolor cyan}
1+
# give it 3 positional parameters
2+
Get-Command "abc" 4 4.3

Tests/Rules/AvoidPositionalParameters.tests.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Import-Module PSScriptAnalyzer
2-
$violationMessage = "Cmdlet 'Write-Host' has positional parameter. Please use named parameters instead of positional parameters when calling a command."
2+
$violationMessage = "Cmdlet 'Get-Command' has positional parameter. Please use named parameters instead of positional parameters when calling a command."
33
$violationName = "PSAvoidUsingPositionalParameters"
44
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
55
$violations = Invoke-ScriptAnalyzer $directory\AvoidPositionalParameters.ps1 | Where-Object {$_.RuleName -eq $violationName}
@@ -8,8 +8,8 @@ $noViolationsDSC = Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue $director
88

99
Describe "AvoidPositionalParameters" {
1010
Context "When there are violations" {
11-
It "has 4 avoid positional parameters violation" {
12-
$violations.Count | Should Be 5
11+
It "has 1 avoid positional parameters violation" {
12+
$violations.Count | Should Be 1
1313
}
1414

1515
It "has the correct description message" {

Tests/Rules/AvoidPositionalParametersNoViolations.ps1

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@ get-service-computername localhost | where {($_.status -eq "Running") -and ($_.C
1313
function TestExternalApplication
1414
{
1515
& "c:\Windows\System32\Calc.exe" parameter1
16-
}
16+
}
17+
18+
# less than 3 arguments so rule won't trigger
19+
Get-Content Test
20+
Get-ChildItem Tests
21+
Write-Output "I don't want to use positional parameters"
22+
Split-Path "RandomPath" -Leaf
23+
Get-Process | ForEach-Object {Write-Host $_.name -foregroundcolor cyan}

0 commit comments

Comments
 (0)