Skip to content

Formatting misses certain keywords (unary operators, 'until') #2095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
TMA-2 opened this issue Apr 15, 2025 · 2 comments
Open

Formatting misses certain keywords (unary operators, 'until') #2095

TMA-2 opened this issue Apr 15, 2025 · 2 comments

Comments

@TMA-2
Copy link

TMA-2 commented Apr 15, 2025

I found that during formatting, some keywords aren't separated from leading braces (while), and some are seemingly ignored entirely (until). Also, unary operators (-not, -bnot, etc.) aren't separated with whitespace the same way as binary operators (-eq, -like, etc.).

Steps to reproduce

  1. Paste the following into a script
  2. Use Invoke-Formatter or the PowerShell VSCode extension to format the document
if(-not$false) {
    do{
        "Hello!"
    }until(
        $True
    )
    do{
        "Oh, hi!"
    }while(
        -not$True
    )
    while(-not$True) {
        "This won't show up."
    }
}

Expected behavior

Roughly the following output. Some is dependent on brace style (OTBS in this case), but all rules for inserting whitespace around keywords and operators should be enabled.

if (-not $false) {
    do {
        'Hello!'
    } until (
        $True
    )
    do {
        'Oh, hi!'
    } while (
        -not $True
    )
    while (-not $True) {
        "This won't show up."
    }
}

Actual behavior

if (-not$false) { # -not isn't separated by whitespace
    do {
        'Hello!'
    }until( # no whitespace around Until
        $True
    )
    do {
        'Oh, hi!'
    }while ( # only trailing whitespace for While
        -not$True # no separation
    )
    while (-not$True) { # no separation for -not and -bnot
        "This won't show up."
    }
}

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.19041.5737
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.5737
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.23.0

1.23.0 is the version included with the current (2025.0.0) VSCode PowerShell extension. I've also installed and tested 1.24.0 with the same results.

@bergmeister
Copy link
Collaborator

Suggest to try the new 1.24.0 release, which is in the preview version of the vc code ps extension. some things around whitespaces are tweaked but not your cases I think. UseConsistentWhitespace has CheckOperator option, which could be enhanced with your -not$false case and keywords like do-while should probably be a new option

@TMA-2
Copy link
Author

TMA-2 commented Apr 22, 2025

Suggest to try the new 1.24.0 release, which is in the preview version of the vc code ps extension. some things around whitespaces are tweaked but not your cases I think. UseConsistentWhitespace has CheckOperator option, which could be enhanced with your -not$false case and keywords like do-while should probably be a new option

Thanks for the reply. I've tried 1.24.0 with the same results, so I think it might just be something that needs to be added like you said.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants