Skip to content

PSPlaceOpenBrace and PSPlaceCloseBrace leave trailing whitespace when expanding one-line blocks #2210

Description

@kjanat

Steps to reproduce

Format a one-line block with IgnoreOneLineBlock = $false:

Invoke-Formatter -ScriptDefinition "if (`$x) { BREAK }" -Settings @{
    IncludeRules = @(
        'PSPlaceOpenBrace'
        'PSPlaceCloseBrace'
        'PSUseConsistentIndentation'
        'PSUseCorrectCasing'
    )
    Rules = @{
        PSPlaceOpenBrace = @{
            Enable = $true
            OnSameLine = $true
            NewLineAfter = $true
            IgnoreOneLineBlock = $false
        }
        PSPlaceCloseBrace = @{
            Enable = $true
            NewLineAfter = $true
            IgnoreOneLineBlock = $false
        }
        PSUseConsistentIndentation = @{ Enable = $true }
        PSUseCorrectCasing = @{ Enable = $true }
    }
}

Expected behavior

Expanding the block should not introduce trailing whitespace:

if ($x) {
    break
}

Actual behavior

The output contains a trailing space after break. The below marks that space:

if ($x) {
    break␠
}

The brace corrections replace only the brace tokens. The spaces that separated the one-line content from the braces survive as trailing whitespace after the block is expanded.

#1992 and merged #1993 are related, but not duplicates. They make PSAvoidTrailingWhitespace available to Invoke-Formatter when explicitly configured. They do not prevent PSPlaceOpenBrace and PSPlaceCloseBrace from introducing trailing whitespace. Callers should not need a separate cleanup rule to remove whitespace created by the formatter itself.

Environment data

Reproduced with:

  • PSScriptAnalyzer 1.25.0 on PowerShell 7.5.2
  • PSScriptAnalyzer built from main at 4b0117c on PowerShell 7.6.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions