PowerShell / PowerShell/PSScriptAnalyzer

PSUseOutputTypeCorrectly is triggered when it shouldn't.

Aberta
#676 3 comentários 3 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

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

Descrição

It looks to me like PSUseOutputTypeCorrectly check is not taking very basic, crucial, fundamental fact about PowerShell into account: by default it will not keep any simple collection, unless you explicitly tell it to.

Result? I have PInvoke code (taken from here) and I correctly defined OutputType as [Connection] (I've actually created namespace for it, but that's irrelevant).
As expected, Get-Member confirms that PowerShell function will return stream of connections, not collection as a whole. Yet, PSScriptAnalyzer will complain about it:

Line: 45 - The cmdlet 'Get-OPNetStat' returns an object of type 'Optiver.Connection[]' but this type is not declared in the OutputType attribute. 
Line: 54 - The cmdlet 'Get-OPNetStat' returns an object of type 'Optiver.Connection[]' but this type is not declared in the OutputType attribute. 

In fact, if I would follow instructions and declare my OutputType as a collection it would be conflicting with what my function actually returns:

Set-StrictMode -Version Latest
Get-OPNetStat | % IsFixedSize
% : The input name "IsFixedSize" cannot be resolved to a member.

IsFixedSize was tab-completed, based on OutputType that PSScriptAnalyzer told me to define. As expected, my Connection object doesn't have it. The only workaround I've found that satisfies both PSScriptAnalyzer and my hope to get nice tab-completion for command I wrote is to pass result of the command thru ForEach-Object:

[Optiver.NetworkUtil]::GetTCP() } | ForEach-Object { $_ }

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 pela regra PSUseOutputTypeCorrectly e reproduza o exemplo relatado de Get-OPNetStat, incluindo a saída de [Optiver.NetworkUtil]::GetTCP() e o comportamento de Get-Member. Compare o tipo de saída declarado [Connection] com o diagnóstico Connection[] do analisador; considera-se concluído quando uma saída de stream válida não produzir mais este falso aviso, enquanto a conclusão por tabulação do tipo de saída continuar precisa.

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
Bug
Dificuldade
4/5
Tempo estimado
3-5 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.