PowerShell / PowerShell/PSScriptAnalyzer

PSPlaceCloseBrace can't handle empty lines

Open
#1,616 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Formatter Issue - Bug
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Basically, it gets fixed if I have:

}
else {

But it doesn't get fixed if I have:

}

else {

And I would really like it to be able to handle:

}
# An explanation of the next code block
else {

And format it like:

} else {
    # An explanation of the next code block

Steps to reproduce

Invoke-Formatter {
if (Test-Path ReadMe.md) {
    Show-Markdown ReadMe.md
}

elseif (Test-Path ReadMe*) {
    Get-Item ReadMe* | Select-Object -First 1 | Get-Content
} 
} -Settings @{
  Rules = @{
    PSPlaceCloseBrace = @{
      Enable = $true
      NoEmptylineBefore = $true
      NewLineAfter = $false
    }
  }
}

Expected behavior

It should output

if (Test-Path ReadMe.md) {
    Show-Markdown ReadMe.md
} elseif (Test-Path ReadMe*) {
    Get-Item ReadMe* | Select-Object -First 1 | Get-Content
} 

Actual behavior

if (Test-Path ReadMe.md) {
    Show-Markdown ReadMe.md
}

elseif (Test-Path ReadMe*) {
    Get-Item ReadMe* | Select-Object -First 1 | Get-Content
} 

Environment data

> $PSVersionTable
$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior with Invoke-Formatter and the PSPlaceCloseBrace settings shown in the issue, comparing blank-line and comment cases. Trace the PSScriptAnalyzer formatter path for this rule; done means a blank line or intervening comment no longer prevents the closing brace and else from being joined as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, powershell
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.