Skip to content

Commit 08765c2

Browse files
author
Quoc Truong
committed
Change positional parameter rule so it will only be triggered if we have 3 positional parameters
1 parent b00bfff commit 08765c2

5 files changed

+24
-14
lines changed

Engine/Helper.cs

Lines changed: 7 additions & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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>
Lines changed: 2 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 1 deletion
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)