Indented.ScriptAnalyzerRules
Indented.ScriptAnalyzerRules copied to clipboard
Rule UseSyntacticallyCorrectExamples fails if parameter uses type that is not loaded
If the rule is testing a function hat has a parameter with a type that is not loaded into the session the rule fails on all parameters in all examples.
For example if having this parameter:
param
(
[Parameter()]
[Microsoft.SqlServer.Management.Smo.Server]
$SqlServerObject
)
This row will return the function but the property Parameters will be $null
https://github.com/indented-automation/Indented.ScriptAnalyzerRules/blob/e1466ed5a82f30173e3f5ae58df6be08c62a2dac/Indented.ScriptAnalyzerRules/public/rules/UseSyntacticallyCorrectExamples.ps1#L25
which result in that this evaluation always return $true
https://github.com/indented-automation/Indented.ScriptAnalyzerRules/blob/e1466ed5a82f30173e3f5ae58df6be08c62a2dac/Indented.ScriptAnalyzerRules/public/rules/UseSyntacticallyCorrectExamples.ps1#L54
Suggest adding a error message when $null -eq $functionInfo.Parameters saying something like "Parameters of the function X could not be parsed, this can for example be due to that a type that is used by a parameter is not loaded into the session.".
I worked around this by loading stubs classes before running Invoke-ScriptAnalyzer.
Thanks! I'll see if I can dream up a way around this.
Chris
Should we just replace "unknown" types with [Object] before creating the ScriptBlock of the function definition? 🤔
https://github.com/indented-automation/Indented.ScriptAnalyzerRules/blob/e1466ed5a82f30173e3f5ae58df6be08c62a2dac/Indented.ScriptAnalyzerRules/public/rules/UseSyntacticallyCorrectExamples.ps1#L24
I was trying to avoid this, it means that every time the rule is evaluated every single script block is being stringified, edited, and tested again.
I was trying to see if I could come up with a different way of generating the syntax block, but I just haven't had time to really look.
This seems to also happen if the [OutputType()] is using a type that is not loaded into the session, e.g. [OutputType([DatabasePermission[]])]. In this case the DatabasePermission is a class that is not available until the module is built. Changing the output type to [OutputType([System.Object[]])] makes the rule pass.
