PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.87k stars 378 forks source link

Invoke-Formatter: Tab Issue #1055

Open midacts opened 6 years ago

midacts commented 6 years ago
# CodeFormatting.psd1
@{
    IncludeRules = @(
        'PSPlaceOpenBrace',
        'PSPlaceCloseBrace',
        'PSUseConsistentWhitespace',
        'PSUseConsistentIndentation',
        'PSAlignAssignmentStatement'
    )

    Rules        = @{
        PSPlaceOpenBrace           = @{
            Enable             = $true
            OnSameLine         = $false
            NewLineAfter       = $true
            IgnoreOneLineBlock = $true
        }

        PSPlaceCloseBrace          = @{
            Enable             = $true
            NewLineAfter       = $true
            IgnoreOneLineBlock = $true
            NoEmptyLineBefore  = $false
        }

        PSUseConsistentIndentation = @{
            Enable          = $true
            **Kind            = 'tab'**
            IndentationSize = 4
        }

        PSUseConsistentWhitespace  = @{
            Enable         = $true
            CheckOpenBrace = $true
            CheckOpenParen = $true
            CheckOperator  = $true
            CheckSeparator = $true
        }

        PSAlignAssignmentStatement = @{
            Enable         = $true
            CheckHashtable = $true
        }
    }
}

Steps to reproduce

$Params = @{
    'AppName'       = "AppTest9001"
    'NTID'          = "TestNTID"
    'Credential'    = $Credential
    'Verbose'       = $True
}

Expected behavior

It should preserve tabs (anywhere) but in this case specifically, between the key and the equal sign

Actual behavior

It is changing out tabs for spaces between the key and equal sign. Also, I noticed it is also did not convert some tabs to spaces. If there is only one tab between the key and equal sign, it didn't convert the tab to spaces. My guess is the tab isn't a full tab (meaning it would only be 1-3 spaces and not the full 4)

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.15063.1155
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.1155
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.17.1
midacts commented 6 years ago

Anyone else running into this?

bergmeister commented 6 years ago

Sorry, I have not triaged this yet but I would not be surprised if what you report, is true as I remember having a similar problem last year. I will try to get back to this month

bergmeister commented 6 years ago

Sorry, I was quite busy, I really hope to have a look at this this month

midacts commented 6 years ago

No problem. I understand. Thanks for the update and for looking into this.

On Mon, Nov 5, 2018, 3:53 PM Christoph Bergmeister [MVP] < notifications@github.com wrote:

Sorry, I was quite busy, I really hope to have a look at this this month

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PowerShell/PSScriptAnalyzer/issues/1055#issuecomment-436030744, or mute the thread https://github.com/notifications/unsubscribe-auth/ADxtkK2QdLT1D4N6xPXaLA1LOVha7M_pks5usKVNgaJpZM4V_1dG .

bergmeister commented 6 years ago

I can repro now and confirm it is a bug. Related: #769 Another problem is the PSAlignAssignmentStatement rule, which is not aware of the tabs setting and tabs in general because the AST treats a tab as one indentation (since it does not know how big a tab is, which is a general problem with tabs anyway). I will try to find a generic solution that tackles the underlying problem and not only this problem in the next days

midacts commented 6 years ago

Awesome. Thanks for looking into it and finding the root cause.

: )

On Mon, Nov 26, 2018, 5:30 PM Christoph Bergmeister [MVP] < notifications@github.com wrote:

I can repro now and confirm it is a bug. Related: #769 https://github.com/PowerShell/PSScriptAnalyzer/issues/769 The problem is the interference of the UseConsistentWhitespace rule around the operator. I will try to find a generic solution that tackles the underlying problem and not only this problem in the next days

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PowerShell/PSScriptAnalyzer/issues/1055#issuecomment-441825339, or mute the thread https://github.com/notifications/unsubscribe-auth/ADxtkMWyDWwzf_7PQyw9Rg045B_JMHqVks5uzGtvgaJpZM4V_1dG .

bergmeister commented 6 years ago

You're welcome. For the moment (as a workaround until I have a fix that gets released), I suggest to disable the PSAlignAssignmentStatement rule due to the conflict with PSUseConsistentIndentation (issue # 769):

        PSAlignAssignmentStatement = @{
            Enable         = $false
            CheckHashtable = $true
        }

If you use VS-Code, then you will also need to turn off the related setting, therefore I suggest to also set the following settings at the moment as a workaround

"powershell.codeFormatting.alignPropertyValuePairs": false,
"powershell.codeFormatting.whitespaceAroundOperator": false,
"editor.insertSpaces": false

To summarise the issues at the moment:

I propose to merge both PSAlignAssignmentStatement and PSUseConsistentIndentation into PSUseConsistentWhitespace to avoid conflicts between the rules and share the type of whitespace between them

May I ask why you prefer tabs (since VS-Code and its extensions actually make it hard by automatically converting stuff to spaces?)?