PSScriptAnalyzer
PSScriptAnalyzer copied to clipboard
Formatting fails to pretty print a bad formatted document
Prerequisites
- [X] I have written a descriptive issue title.
- [X] 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.