Skip to content

codeFormatting.IgnoreOneLineBlock / hashtable problem #1284

Closed

Description

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)

Activity

  1. bergmeister commented on Jul 8, 2019

    @bergmeister
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions