PowerShell / PowerShell/PSScriptAnalyzer

PSUseOutputTypeCorrectly: Fails when using multiple output types

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

Ninguém assumiu esta issue ainda.

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

Descrição

Steps to reproduce

The following code snippet returns to objects with different types.

function Get-Function
{
    [CmdletBinding()]
    [OutputType(
        [System.String],
        [System.UInt32])]
    param ()

    return [System.String] 'hello', [System.UInt32] 1
}

$myString, $myInt = Get-Function
$myString.GetType()
$myInt.GetType()

But when running the analyzer on this it hits rule PSUseOutputTypeCorrectly .

Invoke-ScriptAnalyzer -ScriptDefinition @'
function Get-Function
{
    [CmdletBinding()]
    [OutputType(
        [System.String],
        [System.UInt32])]
    param ()

    return [System.String] 'hello', [System.UInt32] 1
}
'@

Expected behavior

No rule should be hit since the OutputType contains two output types which is valid syntax according to this https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_outputtypeattribute?view=powershell-5.1

Two or more output types could equal to System.Object[]. But preferably it should see if there are multiple output types, and then see how many return values there are.

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseOutputTypeCorrectly            Information             9     The cmdlet 'Get-Function' returns an object of type '
                                                                  System.Object[]' but this type is not declared in the
                                                                   OutputType attribute.

Environment data

> $PSVersionTable


Name                           Value
----                           -----
PSVersion                      5.1.18890.1000
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18890.1000
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }

1.18.1

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 reproduzindo o exemplo com Invoke-ScriptAnalyzer e rastreie como a regra PSUseOutputTypeCorrectly lida com vários valores de OutputType e valores retornados. O trabalho estará concluído quando a função válida não produzir mais um diagnóstico e uma cobertura de regressão tiver sido adicionada onde quer que os testes dessa regra estejam localizados.

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
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
42/100

Receba novas issues na sua caixa de entrada

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