PowerShell / PowerShell/PSScriptAnalyzer

Formatting fails to pretty print a bad formatted document

Aperta
#1,709 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Area - Formatter Issue - Bug
Lingua principale
C#
Stelle
2.2k
Fork
414
Merge medio
13h 1m
PR unite (30g)
2

Descrizione

Prerequisites
  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
Summary

I have some code copied from a pdf. Here it is when I paste it in VSCode :

function Stop-ProcessUsingWMI{    [CmdletBinding(SupportsShouldProcess=$True)]    param(      [parameter(mandatory=$true)] [regex] $pattern    )    foreach ($process in Get-WmiObject Win32_Process |        where { $_.Name -match $pattern })    {        if ($PSCmdlet.ShouldProcess(            "process $($process.Name) " +            " (id: $($process.ProcessId))" ,            "Stop Process"))        {            $process.Terminate()        }    }}

A long one-line code.

After formatting it, I get this :

function Stop-ProcessUsingWMI {
	[CmdletBinding(SupportsShouldProcess = $True)]    param(      [parameter(mandatory = $true)] [regex] $pattern    )    foreach ($process in Get-WmiObject Win32_Process | Where-Object { $_.Name -match $pattern }) {
		if ($PSCmdlet.ShouldProcess(            "process $($process.Name) " + " (id: $($process.ProcessId))" , 'Stop Process')) {
			$process.Terminate()        
		}    
	}
}

It is better but far from what one could expect from a well formatted code. There are multiple spaces everywhere and function body is not standard. Here is what I would expect :

function Stop-ProcessUsingWMI {
	[CmdletBinding(SupportsShouldProcess = $True)]
	param(
		[parameter(mandatory = $true)] [regex] $pattern 
	)
	
	foreach ($process in Get-WmiObject Win32_Process | Where-Object { $_.Name -match $pattern }) {
		if ($PSCmdlet.ShouldProcess( "process $($process.Name) " + " (id: $($process.ProcessId))" , 'Stop Process')) {
			$process.Terminate() 
		} 
	}
}
System Version
OS Win 10 x64 pro (french) 19043.1165
VS Code version 1.59.1
Extension version v2021.8.0
Powershell 7.1.4

PS extension custom settings :

	"powershell.buttons.showPanelMovementButtons": true,
	"powershell.codeFormatting.autoCorrectAliases": true,
	"powershell.codeFormatting.preset": "OTBS",
	"powershell.codeFormatting.trimWhitespaceAroundPipe": true,
	"powershell.codeFormatting.useCorrectCasing": true,
	"powershell.integratedConsole.focusConsoleOnExecute": false,
	"powershell.codeFormatting.useConstantStrings": true,
	"security.workspace.trust.untrustedFiles": "open",
	"editor.suggest.preview": true,
	"powershell.codeFormatting.ignoreOneLineBlock": false,
	"powershell.codeFormatting.whitespaceBetweenParameters": true
Proposed Design
  • remove multiple spaces when not needed
  • format function body, list of parameters and attributes on their respected line.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il risultato della formattazione riportato nell’issue usando la funzione PowerShell fornita e le impostazioni di PSScriptAnalyzer elencate. Confronta l’output con l’esempio previsto, quindi traccia il comportamento del formatter responsabile degli spazi bianchi, dei corpi delle funzioni, dei parametri e degli attributi. Il lavoro è completato quando il documento malformato su una sola riga viene formattato come nell’esempio previsto, senza spazi ripetuti inutilmente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp, powershell
Ambito
tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.