Skip to content

PSPlaceOpenBrace and PSUseConsistentWhitespace don't work together #1490

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

Closed
peetrike opened this issue May 12, 2020 · 2 comments · Fixed by #1491
Closed

PSPlaceOpenBrace and PSUseConsistentWhitespace don't work together #1490

peetrike opened this issue May 12, 2020 · 2 comments · Fixed by #1491

Comments

@peetrike
Copy link

peetrike commented May 12, 2020

Steps to reproduce

Invoke-ScriptAnalyzer -ScriptDefinition @'
if ($true)
{
    "ok"
}
'@ -Settings @{
    Rules = @{
        PSPlaceOpenBrace = @{
            Enable     = $true
            OnSameLine = $false
        }
        PSUseConsistentWhitespace = @{
            Enable = $true
        }
    }
}

Expected behavior

When both rules are used, it should not trigger the PSUseConsistentWhitespace rule.

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseConsistentWhitespace           Warning                 2     Use space before open brace.

When you add 1 space before open brace, it doesn't report violation.

If the whole code block is indented (more than 1 space before open brace), it reports again the problem.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.752
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.19.0
> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.0
@bergmeister
Copy link
Collaborator

bergmeister commented May 12, 2020

Related: #769 and also happened with 1.18.3

@bergmeister
Copy link
Collaborator

bergmeister commented May 12, 2020

Running it without PSPlaceOpenBrace, results in the same, so I don't see how it is an issue of the 2 rules together:

Invoke-ScriptAnalyzer -ScriptDefinition @'
if ($true)
{
    "ok"
}
'@ -Settings @{
    Rules = @{
        PSUseConsistentWhitespace = @{
            Enable = $true
        }
    }
}

I am aware, generally one cannot use the formatting settings and run Invoke-ScriptAnalyzer against them to ensure the style is compliant. I think this is just one of a few things that you'll find, but it's definitely worthwhile boing them down individually and raising them as you did.
It happens exactly here:

if (!IsPreviousTokenApartByWhitespace(lcurly))

I am thinking we could maybe enhance the check in there to only check for whitespace if the previous token is on the same line, which is the issue here I think, I opened a PR with a fix below @peetrike

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