Skip to content
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

codeFormatting.IgnoreOneLineBlock / hashtable problem #1284

Closed
itfranck opened this issue Jul 8, 2019 · 1 comment · Fixed by #1309
Closed

codeFormatting.IgnoreOneLineBlock / hashtable problem #1284

itfranck opened this issue Jul 8, 2019 · 1 comment · Fixed by #1309

Comments

@itfranck
Copy link

itfranck commented Jul 8, 2019

Code formatting does not behave properly when declaring a hashtable in a IF condition.

Steps to reproduce

Set your codeFormatting options in vscode to:

"powershell.codeFormatting.ignoreOneLineBlock": true,
"powershell.codeFormatting.openBraceOnSameLine": true,
"powershell.codeFormatting.newLineAfterCloseBrace": true,
"powershell.codeFormatting.newLineAfterOpenBrace": true,

Write the following statement

  if ($test -eq $null) {$test = @{}}
    if ($test -eq $null) {
        $test = @{ }
    }

Expected behavior

This is actually what happens when $test is an array and what you should expect from an hashtable assignment.

if ($test -eq $null) { $test = @() }
if ($test -eq $null) {
    $test = @()
}

Actual behavior

This is what actually happens with hashtables.

   if ($test -eq $null) { $test = @{ } 
    }
    if ($test -eq $null) {
        $test = @{ }
    }

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.17763.503
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.503
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.18.0
(This is the version that comes with the latest versions of VSCode, from which this is used)
@bergmeister
Copy link
Collaborator

bergmeister commented Jul 8, 2019

Thanks for the detailed report.
First of all, I suggest to install PSSA 1.18.1 for improved formatting and speed (the extension will start picking it up after the next restart of the shell or VSCode).
Secondly, it still happens with the newer version unfortunately. The 1st line is actually sufficient for a repro if one includes the newline in the formatting selection as well:

if ($test -eq $null) { $test = @{ } }


Running Invoke-Formatter with default settings can repro as well and after having attached the culprit is on the lines below as the rule seems to be simply token based
https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Rules/PlaceCloseBrace.cs#L410-L414
One would need to enhance this piece of code to lookup the brace in the AST to see if it is an HashTableAst and if so, ignore it.
There is actually some logic to exclude 1 line hashtable but that should be improved to exclude hashtables in general:
https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Rules/PlaceCloseBrace.cs#L135-L137

UPDATE: I have a found a fix, will open a PR in the next days. Here is a preview of it.

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