PowerShell / PowerShell/PSScriptAnalyzer

AvoidGlobalVars should only trigger on assignment

Abierto
#2,100 1 comentario 2 reacciones 0 asignados Ver en GitHub

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

PowerShell classes requires variables to be initialized in a method before reference. This causes issues with global variables without scoping as the parser will throw a Variable is not assigned in the method error. To get around this the variable must be scoped but this causes PSScriptAnalyzer to throw a violation. The rule should only trigger when assigning a variable to the global scope not reading.

Steps to reproduce

class Test {
  [void] Abc() {
    if ($global:PSEdition -eq 'core') { write-host core }
  }
}

Expected behavior

No warnings

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidGlobalVars                   Warning                 3     Found global variable 'global:PSEdition'.

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

Environment data

7.6-preview4

1.24.0

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza localizando la regla PS AvoidGlobalVars y reproduce el ejemplo de clase de PowerShell del issue en PowerShell 7.6-preview4. El fix está terminado cuando la lectura de $global:PSEdition no produzca ninguna advertencia, mientras que asignar una variable al ámbito global siga activando la regla.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
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
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.