PowerShell / PowerShell/PSScriptAnalyzer

Formatting fails to pretty print a bad formatted document

Offen
#1,709 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Area - Formatter Issue - Bug
Vorherrschende Sprache
C#
Sterne
2.2k
Forks
414
Ø Merge
13 Std. 1 Min.
Gemergte PRs (30 T.)
2

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das Formatierungsergebnis aus dem Issue mithilfe der bereitgestellten PowerShell-Funktion und der aufgeführten PSScriptAnalyzer-Einstellungen. Vergleiche die Ausgabe mit dem erwarteten Beispiel und verfolge anschließend das für Leerraum, Funktionskörper, Parameter und Attribute verantwortliche Verhalten des Formatierers. Als abgeschlossen gilt die Aufgabe, wenn das fehlerhafte einzeilige Dokument wie im erwarteten Beispiel formatiert wird, ohne unnötig wiederholte Leerzeichen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp, powershell
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.