GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — PSAvoidDefaultValueForMandatoryParameter: Fix param block and parameter set handling by liamjpeters · Pull Request #2121 · PowerShell/PSScriptAnalyzer Conversation
|
| Context "When there are no violations" { | |
| It "has 1 provide default value for mandatory parameter violation" { | |
| $violations = Invoke-ScriptAnalyzer -ScriptDefinition 'Function foo{ Param([Parameter(Mandatory=$false)]$Param1=''val1'', [Parameter(Mandatory)]$Param2=''val2'', $Param3=''val3'') }' | | |
| Where-Object { $_.RuleName -eq $ruleName } | |
| $violations.Count | Should -Be 0 | |
| } |
I believe any parameter in the block having Mandatory=$false causes the rule to break for subsequent parameters in the block.
Fixes #1623
The performance of the old rule and new rule are comparable, whilst flagging more cases. This is running the old test file (correcting for the erroneous test) 1,000 times. Times for each run reported by Pester (Duration.TotalMilliseconds field).
| Average (ms) | Min (ms) | Max (ms) | |
|---|---|---|---|
| Old Rule | 108.68 | 92.74 | 174.55 |
| New Rule | 105.28 | 85.46 | 195.63 |
Note: Caching of the string comparer suggested by copilot as an improvement. Not sure on the effectiveness of this change. Function comment-based help also written mostly by copilot - but reading it, it seems sensible.
.cs, .ps1 and .psm1 files have the correct copyright headerWIP: to the beginning of the title and remove the prefix when the PR is ready.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| .ToList(); | ||
|
|
||
| return parameterAttributes.Count > 0 && |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I believe the ToList and Count > 0 checks are unnecessary and that you can simple return .Where(...).All(...) directly.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
All(...) on an empty sequence returns true - which changes things.
The function returns true if the parameter has [Parameter()] attributes AND all of them were mandatory. Perhaps the name could be clearer.
I need an AllButDefinitelyAtLeastOnePlease(...) 🤔. Does that exist? 😅
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You are quite right about that because of the nuance with And(...) but we can still get rid of the ToList() (and a stray boolean comparison):
var parameterAttributes = paramAst.Attributes.OfType<AttributeAst>() .Where(attr => string.Equals(attr.TypeName?.Name, "parameter", StringComparison.OrdinalIgnoreCase)); return parameterAttributes.Any() && parameterAttributes.All(attr => attr.NamedArguments?.OfType<NamedAttributeArgumentAst>() .Any(namedArg => string.Equals(namedArg?.ArgumentName, "mandatory", StringComparison.OrdinalIgnoreCase) && Helper.Instance.GetNamedArgumentAttributeValue(namedArg)));
And I like how that reads: where, any and all which matches the description of the function.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The stray boolean comparison looks funky, I agree. Without it I got a bool? to bool implicit cast issue.
That nullable bool is coming from the null-conditional in attr.NamedArguments?..
Checking it shows it's not actually required, NamedArguments is always at least an empty ReadOnlyCollection.
I'll make those changes
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
715b2a7 to
35d42c1
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
AvoidDefaultValueForMandatoryParameter mistakenly reported.
There was an error while loading. Please reload this page.