PowerShell / PowerShell/PSScriptAnalyzer

Analyzer for Type Reassignment in `foreach` Loop

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

Ninguém assumiu esta issue ainda.

Area - Rules Issue - New Rule Up-for-Grabs
Linguagem predominante
C#
Estrelas
2.2k
Forks
415
Merge médio
13h 1min
PRs com merge (30d)
2

Descrição

Summary of the new feature
Consider the following script:

if ($true) {
    [String]$MyVar = "Blah"
}

[int[]]$MyInts = @(1,2,3)
foreach ($MyVar in $MyInts) {
    Write-Host "This is my type [$($MyVar.GetType())]"
}

A naïve C# reading of the above PowerShell would assume that the above would print out:

This is my type [int]
This is my type [int]
This is my type [int]

However this is PowerShell and due to Scoping Rules this actually returns:

This is my type [string]
This is my type [string]
This is my type [string]

Proposed technical implementation details (optional)

This has already burned me once in production.

As far as I can tell there is not an existing analyzer to determine this.

It is unclear if it is even feasible to do this reliably.

What is the latest version of PSScriptAnalyzer at the point of writing

PS > Get-Module | Where-Object {$_.Name -eq 'PSScriptAnalyzer' }

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     1.19.1                PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke…

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

Nenhum arquivo-fonte, teste ou ponto de entrada é nomeado. Comece revisando as regras existentes do analisador e as regras de escopo do PowerShell vinculadas e, em seguida, determine se a reatribuição de tipos em loops foreach pode ser detectada de forma confiável. Está concluído quando houver um diagnóstico documentado do analisador com testes que cubram o comportamento do exemplo.

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

Avaliação

Stack de tecnologia
csharp, powershell
Domínio
tooling
Tipo de issue
Funcionalidade
Dificuldade
5/5
Tempo estimado
Mais de uma semana
Status de atividade
Estagnada
Clareza
Precisa de esclarecimento
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

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