PowerShell / PowerShell/PSScriptAnalyzer

PSUseOutputTypeCorrectly is triggered when it shouldn't.

Abierto
#676 3 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Issue - Bug
Lenguaje dominante
C#
Estrellas
2.2k
Forks
414
Merge medio
13 h 1 min
PR fusionados (30 d)
2

Descripción

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 { $_ }

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

Empieza por la regla PSUseOutputTypeCorrectly y reproduce el ejemplo reportado de Get-OPNetStat, incluida la salida de [Optiver.NetworkUtil]::GetTCP() y el comportamiento de Get-Member. Compara el tipo de salida declarado [Connection] con el diagnóstico Connection[] del analizador; se considera terminado cuando una salida de stream válida ya no produce esta falsa advertencia, mientras la finalización mediante tabulador del tipo de salida sigue siendo precisa.

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
4/5
Tiempo estimado
3-5 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.