Find a way to avoid `PSUseDeclaredVarsMoreThanAssignments` on `Properties`
#329 aberto em 7 de mar. de 2023
Métricas do repositório
- Stars
- (1.585 stars)
- Métricas de merge de PR
- (Nenhuma PRs mesclada em 30d)
Description
In a psafile.ps1 you usually get the PSUseDeclaredVarsMoreThanAssignments warning, because it seems those variables are not used outside of that block. The only way to supress it is to add:
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', "")]
param()
To the top of the file, but this will suppress this diagnostic everywhere in the file, not only in Properties.
Expected Behavior
I'd like to have the PSUseDeclaredVarsMoreThanAssignments warning to not appear on the Properties section, but to still work in the rest of the file.
Current Behavior
You either accept that the warning will be there in the beginning, or disable it entirely.
Possible Solution
If you add the script scope to the variable it does not warn anymore, I'm not sure if this is the best solution. Like so:
Properties {
$script:x = 1
}
Steps to Reproduce (for bugs)
- Write any psakefile with Properties
- Install the
PSScriptAnalyzermodule - Run
Invoke-ScriptAnalyzer -Path .\psakefile.ps1
Context
I'm trying to write clean code without warnings. If the solution I'm proposing is good enough, maybe it should be in the docs somewhere.
Your Environment
- Module version used:
4.9.0 - Operating System and PowerShell version: Windows 11, PoSh
7.3.3.