Skip to content

Invoke-Formatter: Tab Issue #1055

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
midacts opened this issue Aug 16, 2018 · 7 comments
Open

Invoke-Formatter: Tab Issue #1055

midacts opened this issue Aug 16, 2018 · 7 comments

Comments

@midacts
Copy link

midacts commented Aug 16, 2018

# 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
Copy link
Author

midacts commented Sep 18, 2018

Anyone else running into this?

@bergmeister
Copy link
Collaborator

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
Copy link
Collaborator

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

@midacts
Copy link
Author

midacts commented Nov 5, 2018 via email

@bergmeister
Copy link
Collaborator

bergmeister commented Nov 26, 2018

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
Copy link
Author

midacts commented Nov 27, 2018 via email

@bergmeister
Copy link
Collaborator

bergmeister commented Nov 27, 2018

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:

  • PSAlignAssignmentStatement conflicts with the definition of PSUseConsistentWhitespace, which is issue Unable to use both AlignAssignmentStatement and UseConsistentWhitespace rules #769 but in addition to that PSAlignAssignmentStatement is not tab aware -> The PSAlignAssignmentStatement rule needs to be integrated into the PSUseConsistentWhitespace rule
  • PSUseConsistentIndentation also clashes with PSUseConsistentWhitespace in the sense that PSUseConsistentWhitespace is not aware of the type of whitespace type (it only uses spaces). Also, the documentation around the IndentationSize setting of it needs to be refined as this settings only applies only if the Kind is space but not tab because it gets internally set to 1 when using tabs (IndentationSize is the number of indendation characters)

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?)?

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