Skip to content

Rule PSUseDeclaredVarsMoreThanAssignments generates false positives (warnings) where it should not #201

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

Closed
KirkMunro opened this issue May 22, 2015 · 1 comment

Comments

@KirkMunro
Copy link
Contributor

Problem #1:
It is common in psm1 files to define variables that are used by functions in the rest of the module. These module-scope variables are often initialized and not used in the psm1 file itself. Yet running Invoke-ScriptAnalyzer against a psm1 file that defines these variables and properly initialized them causes warnings to be generated using this rule.

Solution #1:
Once Invoke-ScriptAnalyzer supports invocation against modules (as proposed in #199), this rule should check more broadly to see if variables that are declared and initialized in a script module (psm1) file are used in other files in the module. These variables that are in "script" scope can be found by looking for $script:variableName or by looking for references to Get-/Set-/Remove-Variable -Scope Script -Name variableName.

Problem #2:
This rule generates a warning against a function that sets a built-in variable using the script scope. I have the following line in one of my functions:
$Script:PSDefaultParameterValues['Get-DoPxWebRequestHeader:AccessToken'] = $AccessToken

Solution #2:
That line is perfectly valid, and it is not initializing a variable that is never used because it is a built-in variable. This rule needs to ignore built-in variables, whether they are scoped (script/global, or using numeric scopes with Get-/Set-Variable in the -Scope parameter) or not.

@quoctruong
Copy link

Addressed in #205

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