Indented.ScriptAnalyzerRules icon indicating copy to clipboard operation
Indented.ScriptAnalyzerRules copied to clipboard

Rule UseSyntacticallyCorrectExamples fails if parameter uses type that is not loaded

Open johlju opened this issue 3 years ago • 4 comments

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.

johlju avatar Jun 14 '22 07:06 johlju

Thanks! I'll see if I can dream up a way around this.

Chris

indented-automation avatar Jun 14 '22 08:06 indented-automation

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

johlju avatar Jul 10 '22 07:07 johlju

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.

indented-automation avatar Jul 10 '22 08:07 indented-automation

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.

image

johlju avatar Jul 26 '22 09:07 johlju