-
Notifications
You must be signed in to change notification settings - Fork 400
AvoidDefaultValueForMandatoryParameter triggers when the field has specification: Mandatory=value and value!=0 #969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,12 @@ Describe "AvoidDefaultValueForMandatoryParameter" { | |
Where-Object { $_.RuleName -eq $ruleName } | ||
$violations.Count | Should -Be 1 | ||
} | ||
|
||
It "returns violations when the parameter is specified as mandatory=1 and has a default value" { | ||
$violations = Invoke-ScriptAnalyzer -ScriptDefinition 'Function foo{ Param([Parameter(Mandatory=1)]$Param1=''defaultValue'') }' | | ||
Where-Object { $_.RuleName -eq $ruleName } | ||
$violations.Count | Should -Be 1 | ||
} | ||
} | ||
|
||
Context "When there are no violations" { | ||
|
@@ -21,5 +27,11 @@ Describe "AvoidDefaultValueForMandatoryParameter" { | |
Where-Object { $_.RuleName -eq $ruleName } | ||
$violations.Count | Should -Be 0 | ||
} | ||
|
||
It "returns no violations when the parameter is specified as mandatory=0 and has a default value" { | ||
$violations = Invoke-ScriptAnalyzer -ScriptDefinition 'Function foo{ Param([Parameter(Mandatory=$false)]$Param1=''val1'', [Parameter(Mandatory)]$Param2=''val2'', $Param3=''val3'') }' | | ||
Where-Object { $_.RuleName -eq $ruleName } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test seems to be a copy-paste version of the test above if I am not mistaking? I think you meant to change it t have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
$violations.Count | Should -Be 0 | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C# 7
allows you to inline out parameter declaration as(int.TryParse(namedArgument.Argument.Extent.Text, out int mandatoryValue)
. This would allow you to delete theint mandatoryValue = 0;
line.But more importantly shouldn't the last comparison be
mandatoryValue == 1
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the powershell interpreter accepts every numeric value of Mandatory parameter. So all the following: Parameter(Mandatory=1), Parameter(Mandatory=-1), Parameter(Mandatory=100) are acceptable and mean that PowerShell treats the parameter as mandatory. Only in case Parameter(Mandatory=0), the parameter is not mandatory.
Hence, I adjusted the rule to the way PowerShell works, but definieily specifying parameter as e.g. Parameter(Mandatory=-100) is not a good coding practice... Maybe we should add some special rule for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, sounds like we should raise at least an issue in the PowerShell repo to not allow values other than 0/1. If someone specified something else then it is most likely a so called fat-finger error. I can see the point of warning in PSSA but I am not sure if it is worth the effort and computational expense of having an extra rule just for that specifically but maybe rather a more generic rule for syntax validation ala PossibleUninentionalSyntax that we could use in the future for warnings unrelated to a rule itself. Therefore this question/issue should not concern/block this PR, which is a set of complete changes leading to improved behaviour of PSSA, therefore I will approve.