PowerShell / PowerShell/PSScriptAnalyzer
PSUseDeclaredVarsMoreThanAssignment not correctly handling global vars assigned to within functions
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- C#
- Estrellas
- 2.2k
- Forks
- 414
- Merge medio
- 13 h 1 min
- PR fusionados (30 d)
- 2
Descripción
This rule does not correctly take into account global variables if they are assigned to within a function.
If I set a global variable and then use it later, I still see a warning that the global variable was assigned to, but never used.
Given the following example:
function InitVars()
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="We need a global foo")]
param()
$global:globalFoo = $false
$script:scriptFoo = $false
}
function UseVars()
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Justification="We need a global foo")]
param()
if ($global:globalFoo)
{
Write-Information "`$global:globalFoo is true"
}
if ($scriptFoo)
{
Write-Information "`$script:scriptFoo is true"
}
}
I get the following warning:
- The variable 'globalFoo' is assigned but never used. (line 6)
There are scenarios where using global variables is still necessary, and so in those few instances I am suppressing PSAvoidGlobalVars. To do that, I have to do the assignment within a method.
As you can see above, it hits with global scoped variables, but not with script scoped variables.
This issue doesn't happen if I leave the global assignment in the root of the file (as opposed to within InitVars, but then I can't use SuppressMessageAttribute.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo el ejemplo proporcionado de InitVars y UseVars y, a continuación, sigue la regla PSUseDeclaredVarsMoreThanAssignment y sus pruebas existentes. Se considera terminado cuando la asignación global dentro de una función se reconoce como usada al hacerse referencia a ella posteriormente, mientras que el comportamiento actual para el ámbito del script permanece sin cambios.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- csharp, powershell
- Área
- tooling
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 42/100