Skip to content

Suppressing PSAvoidGlobalVars for a single variable #822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mikeclayton opened this issue Oct 20, 2017 · 2 comments
Open

Suppressing PSAvoidGlobalVars for a single variable #822

mikeclayton opened this issue Oct 20, 2017 · 2 comments

Comments

@mikeclayton
Copy link

Hi, I've got a legacy script which I've started running through PSScriptAnalyzer, and I've got a bunch of global variables which I'm gradually refactoring away. However, one of them is proving difficult to deal with so I'd like to suppress the PSAvoidGlobalVars error on just that one for now, while leaving the others still erroring to remind me I need to come back and fix them later.

I don't really want to exclude the rule entirely, so I've tried this...

myScript.ps1

$global:myGlobalVar1 = "abc";

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "")]
$global:myGlobalVar2 = "pqr";

$global:myGlobalVar3 = "xyz";

... but it doesn't seem to suppress the error for $global:myGlobalVar2.

Is there another way to do this?

@mikeclayton
Copy link
Author

I've also just read the documentation at https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules (which I probably should have done first!) but I'm still not sure how to do this.

I'm guessing I need to add the attribute to a script-level param block and scope / target the variable somehow, but I'm not sure what Scope or Target to use...

[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Scope="???", Target="???")]
param()

$global:myGlobalVar2 = "pqr";

@bergmeister
Copy link
Collaborator

bergmeister commented Feb 8, 2018

@mikeclayton I don't think PSSA supports to limit the scope of syppresion only to a certain variable. The Scope parameter would be e.g. script, function, or class and the Target allows filtering of functions that should suppress this rule. You would need to put the declaration therefore in a function or script of its own, similar to how I do it here.
At the moment, ideas are also floating around in issue 849 to enhance suppression to be able to enable/disable suppresions just for a few lines (similar to ReSharper warnings).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants