2

If a variable is declared in the Describe block of a PSPester script, and it is used in a It block, the PSScriptAnalyzer doesn't recognize the usage of that variable.

How do I have to declare the variable to get rid of the warning, or how do i have to apply the Diagnostics.CodeAnalysis.SuppressMessage for the PSUseDeclaredVarsMoreThanAssignment rule to suppress the warning for that single declaration?

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
abbgrade
  • 548
  • 5
  • 19
  • You are not the only one to face this behaviour : https://github.com/PowerShell/PSScriptAnalyzer/issues/711 (or https://github.com/PowerShell/vscode-powershell/issues/889). Since I use VS Code (1,5 year), this behaviour always happens... – Manu Nov 22 '17 at 16:00

1 Answers1

3

Another work-around would be to assign both variables to the script scope. It seems like PSScriptAnalyzer is trying to be clever and sees the curly braces as sub-scopes. Therfore the $foo variable is not used in the BeforeEach scope, but is used although not assigned in the Itscope.

EX: $script:foo does not result in a linting error.

The actual solution would be to help the vscode-powershell extension development. That might be more difficult though.

derekbaker783
  • 8,109
  • 4
  • 36
  • 50
dizzi90
  • 80
  • 8