PowerShell / PowerShell/PSScriptAnalyzer
Formatting fails to pretty print a bad formatted document
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 2.2k
- Forks
- 414
- Avg merge
- 13h 1m
- Merged PRs (30d)
- 2
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the formatting result from the issue using the provided PowerShell function and the listed PSScriptAnalyzer settings. Compare the output with the expected example, then trace the formatter behavior responsible for whitespace, function bodies, parameters, and attributes. Done means the malformed one-line document is formatted like the expected example without unnecessary repeated spaces.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, powershell
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100