Skip to content
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

AvoidDefaultValueForMandatoryParameter triggers when the field has specification: Mandatory=value and value!=0 #969

Conversation

kalgiz
Copy link
Contributor

@kalgiz kalgiz commented Apr 12, 2018

PR Summary

AvoidDefaultValueForMandatoryParameter triggers when the field has specification: Mandatory=value and value is not equal to 0.

Fixes: #877

PR Checklist

Note: Tick the boxes below that apply to this pull request by putting an x between the square brackets. Please mark anything not applicable to this PR NA.

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • User facing documentation needed
  • Change is not breaking
  • Make sure you've added a new test if existing tests do not effectively test the code changed
  • This PR is ready to merge and is not work in progress
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready

@kalgiz kalgiz requested a review from JamesWTruher April 12, 2018 22:58
Copy link
Collaborator

@bergmeister bergmeister left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Looks mostly OK but I have one question about one comparison, the rest are just minor comments.


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 }
Copy link
Collaborator

@bergmeister bergmeister Apr 13, 2018

Choose a reason for hiding this comment

The 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 Mandatory = 0. A cleanup of the other unnecessary and confusing parameters $Param2 and $Param3 would be nice as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

int mandatoryValue = 0;
if (namedArgument.ExpressionOmitted
|| (String.Equals(namedArgument.Argument.Extent.Text, "$true", StringComparison.OrdinalIgnoreCase))
|| (int.TryParse(namedArgument.Argument.Extent.Text, out mandatoryValue) && mandatoryValue != 0))
Copy link
Collaborator

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 the int mandatoryValue = 0; line.
But more importantly shouldn't the last comparison be mandatoryValue == 1?

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

@bergmeister bergmeister merged commit a035491 into PowerShell:development May 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants