PowerShell / PowerShell/PSScriptAnalyzer

AvoidGlobalVars should only trigger on assignment

Aberta
#2,100 1 comentário 2 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Linguagem predominante
C#
Estrelas
2.2k
Forks
415
Merge médio
13h 1min
PRs com merge (30d)
2

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece localizando a regra PS AvoidGlobalVars e reproduza o exemplo de classe do PowerShell presente na issue no PowerShell 7.6-preview4. O fix estará concluído quando a leitura de $global:PSEdition não produzir nenhum aviso, enquanto atribuir uma variável ao escopo global continuar acionando a regra.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
powershell
Domínio
tooling
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.